Exemple #1
0
        public ColumnDeclaration NewColumn(Expression exp)
        {
            string columnName = GetNextColumnName();
            var    result     = new ColumnDeclaration(columnName, exp);

            columns.Add(result.Name, result);
            return(result);
        }
Exemple #2
0
        public ColumnDeclaration MapColumn(ColumnExpression ce)
        {
            string columnName = GetUniqueColumnName(ce.Name);
            var    result     = new ColumnDeclaration(columnName, ce);

            columns.Add(result.Name, result);
            return(result);
        }
Exemple #3
0
        protected internal virtual ColumnDeclaration VisitColumnDeclaration(ColumnDeclaration c)
        {
            var e = Visit(c.Expression);

            if (e == c.Expression)
            {
                return(c);
            }

            return(new ColumnDeclaration(c.Name, e));
        }
        protected internal override ColumnDeclaration VisitColumnDeclaration(ColumnDeclaration c)
        {
            var e = MakeSqlValue(Visit(c.Expression));

            if (e == c.Expression)
            {
                return(c);
            }

            return(new ColumnDeclaration(c.Name, e));
        }
        private void AppendColumn(ColumnDeclaration column)
        {
            ColumnExpression c = column.Expression as ColumnExpression;

            if (column.Name.HasText() && (c == null || c.Name != column.Name))
            {
                sb.Append(column.Name.SqlEscape());
                sb.Append(" = ");
                this.Visit(column.Expression);
            }
            else
            {
                this.Visit(column.Expression);
            }
        }
Exemple #6
0
 protected internal override Expression VisitSelect(SelectExpression select)
 {
     select = (SelectExpression)base.VisitSelect(select);
     if (lookup.Contains(select.Alias))
     {
         List <ColumnDeclaration> aggColumns = new List <ColumnDeclaration>(select.Columns);
         foreach (AggregateRequestsExpression ae in lookup[select.Alias])
         {
             ColumnDeclaration cd = new ColumnDeclaration("agg" + aggColumns.Count, ae.Aggregate);
             this.map.Add(ae, cd.GetReference(ae.GroupByAlias));
             aggColumns.Add(cd);
         }
         return(new SelectExpression(select.Alias, select.IsDistinct, select.Top, aggColumns, select.From, select.Where, select.OrderBy, select.GroupBy, select.SelectOptions));
     }
     return(select);
 }
        private ReadOnlyCollection <ColumnDeclaration> AnswerAndExpand(ReadOnlyCollection <ColumnDeclaration> columns, Alias currentAlias, Dictionary <ColumnExpression, Expression> askedColumns)
        {
            ColumnGenerator cg = new ColumnGenerator(columns);

            foreach (var col in askedColumns.Keys.ToArray())
            {
                if (col.Alias == currentAlias)
                {
                    Expression expr = columns.SingleEx(cd => (cd.Name ?? "-") == col.Name).Expression;

                    askedColumns[col] = expr is SqlConstantExpression? expr: col;
                }
                else
                {
                    Expression       expr   = CurrentScope[col];
                    ColumnExpression colExp = expr as ColumnExpression;
                    if (colExp != null)
                    {
                        ColumnDeclaration cd = cg.Columns.FirstOrDefault(c => c.Expression.Equals(colExp));
                        if (cd == null)
                        {
                            cd = cg.MapColumn(colExp);
                        }

                        askedColumns[col] = new ColumnExpression(col.Type, currentAlias, cd.Name);
                    }
                    else
                    {
                        askedColumns[col] = expr;
                    }
                }
            }


            if (columns.Count != cg.Columns.Count())
            {
                return(cg.Columns.ToReadOnly());
            }

            return(columns);
        }
Exemple #8
0
        private bool IsCountSumOrAvg(SelectExpression select)
        {
            ColumnDeclaration col = select.Columns.Only();

            if (col == null)
            {
                return(false);
            }

            Expression exp = col.Expression;

            if (exp is IsNullExpression)
            {
                exp = ((IsNullExpression)exp).Expression;
            }

            if (exp.NodeType == ExpressionType.Coalesce)
            {
                var be = ((BinaryExpression)exp);
                if (be.Right.NodeType == ExpressionType.Constant || be.Right is SqlConstantExpression)
                {
                    exp = ((BinaryExpression)exp).Left;
                }
            }

            AggregateExpression aggExp = exp as AggregateExpression;

            if (aggExp == null)
            {
                return(false);
            }

            return(aggExp.AggregateFunction == AggregateSqlFunction.Count ||
                   aggExp.AggregateFunction == AggregateSqlFunction.Sum ||
                   aggExp.AggregateFunction == AggregateSqlFunction.Average ||
                   aggExp.AggregateFunction == AggregateSqlFunction.StdDev ||
                   aggExp.AggregateFunction == AggregateSqlFunction.StdDevP);
        }
Exemple #9
0
 static internal ColumnExpression SingleProjection(ColumnDeclaration declaration, Alias newAlias, Type columnType)
 {
     return(new ColumnExpression(columnType, newAlias, declaration.Name));
 }
        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);
        }
Exemple #11
0
 protected virtual bool CompareColumnDeclaration(ColumnDeclaration a, ColumnDeclaration b)
 {
     return(a.Name == b.Name && Compare(a.Expression, b.Expression));
 }