File: Functions.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (199 lines) | stat: -rw-r--r-- 5,548 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// Functions.cs
//
// Author:
//   Juraj Skripsky (juraj@hotfeet.ch)
//
// (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
//

//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Data;

namespace Mono.Data.SqlExpressions {
	internal class IifFunction : UnaryExpression {
		IExpression trueExpr, falseExpr;
		public IifFunction (IExpression e, IExpression trueExpr, IExpression falseExpr) : base (e)
		{
			this.trueExpr = trueExpr;
			this.falseExpr = falseExpr;
		}

		public override bool Equals(object obj)
		{
			if (!base.Equals (obj))
				return false;

			if (!(obj is IifFunction))
				return false;

			IifFunction other = (IifFunction) obj;
			if (!other.falseExpr.Equals (falseExpr))
				return false;

			if (!other.trueExpr.Equals (trueExpr))
				return false;		

			return true;
		}

		public override int GetHashCode()
		{
			int hashCode = base.GetHashCode ();
			hashCode ^= falseExpr.GetHashCode ();
			hashCode ^= trueExpr.GetHashCode ();
			return hashCode;
		}
		
		override public object Eval (DataRow row)
		{
			object o = expr.Eval (row);
			if (o == DBNull.Value)
				return o;
			bool val = Convert.ToBoolean(o);
			return (val ? trueExpr.Eval (row) : falseExpr.Eval (row));
		}
	}
	
	internal class IsNullFunction : UnaryExpression {
		IExpression defaultExpr;
		public IsNullFunction (IExpression e, IExpression defaultExpr) : base (e)
		{
			this.defaultExpr = defaultExpr;
		}

		public override bool Equals(object obj)
		{
			if (!base.Equals (obj))
				return false;

			if (!(obj is UnaryExpression))
				return false;

			IsNullFunction other = (IsNullFunction) obj;
			if (!other.defaultExpr.Equals (defaultExpr))
				return false;		

			return true;
		}

		public override int GetHashCode()
		{
			return defaultExpr.GetHashCode () ^ base.GetHashCode ();
		}
		
		override public object Eval (DataRow row)
		{
			object val = expr.Eval (row);
			if (val == null || val == DBNull.Value)
				return defaultExpr.Eval (row);
			return val;
		}
	}
	
	internal class ConvertFunction : UnaryExpression {
		Type targetType;
		public ConvertFunction (IExpression e, string targetType) : base (e)
		{
			try {
				this.targetType = Type.GetType (targetType, true);
			} catch (TypeLoadException) {
				throw new EvaluateException (String.Format ("Invalid type name '{0}'.", targetType));
			}
		}

		public override bool Equals(object obj)
		{
			if (!base.Equals (obj))
				return false;

			if (!(obj is ConvertFunction))
				return false;

			ConvertFunction other = (ConvertFunction) obj;
			if (other.targetType != targetType)
				return false;		

			return true;
		}

		public override int GetHashCode()
		{
			return targetType.GetHashCode () ^ base.GetHashCode ();
		}
		
		override public object Eval (DataRow row)
		{
			object val = expr.Eval (row);
			
			// TMPFIX :Eval shud never really return a null.. DBNull.Value but not null
			// needs to be done for all expressions .. for now ,just check for null
			if (val == null)
				return DBNull.Value;

			if (val == DBNull.Value || val.GetType () == targetType)
				return val;

			//--> String is always allowed			
			if (targetType == typeof (string))
				return val.ToString();
				
			//only TimeSpan <--> String is allowed
			if (targetType == typeof (TimeSpan)) {
				if (val is string)
					return TimeSpan.Parse ((string)val);
				else
					ThrowInvalidCastException (val);
			}
			
			if (val is TimeSpan)
				ThrowInvalidCastException (val);
			
			//only Char <--> String/Int32/UInt32 is allowed
			if (val is Char && !(targetType == typeof (Int32) || targetType == typeof (UInt32)))
				ThrowInvalidCastException (val);
				
			if (targetType == typeof (Char) && !(val is Int32 || val is UInt32))
				ThrowInvalidCastException (val);

			//bool <--> Char/Single/Double/Decimal/TimeSpan/DateTime is not allowed
			if (val is Boolean && (targetType == typeof (Single) || targetType == typeof (Double) || targetType == typeof (Decimal)))
				ThrowInvalidCastException (val);
				
			if (targetType == typeof(Boolean) && (val is Single || val is Double || val is Decimal))
				ThrowInvalidCastException (val);

			//Convert throws the remaining invalid casts
			return Convert.ChangeType (val, targetType);

		}
		
		private void ThrowInvalidCastException (object val) {
			throw new InvalidCastException (String.Format ("Type '{0}' cannot be converted to '{1}'.", val.GetType(), targetType));
		}
	}
}