Пример #1
0
        /// <summary>
        /// 追加列名
        /// </summary>
        /// <param name="ag">表别名</param>
        /// <param name="node">列名表达式</param>
        /// <returns>返回解析到的表别名</returns>
        public string AppendMember(AliasGenerator ag, Expression node)
        {
            Expression       expression       = node;
            LambdaExpression lambdaExpression = expression as LambdaExpression;

            if (lambdaExpression != null)
            {
                expression = lambdaExpression.Body;
            }

            if (expression.CanEvaluate())
            {
                ConstantExpression c     = expression.Evaluate();
                string             value = _constor.GetSqlValue(c.Value, _context);
                _innerBuilder.Append(value);
                return(value);
            }
            else
            {
                MemberExpression m     = expression.ReduceUnary() as MemberExpression;
                string           alias = ag == null ? null : ag.GetTableAlias(m);
                this.AppendMember(alias, m.Member, m.Expression != null ? m.Expression.Type : null);
                return(alias);
            }
        }
        /// <summary>
        /// 访问表达式节点
        /// </summary>
        /// <param name="wheres">筛选表达式</param>
        public override Expression Visit(List <DbExpression> wheres)
        {
            if (wheres != null && wheres.Count > 0)
            {
                _builder.AppendNewLine();
                _builder.Append("WHERE ");

                for (int index = 0; index < wheres.Count; index++)
                {
                    DbExpression d    = wheres[index];
                    var          node = d.Expressions[0];
                    if (node.NodeType == ExpressionType.Lambda)
                    {
                        node = ((LambdaExpression)node).Body;
                    }
                    node = BooleanUnaryToBinary(node);

                    if (!(node is ConstantExpression))
                    {
                        base.Visit(node);
                    }
                    else
                    {
                        // 字符符动态SQL,解析参数
                        DbRawSql rawSql = (node as ConstantExpression).Value as DbRawSql;
                        object[] args   = null;
                        if (rawSql.Parameters != null)
                        {
                            args = rawSql.Parameters.Select(x => _constor.GetSqlValue(x, _context)).ToArray();
                        }
                        string sql = rawSql.CommandText;

                        if (args != null && args.Length > 0)
                        {
                            sql = string.Format(sql, args);
                        }
                        _builder.Append(sql);
                    }

                    if (index < wheres.Count - 1)
                    {
                        _builder.Append(" AND ");
                    }
                }
            }

            return(null);
        }