Ejemplo n.º 1
0
        protected override SqlBuilder Join(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.SqlJoinStr += " " + tableAlias + memberExpression.Member.Name;
                sqlBuilder.SqlJoinStr += " = ";
                sqlBuilder.SqlJoinStr += $"{dbParamName}";
            }
            else
            {
                SqlProvider.Join(expression.Operand, sqlBuilder);
            }
            return(sqlBuilder);
        }
Ejemplo n.º 2
0
        private SqlQueryable <T> JoinParser2 <T2, T3>(Expression <Func <T2, T3, bool> > expression, string leftOrRightJoin = "")
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression", "Value cannot be null");
            }

            string joinTableName = typeof(T3).GetTableName(_sqlBuilder._dbSqlParser);

            this._sqlBuilder.SetTableAlias(joinTableName);

            this._sqlBuilder.SqlJoinStr += $"{leftOrRightJoin} join {joinTableName + " " + this._sqlBuilder.GetTableAlias(joinTableName)} on";
            SqlProvider.Join(expression.Body, this._sqlBuilder);
            return(this);
        }
Ejemplo n.º 3
0
        protected override SqlBuilder Join(BinaryExpression expression, SqlBuilder sqlBuilder)
        {
            bool hasHandle = false;

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

            //添加二元表达式左侧sql
            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.SqlJoinStr += " " + tableAlias + memberExpression.Member.Name;
                sqlBuilder.SqlJoinStr += " = ";
                sqlBuilder.SqlJoinStr += $"{dbParamName}";
            }
            else
            {
                SqlProvider.Join(expression.Left, sqlBuilder);
            }

            if (!hasHandle)
            {
                //添加二元表达式操作符
                var joinOperator = " =";

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

                sqlBuilder.SqlJoinStr += $"{joinOperator}";

                //添加二元表达式右侧sql
                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 = string.Empty;
                    if (expression.Right is ConstantExpression)
                    {
                        var value = Convert.ToBoolean(((ConstantExpression)expression.Right).Value) ? 1 : 0;
                        dbParamName = sqlBuilder.AddDbParameter(1);
                    }
                    else
                    {
                        dbParamName = sqlBuilder.AddDbParameter(1);
                    }
                    //添加关联条件
                    sqlBuilder.SqlJoinStr += " " + tableAlias + memberExpression.Member.Name;
                    sqlBuilder.SqlJoinStr += " = ";
                    sqlBuilder.SqlJoinStr += $"{dbParamName}";
                }
                else
                {
                    SqlProvider.Join(expression.Right, sqlBuilder);
                }
            }
            return(sqlBuilder);
        }