예제 #1
0
        protected override Expression VisitSelect(SelectExpression selectExpression)
        {
            if (!IsForBatchEF)
            {
                return(base.VisitSelect(selectExpression));
            }
            if (BatchUtils.IsNonComposedSetOperation(selectExpression))
            {
                GenerateSetOperation((SetOperationBase)selectExpression.Tables[0]);
                return(selectExpression);
            }
            IDisposable disposable = null;

            if (selectExpression.Alias != null)
            {
                Sql.AppendLine("(");
                disposable = Sql.Indent();
            }
            Sql.Append("SELECT ");
            if (selectExpression.IsDistinct)
            {
                Sql.Append("DISTINCT ");
            }
            GenerateTop(selectExpression);
            if (selectExpression.Projection.Any())
            {
                BatchUtils.GenerateList(selectExpression.Projection, Sql, delegate(ProjectionExpression e)
                {
                    var oldSQL = Sql.Build().CommandText;                                  //zack's code
                    Visit(e);
                    string column = BatchUtils.Diff(oldSQL, this.Sql.Build().CommandText); //zack's code
                    this._projectionSQL.Add(column);                                       //zack's code
                });
            }
            else
            {
                Sql.Append("1");
                this._projectionSQL.Add("1");                //zack's code
            }
            if (selectExpression.Tables.Any())
            {
                Sql.AppendLine().Append("FROM ");
                BatchUtils.GenerateList(selectExpression.Tables, Sql, delegate(TableExpressionBase e)
                {
                    Visit(e);
                }, delegate(IRelationalCommandBuilder sql)
                {
                    sql.AppendLine();
                });
            }
            else
            {
                GeneratePseudoFromClause();
            }
            if (selectExpression.Predicate != null)
            {
                Sql.AppendLine().Append("WHERE ");
                var oldSQL = Sql.Build().CommandText;                                      //zack's code
                Visit(selectExpression.Predicate);
                this.PredicateSQL = BatchUtils.Diff(oldSQL, this.Sql.Build().CommandText); //zack's code
            }
            if (selectExpression.GroupBy.Count > 0)
            {
                Sql.AppendLine().Append("GROUP BY ");
                BatchUtils.GenerateList(selectExpression.GroupBy, Sql, delegate(SqlExpression e)
                {
                    Visit(e);
                });
            }
            if (selectExpression.Having != null)
            {
                Sql.AppendLine().Append("HAVING ");
                Visit(selectExpression.Having);
            }
            GenerateOrderings(selectExpression);
            GenerateLimitOffset(selectExpression);
            if (selectExpression.Alias != null)
            {
                disposable.Dispose();
                Sql.AppendLine().Append(")" + AliasSeparator + _sqlGenerationHelper.DelimitIdentifier(selectExpression.Alias));
            }
            return(selectExpression);
        }