static ResultElement CreateResultElement(Type type) { //TODO init _resultElement ResultElement resultElement = new ResultElement(); TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(type); string alias = resultElement.GenerateUniqueTableAlias(typeDescriptor.Table.Name); resultElement.FromTable = CreateRootTable(typeDescriptor.Table, alias); MappingObjectExpression moe = new MappingObjectExpression(typeDescriptor.EntityType.GetConstructor(Type.EmptyTypes)); DbTableSegment tableExp = resultElement.FromTable.Table; DbTable table = new DbTable(alias); foreach (MappingMemberDescriptor item in typeDescriptor.MappingMemberDescriptors.Values) { DbColumnAccessExpression columnAccessExpression = new DbColumnAccessExpression(table, item.Column); moe.AddMemberExpression(item.MemberInfo, columnAccessExpression); if (item.IsPrimaryKey) { moe.PrimaryKey = columnAccessExpression; } } resultElement.MappingObjectExpression = moe; return(resultElement); }
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); }
static ResultElement CreateResultElement(Type type, string explicitTableName) { if (type.IsAbstract || type.IsInterface) { throw new ArgumentException("The type of input can not be abstract class or interface."); } //TODO init _resultElement ResultElement resultElement = new ResultElement(); TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(type); DbTable dbTable = typeDescriptor.Table; if (explicitTableName != null) { dbTable = new DbTable(explicitTableName, dbTable.Schema); } string alias = resultElement.GenerateUniqueTableAlias(dbTable.Name); resultElement.FromTable = CreateRootTable(dbTable, alias); ConstructorInfo constructor = typeDescriptor.EntityType.GetConstructor(Type.EmptyTypes); if (constructor == null) { throw new ArgumentException(string.Format("The type of '{0}' does't define a none parameter constructor.", type.FullName)); } MappingObjectExpression moe = new MappingObjectExpression(constructor); DbTableSegment tableExp = resultElement.FromTable.Table; DbTable table = new DbTable(alias); foreach (MappingMemberDescriptor item in typeDescriptor.MappingMemberDescriptors.Values) { DbColumnAccessExpression columnAccessExpression = new DbColumnAccessExpression(table, item.Column); moe.AddMappingMemberExpression(item.MemberInfo, columnAccessExpression); if (item.IsPrimaryKey) { moe.PrimaryKey = columnAccessExpression; } } resultElement.MappingObjectExpression = moe; return(resultElement); }
public virtual ResultElement ToFromQueryResult() { ResultElement result = new ResultElement(this._resultElement.ScopeParameters, this._resultElement.ScopeTables); string alias = result.GenerateUniqueTableAlias(UtilConstants.DefaultTableAlias); DbSqlQueryExpression sqlQuery = this.CreateSqlQuery(); DbSubQueryExpression subQuery = new DbSubQueryExpression(sqlQuery); DbTableSegment tableSeg = new DbTableSegment(subQuery, alias, LockType.Unspecified); DbFromTableExpression fromTable = new DbFromTableExpression(tableSeg); DbTable table = new DbTable(tableSeg.Alias); IMappingObjectExpression newMoe = this.Result.MappingObjectExpression.ToNewObjectExpression(sqlQuery, table); result.FromTable = fromTable; result.MappingObjectExpression = newMoe; return(result); }
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.OrderSegments.Count > 0) { for (int i = 0; i < this.Result.OrderSegments.Count; i++) { DbOrderSegment orderPart = this.Result.OrderSegments[i]; DbExpression orderExp = orderPart.DbExpression; string alias = null; DbColumnSegment columnExpression = sqlQuery.ColumnSegments.Where(a => DbExpressionEqualityComparer.EqualsCompare(orderExp, a.Body)).FirstOrDefault(); // 对于重复的则不需要往 sqlQuery.Columns 重复添加了 if (columnExpression != null) { alias = columnExpression.Alias; } else { alias = Utils.GenerateUniqueColumnAlias(sqlQuery); DbColumnSegment columnSeg = new DbColumnSegment(orderExp, alias); sqlQuery.ColumnSegments.Add(columnSeg); } DbColumnAccessExpression columnAccessExpression = new DbColumnAccessExpression(orderExp.Type, table, alias); result.OrderSegments.Add(new DbOrderSegment(columnAccessExpression, orderPart.OrderType)); } } result.IsOrderSegmentsFromSubQuery = true; GeneralQueryState queryState = new GeneralQueryState(result); return queryState; }