BigIntegerStorage.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //------------------------------------------------------------------------------
  2. // <copyright file="BigIntStorage.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. //------------------------------------------------------------------------------
  8. namespace System.Data.Common {
  9. using System;
  10. using System.Xml;
  11. using System.Numerics;
  12. using System.Data.SqlTypes;
  13. using System.Collections;
  14. internal sealed class BigIntegerStorage : DataStorage {
  15. private BigInteger[] values;
  16. internal BigIntegerStorage(DataColumn column)
  17. : base(column, typeof(BigInteger), BigInteger.Zero, StorageType.BigInteger)
  18. {
  19. }
  20. override public Object Aggregate(int[] records, AggregateType kind) {
  21. throw ExceptionBuilder.AggregateException(kind, DataType);
  22. }
  23. override public int Compare(int recordNo1, int recordNo2) {
  24. BigInteger valueNo1 = values[recordNo1];
  25. BigInteger valueNo2 = values[recordNo2];
  26. if (valueNo1.IsZero || valueNo2.IsZero) {
  27. int bitCheck = CompareBits(recordNo1, recordNo2);
  28. if (0 != bitCheck) {
  29. return bitCheck;
  30. }
  31. }
  32. return valueNo1.CompareTo(valueNo2);
  33. }
  34. public override int CompareValueTo(int recordNo, object value) {
  35. System.Diagnostics.Debug.Assert(0 <= recordNo, "Invalid record");
  36. System.Diagnostics.Debug.Assert(null != value, "null value");
  37. if (NullValue == value) {
  38. return (HasValue(recordNo) ? 1 : 0);
  39. }
  40. BigInteger valueNo1 = values[recordNo];
  41. if (valueNo1.IsZero && !HasValue(recordNo)) {
  42. return -1;
  43. }
  44. return valueNo1.CompareTo((BigInteger)value);
  45. }
  46. // supported implict casts
  47. internal static BigInteger ConvertToBigInteger(object value, IFormatProvider formatProvider) {
  48. if (value.GetType() == typeof(BigInteger)) { return (BigInteger)value; }
  49. else if (value.GetType() == typeof(String)) { return BigInteger.Parse((string)value, formatProvider); }
  50. else if (value.GetType() == typeof(Int64)) { return (BigInteger)(Int64)value; }
  51. else if (value.GetType() == typeof(Int32)) { return (BigInteger)(Int32)value; }
  52. else if (value.GetType() == typeof(Int16)) { return (BigInteger)(Int16)value; }
  53. else if (value.GetType() == typeof(SByte)) { return (BigInteger)(SByte)value; }
  54. else if (value.GetType() == typeof(UInt64)) { return (BigInteger)(UInt64)value; }
  55. else if (value.GetType() == typeof(UInt32)) { return (BigInteger)(UInt32)value; }
  56. else if (value.GetType() == typeof(UInt16)) { return (BigInteger)(UInt16)value; }
  57. else if (value.GetType() == typeof(Byte)) { return (BigInteger)(Byte)value; }
  58. else { throw ExceptionBuilder.ConvertFailed(value.GetType(), typeof(System.Numerics.BigInteger)); }
  59. }
  60. internal static object ConvertFromBigInteger(BigInteger value, Type type, IFormatProvider formatProvider) {
  61. if (type == typeof(string)) { return value.ToString("D", formatProvider); }
  62. else if (type == typeof(SByte)) { return checked((SByte)value); }
  63. else if (type == typeof(Int16)) { return checked((Int16)value); }
  64. else if (type == typeof(Int32)) { return checked((Int32)value); }
  65. else if (type == typeof(Int64)) { return checked((Int64)value); }
  66. else if (type == typeof(Byte)) { return checked((Byte)value); }
  67. else if (type == typeof(UInt16)) { return checked((UInt16)value); }
  68. else if (type == typeof(UInt32)) { return checked((UInt32)value); }
  69. else if (type == typeof(UInt64)) { return checked((UInt64)value); }
  70. else if (type == typeof(Single)) { return checked((Single)value); }
  71. else if (type == typeof(Double)) { return checked((Double)value); }
  72. else if (type == typeof(Decimal)) { return checked((Decimal)value); }
  73. else if (type == typeof(System.Numerics.BigInteger)) { return value; }
  74. else { throw ExceptionBuilder.ConvertFailed(typeof(System.Numerics.BigInteger), type); }
  75. }
  76. public override object ConvertValue(object value) {
  77. if (NullValue != value) {
  78. if (null != value) {
  79. value = ConvertToBigInteger(value, this.FormatProvider);
  80. }
  81. else {
  82. value = NullValue;
  83. }
  84. }
  85. return value;
  86. }
  87. override public void Copy(int recordNo1, int recordNo2) {
  88. CopyBits(recordNo1, recordNo2);
  89. values[recordNo2] = values[recordNo1];
  90. }
  91. override public Object Get(int record) {
  92. BigInteger value = values[record];
  93. if (!value.IsZero) {
  94. return value;
  95. }
  96. return GetBits(record);
  97. }
  98. override public void Set(int record, Object value) {
  99. System.Diagnostics.Debug.Assert(null != value, "null value");
  100. if (NullValue == value) {
  101. values[record] = BigInteger.Zero;
  102. SetNullBit(record, true);
  103. }
  104. else {
  105. values[record] = ConvertToBigInteger(value, this.FormatProvider);
  106. SetNullBit(record, false);
  107. }
  108. }
  109. override public void SetCapacity(int capacity) {
  110. BigInteger[] newValues = new BigInteger[capacity];
  111. if (null != values) {
  112. Array.Copy(values, 0, newValues, 0, Math.Min(capacity, values.Length));
  113. }
  114. values = newValues;
  115. base.SetCapacity(capacity);
  116. }
  117. override public object ConvertXmlToObject(string s) {
  118. return BigInteger.Parse(s, System.Globalization.CultureInfo.InvariantCulture);
  119. }
  120. override public string ConvertObjectToXml(object value) {
  121. return ((BigInteger)value).ToString("D", System.Globalization.CultureInfo.InvariantCulture);
  122. }
  123. override protected object GetEmptyStorage(int recordCount) {
  124. return new BigInteger[recordCount];
  125. }
  126. override protected void CopyValue(int record, object store, BitArray nullbits, int storeIndex) {
  127. BigInteger[] typedStore = (BigInteger[])store;
  128. typedStore[storeIndex] = values[record];
  129. nullbits.Set(storeIndex, !HasValue(record));
  130. }
  131. override protected void SetStorage(object store, BitArray nullbits) {
  132. values = (BigInteger[])store;
  133. SetNullStorage(nullbits);
  134. }
  135. }
  136. }