예제 #1
0
 /// <summary>
 /// Binds the <see cref="string.Trim()"/> method.
 /// </summary>
 /// <param name="expression">The expression.</param>
 protected virtual Expression BindTrim(MethodCallExpression expression)
 {
     LinqStringBuilder.Append("RTRIM(LTRIM(");
     this.Visit(expression.Object);
     LinqStringBuilder.Append("))");
     return(expression);
 }
예제 #2
0
 /// <summary>
 /// Binds to <see cref="string.ToLower()"/> method.
 /// </summary>
 /// <param name="expression">The expression.</param>
 protected virtual Expression BindToLower(MethodCallExpression expression)
 {
     LinqStringBuilder.Append("LOWER(");
     Visit(expression.Object);
     LinqStringBuilder.Append(")");
     return(expression);
 }
예제 #3
0
 /// <summary>
 /// Binds to <see cref="string.ToUpper()"/> method.
 /// </summary>
 /// <param name="expression">The expression.</param>
 protected virtual Expression BindToUpper(MethodCallExpression expression)
 {
     LinqStringBuilder.Append("UPPER(");
     this.Visit(expression.Object);
     LinqStringBuilder.Append(")");
     return(expression);
 }
예제 #4
0
        /// <summary>
        /// Visits the unary.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        /// <exception cref="System.NotSupportedException">If this unary expression is not supported.</exception>
        protected override Expression VisitUnary(UnaryExpression expression)
        {
            switch (expression.NodeType)
            {
            case ExpressionType.Not:
                if (IsBooleanPropertyExpression(expression.Operand))
                {
                    AppendBooleanCondition((MemberExpression)expression.Operand, true, true);
                }
                else
                {
                    LinqStringBuilder.Append(" NOT ");
                    Visit(expression.Operand);
                }
                break;

            case ExpressionType.Convert:
            case ExpressionType.ConvertChecked:
                Visit(expression.Operand);
                break;

            default:
                throw new NotSupportedException(string.Format(Resources.UnaryOperatorIsNotSupported, expression.NodeType));
            }

            return(expression);
        }
예제 #5
0
 /// <inheritdoc/>
 protected override Expression BindToLower(MethodCallExpression expression)
 {
     LinqStringBuilder.Append("LCASE(");
     this.Visit(expression.Object);
     LinqStringBuilder.Append(")");
     return(expression);
 }
예제 #6
0
 /// <summary>
 /// Binds the <see cref="string.EndsWith(string)"/> method.
 /// </summary>
 /// <param name="expression">The expression.</param>
 protected virtual Expression BindEndWith(MethodCallExpression expression)
 {
     LinqStringBuilder.Append("(");
     Visit(expression.Object);
     LinqStringBuilder.Append(" LIKE '%' + ");
     Visit(expression.Arguments[0]);
     LinqStringBuilder.Append(")");
     return(expression);
 }
예제 #7
0
 /// <summary>
 /// Binds the <see cref="string.StartsWith(string)"/> method.
 /// </summary>
 /// <param name="expression">The expression.</param>
 protected virtual Expression BindStartWith(MethodCallExpression expression)
 {
     LinqStringBuilder.Append("(");
     this.Visit(expression.Object);
     LinqStringBuilder.Append(" LIKE ");
     this.Visit(expression.Arguments[0]);
     LinqStringBuilder.Append(" + '%')");
     return(expression);
 }
예제 #8
0
 /// <summary>
 /// Binds the <see cref="string.IsNullOrEmpty(string)"/> method.
 /// </summary>
 /// <param name="expression">The expression.</param>
 protected virtual Expression BindIsNullOrEmpty(MethodCallExpression expression)
 {
     LinqStringBuilder.Append("(");
     this.Visit(expression.Arguments[0]);
     LinqStringBuilder.Append(" IS NULL OR ");
     this.Visit(expression.Arguments[0]);
     LinqStringBuilder.Append(" = '')");
     return(expression);
 }
예제 #9
0
        private Expression VisitOrderBy(MethodCallExpression expression, OrderType orderType)
        {
            var lambda = (LambdaExpression)StripQuotes(expression.Arguments[1]);

            var ret = Visit(lambda);

            Orders.Add(LinqStringBuilder.ToString() + " " + (orderType == OrderType.Ascending ? "ASC" : "DESC"));
            LinqStringBuilder.Clear();

            return(ret);
        }
예제 #10
0
        /// <summary>
        /// Visits the Linq aggregate methods.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <param name="aggregateName">Name of aggreage method.</param>
        protected virtual Expression VisitAggregate(MethodCallExpression expression, string aggregateName)
        {
            LinqStringBuilder.Append($"{aggregateName}(");
            this.Visit(StripQuotes(expression.Arguments[1]));
            LinqStringBuilder.Append(")");

            SelectExpression.SetColumnsExpression(new ColumnsExpression(LinqStringBuilder.ToString()));
            LinqStringBuilder.Clear();

            return(expression);
        }
예제 #11
0
 /// <summary>
 /// Binds the <see cref="string.Replace(string, string)"/> method.
 /// </summary>
 /// <param name="expression">The expression.</param>
 protected virtual Expression BindReplace(MethodCallExpression expression)
 {
     LinqStringBuilder.Append("REPLACE(");
     this.Visit(expression.Object);
     LinqStringBuilder.Append(", ");
     this.Visit(expression.Arguments[0]);
     LinqStringBuilder.Append(", ");
     this.Visit(expression.Arguments[1]);
     LinqStringBuilder.Append(")");
     return(expression);
 }
예제 #12
0
        /// <summary>
        /// Visits the Linq Binary.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        /// <exception cref="System.NotSupportedException">If this binary expression is not supported.</exception>
        protected override Expression VisitBinary(BinaryExpression expression)
        {
            bool hasBooleanOperator = HasBooleanOperator(expression);

            LinqStringBuilder.Append("(");
            ProcessBinaryExpressionOperand(expression.Left, hasBooleanOperator);
            AppendOperator(expression);
            ProcessBinaryExpressionOperand(expression.Right, hasBooleanOperator);
            LinqStringBuilder.Append(")");
            return(expression);
        }
예제 #13
0
        /// <summary>
        /// Visits the Linq Where method.
        /// </summary>
        /// <param name="expression">The expression.</param>
        protected virtual Expression VisitWhere(MethodCallExpression expression)
        {
            LambdaExpression lambda = (LambdaExpression)StripQuotes(expression.Arguments[1]);

            this.Visit(lambda.Body);

            SelectExpression.SetWhereExpression(new WhereExpression(LinqStringBuilder.ToString(), LinqParameters.GetParams()));
            LinqParameters.Clear();
            LinqStringBuilder = null;

            return(expression);
        }
예제 #14
0
 /// <summary>
 /// Visits the member.
 /// </summary>
 /// <param name="expression">The expression.</param>
 /// <exception cref="System.NotSupportedException">If the member type is not supported.</exception>
 protected override Expression VisitMember(MemberExpression expression)
 {
     if (expression.Expression != null &&
         (expression.Expression.NodeType == ExpressionType.Parameter ||
          expression.Expression.NodeType == ExpressionType.Convert))
     {
         var columnInfo = DatabaseMapper.GetTableInfo(expression.Member.DeclaringType).GetColumnInfoByPropertyName(expression.Member.Name);
         LinqStringBuilder.Append(columnInfo.Name);
         return(expression);
     }
     throw new NotSupportedException(string.Format("The member '{0}' is not supported.", expression.Member.Name));
 }
예제 #15
0
        private void AppendOperator(BinaryExpression expression)
        {
            string op = GetOperator(expression);

            if (string.IsNullOrEmpty(op))
            {
                throw new NotSupportedException(string.Format(Resources.BinaryOperatorIsNotSupported, expression.NodeType));
            }
            else
            {
                LinqStringBuilder.Append($" {op} ");
            }
        }
예제 #16
0
        private Expression VisitOrderBy(MethodCallExpression expression, OrderType orderType)
        {
            var lambda = (LambdaExpression)StripQuotes(expression.Arguments[1]);

            var ret = this.Visit(lambda);

            _orders.Add(new OrderBy()
            {
                ColumnaName = LinqStringBuilder.ToString(), Type = orderType
            });
            LinqStringBuilder.Clear();

            return(ret);
        }
예제 #17
0
 /// <summary>
 /// Visits the constant.
 /// </summary>
 /// <param name="expression">The expression.</param>
 /// <exception cref="System.NotSupportedException">If type of constant is <see cref="System.Object"/>.</exception>
 protected override Expression VisitConstant(ConstantExpression expression)
 {
     if (expression.Value == null)
     {
         LinqStringBuilder.Append("NULL");
     }
     else if (expression.Value.GetType() == typeof(bool))
     {
         LinqStringBuilder.Append((bool)expression.Value ? SqlTrue : SqlFalse);
     }
     else
     {
         AddParameterWithValue(GetConstantExpressionValue(expression));
     }
     return(expression);
 }
예제 #18
0
        /// <summary>
        /// Visits the Linq Take method.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <exception cref="System.NotSupportedException">If call of Take method is not supported.</exception>
        protected virtual Expression VisitTake(MethodCallExpression expression)
        {
            this.Visit(StripQuotes(expression.Arguments[1]));
            LinqStringBuilder.Clear();

            try
            {
                _top = (int)LinqParameters.GetParams().First();

                LinqParameters = new Parameters();
                return(expression);
            }
            catch (Exception e)
            {
                throw new NotSupportedException("This call of Take method is not supported", e);
            }
        }
예제 #19
0
        /// <summary>
        /// Visits the Linq Take method.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <exception cref="System.NotSupportedException">If call of Take method is not supported.</exception>
        protected virtual Expression VisitTake(MethodCallExpression expression)
        {
            Visit(StripQuotes(expression.Arguments[1]));
            LinqStringBuilder.Clear();

            try
            {
                _top = (int)LinqParameters.GetParams().First();

                LinqParameters = new Parameters();
                return(expression);
            }
            catch (Exception ex)
            {
                throw new NotSupportedException(Resources.ThisCallOfTakeMethodIsNotSupported, ex);
            }
        }
예제 #20
0
 /// <summary>
 /// Binds the <see cref="string.Substring(int)" autoUpgrade="true" /> method.
 /// </summary>
 /// <param name="expression">The expression.</param>
 protected virtual Expression BindSubstring(MethodCallExpression expression)
 {
     LinqStringBuilder.Append("SUBSTRING(");
     this.Visit(expression.Object);
     LinqStringBuilder.Append(", ");
     this.Visit(expression.Arguments[0]);
     LinqStringBuilder.Append(" + 1, ");
     if (expression.Arguments.Count == 2)
     {
         this.Visit(expression.Arguments[1]);
     }
     else
     {
         LinqStringBuilder.Append(LinqParameters.GetNextParamName());
         LinqParameters.AddParameter(8000);
     }
     LinqStringBuilder.Append(")");
     return(expression);
 }
예제 #21
0
        /// <summary>
        /// Visits the Linq Binary.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        /// <exception cref="System.NotSupportedException">If this binary expression is not supported.</exception>
        protected override Expression VisitBinary(BinaryExpression expression)
        {
            LinqStringBuilder.Append("(");

            this.Visit(expression.Left);

            var op = GetOperator(expression);

            switch (expression.NodeType)
            {
            case ExpressionType.And:
            case ExpressionType.AndAlso:
            case ExpressionType.Or:
            case ExpressionType.OrElse:
            case ExpressionType.Equal:
            case ExpressionType.NotEqual:
            case ExpressionType.LessThan:
            case ExpressionType.LessThanOrEqual:
            case ExpressionType.GreaterThan:
            case ExpressionType.GreaterThanOrEqual:
            case ExpressionType.Add:
            case ExpressionType.AddChecked:
            case ExpressionType.Subtract:
            case ExpressionType.SubtractChecked:
            case ExpressionType.Multiply:
            case ExpressionType.MultiplyChecked:
            case ExpressionType.Divide:
            case ExpressionType.Modulo:
            case ExpressionType.ExclusiveOr:
                LinqStringBuilder.Append($" {op} ");
                break;

            default:
                throw new NotSupportedException(string.Format("The binary operator '{0}' is not supported.", expression.NodeType));
            }

            this.Visit(expression.Right);

            LinqStringBuilder.Append(")");

            return(expression);
        }
예제 #22
0
        /// <summary>
        /// Visits the unary.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        /// <exception cref="System.NotSupportedException">If this unary expression is not supported.</exception>
        protected override Expression VisitUnary(UnaryExpression expression)
        {
            switch (expression.NodeType)
            {
            case ExpressionType.Not:
                LinqStringBuilder.Append(" NOT ");
                this.Visit(expression.Operand);
                break;

            case ExpressionType.Convert:
            case ExpressionType.ConvertChecked:
                this.Visit(expression.Operand);
                break;

            default:
                throw new NotSupportedException(string.Format("The unary operator '{0}' is not supported", expression.NodeType));
            }

            return(expression);
        }
예제 #23
0
        /// <summary>
        /// Binds the <see cref="string.Substring(int)" autoUpgrade="true" /> method.
        /// </summary>
        /// <param name="expression">The expression.</param>
        protected virtual Expression BindSubstring(MethodCallExpression expression)
        {
            const int substringMaxLength = 8000;

            LinqStringBuilder.Append("SUBSTRING(");
            Visit(expression.Object);
            LinqStringBuilder.Append(", ");
            Visit(expression.Arguments[0]);
            LinqStringBuilder.Append(" + 1, ");
            if (expression.Arguments.Count == 2)
            {
                Visit(expression.Arguments[1]);
            }
            else
            {
                AddParameterWithValue(substringMaxLength);
            }
            LinqStringBuilder.Append(")");
            return(expression);
        }
예제 #24
0
        /// <summary>
        /// Visits the unary.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        /// <exception cref="System.NotSupportedException">If this unary expression is not supported.</exception>
        protected override Expression VisitUnary(UnaryExpression expression)
        {
            switch (expression.NodeType)
            {
            case ExpressionType.Not:
                LinqStringBuilder.Append(" NOT ");
                Visit(expression.Operand);
                break;

            case ExpressionType.Convert:
            case ExpressionType.ConvertChecked:
                Visit(expression.Operand);
                break;

            default:
                throw new NotSupportedException(string.Format(Resources.UnaryOperatorIsNotSupported, expression.NodeType));
            }

            return(expression);
        }
예제 #25
0
        /// <summary>
        /// Visits the constant.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <exception cref="System.NotSupportedException">If type of constant is <see cref="System.Object"/>.</exception>
        protected override Expression VisitConstant(ConstantExpression expression)
        {
            if (expression.Value == null)
            {
                LinqStringBuilder.Append("NULL");
            }
            else
            {
                var type = expression.Value.GetType();

                if (Type.GetTypeCode(type) == TypeCode.Object && (type != typeof(Guid)))
                {
                    throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", expression.Value));
                }
                else
                {
                    LinqStringBuilder.Append(LinqParameters.GetNextParamName());
                    LinqParameters.AddParameter(expression.Value);
                }
            }

            return(expression);
        }
예제 #26
0
        private void AppendBooleanCondition(MemberExpression node, bool value, bool negate)
        {
            string op = negate ? "<>" : "=";

            LinqStringBuilder.AppendFormat("({0} {1} {2})", GetColumnNameFromMember(node), op, value ? SqlTrue : SqlFalse);
        }
예제 #27
0
 private void AppendMemberColumnName(MemberExpression node) => LinqStringBuilder.Append(GetColumnNameFromMember(node));