Author: joelreed
Date: 2007-10-01 17:00:51 -0400 (Mon, 01 Oct 2007)
New Revision: 86725

Modified:
   trunk/mcs/class/System.Data/ChangeLog
   trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/Functions.cs
   trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/Parser.jay
   trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/StringFunctions.cs
   
trunk/mcs/class/System.Data/Test/Mono.Data.SqlExpressions/DataColumnExpressionTest.cs
Log:
enhance parsing of Mono.Data.SqlExpressions's SUBSTRING and IIF


Modified: trunk/mcs/class/System.Data/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data/ChangeLog       2007-10-01 20:37:32 UTC (rev 
86724)
+++ trunk/mcs/class/System.Data/ChangeLog       2007-10-01 21:00:51 UTC (rev 
86725)
@@ -1,3 +1,15 @@
+2007-10-01  Joel Reed  <[EMAIL PROTECTED]>
+
+       * Mono.Data.SqlExpressions/Functions.cs: accept SingleColumnValues and 
+       BoolLiterals for IIF condition
+       * Mono.Data.SqlExpressions/Parser.jay: accept arithmetic expressions
+       for SUBSTRING start and length parameters, accept SingleColumnValue and 
+       BoolLiteral for IIF condition, fix copy and paste bug in IS NOT NULL
+       * Mono.Data.SqlExpressions/StringFunctions.cs: accept arithmetic 
expressions
+       for substring start and length parameters
+       * Test/Mono.Data.SqlExpressions/DataColumnExpressionTest.cs: add tests 
for 
+       IIF SingleColumnValue and BoolLiteral conditions, and SUBSTRING 
arithmetic expressions
+
 2007-09-27  Nagappan A <[EMAIL PROTECTED]> 
 
        * System.Data.dll.sources: Added new files SqlDataSourceConverter.cs,

Modified: trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/Functions.cs
===================================================================
--- trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/Functions.cs   
2007-10-01 20:37:32 UTC (rev 86724)
+++ trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/Functions.cs   
2007-10-01 21:00:51 UTC (rev 86725)
@@ -74,7 +74,7 @@
                        object o = expr.Eval (row);
                        if (o == DBNull.Value)
                                return o;
-                       bool val = (bool)o;
+                       bool val = Convert.ToBoolean(o);
                        return (val ? trueExpr.Eval (row) : falseExpr.Eval 
(row));
                }
        }

Modified: trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/Parser.jay
===================================================================
--- trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/Parser.jay     
2007-10-01 20:37:32 UTC (rev 86724)
+++ trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/Parser.jay     
2007-10-01 21:00:51 UTC (rev 86725)
@@ -250,6 +250,7 @@
 Function
        : CalcFunction
        | AggFunction
+       | StringFunction
        ;
 
 AggFunction
@@ -269,17 +270,28 @@
        | VAR           { $$ = AggregationFunction.Var; }
        ;
 
+StringExpr
+       : SingleColumnValue
+       | StringLiteral
+       | StringFunction
+       ;
+
+StringFunction
+       : TRIM PAROPEN StringExpr PARCLOSE
+       {
+               $$ = new TrimFunction ((IExpression)$3);
+       }
+       | SUBSTRING PAROPEN StringExpr COMMA ArithExpr COMMA ArithExpr PARCLOSE
+       {
+               $$ = new SubstringFunction ((IExpression)$3, (IExpression)$5, 
(IExpression)$7);
+       }
+       ;
+
 CalcFunction
-       : IIF PAROPEN BoolExpr COMMA Expr COMMA Expr PARCLOSE
+       : IIF PAROPEN Expr COMMA Expr COMMA Expr PARCLOSE
        {
                $$ = new IifFunction ((IExpression)$3, (IExpression)$5, 
(IExpression)$7);
        }
-       | SUBSTRING PAROPEN Expr COMMA NumberLiteral COMMA NumberLiteral 
PARCLOSE
-       {
-               long arg1 = (long) $5;
-               long arg2 = (long) $7;
-               $$ = new SubstringFunction ((IExpression)$3, 
Convert.ToInt32(arg1), Convert.ToInt32(arg2));
-       }
        | ISNULL PAROPEN Expr COMMA Expr PARCLOSE
        {
                $$ = new IsNullFunction ((IExpression)$3, (IExpression)$5);
@@ -288,10 +300,6 @@
        {
                $$ = new LenFunction ((IExpression)$3);
        }
-       | TRIM PAROPEN Expr PARCLOSE
-       {
-               $$ = new TrimFunction ((IExpression)$3);
-       }
        | CONVERT PAROPEN Expr COMMA TypeSpecifier PARCLOSE
        {
                $$ = new ConvertFunction ((IExpression)$3, (string)$5);
@@ -321,7 +329,7 @@
        }
        | ArithExpr NOT LIKE StringLiteral
        {
-               $$ = new Negation (new Like ((IExpression)$1, (string)$3));
+               $$ = new Negation (new Like ((IExpression)$1, (string)$4));
        }
        ;
 

Modified: 
trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/StringFunctions.cs
===================================================================
--- trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/StringFunctions.cs     
2007-10-01 20:37:32 UTC (rev 86724)
+++ trunk/mcs/class/System.Data/Mono.Data.SqlExpressions/StringFunctions.cs     
2007-10-01 21:00:51 UTC (rev 86725)
@@ -56,8 +56,9 @@
        }
        
        internal class SubstringFunction : StringFunction {
-               int start, len;
-               public SubstringFunction (IExpression e, int start, int len) : 
base (e)
+               IExpression start;
+               IExpression len;
+               public SubstringFunction (IExpression e, IExpression start, 
IExpression len) : base (e)
                {
                        this.start = start;
                        this.len = len;
@@ -92,13 +93,17 @@
                override public object Eval (DataRow row)
                {
                        string str = (string)base.Eval (row);
+                       object x = start.Eval (row);
+                       int istart = Convert.ToInt32 (start.Eval (row));
+                       int ilen = Convert.ToInt32 (len.Eval (row));
+                       
                        if(str == null)
                                return null;
                                
-                       if (start > str.Length)
+                       if (istart > str.Length)
                                return String.Empty;
                        
-                       return str.Substring (start - 1, System.Math.Min (len, 
str.Length - (start - 1)));
+                       return str.Substring (istart - 1, System.Math.Min 
(ilen, str.Length - (istart - 1)));
                }
        }
        

Modified: 
trunk/mcs/class/System.Data/Test/Mono.Data.SqlExpressions/DataColumnExpressionTest.cs
===================================================================
--- 
trunk/mcs/class/System.Data/Test/Mono.Data.SqlExpressions/DataColumnExpressionTest.cs
       2007-10-01 20:37:32 UTC (rev 86724)
+++ 
trunk/mcs/class/System.Data/Test/Mono.Data.SqlExpressions/DataColumnExpressionTest.cs
       2007-10-01 21:00:51 UTC (rev 86725)
@@ -8,6 +8,36 @@
        public class DataColumnExprTest
        {
                [Test]
+               public void TestDataColumnExpr0SingleColumnValue ()
+               {
+                       DataTable table = new DataTable ();
+                       table.Columns.Add ("Col_0.Value", Type.GetType 
("System.Int32"));
+                       table.Columns.Add ("Col_1", Type.GetType 
("System.Int32"));
+                       table.Columns.Add ("Result", Type.GetType 
("System.Int32"), "IIF(Col_0.Value, Col_1 + 5, 0)");
+
+                       DataRow row = table.NewRow ();
+                       row ["Col_0.Value"] = 0;
+                       row ["Col_1"] = 10;
+
+                       table.Rows.Add (row);
+                       Assert.AreEqual (0, (int)table.Rows[0][2], "#1");
+               }
+               [Test]
+               public void TestDataColumnExpr0Literal ()
+               {
+                       DataTable table = new DataTable ();
+                       table.Columns.Add ("Col_0.Value", Type.GetType 
("System.Int32"));
+                       table.Columns.Add ("Col_1", Type.GetType 
("System.Int32"));
+                       table.Columns.Add ("Result", Type.GetType 
("System.Int32"), "IIF(false, Col_1 + 5, 0)");
+
+                       DataRow row = table.NewRow ();
+                       row ["Col_0.Value"] = 0;
+                       row ["Col_1"] = 10;
+
+                       table.Rows.Add (row);
+                       Assert.AreEqual (0, (int)table.Rows[0][2], "#1");
+               }
+               [Test]
                public void TestDataColumnExpr1 ()
                {
                        DataTable table = new DataTable ();
@@ -37,6 +67,19 @@
                        table.Rows.Add (row);
                        Assert.AreEqual (0, (int)table.Rows[0][2], "#1");
                }
+               [Test]
+               public void TestDataColumnSubstring ()
+               {
+                       DataTable table = new DataTable ();
+                       table.Columns.Add ("Col_0", Type.GetType 
("System.String"));
+                       table.Columns.Add ("Result", Type.GetType 
("System.String"), "SUBSTRING(Col_0, 2+2, 2)");
+
+                       DataRow row = table.NewRow ();
+                       row ["Col_0"] = "Is OK?";
+
+                       table.Rows.Add (row);
+                       Assert.AreEqual ("OK", (string)table.Rows[0][1], "#1");
+               }
        }
        [TestFixture]
        public class DataColumnCharTest

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to