Functions.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // Functions.cs
  3. //
  4. // Author:
  5. // Juraj Skripsky ([email protected])
  6. //
  7. // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Data;
  12. namespace Mono.Data.SqlExpressions {
  13. public class IifFunction : UnaryExpression {
  14. IExpression trueExpr, falseExpr;
  15. public IifFunction (IExpression e, IExpression trueExpr, IExpression falseExpr) : base (e)
  16. {
  17. this.trueExpr = trueExpr;
  18. this.falseExpr = falseExpr;
  19. }
  20. override public object Eval (DataRow row)
  21. {
  22. bool val = (bool)expr.Eval (row);
  23. return (val ? trueExpr.Eval (row) : falseExpr.Eval (row));
  24. }
  25. }
  26. public class IsNullFunction : UnaryExpression {
  27. IExpression defaultExpr;
  28. public IsNullFunction (IExpression e, IExpression defaultExpr) : base (e)
  29. {
  30. this.defaultExpr = defaultExpr;
  31. }
  32. override public object Eval (DataRow row)
  33. {
  34. object val = expr.Eval (row);
  35. return (val != null ? val : defaultExpr.Eval (row));
  36. }
  37. }
  38. public class ConvertFunction : UnaryExpression {
  39. Type targetType;
  40. public ConvertFunction (IExpression e, string targetType) : base (e)
  41. {
  42. try {
  43. this.targetType = Type.GetType (targetType, true);
  44. } catch (TypeLoadException) {
  45. throw new EvaluateException (String.Format ("Invalid type name '{0}'.", targetType));
  46. }
  47. }
  48. override public object Eval (DataRow row)
  49. {
  50. object val = expr.Eval (row);
  51. if (val.GetType () == targetType)
  52. return val;
  53. //--> String is always allowed
  54. if (targetType == typeof (string))
  55. return val.ToString();
  56. //only TimeSpan <--> String is allowed
  57. if (targetType == typeof (TimeSpan)) {
  58. if (val is string)
  59. return TimeSpan.Parse ((string)val);
  60. else
  61. ThrowInvalidCastException (val);
  62. }
  63. if (val is TimeSpan)
  64. ThrowInvalidCastException (val);
  65. //only Char <--> String/Int32/UInt32 is allowed
  66. if (val is Char && !(targetType == typeof (Int32) || targetType == typeof (UInt32)))
  67. ThrowInvalidCastException (val);
  68. if (targetType == typeof (Char) && !(val is Int32 || val is UInt32))
  69. ThrowInvalidCastException (val);
  70. //bool <--> Char/Single/Double/Decimal/TimeSpan/DateTime is not allowed
  71. if (val is Boolean && (targetType == typeof (Single) || targetType == typeof (Double) || targetType == typeof (Decimal)))
  72. ThrowInvalidCastException (val);
  73. if (targetType == typeof(Boolean) && (val is Single || val is Double || val is Decimal))
  74. ThrowInvalidCastException (val);
  75. //Convert throws the remaining invalid casts
  76. return Convert.ChangeType (val, targetType);
  77. }
  78. private void ThrowInvalidCastException (object val) {
  79. throw new InvalidCastException (String.Format ("Type '{0}' cannot be converted to '{1}'.", val.GetType(), targetType));
  80. }
  81. }
  82. }