5 // Juraj Skripsky (juraj@hotfeet.ch)
7 // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
8 // Copyright 2011 Xamarin Inc.
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
35 using System.Collections;
38 namespace Mono.Data.SqlExpressions {
39 internal abstract class StringFunction : UnaryExpression {
40 protected StringFunction (IExpression e) : base (e) {}
42 override public object Eval (DataRow row)
44 object val = expr.Eval (row);
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));
59 internal class SubstringFunction : StringFunction {
62 public SubstringFunction (IExpression e, IExpression start, IExpression len) : base (e)
68 public override bool Equals(object obj)
70 if (!base.Equals (obj))
73 if (!(obj is SubstringFunction))
76 SubstringFunction other = (SubstringFunction) obj;
77 if (other.start != start)
86 public override int GetHashCode()
88 int hashCode = base.GetHashCode ();
89 hashCode ^= start.GetHashCode ();
90 hashCode ^= len.GetHashCode ();
94 override public object Eval (DataRow row)
96 string str = (string)base.Eval (row);
98 int istart = Convert.ToInt32 (start.Eval (row));
99 int ilen = Convert.ToInt32 (len.Eval (row));
104 if (istart > str.Length)
107 return str.Substring (istart - 1, System.Math.Min (ilen, str.Length - (istart - 1)));
111 internal class LenFunction : StringFunction {
112 public LenFunction (IExpression e) : base (e) {}
114 override public object Eval (DataRow row)
116 string str = (string)base.Eval (row);
124 internal class TrimFunction : StringFunction {
125 public TrimFunction (IExpression e) : base (e) {}
127 override public object Eval (DataRow row)
129 string str = (string)base.Eval (row);