Example #1
0
        protected internal override Expression VisitSelect(SelectExpression select)
        {
            bool isFirst = sb.Length == 0;

            if (!isFirst)
            {
                AppendNewLine(Indentation.Inner);
                sb.Append("(");
            }

            sb.Append("SELECT ");
            if (select.IsDistinct)
            {
                sb.Append("DISTINCT ");
            }

            if (select.Top != null)
            {
                sb.Append("TOP (");
                Visit(select.Top);
                sb.Append(") ");
            }

            if (select.Columns.Count == 0)
            {
                sb.Append("0 as Dummy");
            }
            else
            {
                this.AppendNewLine(Indentation.Inner);
                for (int i = 0, n = select.Columns.Count; i < n; i++)
                {
                    ColumnDeclaration column = select.Columns[i];
                    AppendColumn(column);
                    if (i < (n - 1))
                    {
                        sb.Append(", ");
                        this.AppendNewLine(Indentation.Same);
                    }
                    else
                    {
                        this.Indent(Indentation.Outer);
                    }
                }
            }

            if (select.From != null)
            {
                this.AppendNewLine(Indentation.Same);
                sb.Append("FROM ");
                this.VisitSource(select.From);
            }
            if (select.Where != null)
            {
                this.AppendNewLine(Indentation.Same);
                sb.Append("WHERE ");
                this.Visit(select.Where);
            }
            if (select.GroupBy.Count > 0)
            {
                this.AppendNewLine(Indentation.Same);
                sb.Append("GROUP BY ");
                for (int i = 0, n = select.GroupBy.Count; i < n; i++)
                {
                    Expression exp = select.GroupBy[i];
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }
                    this.Visit(exp);
                }
            }
            if (select.OrderBy.Count > 0)
            {
                this.AppendNewLine(Indentation.Same);
                sb.Append("ORDER BY ");
                for (int i = 0, n = select.OrderBy.Count; i < n; i++)
                {
                    OrderExpression exp = select.OrderBy[i];
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }
                    this.Visit(exp.Expression);
                    if (exp.OrderType != OrderType.Ascending)
                    {
                        sb.Append(" DESC");
                    }
                }
            }

            if (select.IsForXmlPathEmpty)
            {
                this.AppendNewLine(Indentation.Same);
                sb.Append("FOR XML PATH('')");
            }

            if (!isFirst)
            {
                sb.Append(")");
                AppendNewLine(Indentation.Outer);
            }

            return(select);
        }
Example #2
0
 protected virtual bool CompareOrder(OrderExpression a, OrderExpression b)
 {
     return(a.OrderType == b.OrderType && Compare(a.Expression, b.Expression));
 }