Exemplo n.º 1
0
        public override SqlFragment Visit(DbSortExpression expression)
        {
            SelectStatement select = VisitInputExpressionEnsureSelect(expression.Input.Expression,
                                                                      expression.Input.VariableName, expression.Input.VariableType);

            select = WrapIfNotCompatible(select, expression.ExpressionKind);

            foreach (DbSortClause sortClause in expression.SortOrder)
            {
                select.AddOrderBy(new SortFragment(
                                      sortClause.Expression.Accept(this), sortClause.Ascending));
            }
            return(select);
        }
Exemplo n.º 2
0
        public override SqlFragment Visit(DbSkipExpression expression)
        {
            SelectStatement select = VisitInputExpressionEnsureSelect(expression.Input.Expression, expression.Input.VariableName,
                                                                      expression.Input.VariableType);

            select = WrapIfNotCompatible(select, DbExpressionKind.Sort);
            foreach (DbSortClause sortClause in expression.SortOrder)
            {
                select.AddOrderBy(
                    new SortFragment(sortClause.Expression.Accept(this), sortClause.Ascending));
            }

            // if we wrapped above, then this wrap will not create a new one so there
            // is no harm in calling it
            select      = WrapIfNotCompatible(select, expression.ExpressionKind);
            select.Skip = expression.Count.Accept(this);
            return(select);
        }
Exemplo n.º 3
0
        protected internal SelectStatement FuseSelectWithInnerSelect(SelectStatement outer, SelectStatement inner)
        {
            string oldTableName = (inner.From as TableFragment).Name;
            string newTableName = inner.Name;
            Dictionary <string, ColumnFragment> dicColumns = new Dictionary <string, ColumnFragment>();

            foreach (ColumnFragment cf in inner.Columns)
            {
                if (cf.ColumnAlias != null)
                {
                    dicColumns.Add(cf.ColumnAlias, cf);
                }
            }
            outer.From = inner.From;
            (outer.From as TableFragment).Name = newTableName;
            // Dispatch Where
            if (outer.Where == null)
            {
                outer.Where = inner.Where;
            }
            else if (inner.Where != null)
            {
                outer.Where = new BinaryFragment()
                {
                    Left = outer.Where, Right = inner.Where, Operator = "AND"
                };
            }
            VisitAndReplaceTableName(outer.Where, oldTableName, newTableName, dicColumns);
            // For the next constructions, either is defined on outer or at inner, not both
            // Dispatch Limit
            if (outer.Limit == null)
            {
                outer.Limit = inner.Limit;
                VisitAndReplaceTableName(outer.Limit, oldTableName, newTableName, dicColumns);
            }
            // Dispatch GroupBy
            if (outer.GroupBy == null && inner.GroupBy != null)
            {
                foreach (SqlFragment sf in inner.GroupBy)
                {
                    outer.AddGroupBy(sf);
                }
                foreach (SqlFragment sf in outer.GroupBy)
                {
                    VisitAndReplaceTableName(sf, oldTableName, newTableName, dicColumns);
                }
            }
            // Dispatch OrderBy
            if (outer.OrderBy != null || inner.OrderBy != null)
            {
                if (inner.OrderBy != null)
                {
                    foreach (SortFragment sf in inner.OrderBy)
                    {
                        outer.AddOrderBy(sf);
                    }
                }
                foreach (SortFragment sf in outer.OrderBy)
                {
                    VisitAndReplaceTableName(sf, oldTableName, newTableName, dicColumns);
                }
            }
            // Dispatch Skip
            if (outer.Skip == null)
            {
                outer.Skip = inner.Skip;
            }
            return(outer);
        }