2004-03-31 Juraj Skripsky <juraj@hotfeet.ch>
[mono.git] / mcs / class / System.Data / Mono.Data.SqlExpressions / Comparison.cs
1 //
2 // Comparison.cs
3 //
4 // Author:
5 //   Juraj Skripsky (juraj@hotfeet.ch)
6 //
7 // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
8 //
9
10 using System;
11 using System.Data;
12
13 namespace Mono.Data.SqlExpressions {
14         public class Comparison : BinaryOpExpression {
15                 public Comparison (Operation op, IExpression e1, IExpression e2) : base (op, e1, e2) {}
16         
17                 override public object Eval (DataRow row)
18                 {
19                         IComparable o1 = (IComparable)expr1.Eval (row);
20                         IComparable o2 = (IComparable)expr2.Eval (row);
21
22                         if (o1 == null || o2 == null) {
23                                 if (o1 == null && o2 == null)
24                                         return (op == Operation.EQ);
25                                 else
26                                         return (op == Operation.NE);
27                         }
28
29                         switch(Compare (o1, o2, row.Table.CaseSensitive)) {
30                         case -1:
31                                 return (op == Operation.NE || op == Operation.LE || op == Operation.LT);
32                         case 0:
33                         default:
34                                 return (op == Operation.EQ || op == Operation.LE || op == Operation.GE);
35                         case 1:
36                                 return (op == Operation.NE || op == Operation.GE || op == Operation.GT);
37                         }
38                 }
39                         
40                 internal static int Compare (IComparable o1, IComparable o2, bool caseSensitive)
41                 {
42                         //TODO: turn this "conversion pipeline" into something nicer
43
44                         try {
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()));
51                         }
52
53                         if (o1 is string && o2 is string && !caseSensitive) {
54                                 o1 = ((string)o1).ToLower();
55                                 o2 = ((string)o2).ToLower();
56                         }
57                         
58                         if (o1.GetType () != o2.GetType ())
59                                 o2 = (IComparable)Convert.ChangeType (o2, o1.GetType ());
60
61                         return o1.CompareTo (o2);
62                 }
63         }
64 }