/// <summary>
        /// 访问 <see cref="T:System.Linq.Expressions.BinaryExpression"/> 的子级。
        /// </summary>
        /// <returns>
        /// 如果修改了该表达式或任何子表达式,则为修改后的表达式;否则返回原始表达式。
        /// </returns>
        /// <param name="binaryExpression">要访问的表达式。</param>
        protected override Expression VisitBinary(BinaryExpression binaryExpression)
        {
            Check.NotNull(binaryExpression, nameof(binaryExpression));

            if (binaryExpression.NodeType == ExpressionType.Coalesce)
            {
                Sql.Append("COALESCE(");
                Visit(binaryExpression.Left);
                Sql.Append(", ");
                Visit(binaryExpression.Right);
                Sql.Append(")");
            }
            else
            {
                var needParentheses
                    = !binaryExpression.Left.IsSimpleExpression() ||
                      !binaryExpression.Right.IsSimpleExpression() ||
                      binaryExpression.IsLogicalOperation();

                if (needParentheses)
                {
                    Sql.Append("(");
                }

                Visit(binaryExpression.Left);

                if (binaryExpression.IsLogicalOperation() &&
                    binaryExpression.Left.IsSimpleExpression())
                {
                    Sql.Append(" = ");
                    Sql.Append(_sqlHelper.EscapeLiteral(true));
                }

                if (!TryGenerateBinaryOperator(binaryExpression.NodeType, out var op))
                {
                    throw new ArgumentOutOfRangeException();
                }

                if ((binaryExpression.NodeType == ExpressionType.NotEqual ||
                     binaryExpression.NodeType == ExpressionType.Equal) &&
                    binaryExpression.Right.NodeType == ExpressionType.Constant)
                {
                    var value = binaryExpression.Right.Invoke();
                    if (value == null)
                    {
                        Sql.Append(binaryExpression.NodeType == ExpressionType.Equal
                            ? " IS NULL "
                            : " IS NOT NULL ");
                    }
                    else
                    {
                        Sql.Append(op);
                        Sql.Append(_sqlHelper.EscapeLiteral(value));
                    }
                }
                else
                {
                    Sql.Append(op);

                    Visit(binaryExpression.Right);

                    if (binaryExpression.IsLogicalOperation() &&
                        binaryExpression.Right.IsSimpleExpression())
                    {
                        Sql.Append(" = ");
                        Sql.Append(_sqlHelper.EscapeLiteral(true));
                    }
                }

                if (needParentheses)
                {
                    Sql.Append(")");
                }
            }

            return(binaryExpression);
        }
Beispiel #2
0
 /// <summary>
 /// 格式化SQL查询字符串。
 /// </summary>
 /// <param name="value">值对象。</param>
 /// <returns>返回格式化后的字符串。</returns>
 protected string Escape(object value) => SqlHelper.EscapeLiteral(value);