SqlFunctionExpression holds the sql specific function with its parameters.
상속: Remotion.Linq.Clauses.Expressions.ExtensionExpression
    public void Transform ()
    {
      var method = typeof (string).GetMethod ("Trim", new Type[] { });
      var objectExpression = Expression.Constant ("Test");
      var expression = Expression.Call (objectExpression, method);
      var transformer = new TrimMethodCallTransformer ();
      
      var result = transformer.Transform (expression);

      var expectedResult = new SqlFunctionExpression(typeof(string), "LTRIM", new SqlFunctionExpression (typeof (string), "RTRIM", objectExpression));

      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
        public static Expression Escape(Expression expression, string escapeSequence)
        {
            ArgumentUtility.CheckNotNull("expression", expression);
            ArgumentUtility.CheckNotNull("escapeSequence", escapeSequence);

            SqlFunctionExpression result = Escape(expression, escapeSequence, escapeSequence);

            result = Escape(result, "%", escapeSequence);
            result = Escape(result, "_", escapeSequence);
            result = Escape(result, "[", escapeSequence);

            return(result);
        }
    public void Transform ()
    {
      var method = typeof (string).GetMethod ("Insert", new[] { typeof(int), typeof(string) });
      var objectExpression = Expression.Constant ("Test");
      var argument1 = Expression.Constant(3);
      var argument2 = Expression.Constant("what");
      var expression = Expression.Call (objectExpression, method, argument1, argument2);
      var transformer = new InsertMethodCallTransformer ();

      var result = transformer.Transform (expression);

      var expectedTestExpression = Expression.Equal (new SqlLengthExpression (objectExpression), Expression.Add (argument1, new SqlLiteralExpression (1)));
      var concatMethod = typeof (string).GetMethod ("Concat", new[] { typeof (string), typeof (string) });
      var expectedThenExpression = Expression.Add (objectExpression, argument2, concatMethod);
      var expectedElseExpression = new SqlFunctionExpression (typeof (string), "STUFF", objectExpression, Expression.Add (argument1, new SqlLiteralExpression (1)), new SqlLiteralExpression(0), argument2);
      var expectedResult = Expression.Condition (expectedTestExpression, expectedThenExpression, expectedElseExpression);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public void Transform_WithOneArgument ()
    {
      var method = typeof (string).GetMethod ("Remove", new [] { typeof (int) });
      var objectExpression = Expression.Constant ("Test");
      var expression = Expression.Call (objectExpression, method, Expression.Constant (1));

      var transformer = new RemoveMethodCallTransformer();
      var result = transformer.Transform (expression);

      var expectedResult = new SqlFunctionExpression (
          expression.Type,
          "STUFF",
          objectExpression,
          Expression.Add (Expression.Constant (1), new SqlLiteralExpression (1)),
          new SqlLengthExpression (objectExpression),
          new SqlLiteralExpression (""));

      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public void Transform_WithTwoArgument ()
    {
      var method = typeof (string).GetMethod ("Substring", new[] { typeof (int), typeof (int) });
      var objectExpression = Expression.Constant ("Test");
      var expression = Expression.Call (objectExpression, method, Expression.Constant (1), Expression.Constant (3));

      var transformer = new SubstringMethodCallTransformer();
      var result = transformer.Transform (expression);

      var expectedResult = new SqlFunctionExpression (
          expression.Type,
          "SUBSTRING",
          objectExpression,
          Expression.Add (Expression.Constant (1), new SqlLiteralExpression (1)),
          Expression.Constant (3)
          );

      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public void Transform_OneArgument ()
    {
      var method = typeof (StringExtensions).GetMethod (
          "SqlContainsFulltext",
          BindingFlags.Public | BindingFlags.Static,
          null,
          CallingConventions.Any,
          new[] { typeof (string), typeof (string) },
          null);
      var objectExpression = Expression.Constant ("Test");
      var argument1 = Expression.Constant ("es");
      var expression = Expression.Call (method, objectExpression, argument1);
      var transformer = new ContainsFreetextMethodCallTransformer();

      var result = transformer.Transform (expression);

      var rightExpression = Expression.Constant (string.Format ("{0}", argument1.Value));
      var expectedResult = new SqlFunctionExpression (typeof (bool), "FREETEXT", objectExpression, rightExpression);

      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public void Transform_ArgumentIsNotNullAndIsNoConstantValue ()
    {
      var method = typeof (string).GetMethod ("Contains", new[] { typeof (string) });
      var objectExpression = Expression.Constant ("Test");
      var argument1 = Expression.MakeMemberAccess (Expression.Constant (new Cook()), typeof (Cook).GetProperty ("Name"));
      var expression = Expression.Call (objectExpression, method, argument1);
      var transformer = new ContainsMethodCallTransformer();

      var result = transformer.Transform (expression);

      Expression rightExpression = new SqlFunctionExpression (
          typeof (string),
          "REPLACE",
          new SqlFunctionExpression (
              typeof (string),
              "REPLACE",
                 new SqlFunctionExpression (
                      typeof (string),
                      "REPLACE",
                      new SqlFunctionExpression (
                          typeof (string),
                          "REPLACE",
                          argument1,
                          new SqlLiteralExpression (@"\"),
                          new SqlLiteralExpression (@"\\")),
                      new SqlLiteralExpression (@"%"),
                      new SqlLiteralExpression (@"\%")),
                  new SqlLiteralExpression (@"_"),
                  new SqlLiteralExpression (@"\_")),
              new SqlLiteralExpression (@"["),
              new SqlLiteralExpression (@"\["));
      rightExpression = Expression.Add (
          new SqlLiteralExpression ("%"), rightExpression, typeof (string).GetMethod ("Concat", new[] { typeof (string), typeof (string) }));
      rightExpression = Expression.Add (
          rightExpression, new SqlLiteralExpression ("%"), typeof (string).GetMethod ("Concat", new[] { typeof (string), typeof (string) }));
      var expectedResult = new SqlLikeExpression (objectExpression, rightExpression, new SqlLiteralExpression (@"\"));

      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public void Transform_Add_TimeSpan ()
    {
      var value = Expression.Constant (new TimeSpan (123456789L));
      var methodInfo = ReflectionUtility.GetMethod (() => DateTime.Now.Add (new TimeSpan()));
      var expression = Expression.Call (_dateTimeInstance, methodInfo, value);

      var result = _transformer.Transform (expression);

      var expectedResult = new SqlFunctionExpression (
          typeof (DateTime),
          "DATEADD",
          new SqlCustomTextExpression ("millisecond", typeof (string)),
          Expression.Modulo (
              Expression.Divide (Expression.Constant (123456789L), new SqlLiteralExpression (10000L)), new SqlLiteralExpression (86400000L)),
          new SqlFunctionExpression (
              typeof (DateTime),
              "DATEADD",
              new SqlCustomTextExpression ("day", typeof (string)),
              Expression.Divide (
                  Expression.Divide (Expression.Constant (123456789L), new SqlLiteralExpression (10000L)), new SqlLiteralExpression (86400000L)),
              _dateTimeInstance));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public Expression Transform (MethodCallExpression methodCallExpression)
    {
      ArgumentUtility.CheckNotNull ("methodCallExpression", methodCallExpression);

      MethodCallTransformerUtility.CheckArgumentCount (methodCallExpression, 2);
      MethodCallTransformerUtility.CheckInstanceMethod (methodCallExpression);
      MethodCallTransformerUtility.CheckConstantExpression ("Insert", methodCallExpression.Arguments[0], "insertionIndex");

      var insertionIndexExpression = Expression.Add (methodCallExpression.Arguments[0], new SqlLiteralExpression (1));
      var testExpression = Expression.Equal(new SqlLengthExpression (methodCallExpression.Object), insertionIndexExpression);

      var concatMethod = typeof (string).GetMethod ("Concat", new[] { typeof (string), typeof (string) });
      var thenExpression = Expression.Add (methodCallExpression.Object, methodCallExpression.Arguments[1], concatMethod);

      var elseExpression = new SqlFunctionExpression (
          methodCallExpression.Type,
          "STUFF",
          methodCallExpression.Object,
          insertionIndexExpression,
          new SqlLiteralExpression (0),
          methodCallExpression.Arguments[1]);

      return Expression.Condition(testExpression, thenExpression, elseExpression);
    }
    public void Transform_TwoArguments ()
    {
      var method = typeof (StringExtensions).GetMethod (
          "SqlContainsFulltext",
          BindingFlags.Public | BindingFlags.Static,
          null,
          CallingConventions.Any,
          new[] { typeof (string), typeof (string), typeof (string) },
          null);
      var objectExpression = Expression.Constant ("Test");
      var argument1 = Expression.Constant ("es");
      var language = Expression.Constant ("language");
      var expression = Expression.Call (method, objectExpression, argument1, language);
      var transformer = new ContainsFreetextMethodCallTransformer();

      var result = transformer.Transform (expression);

      var argumentExpression = Expression.Constant (string.Format ("{0}", argument1.Value));

      var compositeExpression = new SqlCompositeCustomTextGeneratorExpression (
          typeof (string), new SqlCustomTextExpression ("LANGUAGE ", typeof (string)), language);

      var expectedResult =
          new SqlFunctionExpression (typeof (bool), "FREETEXT", objectExpression, argumentExpression, compositeExpression);

      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
 public Expression VisitSqlFunctionExpression (SqlFunctionExpression expression)
 {
   return VisitChildrenWithGivenSemantics (expression, SqlExpressionContext.SingleValueRequired);
 }
    public void VisitSqlFunctionExpression ()
    {
      var sqlFunctionExpression = new SqlFunctionExpression (typeof (int), "LENFUNC", new SqlLiteralExpression ("test"), new SqlLiteralExpression (1));

      SqlGeneratingExpressionVisitor.GenerateSql (
          sqlFunctionExpression, _commandBuilder, _stageMock);

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("LENFUNC('test', 1)"));
    }
    public void VisitSqlFunctionExpression ()
    {
      var expression = new SqlFunctionExpression (typeof (int), "Test", Expression.Constant (true));
      var expectedResult = new SqlFunctionExpression (typeof (int), "Test", new SqlConvertedBooleanExpression (Expression.Constant (1)));

      var result = _predicateRequiredVisitor.VisitSqlFunctionExpression (expression);

      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
 public void SetUp ()
 {
   _sqlFunctionExpression = new SqlFunctionExpression (typeof (string), "Test", Expression.Constant("test"), Expression.Constant (1), Expression.Constant (2));
 }
    public void Transform_AddDays ()
    {
      var value = new CustomExpression (typeof (double));
      var methodInfo = ReflectionUtility.GetMethod (() => DateTime.Now.AddDays (0.0));
      var expression = Expression.Call (_dateTimeInstance, methodInfo, value);

      var result = _transformer.Transform (expression);

      var expectedResult = new SqlFunctionExpression (
          typeof (DateTime),
          "DATEADD",
          new SqlCustomTextExpression ("millisecond", typeof (string)),
          Expression.Modulo (
              new SqlConvertExpression (typeof (long), Expression.Multiply (value, new SqlLiteralExpression (86400000.0))),
              new SqlLiteralExpression (86400000L)),
          new SqlFunctionExpression
              (
              typeof (DateTime),
              "DATEADD",
              new SqlCustomTextExpression ("day", typeof (string)),
              Expression.Divide (
                  new SqlConvertExpression (typeof (long), Expression.Multiply (value, new SqlLiteralExpression (86400000.0))),
                  new SqlLiteralExpression (86400000L)),
              _dateTimeInstance));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public Expression Transform (MethodCallExpression methodCallExpression)
    {
      MethodCallTransformerUtility.CheckArgumentCount (methodCallExpression, 1, 2, 3);
      MethodCallTransformerUtility.CheckInstanceMethod (methodCallExpression);

      if (methodCallExpression.Arguments.Count == 1)
      {
        var lenExpression = new SqlLengthExpression (methodCallExpression.Arguments[0]);
        var testPredicate = Expression.Equal (lenExpression, new SqlLiteralExpression (0));
        var charIndexExpression = new SqlFunctionExpression (
            methodCallExpression.Type, "CHARINDEX", methodCallExpression.Arguments[0], methodCallExpression.Object);
        var elseValue = Expression.Subtract (charIndexExpression, new SqlLiteralExpression (1));

        return Expression.Condition (testPredicate, new SqlLiteralExpression (0), elseValue);
      }
      else if (methodCallExpression.Arguments.Count == 2)
      {
        var startIndexExpression = Expression.Add (methodCallExpression.Arguments[1], new SqlLiteralExpression (1));

        var lenArgExpression = new SqlLengthExpression (methodCallExpression.Arguments[0]);
        var leftTestPredicate = Expression.Equal (lenArgExpression, new SqlLiteralExpression (0));

        var lenObjectExpression = new SqlLengthExpression (methodCallExpression.Object);
        var rightTestpredicate = Expression.LessThanOrEqual (startIndexExpression, lenObjectExpression);
        var testPredicate = Expression.AndAlso (leftTestPredicate, rightTestpredicate);

        var charIndexExpression = new SqlFunctionExpression (
            methodCallExpression.Type, "CHARINDEX", methodCallExpression.Arguments[0], methodCallExpression.Object, startIndexExpression);

        var elseValue = Expression.Subtract (charIndexExpression, new SqlLiteralExpression (1));

        return Expression.Condition (testPredicate, methodCallExpression.Arguments[1], elseValue);
      }
      else if (methodCallExpression.Arguments.Count == 3)
      {
        var startIndexExpression = Expression.Add (methodCallExpression.Arguments[1], new SqlLiteralExpression (1));

        var lenArgExpression = new SqlLengthExpression (methodCallExpression.Arguments[0]);
        var leftTestPredicate = Expression.Equal (lenArgExpression, new SqlLiteralExpression (0));

        var lenObjectExpression = new SqlLengthExpression (methodCallExpression.Object);
        var rightTestpredicate = Expression.LessThanOrEqual (startIndexExpression, lenObjectExpression);
        var testPredicate = Expression.AndAlso (leftTestPredicate, rightTestpredicate);

        var startAddCountExpression = Expression.Add (methodCallExpression.Arguments[1], methodCallExpression.Arguments[2]);
        var substringExpression = new SqlFunctionExpression (
            typeof (string), "SUBSTRING", methodCallExpression.Object, new SqlLiteralExpression (1), startAddCountExpression);

        var charIndexExpression = new SqlFunctionExpression (
            methodCallExpression.Type, "CHARINDEX", methodCallExpression.Arguments[0], substringExpression, startIndexExpression);

        var elseValue = Expression.Subtract (charIndexExpression, new SqlLiteralExpression (1));

        return Expression.Condition (testPredicate, methodCallExpression.Arguments[1], elseValue);
      }
      else
      {
        var message = string.Format (
            "IndexOf function with {0} arguments is not supported. Expression: '{1}'",
            methodCallExpression.Arguments.Count,
            FormattingExpressionTreeVisitor.Format (methodCallExpression));
        throw new NotSupportedException (message);
      }
    }
    public void Escape_Expression ()
    {
      var expression = Expression.Constant ("test[test");

      var result = SqlLikeExpression.Escape (expression, @"\");

      var expectedResult =
        new SqlFunctionExpression (typeof (string), "REPLACE",
          new SqlFunctionExpression (typeof (string), "REPLACE",
            new SqlFunctionExpression (typeof (string), "REPLACE",
              new SqlFunctionExpression (typeof (string), "REPLACE", expression,
                new SqlLiteralExpression (@"\"), new SqlLiteralExpression (@"\\")),
                  new SqlLiteralExpression ("%"), new SqlLiteralExpression (@"\%")),
                    new SqlLiteralExpression ("_"), new SqlLiteralExpression (@"\_")),
                      new SqlLiteralExpression ("["), new SqlLiteralExpression (@"\["));

      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public void Transform_AddYears ()
    {
      var value = new CustomExpression (typeof (int));
      var methodInfo = ReflectionUtility.GetMethod (() => DateTime.Now.AddYears (0));
      var expression = Expression.Call (_dateTimeInstance, methodInfo, value);

      var result = _transformer.Transform (expression);

      var expectedResult = new SqlFunctionExpression (
          typeof (DateTime),
          "DATEADD",
          new SqlCustomTextExpression ("year", typeof (string)),
          value,
          _dateTimeInstance);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }