2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Data / Mono.Data.SqlExpressions / Functions.cs
1 //
2 // Functions.cs
3 //
4 // Author:
5 //   Juraj Skripsky (juraj@hotfeet.ch)
6 //
7 // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Data;
36
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)
41                 {
42                         this.trueExpr = trueExpr;
43                         this.falseExpr = falseExpr;
44                 }
45                 
46                 override public object Eval (DataRow row)
47                 {
48                         bool val = (bool)expr.Eval (row);
49                         return (val ? trueExpr.Eval (row) : falseExpr.Eval (row));
50                 }
51         }
52         
53         internal class IsNullFunction : UnaryExpression {
54                 IExpression defaultExpr;
55                 public IsNullFunction (IExpression e, IExpression defaultExpr) : base (e)
56                 {
57                         this.defaultExpr = defaultExpr;
58                 }
59                 
60                 override public object Eval (DataRow row)
61                 {
62                         object val = expr.Eval (row);
63                         return (val != null ? val : defaultExpr.Eval (row));
64                 }
65         }
66         
67         internal class ConvertFunction : UnaryExpression {
68                 Type targetType;
69                 public ConvertFunction (IExpression e, string targetType) : base (e)
70                 {
71                         try {
72                                 this.targetType = Type.GetType (targetType, true);
73                         } catch (TypeLoadException) {
74                                 throw new EvaluateException (String.Format ("Invalid type name '{0}'.", targetType));
75                         }
76                 }
77                 
78                 override public object Eval (DataRow row)
79                 {
80                         object val = expr.Eval (row);
81                         
82                         if (val.GetType () == targetType)
83                                 return val;
84
85                         //--> String is always allowed                  
86                         if (targetType == typeof (string))
87                                 return val.ToString();
88                                 
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);
95                         }
96                         
97                         if (val is TimeSpan)
98                                 ThrowInvalidCastException (val);
99                         
100                         //only Char <--> String/Int32/UInt32 is allowed
101                         if (val is Char && !(targetType == typeof (Int32) || targetType == typeof (UInt32)))
102                                 ThrowInvalidCastException (val);
103                                 
104                         if (targetType == typeof (Char) && !(val is Int32 || val is UInt32))
105                                 ThrowInvalidCastException (val);
106
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);
110                                 
111                         if (targetType == typeof(Boolean) && (val is Single || val is Double || val is Decimal))
112                                 ThrowInvalidCastException (val);
113
114                         //Convert throws the remaining invalid casts
115                         return Convert.ChangeType (val, targetType);
116
117                 }
118                 
119                 private void ThrowInvalidCastException (object val) {
120                         throw new InvalidCastException (String.Format ("Type '{0}' cannot be converted to '{1}'.", val.GetType(), targetType));
121                 }
122         }
123 }