protected internal SelectStatement FuseSelectWithInnerSelect(SelectStatement outer, SelectStatement inner) { string oldTableName = (inner.From as TableFragment).Name; string newTableName = inner.Name; outer.From = inner.From; //if (outer.Name != null) // outer.Name = newTableName; (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); // 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); } // 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); } } // Dispatch OrderBy if (outer.OrderBy == null && inner.OrderBy != null) { foreach (SortFragment sf in inner.OrderBy) { outer.AddOrderBy(sf); } foreach (SortFragment sf in outer.OrderBy) { VisitAndReplaceTableName(sf, oldTableName, newTableName); } } // Dispatch Skip if (outer.Skip == null) { outer.Skip = inner.Skip; } return(outer); }
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); }
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); }