5 // Juraj Skripsky (juraj@hotfeet.ch)
7 // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Data.Common;
35 using System.Collections;
38 namespace Mono.Data.SqlExpressions {
39 internal interface IExpression {
40 object Eval (DataRow row);
41 bool DependsOn(DataColumn other);
43 bool EvalBoolean (DataRow row);
44 void ResetExpression ();
47 internal abstract class BaseExpression : IExpression {
48 public abstract object Eval (DataRow row);
49 public abstract bool DependsOn(DataColumn other);
51 public virtual bool EvalBoolean (DataRow row)
53 throw new EvaluateException ("Not a Boolean Expression");
56 public override bool Equals(object obj)
58 if (obj == null || !(obj is BaseExpression))
64 public override int GetHashCode()
69 public virtual void ResetExpression ()
74 // abstract base classes
75 internal abstract class UnaryExpression : BaseExpression {
76 protected IExpression expr;
78 public override bool Equals(object obj)
80 if (!base.Equals (obj))
83 if (!(obj is UnaryExpression))
86 UnaryExpression other = (UnaryExpression) obj;
87 if (!other.expr.Equals (expr))
93 public override int GetHashCode()
95 return base.GetHashCode () ^ expr.GetHashCode ();
98 public UnaryExpression (IExpression e)
103 override public bool DependsOn(DataColumn other) {
104 return expr.DependsOn(other);
107 override public bool EvalBoolean (DataRow row)
109 return (bool) Eval (row);
113 internal abstract class BinaryExpression : BaseExpression {
114 protected IExpression expr1, expr2;
116 public override bool Equals(object obj)
118 if (!base.Equals (obj))
121 if (!(obj is BinaryExpression))
124 BinaryExpression other = (BinaryExpression) obj;
125 if (!other.expr1.Equals (expr1) || !other.expr2.Equals (expr2))
131 public override int GetHashCode()
133 int hashCode = base.GetHashCode ();
134 hashCode ^= expr1.GetHashCode ();
135 hashCode ^= expr2.GetHashCode ();
139 protected BinaryExpression (IExpression e1, IExpression e2)
144 override public bool DependsOn(DataColumn other)
146 return expr1.DependsOn(other) || expr2.DependsOn(other);
149 override public void ResetExpression ()
151 expr1.ResetExpression ();
152 expr2.ResetExpression ();
156 internal enum Operation {
158 EQ, NE, LT, LE, GT, GE,
159 ADD, SUB, MUL, DIV, MOD
162 internal abstract class BinaryOpExpression : BinaryExpression {
163 protected Operation op;
165 public override bool Equals(object obj)
167 if (!base.Equals (obj))
170 if (!(obj is BinaryOpExpression))
173 BinaryOpExpression other = (BinaryOpExpression) obj;
180 public override int GetHashCode()
182 return base.GetHashCode () ^ op.GetHashCode ();
185 protected BinaryOpExpression (Operation op, IExpression e1, IExpression e2) : base (e1, e2)