CharStorage.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //------------------------------------------------------------------------------
  2. // <copyright file="CharStorage.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. // <owner current="true" primary="false">Microsoft</owner>
  7. // <owner current="false" primary="false">Microsoft</owner>
  8. //------------------------------------------------------------------------------
  9. namespace System.Data.Common {
  10. using System;
  11. using System.Xml;
  12. using System.Data.SqlTypes;
  13. using System.Collections;
  14. internal sealed class CharStorage : DataStorage {
  15. private const Char defaultValue = '\0';
  16. private Char[] values;
  17. internal CharStorage(DataColumn column)
  18. : base(column, typeof(Char), defaultValue, StorageType.Char) {
  19. }
  20. override public Object Aggregate(int[] records, AggregateType kind) {
  21. bool hasData = false;
  22. try {
  23. switch (kind) {
  24. case AggregateType.Min:
  25. Char min = Char.MaxValue;
  26. for (int i = 0; i < records.Length; i++) {
  27. int record = records[i];
  28. if (IsNull(record))
  29. continue;
  30. min=(values[record] < min) ? values[record] : min;
  31. hasData = true;
  32. }
  33. if (hasData) {
  34. return min;
  35. }
  36. return NullValue;
  37. case AggregateType.Max:
  38. Char max = Char.MinValue;
  39. for (int i = 0; i < records.Length; i++) {
  40. int record = records[i];
  41. if (IsNull(record))
  42. continue;
  43. max=(values[record] > max) ? values[record] : max;
  44. hasData = true;
  45. }
  46. if (hasData) {
  47. return max;
  48. }
  49. return NullValue;
  50. case AggregateType.First:
  51. if (records.Length > 0) {
  52. return values[records[0]];
  53. }
  54. return null;
  55. case AggregateType.Count:
  56. return base.Aggregate(records, kind);
  57. }
  58. }
  59. catch (OverflowException) {
  60. throw ExprException.Overflow(typeof(Char));
  61. }
  62. throw ExceptionBuilder.AggregateException(kind, DataType);
  63. }
  64. override public int Compare(int recordNo1, int recordNo2) {
  65. Char valueNo1 = values[recordNo1];
  66. Char valueNo2 = values[recordNo2];
  67. if (valueNo1 == defaultValue || valueNo2 == defaultValue) {
  68. int bitCheck = CompareBits(recordNo1, recordNo2);
  69. if (0 != bitCheck)
  70. return bitCheck;
  71. }
  72. return valueNo1.CompareTo(valueNo2);
  73. //return (valueNo1-valueNo2); // copied from Char.CompareTo(Char)
  74. }
  75. public override int CompareValueTo(int recordNo, object value) {
  76. System.Diagnostics.Debug.Assert(0 <= recordNo, "Invalid record");
  77. System.Diagnostics.Debug.Assert(null != value, "null value");
  78. if (NullValue == value) {
  79. if (IsNull(recordNo)) {
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. Char valueNo1 = values[recordNo];
  85. if ((defaultValue == valueNo1) && IsNull(recordNo)) {
  86. return -1;
  87. }
  88. return valueNo1.CompareTo((Char)value);
  89. //return (valueNo1-valueNo2); // copied from Char.CompareTo(Char)
  90. }
  91. public override object ConvertValue(object value) {
  92. if (NullValue != value) {
  93. if (null != value) {
  94. value = ((IConvertible)value).ToChar(FormatProvider);
  95. }
  96. else {
  97. value = NullValue;
  98. }
  99. }
  100. return value;
  101. }
  102. override public void Copy(int recordNo1, int recordNo2) {
  103. CopyBits(recordNo1, recordNo2);
  104. values[recordNo2] = values[recordNo1];
  105. }
  106. override public Object Get(int record) {
  107. Char value = values[record];
  108. if (value != defaultValue) {
  109. return value;
  110. }
  111. return GetBits(record);
  112. }
  113. override public void Set(int record, Object value) {
  114. System.Diagnostics.Debug.Assert(null != value, "null value");
  115. if (NullValue == value) {
  116. values[record] = defaultValue;
  117. SetNullBit(record, true);
  118. }
  119. else {
  120. Char ch = ((IConvertible)value).ToChar(FormatProvider);
  121. if ((ch >= (char)0xd800 && ch <= (char)0xdfff) || (ch < (char)0x21 && (ch == (char)0x9 || ch == (char)0xa || ch == (char)0xd ))) {
  122. throw ExceptionBuilder.ProblematicChars(ch);
  123. }
  124. values[record] = ch;
  125. SetNullBit(record, false);
  126. }
  127. }
  128. override public void SetCapacity(int capacity) {
  129. Char[] newValues = new Char[capacity];
  130. if (null != values) {
  131. Array.Copy(values, 0, newValues, 0, Math.Min(capacity, values.Length));
  132. }
  133. values = newValues;
  134. base.SetCapacity(capacity);
  135. }
  136. override public object ConvertXmlToObject(string s) {
  137. return XmlConvert.ToChar(s);
  138. }
  139. override public string ConvertObjectToXml(object value) {
  140. return XmlConvert.ToString((Char) value);
  141. }
  142. override protected object GetEmptyStorage(int recordCount) {
  143. return new Char[recordCount];
  144. }
  145. override protected void CopyValue(int record, object store, BitArray nullbits, int storeIndex) {
  146. Char[] typedStore = (Char[]) store;
  147. typedStore[storeIndex] = values[record];
  148. nullbits.Set(storeIndex, IsNull(record));
  149. }
  150. override protected void SetStorage(object store, BitArray nullbits) {
  151. values = (Char[]) store;
  152. SetNullStorage(nullbits);
  153. }
  154. }
  155. }