2009-08-07 Rodrigo Kumpera <rkumpera@novell.com>
[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 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System;
34 using System.Data;
35 using System.Threading;
36 using System.Globalization;
37
38 namespace Mono.Data.SqlExpressions {
39         internal class Comparison : BinaryOpExpression {
40                 public Comparison (Operation op, IExpression e1, IExpression e2) : base (op, e1, e2) {}
41         
42                 override public object Eval (DataRow row)
43                 {
44                         return EvalBoolean (row);
45                 }
46
47                 public override bool EvalBoolean (DataRow row)
48                 {
49                         IComparable o1 = expr1.Eval (row) as IComparable;
50                         IComparable o2 = expr2.Eval (row) as IComparable;
51
52                         if (o1 == null || o2 == null) {
53                                 if (o1 == null && o2 == null)
54                                         return (op == Operation.EQ);
55                                 else
56                                         return (op == Operation.NE);
57                         }
58
59                         int result = Compare (o1, o2, row.Table.CaseSensitive);
60                         if (result < 0) {
61                                 return (op == Operation.NE || op == Operation.LE || op == Operation.LT);
62                         }
63                         if (result > 0) {
64                                 return (op == Operation.NE || op == Operation.GE || op == Operation.GT);
65                         }
66                         // result == 0
67                         return (op == Operation.EQ || op == Operation.LE || op == Operation.GE);
68                         
69                 }
70                         
71                 // certain trailing whitespace chars (including space) are ignored by .NET when comparing strings. See bug #79695.  
72                 private static readonly char[] IgnoredTrailingChars = { (char) 0x20, (char) 0x3000, (char) 0xFEFF };
73
74                 internal static int Compare (IComparable o1, IComparable o2, bool caseSensitive)
75                 {
76                         //TODO: turn this "conversion pipeline" into something nicer
77
78                         try {
79                                 if (o1 is string && Numeric.IsNumeric (o2))
80                                         o1 = (IComparable) Convert.ChangeType (o1, o2.GetType ());
81                                 else if (o2 is string && Numeric.IsNumeric (o1))
82                                         o2 = (IComparable) Convert.ChangeType (o2, o1.GetType ());
83                                 else if (o1 is string && o2 is Guid)
84                                         o2 = o2.ToString ();
85                                 else if (o2 is string && o1 is Guid)
86                                         o1 = o1.ToString ();
87                         } catch (FormatException) {
88                                 throw new EvaluateException (String.Format ("Cannot perform compare operation on {0} and {1}.", o1.GetType(), o2.GetType()));
89                         }
90
91                         if (o1 is string && o2 is string) {
92                                 o1 = ((string) o1).TrimEnd (IgnoredTrailingChars);
93                                 o2 = ((string) o2).TrimEnd (IgnoredTrailingChars);
94                                 if (!caseSensitive) {
95                                         o1 = ((string) o1).ToLower ();
96                                         o2 = ((string) o2).ToLower ();
97                                 }
98                         }
99
100                         if (o1 is DateTime && o2 is string && Thread.CurrentThread.CurrentCulture != CultureInfo.InvariantCulture) {
101                                 // DateTime is always CultureInfo.InvariantCulture
102                                 o2 = (IComparable) DateTime.Parse ((string)o2, CultureInfo.InvariantCulture);
103                         }
104
105                         if (o1.GetType () != o2.GetType ())
106                                 o2 = (IComparable)Convert.ChangeType (o2, o1.GetType ());
107
108                         return o1.CompareTo (o2);
109                 }
110         }
111 }