Hi *,

while checking some test failures I noted that when string concatenation
in the form of "+" operator is used in a query the resulting SQL does
not use the "||" operator but "+" instead (and fails on PosgreSQL and
maybe other backends). The attached patch fixes this in the expression
analyzer that now generates a SpecialExpression of type Concat when the
left and right operands are both strings.

This resulted in an error in SpecialExpressionTranslator.cs where
TranslateConcat is wrong (Add expression does not work with Strings) but
nobody ever noticed because I guess that code never got called. I fixed
that too generating the correct String.Concat() call expression.

No tests because the tests are already there (and now pass).

Ok to commit?

federico

-- 
Federico Di Gregorio                         http://people.initd.org/fog
Debian GNU/Linux Developer                                [email protected]
INIT.D Developer                                           [email protected]
  Spesso crescere ed andare a vivere da soli è l'unico modo di restare
   bambini.                                             -- Alice Fontana

Index: src/DbLinq/Data/Linq/Sugar/Implementation/ExpressionDispatcher.Analyzer.cs
===================================================================
--- src/DbLinq/Data/Linq/Sugar/Implementation/ExpressionDispatcher.Analyzer.cs	(revisione 1184)
+++ src/DbLinq/Data/Linq/Sugar/Implementation/ExpressionDispatcher.Analyzer.cs	(copia locale)
@@ -79,9 +79,10 @@
                     return AnalyzeQuote(expression, parameters, builderContext);
                 case ExpressionType.MemberAccess:
                     return AnalyzeMember(expression, builderContext);
-                #region case ExpressionType.<Common operators>:
                 case ExpressionType.Add:
                 case ExpressionType.AddChecked:
+                    return AnalyzeAddOperator(expression, builderContext);
+                #region case ExpressionType.<Common operators>:
                 case ExpressionType.Divide:
                 case ExpressionType.Modulo:
                 case ExpressionType.Multiply:
@@ -1005,6 +1006,18 @@
             return expression.ChangeOperands(operands);
         }
 
+        protected virtual Expression AnalyzeAddOperator(Expression expression, BuilderContext builderContext)
+        {
+            var operands = expression.GetOperands().ToList();           
+            operands[0] = Analyze(operands[0], builderContext);
+            operands[1] = Analyze(operands[1], builderContext);
+            
+            if (expression.Type == typeof(String))
+                return new SpecialExpression(SpecialExpressionType.Concat, operands[0], operands[1]);
+            else
+                return expression.ChangeOperands(operands);
+        }
+        
         protected virtual Expression AnalyzeNewOperator(Expression expression, BuilderContext builderContext)
         {
             if (builderContext.ExpectMetaTableDefinition)
Index: src/DbLinq/Data/Linq/Sugar/Implementation/SpecialExpressionTranslator.cs
===================================================================
--- src/DbLinq/Data/Linq/Sugar/Implementation/SpecialExpressionTranslator.cs	(revisione 1184)
+++ src/DbLinq/Data/Linq/Sugar/Implementation/SpecialExpressionTranslator.cs	(copia locale)
@@ -28,6 +28,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq.Expressions;
+using System.Reflection;
 
 using DbLinq.Data.Linq.Sugar;
 using DbLinq.Data.Linq.Sugar.ExpressionMutator;
@@ -213,9 +214,11 @@
         //    return Expression.Call(operands[0], typeof(string).GetMethod("ToUpper", new Type[0]));
         //}
 
+        static readonly MethodInfo TranslateConcat_Concat = typeof(String).GetMethod("Concat", new Type[2] { typeof(String), typeof(String) });
+        
         protected virtual Expression TranslateConcat(List<Expression> operands)
         {
-            return Expression.Add(operands[0], operands[1]);
+            return Expression.Call(null, TranslateConcat_Concat, operands[0], operands[1]);
         }
 
         protected virtual Expression TranslateIsNotNull(List<Expression> operands)

Attachment: signature.asc
Description: Questa è una parte del messaggio firmata digitalmente

Reply via email to