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.
35 using System.Threading;
36 using System.Globalization;
38 namespace Mono.Data.SqlExpressions {
39 internal class Comparison : BinaryOpExpression {
40 public Comparison (Operation op, IExpression e1, IExpression e2) : base (op, e1, e2) {}
42 override public object Eval (DataRow row)
44 return EvalBoolean (row);
47 public override bool EvalBoolean (DataRow row)
49 IComparable o1 = expr1.Eval (row) as IComparable;
50 IComparable o2 = expr2.Eval (row) as IComparable;
52 if (o1 == null || o2 == null) {
53 if (o1 == null && o2 == null)
54 return (op == Operation.EQ);
56 return (op == Operation.NE);
59 int result = Compare (o1, o2, row.Table.CaseSensitive);
61 return (op == Operation.NE || op == Operation.LE || op == Operation.LT);
64 return (op == Operation.NE || op == Operation.GE || op == Operation.GT);
67 return (op == Operation.EQ || op == Operation.LE || op == Operation.GE);
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 };
74 internal static int Compare (IComparable o1, IComparable o2, bool caseSensitive)
76 //TODO: turn this "conversion pipeline" into something nicer
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)
85 else if (o2 is string && o1 is Guid)
87 } catch (FormatException) {
88 throw new EvaluateException (String.Format ("Cannot perform compare operation on {0} and {1}.", o1.GetType(), o2.GetType()));
91 if (o1 is string && o2 is string) {
92 o1 = ((string) o1).TrimEnd (IgnoredTrailingChars);
93 o2 = ((string) o2).TrimEnd (IgnoredTrailingChars);
95 o1 = ((string) o1).ToLower ();
96 o2 = ((string) o2).ToLower ();
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);
105 if (o1.GetType () != o2.GetType ())
106 o2 = (IComparable)Convert.ChangeType (o2, o1.GetType ());
108 return o1.CompareTo (o2);