Numeric.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Collections;
  25. namespace Mono.Data.SqlExpressions {
  26. internal class Numeric {
  27. internal static bool IsNumeric (object o) {
  28. if (o is IConvertible) {
  29. TypeCode tc = ((IConvertible)o).GetTypeCode();
  30. if(TypeCode.Char <= tc && tc <= TypeCode.Decimal)
  31. return true;
  32. }
  33. return false;
  34. }
  35. //extends to Int32/Int64/Decimal/Double
  36. internal static IConvertible Unify (IConvertible o)
  37. {
  38. switch (o.GetTypeCode()) {
  39. case TypeCode.Char:
  40. case TypeCode.SByte:
  41. case TypeCode.Byte:
  42. case TypeCode.Int16:
  43. case TypeCode.UInt16:
  44. return (IConvertible)Convert.ChangeType (o, TypeCode.Int32);
  45. case TypeCode.UInt32:
  46. return (IConvertible)Convert.ChangeType (o, TypeCode.Int64);
  47. case TypeCode.UInt64:
  48. return (IConvertible)Convert.ChangeType (o, TypeCode.Decimal);
  49. case TypeCode.Single:
  50. return (IConvertible)Convert.ChangeType (o, TypeCode.Double);
  51. default:
  52. return o;
  53. }
  54. }
  55. //(note: o1 and o2 must both be of type Int32/Int64/Decimal/Double)
  56. internal static void ToSameType (ref IConvertible o1, ref IConvertible o2)
  57. {
  58. TypeCode tc1 = o1.GetTypeCode();
  59. TypeCode tc2 = o2.GetTypeCode();
  60. if (tc1 == tc2)
  61. return;
  62. // is it ok to make such assumptions about the order of an enum?
  63. if (tc1 < tc2)
  64. Convert.ChangeType (o1, tc2);
  65. else
  66. Convert.ChangeType (o2, tc1);
  67. }
  68. internal static IConvertible Add (IConvertible o1, IConvertible o2)
  69. {
  70. ToSameType (ref o1, ref o2);
  71. switch (o1.GetTypeCode()) {
  72. case TypeCode.Int32:
  73. default:
  74. return (int)o1 + (int)o2;
  75. case TypeCode.Int64:
  76. return (long)o1 + (long)o2;
  77. case TypeCode.Double:
  78. return (double)o1 + (double)o2;
  79. case TypeCode.Decimal:
  80. return (decimal)o1 + (decimal)o2;
  81. }
  82. }
  83. internal static IConvertible Subtract (IConvertible o1, IConvertible o2)
  84. {
  85. ToSameType (ref o1, ref o2);
  86. switch (o1.GetTypeCode()) {
  87. case TypeCode.Int32:
  88. default:
  89. return (int)o1 - (int)o2;
  90. case TypeCode.Int64:
  91. return (long)o1 - (long)o2;
  92. case TypeCode.Double:
  93. return (double)o1 - (double)o2;
  94. case TypeCode.Decimal:
  95. return (decimal)o1 - (decimal)o2;
  96. }
  97. }
  98. internal static IConvertible Multiply (IConvertible o1, IConvertible o2)
  99. {
  100. ToSameType (ref o1, ref o2);
  101. switch (o1.GetTypeCode()) {
  102. case TypeCode.Int32:
  103. default:
  104. return (int)o1 * (int)o2;
  105. case TypeCode.Int64:
  106. return (long)o1 * (long)o2;
  107. case TypeCode.Double:
  108. return (double)o1 * (double)o2;
  109. case TypeCode.Decimal:
  110. return (decimal)o1 * (decimal)o2;
  111. }
  112. }
  113. internal static IConvertible Divide (IConvertible o1, IConvertible o2)
  114. {
  115. ToSameType (ref o1, ref o2);
  116. switch (o1.GetTypeCode()) {
  117. case TypeCode.Int32:
  118. default:
  119. return (int)o1 / (int)o2;
  120. case TypeCode.Int64:
  121. return (long)o1 / (long)o2;
  122. case TypeCode.Double:
  123. return (double)o1 / (double)o2;
  124. case TypeCode.Decimal:
  125. return (decimal)o1 / (decimal)o2;
  126. }
  127. }
  128. internal static IConvertible Modulo (IConvertible o1, IConvertible o2)
  129. {
  130. ToSameType (ref o1, ref o2);
  131. switch (o1.GetTypeCode()) {
  132. case TypeCode.Int32:
  133. default:
  134. return (int)o1 % (int)o2;
  135. case TypeCode.Int64:
  136. return (long)o1 % (long)o2;
  137. case TypeCode.Double:
  138. return (double)o1 % (double)o2;
  139. case TypeCode.Decimal:
  140. return (decimal)o1 % (decimal)o2;
  141. }
  142. }
  143. internal static IConvertible Negative (IConvertible o)
  144. {
  145. switch (o.GetTypeCode()) {
  146. case TypeCode.Int32:
  147. default:
  148. return -((int)o);
  149. case TypeCode.Int64:
  150. return -((long)o);
  151. case TypeCode.Double:
  152. return -((double)o);
  153. case TypeCode.Decimal:
  154. return -((decimal)o);
  155. }
  156. }
  157. internal static IConvertible Min (IConvertible o1, IConvertible o2)
  158. {
  159. switch (o1.GetTypeCode()) {
  160. case TypeCode.Int32:
  161. default:
  162. return System.Math.Min ((int)o1, (int)o2);
  163. case TypeCode.Int64:
  164. return System.Math.Min ((long)o1, (long)o2);
  165. case TypeCode.Double:
  166. return System.Math.Min ((double)o1, (double)o2);
  167. case TypeCode.Decimal:
  168. return System.Math.Min ((decimal)o1, (decimal)o2);
  169. }
  170. }
  171. internal static IConvertible Max (IConvertible o1, IConvertible o2)
  172. {
  173. switch (o1.GetTypeCode()) {
  174. case TypeCode.Int32:
  175. default:
  176. return System.Math.Max ((int)o1, (int)o2);
  177. case TypeCode.Int64:
  178. return System.Math.Max ((long)o1, (long)o2);
  179. case TypeCode.Double:
  180. return System.Math.Max ((double)o1, (double)o2);
  181. case TypeCode.Decimal:
  182. return System.Math.Max ((decimal)o1, (decimal)o2);
  183. }
  184. }
  185. }
  186. }