Add missing assembly references to test targets
[mono.git] / mcs / class / System.Data / Mono.Data.SqlExpressions / StringFunctions.cs
1 //
2 // StringFunctions.cs
3 //
4 // Author:
5 //   Juraj Skripsky (juraj@hotfeet.ch)
6 //
7 // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
8 // Copyright 2011 Xamarin Inc.
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Collections;
36 using System.Data;
37
38 namespace Mono.Data.SqlExpressions {
39         internal abstract class StringFunction : UnaryExpression {
40                 protected StringFunction (IExpression e) : base (e) {}
41
42                 override public object Eval (DataRow row)
43                 {
44                         object val = expr.Eval (row);
45                         if(val == null)
46                                 return null;
47                                 
48                         if (!(val is string)) {
49                                 string fnct = this.GetType ().ToString ();
50                                 int start = fnct.LastIndexOf('.') + 1;
51                                 fnct = fnct.Substring (start, fnct.Length - start - "Function".Length);
52                                 throw new EvaluateException (String.Format ("'{0}' can be applied only to strings.", fnct));
53                         }
54                                 
55                         return val;
56                 }
57         }
58
59         internal class SubstringFunction : StringFunction {
60                 IExpression start;
61                 IExpression len;
62                 public SubstringFunction (IExpression e, IExpression start, IExpression len) : base (e)
63                 {
64                         this.start = start;
65                         this.len = len;
66                 }
67
68                 public override bool Equals(object obj)
69                 {
70                         if (!base.Equals (obj))
71                                 return false;
72
73                         if (!(obj is SubstringFunction))
74                                 return false;
75
76                         SubstringFunction other = (SubstringFunction) obj;
77                         if (other.start != start)
78                                 return false;
79
80                         if (other.len != len)
81                                 return false;           
82
83                         return true;
84                 }
85
86                 public override int GetHashCode()
87                 {
88                         int hashCode = base.GetHashCode ();
89                         hashCode ^= start.GetHashCode ();
90                         hashCode ^= len.GetHashCode ();
91                         return hashCode;
92                 }
93                 
94                 override public object Eval (DataRow row)
95                 {
96                         string str = (string)base.Eval (row);
97                         start.Eval (row);
98                         int istart = Convert.ToInt32 (start.Eval (row));
99                         int ilen = Convert.ToInt32 (len.Eval (row));
100                         
101                         if(str == null)
102                                 return null;
103                                 
104                         if (istart > str.Length)
105                                 return String.Empty;
106                         
107                         return str.Substring (istart - 1, System.Math.Min (ilen, str.Length - (istart - 1)));
108                 }
109         }
110         
111         internal class LenFunction : StringFunction {
112                 public LenFunction (IExpression e) : base (e) {}
113                 
114                 override public object Eval (DataRow row)
115                 {
116                         string str = (string)base.Eval (row);
117                         if(str == null)
118                                 return 0;
119                                 
120                         return str.Length;
121                 }
122         }
123
124         internal class TrimFunction : StringFunction {
125                 public TrimFunction (IExpression e) : base (e) {}
126                 
127                 override public object Eval (DataRow row)
128                 {
129                         string str = (string)base.Eval (row);
130                         if(str == null)
131                                 return null;
132                                 
133                         return str.Trim();
134                 }
135         }
136 }