Functions.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Functions.cs
  3. //
  4. // Author:
  5. // Juraj Skripsky ([email protected])
  6. //
  7. // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Data;
  34. namespace Mono.Data.SqlExpressions {
  35. internal class IifFunction : UnaryExpression {
  36. IExpression trueExpr, falseExpr;
  37. public IifFunction (IExpression e, IExpression trueExpr, IExpression falseExpr) : base (e)
  38. {
  39. this.trueExpr = trueExpr;
  40. this.falseExpr = falseExpr;
  41. }
  42. public override bool Equals(object obj)
  43. {
  44. if (!base.Equals (obj))
  45. return false;
  46. if (!(obj is IifFunction))
  47. return false;
  48. IifFunction other = (IifFunction) obj;
  49. if (!other.falseExpr.Equals (falseExpr))
  50. return false;
  51. if (!other.trueExpr.Equals (trueExpr))
  52. return false;
  53. return true;
  54. }
  55. public override int GetHashCode()
  56. {
  57. int hashCode = base.GetHashCode ();
  58. hashCode ^= falseExpr.GetHashCode ();
  59. hashCode ^= trueExpr.GetHashCode ();
  60. return hashCode;
  61. }
  62. override public object Eval (DataRow row)
  63. {
  64. object o = expr.Eval (row);
  65. if (o == DBNull.Value)
  66. return o;
  67. bool val = (bool)o;
  68. return (val ? trueExpr.Eval (row) : falseExpr.Eval (row));
  69. }
  70. }
  71. internal class IsNullFunction : UnaryExpression {
  72. IExpression defaultExpr;
  73. public IsNullFunction (IExpression e, IExpression defaultExpr) : base (e)
  74. {
  75. this.defaultExpr = defaultExpr;
  76. }
  77. public override bool Equals(object obj)
  78. {
  79. if (!base.Equals (obj))
  80. return false;
  81. if (!(obj is UnaryExpression))
  82. return false;
  83. IsNullFunction other = (IsNullFunction) obj;
  84. if (!other.defaultExpr.Equals (defaultExpr))
  85. return false;
  86. return true;
  87. }
  88. public override int GetHashCode()
  89. {
  90. return defaultExpr.GetHashCode () ^ base.GetHashCode ();
  91. }
  92. override public object Eval (DataRow row)
  93. {
  94. object val = expr.Eval (row);
  95. if (val == null || val == DBNull.Value)
  96. return defaultExpr.Eval (row);
  97. return val;
  98. }
  99. }
  100. internal class ConvertFunction : UnaryExpression {
  101. Type targetType;
  102. public ConvertFunction (IExpression e, string targetType) : base (e)
  103. {
  104. try {
  105. this.targetType = Type.GetType (targetType, true);
  106. } catch (TypeLoadException) {
  107. throw new EvaluateException (String.Format ("Invalid type name '{0}'.", targetType));
  108. }
  109. }
  110. public override bool Equals(object obj)
  111. {
  112. if (!base.Equals (obj))
  113. return false;
  114. if (!(obj is ConvertFunction))
  115. return false;
  116. ConvertFunction other = (ConvertFunction) obj;
  117. if (other.targetType != targetType)
  118. return false;
  119. return true;
  120. }
  121. public override int GetHashCode()
  122. {
  123. return targetType.GetHashCode () ^ base.GetHashCode ();
  124. }
  125. override public object Eval (DataRow row)
  126. {
  127. object val = expr.Eval (row);
  128. // TMPFIX :Eval shud never really return a null.. DBNull.Value but not null
  129. // needs to be done for all expressions .. for now ,just check for null
  130. if (val == null)
  131. return DBNull.Value;
  132. if (val == DBNull.Value || val.GetType () == targetType)
  133. return val;
  134. //--> String is always allowed
  135. if (targetType == typeof (string))
  136. return val.ToString();
  137. //only TimeSpan <--> String is allowed
  138. if (targetType == typeof (TimeSpan)) {
  139. if (val is string)
  140. return TimeSpan.Parse ((string)val);
  141. else
  142. ThrowInvalidCastException (val);
  143. }
  144. if (val is TimeSpan)
  145. ThrowInvalidCastException (val);
  146. //only Char <--> String/Int32/UInt32 is allowed
  147. if (val is Char && !(targetType == typeof (Int32) || targetType == typeof (UInt32)))
  148. ThrowInvalidCastException (val);
  149. if (targetType == typeof (Char) && !(val is Int32 || val is UInt32))
  150. ThrowInvalidCastException (val);
  151. //bool <--> Char/Single/Double/Decimal/TimeSpan/DateTime is not allowed
  152. if (val is Boolean && (targetType == typeof (Single) || targetType == typeof (Double) || targetType == typeof (Decimal)))
  153. ThrowInvalidCastException (val);
  154. if (targetType == typeof(Boolean) && (val is Single || val is Double || val is Decimal))
  155. ThrowInvalidCastException (val);
  156. //Convert throws the remaining invalid casts
  157. return Convert.ChangeType (val, targetType);
  158. }
  159. private void ThrowInvalidCastException (object val) {
  160. throw new InvalidCastException (String.Format ("Type '{0}' cannot be converted to '{1}'.", val.GetType(), targetType));
  161. }
  162. }
  163. }