DateTimeStorage.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //------------------------------------------------------------------------------
  2. // <copyright file="DateTimeStorage.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 DateTimeStorage : DataStorage {
  15. private static readonly DateTime defaultValue = DateTime.MinValue;
  16. private DateTime[] values;
  17. internal DateTimeStorage(DataColumn column)
  18. : base(column, typeof(DateTime), defaultValue, StorageType.DateTime) {
  19. }
  20. override public Object Aggregate(int[] records, AggregateType kind) {
  21. bool hasData = false;
  22. try {
  23. switch (kind) {
  24. case AggregateType.Min:
  25. DateTime min = DateTime.MaxValue;
  26. for (int i = 0; i < records.Length; i++) {
  27. int record = records[i];
  28. if (HasValue(record)) {
  29. min=(DateTime.Compare(values[record],min) < 0) ? values[record] : min;
  30. hasData = true;
  31. }
  32. }
  33. if (hasData) {
  34. return min;
  35. }
  36. return NullValue;
  37. case AggregateType.Max:
  38. DateTime max = DateTime.MinValue;
  39. for (int i = 0; i < records.Length; i++) {
  40. int record = records[i];
  41. if (HasValue(record)) {
  42. max=(DateTime.Compare(values[record],max) >= 0) ? values[record] : max;
  43. hasData = true;
  44. }
  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. int count = 0;
  57. for (int i = 0; i < records.Length; i++) {
  58. if (HasValue(records[i])) {
  59. count++;
  60. }
  61. }
  62. return count;
  63. }
  64. }
  65. catch (OverflowException) {
  66. throw ExprException.Overflow(typeof(DateTime));
  67. }
  68. throw ExceptionBuilder.AggregateException(kind, DataType);
  69. }
  70. override public int Compare(int recordNo1, int recordNo2) {
  71. DateTime valueNo1 = values[recordNo1];
  72. DateTime valueNo2 = values[recordNo2];
  73. if (valueNo1 == defaultValue || valueNo2 == defaultValue) {
  74. int bitCheck = CompareBits(recordNo1, recordNo2);
  75. if (0 != bitCheck) {
  76. return bitCheck;
  77. }
  78. }
  79. return DateTime.Compare(valueNo1, valueNo2);
  80. }
  81. public override int CompareValueTo(int recordNo, object value) {
  82. System.Diagnostics.Debug.Assert(0 <= recordNo, "Invalid record");
  83. System.Diagnostics.Debug.Assert(null != value, "null value");
  84. if (NullValue == value) {
  85. return (HasValue(recordNo) ? 1 : 0);
  86. }
  87. DateTime valueNo1 = values[recordNo];
  88. if ((defaultValue == valueNo1) && !HasValue(recordNo)) {
  89. return -1;
  90. }
  91. return DateTime.Compare(valueNo1, (DateTime)value);
  92. }
  93. public override object ConvertValue(object value) {
  94. if (NullValue != value) {
  95. if (null != value) {
  96. value = ((IConvertible)value).ToDateTime(FormatProvider);
  97. }
  98. else {
  99. value = NullValue;
  100. }
  101. }
  102. return value;
  103. }
  104. override public void Copy(int recordNo1, int recordNo2) {
  105. CopyBits(recordNo1, recordNo2);
  106. values[recordNo2] = values[recordNo1];
  107. }
  108. override public Object Get(int record) {
  109. // SQLBU 408233: two equal DateTime with different .Kind are equal
  110. DateTime value = values[record];
  111. if ((value != defaultValue) || HasValue(record)) {
  112. return value;
  113. }
  114. return NullValue;
  115. }
  116. override public void Set(int record, Object value) {
  117. System.Diagnostics.Debug.Assert(null != value, "null value");
  118. if (NullValue == value) {
  119. values[record] = defaultValue;
  120. SetNullBit(record, true);
  121. }
  122. else {
  123. DateTime retVal;
  124. DateTime tmpValue = ((IConvertible)value).ToDateTime(FormatProvider);
  125. switch (DateTimeMode) {
  126. case DataSetDateTime.Utc:
  127. if (tmpValue.Kind == DateTimeKind.Utc) {
  128. retVal = tmpValue;
  129. }
  130. else if (tmpValue.Kind == DateTimeKind.Local) {
  131. retVal = tmpValue.ToUniversalTime();
  132. }
  133. else {
  134. retVal = DateTime.SpecifyKind(tmpValue, DateTimeKind.Utc);
  135. }
  136. break;
  137. case DataSetDateTime.Local:
  138. if (tmpValue.Kind == DateTimeKind.Local) {
  139. retVal = tmpValue;
  140. }
  141. else if (tmpValue.Kind == DateTimeKind.Utc) {
  142. retVal = tmpValue.ToLocalTime();
  143. }
  144. else {
  145. retVal = DateTime.SpecifyKind(tmpValue, DateTimeKind.Local);
  146. }
  147. break;
  148. case DataSetDateTime.Unspecified:
  149. case DataSetDateTime.UnspecifiedLocal:
  150. retVal = DateTime.SpecifyKind(tmpValue, DateTimeKind.Unspecified);
  151. break;
  152. default:
  153. throw ExceptionBuilder.InvalidDateTimeMode(DateTimeMode);
  154. }
  155. values[record] = retVal;
  156. SetNullBit(record, false);
  157. }
  158. }
  159. override public void SetCapacity(int capacity) {
  160. DateTime[] newValues = new DateTime[capacity];
  161. if (null != values) {
  162. Array.Copy(values, 0, newValues, 0, Math.Min(capacity, values.Length));
  163. }
  164. values = newValues;
  165. base.SetCapacity(capacity);
  166. }
  167. override public object ConvertXmlToObject(string s) {
  168. object retValue;
  169. if (DateTimeMode == DataSetDateTime.UnspecifiedLocal) {
  170. retValue = XmlConvert.ToDateTime(s, XmlDateTimeSerializationMode.Unspecified);
  171. }
  172. else {
  173. retValue = XmlConvert.ToDateTime(s, XmlDateTimeSerializationMode.RoundtripKind);
  174. }
  175. return retValue;
  176. }
  177. override public string ConvertObjectToXml(object value) {
  178. string retValue;
  179. if (DateTimeMode == DataSetDateTime.UnspecifiedLocal) {
  180. retValue = XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.Local);
  181. }
  182. else {
  183. retValue = XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind);
  184. }
  185. return retValue;
  186. }
  187. override protected object GetEmptyStorage(int recordCount) {
  188. return new DateTime[recordCount];
  189. }
  190. override protected void CopyValue(int record, object store, BitArray nullbits, int storeIndex) {
  191. DateTime[] typedStore = (DateTime[]) store;
  192. bool isnull = !HasValue(record);
  193. if (isnull || ( 0 == (DateTimeMode & DataSetDateTime.Local))) {
  194. typedStore[storeIndex] = values[record]; // already universal time
  195. }
  196. else {
  197. typedStore[storeIndex] = values[record].ToUniversalTime();
  198. }
  199. nullbits.Set(storeIndex, isnull);
  200. }
  201. override protected void SetStorage(object store, BitArray nullbits) {
  202. values = (DateTime[]) store;
  203. SetNullStorage(nullbits);
  204. if (DateTimeMode == DataSetDateTime.UnspecifiedLocal) {
  205. for (int i = 0; i < values.Length; i++) {
  206. if (HasValue(i)) {
  207. values[i] = DateTime.SpecifyKind(values[i].ToLocalTime(), DateTimeKind.Unspecified); //Strip the kind for UnspecifiedLocal.
  208. }
  209. }
  210. }
  211. else if (DateTimeMode == DataSetDateTime.Local) {
  212. for (int i = 0; i < values.Length; i++) {
  213. if (HasValue(i)) {
  214. values[i] = values[i].ToLocalTime();
  215. }
  216. }
  217. }
  218. }
  219. }
  220. }