public virtual IQueryState Accept(GroupingQueryExpression exp) { List <IMappingObjectExpression> moeList = this.MoeList; foreach (LambdaExpression item in exp.GroupKeySelectors) { var dbExp = GeneralExpressionVisitor.ParseLambda(item, moeList); this._resultElement.GroupSegments.Add(dbExp); } foreach (LambdaExpression havingPredicate in exp.HavingPredicates) { var havingCondition = FilterPredicateExpressionVisitor.ParseFilterPredicate(havingPredicate, moeList); this._resultElement.AppendHavingCondition(havingCondition); } if (exp.Orderings.Count > 0) { this._resultElement.Orderings.Clear(); this._resultElement.InheritOrderings = false; for (int i = 0; i < exp.Orderings.Count; i++) { GroupingQueryOrdering groupOrdering = exp.Orderings[i]; DbExpression orderingDbExp = GeneralExpressionVisitor.ParseLambda(groupOrdering.KeySelector, moeList); DbOrdering ordering = new DbOrdering(orderingDbExp, groupOrdering.OrderType); this._resultElement.Orderings.Add(ordering); } } var newResult = this.CreateNewResult(exp.Selector); return(new GroupingQueryState(newResult)); }
public virtual GeneralQueryState AsSubQueryState() { DbSqlQueryExpression sqlQuery = this.CreateSqlQuery(); DbSubQueryExpression subQuery = new DbSubQueryExpression(sqlQuery); ResultElement result = new ResultElement(); DbTableSegment tableSeg = new DbTableSegment(subQuery, result.GenerateUniqueTableAlias()); DbFromTableExpression fromTable = new DbFromTableExpression(tableSeg); result.FromTable = fromTable; DbTable table = new DbTable(tableSeg.Alias); //TODO 根据旧的生成新 MappingMembers IMappingObjectExpression newMoe = this.Result.MappingObjectExpression.ToNewObjectExpression(sqlQuery, table); result.MappingObjectExpression = newMoe; //得将 subQuery.SqlQuery.Orders 告诉 以下创建的 result //将 orderPart 传递下去 if (this.Result.Orderings.Count > 0) { for (int i = 0; i < this.Result.Orderings.Count; i++) { DbOrdering ordering = this.Result.Orderings[i]; DbExpression orderingExp = ordering.Expression; string alias = null; DbColumnSegment columnExpression = sqlQuery.ColumnSegments.Where(a => DbExpressionEqualityComparer.EqualsCompare(orderingExp, a.Body)).FirstOrDefault(); // 对于重复的则不需要往 sqlQuery.Columns 重复添加了 if (columnExpression != null) { alias = columnExpression.Alias; } else { alias = Utils.GenerateUniqueColumnAlias(sqlQuery); DbColumnSegment columnSeg = new DbColumnSegment(orderingExp, alias); sqlQuery.ColumnSegments.Add(columnSeg); } DbColumnAccessExpression columnAccessExpression = new DbColumnAccessExpression(orderingExp.Type, table, alias); result.Orderings.Add(new DbOrdering(columnAccessExpression, ordering.OrderType)); } } result.InheritOrderings = true; GeneralQueryState queryState = new GeneralQueryState(result); return(queryState); }
public virtual GeneralQueryState AsSubQueryState() { DbSqlQueryExpression sqlQuery = this.CreateSqlQuery(); DbSubQueryExpression subQuery = new DbSubQueryExpression(sqlQuery); QueryModel newQueryModel = new QueryModel(this._queryModel.ScopeParameters, this._queryModel.ScopeTables, this._queryModel.IgnoreFilters); DbTableSegment tableSeg = new DbTableSegment(subQuery, newQueryModel.GenerateUniqueTableAlias(), LockType.Unspecified); DbFromTableExpression fromTable = new DbFromTableExpression(tableSeg); newQueryModel.FromTable = fromTable; DbTable aliasTable = new DbTable(tableSeg.Alias); //TODO 根据旧的生成新 ResultModel IObjectModel newResultModel = this.QueryModel.ResultModel.ToNewObjectModel(sqlQuery, aliasTable, fromTable); newQueryModel.ResultModel = newResultModel; //得将 subQuery.SqlQuery.Orders 告诉 以下创建的 result //将 orderPart 传递下去 for (int i = 0; i < this.QueryModel.Orderings.Count; i++) { DbOrdering ordering = this.QueryModel.Orderings[i]; DbExpression orderingExp = ordering.Expression; string alias = null; DbColumnSegment columnExpression = sqlQuery.ColumnSegments.Find(a => DbExpressionEqualityComparer.EqualsCompare(orderingExp, a.Body)); // 对于重复的则不需要往 sqlQuery.Columns 重复添加了 if (columnExpression != null) { alias = columnExpression.Alias; } else { alias = Utils.GenerateUniqueColumnAlias(sqlQuery); DbColumnSegment columnSeg = new DbColumnSegment(orderingExp, alias); sqlQuery.ColumnSegments.Add(columnSeg); } DbColumnAccessExpression columnAccessExpression = new DbColumnAccessExpression(aliasTable, DbColumn.MakeColumn(orderingExp, alias)); newQueryModel.Orderings.Add(new DbOrdering(columnAccessExpression, ordering.OrderType)); } newQueryModel.InheritOrderings = true; GeneralQueryState queryState = new GeneralQueryState(newQueryModel); return(queryState); }
void AppendOrdering(DbOrdering ordering) { if (ordering.OrderType == DbOrderType.Asc) { ordering.Expression.Accept(this); this._sqlBuilder.Append(" ASC"); return; } else if (ordering.OrderType == DbOrderType.Desc) { ordering.Expression.Accept(this); this._sqlBuilder.Append(" DESC"); return; } throw new NotSupportedException("OrderType: " + ordering.OrderType); }
public virtual IQueryState Accept(OrderExpression exp) { if (exp.NodeType == QueryExpressionType.OrderBy || exp.NodeType == QueryExpressionType.OrderByDesc) this._resultElement.Orderings.Clear(); DbOrdering ordering = ParseOrderExpression(exp); if (this._resultElement.InheritOrderings) { this._resultElement.Orderings.Clear(); this._resultElement.InheritOrderings = false; } this._resultElement.Orderings.Add(ordering); return this; }
ComplexObjectModel GenCollectionElementObjectModel(TypeDescriptor elementTypeDescriptor, NavigationNode navigationNode, QueryModel queryModel) { DbTable dbTable = elementTypeDescriptor.Table; DbTableExpression tableExp = new DbTableExpression(dbTable); string alias = queryModel.GenerateUniqueTableAlias(dbTable.Name); DbTableSegment joinTableSeg = new DbTableSegment(tableExp, alias, queryModel.FromTable.Table.Lock); DbTable aliasTable = new DbTable(alias); ComplexObjectModel elementObjectModel = elementTypeDescriptor.GenObjectModel(aliasTable); elementObjectModel.NullChecking = elementObjectModel.PrimaryKey; ComplexPropertyDescriptor navigationDescriptor = elementTypeDescriptor.ComplexPropertyDescriptors.Where(a => a.PropertyType == this.ObjectType).FirstOrDefault(); if (navigationDescriptor == null) { throw new ChloeException($"You have to define a navigation property which type is '{this.ObjectType.FullName}' on class '{elementTypeDescriptor.Definition.Type.FullName}'."); } DbExpression elementForeignKeyColumn = elementObjectModel.GetPrimitiveMember(navigationDescriptor.ForeignKeyProperty.Property); DbExpression joinCondition = DbExpression.Equal(this.PrimaryKey, elementForeignKeyColumn); DbJoinTableExpression joinTableExp = new DbJoinTableExpression(DbJoinType.LeftJoin, joinTableSeg, joinCondition); this.DependentTable.JoinTables.Add(joinTableExp); elementObjectModel.DependentTable = joinTableExp; var condition = this.ParseCondition(navigationNode.Condition, elementObjectModel, queryModel.ScopeTables); //AndWhere的条件放到join条件里去 joinTableExp.AppendCondition(condition); elementObjectModel.Filter = this.ParseCondition(navigationNode.Filter, elementObjectModel, queryModel.ScopeTables); bool orderByPrimaryKeyExists = queryModel.Orderings.Where(a => a.Expression == this.PrimaryKey).Any(); if (!orderByPrimaryKeyExists) { //结果集分组 DbOrdering ordering = new DbOrdering(this.PrimaryKey, DbOrderType.Asc); queryModel.Orderings.Add(ordering); } //queryModel.Filters.Add(elementObjectModel.Filter); return(elementObjectModel); }
protected override void BuildLimitSql(DbExpressions.DbSqlQueryExpression exp) { //order by number offset 10 rows fetch next 20 rows only; this.SqlBuilder.Append("SELECT "); this.AppendDistinct(exp.IsDistinct); List <DbColumnSegment> columns = exp.ColumnSegments; for (int i = 0; i < columns.Count; i++) { DbColumnSegment column = columns[i]; if (i > 0) { this.SqlBuilder.Append(","); } this.AppendColumnSegment(column); } this.SqlBuilder.Append(" FROM "); exp.Table.Accept(this); this.BuildWhereState(exp.Condition); this.BuildGroupState(exp); List <DbOrdering> orderings = exp.Orderings; if (orderings.Count == 0) { DbExpression orderingExp = DbExpression.Add(PublicConstants.DbParameter_1, DbConstantExpression.Zero, DbConstantExpression.Zero.Type, null); DbOrdering ordering = new DbOrdering(orderingExp, DbOrderType.Asc); orderings = new List <DbOrdering>(1); orderings.Add(ordering); } this.BuildOrderState(orderings); this.SqlBuilder.Append(" OFFSET ", exp.SkipCount.Value.ToString(), " ROWS"); if (exp.TakeCount != null) { this.SqlBuilder.Append(" FETCH NEXT ", exp.TakeCount.Value.ToString(), " ROWS ONLY"); } }
public virtual IQueryState Accept(OrderExpression exp) { if (exp.NodeType == QueryExpressionType.OrderBy || exp.NodeType == QueryExpressionType.OrderByDesc) { this._queryModel.Orderings.Clear(); } DbOrdering ordering = ParseOrderExpression(exp); if (this._queryModel.InheritOrderings) { this._queryModel.Orderings.Clear(); this._queryModel.InheritOrderings = false; } this._queryModel.Orderings.Add(ordering); return(this); }
public virtual IQueryState Accept(OrderExpression exp) { if (exp.NodeType == QueryExpressionType.OrderBy || exp.NodeType == QueryExpressionType.OrderByDesc) { this._resultElement.Orderings.Clear(); } DbOrdering ordering = VisistOrderExpression(this.MoeList, exp); if (this._resultElement.OrderingsComeFromSubQuery) { this._resultElement.Orderings.Clear(); this._resultElement.OrderingsComeFromSubQuery = false; } this._resultElement.Orderings.Add(ordering); return(this); }
public virtual IQueryState Accept(GroupingQueryExpression exp) { foreach (LambdaExpression item in exp.GroupKeySelectors) { var keySelector = (LambdaExpression)item; ScopeParameterDictionary scopeParameters = this._resultElement.ScopeParameters.Clone(keySelector.Parameters[0], this._resultElement.MappingObjectExpression); var dbExp = GeneralExpressionVisitor.ParseLambda(keySelector, scopeParameters, this._resultElement.ScopeTables); this._resultElement.GroupSegments.Add(dbExp); } foreach (LambdaExpression havingPredicate in exp.HavingPredicates) { ScopeParameterDictionary scopeParameters = this._resultElement.ScopeParameters.Clone(havingPredicate.Parameters[0], this._resultElement.MappingObjectExpression); var havingCondition = FilterPredicateExpressionVisitor.ParseFilterPredicate(havingPredicate, scopeParameters, this._resultElement.ScopeTables); this._resultElement.AppendHavingCondition(havingCondition); } if (exp.Orderings.Count > 0) { this._resultElement.Orderings.Clear(); this._resultElement.InheritOrderings = false; for (int i = 0; i < exp.Orderings.Count; i++) { GroupingQueryOrdering groupOrdering = exp.Orderings[i]; ScopeParameterDictionary scopeParameters = this._resultElement.ScopeParameters.Clone(groupOrdering.KeySelector.Parameters[0], this._resultElement.MappingObjectExpression); DbExpression orderingDbExp = GeneralExpressionVisitor.ParseLambda(groupOrdering.KeySelector, scopeParameters, this._resultElement.ScopeTables); DbOrdering ordering = new DbOrdering(orderingDbExp, groupOrdering.OrderType); this._resultElement.Orderings.Add(ordering); } } var newResult = this.CreateNewResult(exp.Selector); return(new GroupingQueryState(newResult)); }
protected DbOrdering ParseOrderExpression(OrderExpression orderExp) { ScopeParameterDictionary scopeParameters = this._resultElement.ScopeParameters.Clone(orderExp.KeySelector.Parameters[0], this._resultElement.MappingObjectExpression); DbExpression dbExpression = GeneralExpressionVisitor.ParseLambda(orderExp.KeySelector, scopeParameters, this._resultElement.ScopeTables); DbOrderType orderType; if (orderExp.NodeType == QueryExpressionType.OrderBy || orderExp.NodeType == QueryExpressionType.ThenBy) { orderType = DbOrderType.Asc; } else if (orderExp.NodeType == QueryExpressionType.OrderByDesc || orderExp.NodeType == QueryExpressionType.ThenByDesc) { orderType = DbOrderType.Desc; } else throw new NotSupportedException(orderExp.NodeType.ToString()); DbOrdering ordering = new DbOrdering(dbExpression, orderType); return ordering; }
public virtual IQueryState Accept(GroupingQueryExpression exp) { foreach (LambdaExpression item in exp.GroupKeySelectors) { var keySelector = (LambdaExpression)item; ScopeParameterDictionary scopeParameters = this._queryModel.ScopeParameters.Clone(keySelector.Parameters[0], this._queryModel.ResultModel); this._queryModel.GroupSegments.AddRange(GroupKeySelectorParser.Parse(keySelector, scopeParameters, this._queryModel.ScopeTables)); } foreach (LambdaExpression havingPredicate in exp.HavingPredicates) { ScopeParameterDictionary scopeParameters = this._queryModel.ScopeParameters.Clone(havingPredicate.Parameters[0], this._queryModel.ResultModel); var havingCondition = FilterPredicateParser.Parse(havingPredicate, scopeParameters, this._queryModel.ScopeTables); this._queryModel.AppendHavingCondition(havingCondition); } if (exp.Orderings.Count > 0) { this._queryModel.Orderings.Clear(); this._queryModel.InheritOrderings = false; for (int i = 0; i < exp.Orderings.Count; i++) { GroupingQueryOrdering groupOrdering = exp.Orderings[i]; ScopeParameterDictionary scopeParameters = this._queryModel.ScopeParameters.Clone(groupOrdering.KeySelector.Parameters[0], this._queryModel.ResultModel); DbExpression orderingDbExp = GeneralExpressionParser.Parse(groupOrdering.KeySelector, scopeParameters, this._queryModel.ScopeTables); DbOrdering ordering = new DbOrdering(orderingDbExp, groupOrdering.OrderType); this._queryModel.Orderings.Add(ordering); } } QueryModel newQueryModel = this.CreateNewQueryModel(exp.Selector); return(new GroupingQueryState(newQueryModel)); }
protected static DbOrdering VisistOrderExpression(List <IMappingObjectExpression> moeList, OrderExpression orderExp) { DbExpression dbExpression = GeneralExpressionVisitor.ParseLambda(orderExp.KeySelector, moeList); DbOrderType orderType; if (orderExp.NodeType == QueryExpressionType.OrderBy || orderExp.NodeType == QueryExpressionType.ThenBy) { orderType = DbOrderType.Asc; } else if (orderExp.NodeType == QueryExpressionType.OrderByDesc || orderExp.NodeType == QueryExpressionType.ThenByDesc) { orderType = DbOrderType.Desc; } else { throw new NotSupportedException(orderExp.NodeType.ToString()); } DbOrdering ordering = new DbOrdering(dbExpression, orderType); return(ordering); }
protected DbOrdering ParseOrderExpression(OrderExpression orderExp) { ScopeParameterDictionary scopeParameters = this._queryModel.ScopeParameters.Clone(orderExp.KeySelector.Parameters[0], this._queryModel.ResultModel); DbExpression dbExpression = GeneralExpressionParser.Parse(orderExp.KeySelector, scopeParameters, this._queryModel.ScopeTables); DbOrderType orderType; if (orderExp.NodeType == QueryExpressionType.OrderBy || orderExp.NodeType == QueryExpressionType.ThenBy) { orderType = DbOrderType.Asc; } else if (orderExp.NodeType == QueryExpressionType.OrderByDesc || orderExp.NodeType == QueryExpressionType.ThenByDesc) { orderType = DbOrderType.Desc; } else { throw new NotSupportedException(orderExp.NodeType.ToString()); } DbOrdering ordering = new DbOrdering(dbExpression, orderType); return(ordering); }
protected virtual void BuildLimitSql(DbSqlQueryExpression exp) { this._sqlBuilder.Append("SELECT "); if (exp.TakeCount != null) { this._sqlBuilder.Append("TOP (", exp.TakeCount.ToString(), ") "); } string tableAlias = "T"; List <DbColumnSegment> columns = exp.ColumnSegments; for (int i = 0; i < columns.Count; i++) { DbColumnSegment column = columns[i]; if (i > 0) { this._sqlBuilder.Append(","); } this.QuoteName(tableAlias); this._sqlBuilder.Append("."); this.QuoteName(column.Alias); this._sqlBuilder.Append(" AS "); this.QuoteName(column.Alias); } this._sqlBuilder.Append(" FROM "); this._sqlBuilder.Append("("); //------------------------// this._sqlBuilder.Append("SELECT "); for (int i = 0; i < columns.Count; i++) { DbColumnSegment column = columns[i]; if (i > 0) { this._sqlBuilder.Append(","); } column.Body.Accept(this.ValueExpressionVisitor); this._sqlBuilder.Append(" AS "); this.QuoteName(column.Alias); } List <DbOrdering> orderings = exp.Orderings; if (orderings.Count == 0) { DbOrdering ordering = new DbOrdering(UtilConstants.DbParameter_1, OrderType.Asc); orderings = new List <DbOrdering>(1); orderings.Add(ordering); } string row_numberName = CreateRowNumberName(columns); this._sqlBuilder.Append(",ROW_NUMBER() OVER(ORDER BY "); this.ConcatOrderings(orderings); this._sqlBuilder.Append(") AS "); this.QuoteName(row_numberName); this._sqlBuilder.Append(" FROM "); exp.Table.Accept(this); this.BuildWhereState(exp.Condition); this.BuildGroupState(exp); //------------------------// this._sqlBuilder.Append(")"); this._sqlBuilder.Append(" AS "); this.QuoteName(tableAlias); this._sqlBuilder.Append(" WHERE "); this.QuoteName(tableAlias); this._sqlBuilder.Append("."); this.QuoteName(row_numberName); this._sqlBuilder.Append(" > "); this._sqlBuilder.Append(exp.SkipCount.ToString()); }
protected virtual void BuildLimitSql(DbSqlQueryExpression exp) { bool shouldSortResults = false; if (exp.TakeCount != null) { shouldSortResults = true; } else if (this._sqlBuilder.Length == 0) { shouldSortResults = true; } this._sqlBuilder.Append("SELECT "); this.AppendDistinct(exp.IsDistinct); if (exp.TakeCount != null) { this._sqlBuilder.Append("TOP ", exp.TakeCount.ToString(), " "); } string tableAlias = "T"; List <DbColumnSegment> columns = exp.ColumnSegments; for (int i = 0; i < columns.Count; i++) { DbColumnSegment column = columns[i]; if (i > 0) { this._sqlBuilder.Append(","); } this.QuoteName(tableAlias); this._sqlBuilder.Append("."); this.QuoteName(column.Alias); this._sqlBuilder.Append(" AS "); this.QuoteName(column.Alias); } this._sqlBuilder.Append(" FROM "); this._sqlBuilder.Append("("); //------------------------// this._sqlBuilder.Append("SELECT "); for (int i = 0; i < columns.Count; i++) { DbColumnSegment column = columns[i]; if (i > 0) { this._sqlBuilder.Append(","); } DbValueExpressionTransformer.Transform(column.Body).Accept(this); this._sqlBuilder.Append(" AS "); this.QuoteName(column.Alias); } List <DbOrdering> orderings = exp.Orderings; if (orderings.Count == 0) { DbOrdering ordering = new DbOrdering(PublicConstants.DbParameter_1, DbOrderType.Asc); orderings = new List <DbOrdering>(1); orderings.Add(ordering); } string row_numberName = GenRowNumberName(columns); this._sqlBuilder.Append(",ROW_NUMBER() OVER(ORDER BY "); this.ConcatOrderings(orderings); this._sqlBuilder.Append(") AS "); this.QuoteName(row_numberName); this._sqlBuilder.Append(" FROM "); exp.Table.Accept(this); this.BuildWhereState(exp.Condition); this.BuildGroupState(exp); //------------------------// this._sqlBuilder.Append(")"); this._sqlBuilder.Append(" AS "); this.QuoteName(tableAlias); this._sqlBuilder.Append(" WHERE "); this.QuoteName(tableAlias); this._sqlBuilder.Append("."); this.QuoteName(row_numberName); this._sqlBuilder.Append(" > "); this._sqlBuilder.Append(exp.SkipCount.ToString()); if (shouldSortResults) { this._sqlBuilder.Append(" ORDER BY "); this.QuoteName(tableAlias); this._sqlBuilder.Append("."); this.QuoteName(row_numberName); this._sqlBuilder.Append(" ASC"); } }