public AssociatedTableContext(ExpressionParser parser, TableContext parent, Association association) : base(parser, parent.SqlQuery) { var type = TypeHelper.GetMemberType(association.MemberAccessor.MemberInfo); var left = association.CanBeNull; var isList = false; if (TypeHelper.IsSameOrParent(typeof(IEnumerable), type)) { var etypes = TypeHelper.GetGenericArguments(type, typeof(IEnumerable)); type = etypes != null && etypes.Length > 0 ? etypes[0] : TypeHelper.GetListItemType(type); isList = true; } OriginalType = type; ObjectType = GetObjectType(); ObjectMapper = Parser.MappingSchema.GetObjectMapper(ObjectType); SqlTable = new SqlTable(parser.MappingSchema, ObjectType); var psrc = parent.SqlQuery.From[parent.SqlTable]; var join = left ? SqlTable.WeakLeftJoin() : isList?SqlTable.InnerJoin() : SqlTable.WeakInnerJoin(); _parentAssociation = parent; ParentAssociationJoin = join.JoinedTable; psrc.Joins.Add(join.JoinedTable); //Init(mappingSchema); for (var i = 0; i < association.ThisKey.Length; i++) { SqlField field1; SqlField field2; if (!parent.SqlTable.Fields.TryGetValue(association.ThisKey[i], out field1)) { throw new LinqException("Association key '{0}' not found for type '{1}.", association.ThisKey[i], parent.ObjectType); } if (!SqlTable.Fields.TryGetValue(association.OtherKey[i], out field2)) { throw new LinqException("Association key '{0}' not found for type '{1}.", association.OtherKey[i], ObjectType); } join.Field(field1).Equal.Field(field2); } }
Table(MappingSchema mappingSchema, SqlQuery sqlQuery, Association association, Table parent) : base(sqlQuery, null) { var type = TypeHelper.GetMemberType(association.MemberAccessor.MemberInfo); var left = association.CanBeNull; var isList = false; if (TypeHelper.IsSameOrParent(typeof(IEnumerable), type)) { var etypes = TypeHelper.GetGenericArguments(type, typeof(IEnumerable)); type = etypes != null && etypes.Length > 0 ? etypes[0] : TypeHelper.GetListItemType(type); isList = true; } OriginalType = type; _objectType = GetObjectType(OriginalType, mappingSchema); SqlTable = new SqlTable(mappingSchema, ObjectType); var psrc = sqlQuery.From[parent.SqlTable]; var join = left ? SqlTable.WeakLeftJoin() : isList?SqlTable.InnerJoin() : SqlTable.WeakInnerJoin(); _parentAssociation = parent; _parentAssociationJoin = join.JoinedTable; psrc.Joins.Add(join.JoinedTable); Init(mappingSchema); for (var i = 0; i < association.ThisKey.Length; i++) { Column col1, col2; if (!parent._columns.TryGetValue(association.ThisKey[i], out col1)) { throw new LinqException("Association key '{0}' not found for type '{1}.", association.ThisKey[i], parent.ObjectType); } if (!_columns.TryGetValue(association.OtherKey[i], out col2)) { throw new LinqException("Association key '{0}' not found for type '{1}.", association.OtherKey[i], ObjectType); } join.Field(col1.Field).Equal.Field(col2.Field); } ParsingTracer.DecIndentLevel(); }
public AssociatedTableContext(ExpressionBuilder builder, TableContext parent, AssociationDescriptor association) : base(builder, parent.SelectQuery) { var type = association.MemberInfo.GetMemberType(); var left = association.CanBeNull; if (typeof(IEnumerable).IsSameOrParentOf(type)) { var etypes = type.GetGenericArguments(typeof(IEnumerable <>)); type = etypes != null && etypes.Length > 0 ? etypes[0] : type.GetListItemType(); IsList = true; } OriginalType = type; ObjectType = GetObjectType(); EntityDescriptor = Builder.MappingSchema.GetEntityDescriptor(ObjectType); SqlTable = new SqlTable(builder.MappingSchema, ObjectType); var psrc = parent.SelectQuery.From[parent.SqlTable]; var join = left ? SqlTable.WeakLeftJoin() : IsList?SqlTable.InnerJoin() : SqlTable.WeakInnerJoin(); Association = association; ParentAssociation = parent; ParentAssociationJoin = join.JoinedTable; psrc.Joins.Add(join.JoinedTable); for (var i = 0; i < association.ThisKey.Length; i++) { SqlField field1; SqlField field2; if (!parent.SqlTable.Fields.TryGetValue(association.ThisKey[i], out field1)) { throw new LinqException("Association key '{0}' not found for type '{1}.", association.ThisKey[i], parent.ObjectType); } if (!SqlTable.Fields.TryGetValue(association.OtherKey[i], out field2)) { throw new LinqException("Association key '{0}' not found for type '{1}.", association.OtherKey[i], ObjectType); } join.Field(field1).Equal.Field(field2); } Init(); }
private SqlSelect BuildJoinQuery(IndexInfo index) { SqlTable resultTable = null; SqlTable rootTable = null; int keyColumnCount = index.KeyColumns.Count; var underlyingQueries = index.UnderlyingIndexes.Select(BuildProviderQuery); var sourceTables = index.UnderlyingIndexes.Any(i => i.IsVirtual) ? underlyingQueries.Select(SqlDml.QueryRef).Cast <SqlTable>().ToList() : underlyingQueries.Select(q => { var tableRef = (SqlTableRef)q.From; return((SqlTable)SqlDml.TableRef(tableRef.DataTable, tableRef.Name, q.Columns.Select(c => c.Name))); }).ToList(); foreach (var table in sourceTables) { if (resultTable == null) { resultTable = rootTable = table; } else { SqlExpression joinExpression = null; for (int i = 0; i < keyColumnCount; i++) { var binary = (table.Columns[i] == rootTable.Columns[i]); if (joinExpression.IsNullReference()) { joinExpression = binary; } else { joinExpression &= binary; } } resultTable = resultTable.InnerJoin(table, joinExpression); } } var columns = new List <SqlColumn>(); foreach (var map in index.ValueColumnsMap) { var table = sourceTables[map.First]; if (columns.Count == 0) { var keyColumns = Enumerable .Range(0, keyColumnCount) .Select(i => table.Columns[i]) .Cast <SqlColumn>(); columns.AddRange(keyColumns); } var valueColumns = map.Second .Select(columnIndex => table.Columns[columnIndex + keyColumnCount]) .Cast <SqlColumn>(); columns.AddRange(valueColumns); } var query = SqlDml.Select(resultTable); query.Columns.AddRange(columns); return(query); }