TimeSpanStorage.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //------------------------------------------------------------------------------
  2. // <copyright file="TimeSpanStorage.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 TimeSpanStorage : DataStorage {
  15. private static readonly TimeSpan defaultValue = TimeSpan.Zero;
  16. private TimeSpan[] values;
  17. public TimeSpanStorage(DataColumn column)
  18. : base(column, typeof(TimeSpan), defaultValue, StorageType.TimeSpan) {
  19. }
  20. override public Object Aggregate(int[] records, AggregateType kind) {
  21. bool hasData = false;
  22. try {
  23. switch (kind) {
  24. case AggregateType.Min:
  25. TimeSpan min = TimeSpan.MaxValue;
  26. for (int i = 0; i < records.Length; i++) {
  27. int record = records[i];
  28. if (IsNull(record))
  29. continue;
  30. min=(TimeSpan.Compare(values[record],min) < 0) ? values[record] : min;
  31. hasData = true;
  32. }
  33. if (hasData) {
  34. return min;
  35. }
  36. return NullValue;
  37. case AggregateType.Max:
  38. TimeSpan max = TimeSpan.MinValue;
  39. for (int i = 0; i < records.Length; i++) {
  40. int record = records[i];
  41. if (IsNull(record))
  42. continue;
  43. max=(TimeSpan.Compare(values[record],max) >= 0) ? 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. case AggregateType.Sum: {
  58. decimal sum = 0;
  59. foreach (int record in records) {
  60. if (IsNull(record))
  61. continue;
  62. sum += values[record].Ticks;
  63. hasData = true;
  64. }
  65. if (hasData) {
  66. return TimeSpan.FromTicks((long)Math.Round(sum));
  67. }
  68. return null;
  69. }
  70. case AggregateType.Mean: {
  71. decimal meanSum = 0;
  72. int meanCount = 0;
  73. foreach (int record in records) {
  74. if (IsNull(record))
  75. continue;
  76. meanSum += values[record].Ticks;
  77. meanCount++;
  78. }
  79. if (meanCount > 0) {
  80. return TimeSpan.FromTicks((long)Math.Round(meanSum / meanCount));
  81. }
  82. return null;
  83. }
  84. case AggregateType.StDev: {
  85. int count = 0;
  86. decimal meanSum = 0;
  87. foreach (int record in records) {
  88. if (IsNull(record))
  89. continue;
  90. meanSum += values[record].Ticks;
  91. count++;
  92. }
  93. if (count > 1) {
  94. double varSum = 0;
  95. decimal mean=meanSum/count;
  96. foreach (int record in records) {
  97. if (IsNull(record))
  98. continue;
  99. double x = (double)(values[record].Ticks - mean);
  100. varSum += x * x;
  101. }
  102. ulong stDev = (ulong)Math.Round(Math.Sqrt(varSum / (count - 1)));
  103. if (stDev > long.MaxValue) {
  104. stDev = long.MaxValue;
  105. }
  106. return TimeSpan.FromTicks((long)stDev);
  107. }
  108. return null;
  109. }
  110. }
  111. }
  112. catch (OverflowException) {
  113. throw ExprException.Overflow(typeof(TimeSpan));
  114. }
  115. throw ExceptionBuilder.AggregateException(kind, DataType);
  116. }
  117. override public int Compare(int recordNo1, int recordNo2) {
  118. TimeSpan valueNo1 = values[recordNo1];
  119. TimeSpan valueNo2 = values[recordNo2];
  120. if (valueNo1 == defaultValue || valueNo2 == defaultValue) {
  121. int bitCheck = CompareBits(recordNo1, recordNo2);
  122. if (0 != bitCheck)
  123. return bitCheck;
  124. }
  125. return TimeSpan.Compare(valueNo1, valueNo2);
  126. }
  127. public override int CompareValueTo(int recordNo, object value) {
  128. System.Diagnostics.Debug.Assert(0 <= recordNo, "Invalid record");
  129. System.Diagnostics.Debug.Assert(null != value, "null value");
  130. if (NullValue == value) {
  131. if (IsNull(recordNo)) {
  132. return 0;
  133. }
  134. return 1;
  135. }
  136. TimeSpan valueNo1 = values[recordNo];
  137. if ((defaultValue == valueNo1) && IsNull(recordNo)) {
  138. return -1;
  139. }
  140. return valueNo1.CompareTo((TimeSpan)value);
  141. }
  142. private static TimeSpan ConvertToTimeSpan(object value) {
  143. // Webdata 94686: Do not change this checks
  144. Type typeofValue= value.GetType();
  145. if (typeofValue == typeof(string)) {
  146. return TimeSpan.Parse((string)value);
  147. }
  148. else if (typeofValue == typeof(Int32)) {
  149. return new TimeSpan((Int64)((Int32)value));
  150. }
  151. else if (typeofValue == typeof(Int64)) {
  152. return new TimeSpan((Int64)value);
  153. }
  154. else {
  155. return (TimeSpan) value;
  156. }
  157. }
  158. public override object ConvertValue(object value) {
  159. if (NullValue != value) {
  160. if (null != value) {
  161. value = ConvertToTimeSpan(value);
  162. }
  163. else {
  164. value = NullValue;
  165. }
  166. }
  167. return value;
  168. }
  169. override public void Copy(int recordNo1, int recordNo2) {
  170. CopyBits(recordNo1, recordNo2);
  171. values[recordNo2] = values[recordNo1];
  172. }
  173. override public Object Get(int record) {
  174. TimeSpan value = values[record];
  175. if (value != defaultValue) {
  176. return value;
  177. }
  178. return GetBits(record);
  179. }
  180. override public void Set(int record, Object value) {
  181. System.Diagnostics.Debug.Assert(null != value, "null value");
  182. if (NullValue == value) {
  183. values[record] = defaultValue;
  184. SetNullBit(record, true);
  185. }
  186. else {
  187. values[record] = ConvertToTimeSpan(value);
  188. SetNullBit(record, false);
  189. }
  190. }
  191. override public void SetCapacity(int capacity) {
  192. TimeSpan[] newValues = new TimeSpan[capacity];
  193. if (null != values) {
  194. Array.Copy(values, 0, newValues, 0, Math.Min(capacity, values.Length));
  195. }
  196. values = newValues;
  197. base.SetCapacity(capacity);
  198. }
  199. override public object ConvertXmlToObject(string s) {
  200. return XmlConvert.ToTimeSpan(s);
  201. }
  202. override public string ConvertObjectToXml(object value) {
  203. return XmlConvert.ToString((TimeSpan)value);
  204. }
  205. override protected object GetEmptyStorage(int recordCount) {
  206. return new TimeSpan[recordCount];
  207. }
  208. override protected void CopyValue(int record, object store, BitArray nullbits, int storeIndex) {
  209. TimeSpan[] typedStore = (TimeSpan[]) store;
  210. typedStore[storeIndex] = values[record];
  211. nullbits.Set(storeIndex, IsNull(record));
  212. }
  213. override protected void SetStorage(object store, BitArray nullbits) {
  214. values = (TimeSpan[]) store;
  215. SetNullStorage(nullbits);
  216. }
  217. }
  218. }