Example #1
0
        protected override SqlBuilder Where(UnaryExpression expression, SqlBuilder sqlBuilder)
        {
            //解析x => !x.isDeletion这样的
            if (expression.NodeType == ExpressionType.Not && expression.Operand is MemberExpression && expression.Type == typeof(bool))
            {
                var dbParamName = sqlBuilder.AddDbParameter(0);

                var memberExpression = expression.Operand as MemberExpression;
                //添加到查询条件
                var tableName = memberExpression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser);
                sqlBuilder.SetTableAlias(tableName);
                string tableAlias = sqlBuilder.GetTableAlias(tableName);
                if (!string.IsNullOrWhiteSpace(tableAlias))
                {
                    tableAlias += ".";
                }
                sqlBuilder.SqlWhereStr += " " + tableAlias + memberExpression.Member.Name;
                sqlBuilder.SqlWhereStr += " = ";
                sqlBuilder.SqlWhereStr += $"{dbParamName}";
            }
            else
            {
                SqlProvider.Where(expression.Operand, sqlBuilder);
            }

            return(sqlBuilder);
        }
 private static void LikeLeft(MethodCallExpression expression, SqlBuilder sqlBuilder)
 {
     if (expression.Object != null)
     {
         SqlProvider.Where(expression.Object, sqlBuilder);
     }
     SqlProvider.Where(expression.Arguments[0], sqlBuilder);
     sqlBuilder.SqlWhereStr += " like '%' +";
     SqlProvider.Where(expression.Arguments[1], sqlBuilder);
 }
Example #3
0
        public SqlQueryable <T> Where(Expression <Func <T, bool> > expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression", "Value cannot be null");
            }

            if (expression.Body != null && expression.Body.NodeType == ExpressionType.Constant)
            {
                throw new ArgumentException("Cannot be parse expression", "expression");
            }

            if (!this._sqlBuilder.IsAddWhere)
            {
                this._sqlBuilder.SqlWhereStr += " where";
            }
            else
            {
                this._sqlBuilder.SqlWhereStr += " and";
            }
            SqlProvider.Where(expression.Body, this._sqlBuilder);
            return(this);
        }
 protected override SqlBuilder Where(ParameterExpression expression, SqlBuilder sqlBuilder)
 {
     SqlProvider.Where(expression, sqlBuilder);
     return(sqlBuilder);
 }
 private static void InnerIn(MethodCallExpression expression, SqlBuilder sqlBuilder)
 {
     SqlProvider.Where(expression.Arguments[0], sqlBuilder);
     sqlBuilder.SqlWhereStr += " in";
     SqlProvider.In(expression.Arguments[1], sqlBuilder);
 }
        protected override SqlBuilder Where(BinaryExpression expression, SqlBuilder sqlBuilder)
        {
            bool hasHandle = false;

            if (CheckMemberIsNotColumn(expression, sqlBuilder))
            {
                return(sqlBuilder);
            }

            //添加Where表达式左侧sql
            if (IsNeedsParentheses(expression, expression.Left))
            {
                sqlBuilder.SqlWhereStr += "(";
            }

            if (expression.Left is MemberExpression && expression.Left.Type == typeof(bool) && expression.Left.NodeType == ExpressionType.MemberAccess && (expression.NodeType == ExpressionType.AndAlso || expression.Right is ConstantExpression))
            {
                //解析(m=>m.IsAdmin)
                var memberExpression = expression.Left as MemberExpression;

                var tableName = memberExpression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser);
                sqlBuilder.SetTableAlias(tableName);
                string tableAlias = sqlBuilder.GetTableAlias(tableName);
                if (!string.IsNullOrWhiteSpace(tableAlias))
                {
                    tableAlias += ".";
                }
                //添加参数
                var dbParamName = string.Empty;
                if (expression.Right is ConstantExpression)
                {
                    hasHandle = true;
                    var value = Convert.ToBoolean(((ConstantExpression)expression.Right).Value) ? 1 : 0;
                    dbParamName = sqlBuilder.AddDbParameter(value);
                }
                else
                {
                    dbParamName = sqlBuilder.AddDbParameter(1);
                }
                //添加关联条件
                sqlBuilder.SqlWhereStr += " " + tableAlias + memberExpression.Member.Name;
                sqlBuilder.SqlWhereStr += " = ";
                sqlBuilder.SqlWhereStr += $"{dbParamName}";
            }
            else
            {
                SqlProvider.Where(expression.Left, sqlBuilder);
            }

            if (IsNeedsParentheses(expression, expression.Left))
            {
                sqlBuilder.SqlWhereStr += ")";
            }

            if (!hasHandle)
            {
                //添加Where操作符
                var whereOperator = " =";

                if ((expression.Right is ConstantExpression) && ((ConstantExpression)expression.Right).Value == null)
                {
                    whereOperator = OperatorParser(expression.NodeType, true);
                }
                else
                {
                    whereOperator = OperatorParser(expression.NodeType);
                }

                sqlBuilder.SqlWhereStr += $"{whereOperator}";

                //添加Where表达式右侧sql
                if (IsNeedsParentheses(expression, expression.Right))
                {
                    sqlBuilder.SqlWhereStr += "(";
                }

                if (expression.Right is MemberExpression && expression.Right.Type == typeof(bool) && expression.Right.NodeType == ExpressionType.MemberAccess && expression.NodeType == ExpressionType.AndAlso)
                {
                    //解析(m=>m.IsAdmin)
                    var memberExpression = expression.Right as MemberExpression;

                    var tableName = memberExpression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser);
                    sqlBuilder.SetTableAlias(tableName);
                    string tableAlias = sqlBuilder.GetTableAlias(tableName);
                    if (!string.IsNullOrWhiteSpace(tableAlias))
                    {
                        tableAlias += ".";
                    }
                    //添加参数
                    var dbParamName = sqlBuilder.AddDbParameter(1);
                    //添加关联条件
                    sqlBuilder.SqlWhereStr += " " + tableAlias + memberExpression.Member.Name;
                    sqlBuilder.SqlWhereStr += " = ";
                    sqlBuilder.SqlWhereStr += $"{dbParamName}";
                }
                else
                {
                    SqlProvider.Where(expression.Right, sqlBuilder);
                }

                if (IsNeedsParentheses(expression, expression.Right))
                {
                    sqlBuilder.SqlWhereStr += ")";
                }
            }
            return(sqlBuilder);
        }