(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Data / Mono.Data.SqlExpressions / Functions.cs
blob92c826902f19e5b4f211371ff9d2ac0e2dda5f57
1 //
2 // Functions.cs
3 //
4 // Author:
5 // Juraj Skripsky (juraj@hotfeet.ch)
6 //
7 // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
8 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System;
34 using System.Collections;
35 using System.Data;
37 namespace Mono.Data.SqlExpressions {
38 internal class IifFunction : UnaryExpression {
39 IExpression trueExpr, falseExpr;
40 public IifFunction (IExpression e, IExpression trueExpr, IExpression falseExpr) : base (e)
42 this.trueExpr = trueExpr;
43 this.falseExpr = falseExpr;
46 override public object Eval (DataRow row)
48 bool val = (bool)expr.Eval (row);
49 return (val ? trueExpr.Eval (row) : falseExpr.Eval (row));
53 internal class IsNullFunction : UnaryExpression {
54 IExpression defaultExpr;
55 public IsNullFunction (IExpression e, IExpression defaultExpr) : base (e)
57 this.defaultExpr = defaultExpr;
60 override public object Eval (DataRow row)
62 object val = expr.Eval (row);
63 return (val != null ? val : defaultExpr.Eval (row));
67 internal class ConvertFunction : UnaryExpression {
68 Type targetType;
69 public ConvertFunction (IExpression e, string targetType) : base (e)
71 try {
72 this.targetType = Type.GetType (targetType, true);
73 } catch (TypeLoadException) {
74 throw new EvaluateException (String.Format ("Invalid type name '{0}'.", targetType));
78 override public object Eval (DataRow row)
80 object val = expr.Eval (row);
82 if (val.GetType () == targetType)
83 return val;
85 //--> String is always allowed
86 if (targetType == typeof (string))
87 return val.ToString();
89 //only TimeSpan <--> String is allowed
90 if (targetType == typeof (TimeSpan)) {
91 if (val is string)
92 return TimeSpan.Parse ((string)val);
93 else
94 ThrowInvalidCastException (val);
97 if (val is TimeSpan)
98 ThrowInvalidCastException (val);
100 //only Char <--> String/Int32/UInt32 is allowed
101 if (val is Char && !(targetType == typeof (Int32) || targetType == typeof (UInt32)))
102 ThrowInvalidCastException (val);
104 if (targetType == typeof (Char) && !(val is Int32 || val is UInt32))
105 ThrowInvalidCastException (val);
107 //bool <--> Char/Single/Double/Decimal/TimeSpan/DateTime is not allowed
108 if (val is Boolean && (targetType == typeof (Single) || targetType == typeof (Double) || targetType == typeof (Decimal)))
109 ThrowInvalidCastException (val);
111 if (targetType == typeof(Boolean) && (val is Single || val is Double || val is Decimal))
112 ThrowInvalidCastException (val);
114 //Convert throws the remaining invalid casts
115 return Convert.ChangeType (val, targetType);
119 private void ThrowInvalidCastException (object val) {
120 throw new InvalidCastException (String.Format ("Type '{0}' cannot be converted to '{1}'.", val.GetType(), targetType));