5 // Juraj Skripsky (juraj@hotfeet.ch)
7 // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
13 namespace Mono.Data.SqlExpressions {
14 public class Comparison : BinaryOpExpression {
15 public Comparison (Operation op, IExpression e1, IExpression e2) : base (op, e1, e2) {}
17 override public object Eval (DataRow row)
19 IComparable o1 = (IComparable)expr1.Eval (row);
20 IComparable o2 = (IComparable)expr2.Eval (row);
22 if (o1 == null || o2 == null) {
23 if (o1 == null && o2 == null)
24 return (op == Operation.EQ);
26 return (op == Operation.NE);
29 switch(Compare (o1, o2, row.Table.CaseSensitive)) {
31 return (op == Operation.NE || op == Operation.LE || op == Operation.LT);
34 return (op == Operation.EQ || op == Operation.LE || op == Operation.GE);
36 return (op == Operation.NE || op == Operation.GE || op == Operation.GT);
40 internal static int Compare (IComparable o1, IComparable o2, bool caseSensitive)
42 //TODO: turn this "conversion pipeline" into something nicer
45 if (o1 is string && Numeric.IsNumeric (o2))
46 o1 = (IComparable)Convert.ChangeType (o1, o2.GetType ());
47 if (o2 is string && Numeric.IsNumeric (o1))
48 o2 = (IComparable)Convert.ChangeType (o2, o1.GetType ());
49 } catch (FormatException) {
50 throw new EvaluateException (String.Format ("Cannot perform compare operation on {0} and {1}.", o1.GetType(), o2.GetType()));
53 if (o1 is string && o2 is string && !caseSensitive) {
54 o1 = ((string)o1).ToLower();
55 o2 = ((string)o2).ToLower();
58 if (o1.GetType () != o2.GetType ())
59 o2 = (IComparable)Convert.ChangeType (o2, o1.GetType ());
61 return o1.CompareTo (o2);