private void CreateTableCopyForReference(BimRelationship rr, BimGeneratorModel model, int i) { var dd = rr.FromColumn.Table.DatabaseDefinition; var toSqlTable = dd.GetTable(rr.ToTableSchemaAndTableName); var suffix = " " + i.ToString("D", CultureInfo.InvariantCulture); if (rr.RelationshipIdentifier != null) { suffix = " " + rr.RelationshipIdentifier; } var copyTableName = GetBimTableName(toSqlTable.SchemaAndTableName) + suffix; var copySchemaAndTableName = new SchemaAndTableName(toSqlTable.SchemaAndTableName.Schema, toSqlTable.SchemaAndTableName.TableName + suffix); rr.ToTableSchemaAndTableName = copySchemaAndTableName; if (!model.Tables.Any(t => t.Name == copyTableName)) { Context.Logger.Log(LogSeverity.Information, "Table {TableName} COPIED to bim model, multiple references from {FromTable}", "BimGenerator", copySchemaAndTableName, rr.FromColumn.Table.SchemaAndTableName); model.Tables.Add(GenerateTable(toSqlTable, copyTableName)); } }
private void GenerateReferences(RelationShipRegistrations relationShipRegistrations, BimGeneratorModel model) { // Create shadow copies where multiple FKs are on a source table // Use same RelationShipIdentifier // Omit Source table if no other FK is pointing to it foreach (var fromTable in relationShipRegistrations.FromTables()) { var toTables = new Dictionary <string, int>(); var to = relationShipRegistrations.GetByFromTable(fromTable); foreach (var rr in to) { if (rr.FromTableSchemaAndTableName == rr.ToTableSchemaAndTableName) { Context.Logger.Log(LogSeverity.Warning, "Table {TableName} is referencing itself. SKIPPED.", "BimGenerator", fromTable); continue; } var trp = rr.FromColumn.Properties.OfType <TabularRelationProperty>().FirstOrDefault(); if (trp != null) { rr.RelationshipIdentifier = trp.RelationshipIdentifier; } // TODO TabularRelationProperty without FK should create replationship // TODO IF we have a RelationshipIdentifier // A/ Create a Copy -> TableName + " " + RelationshipIdentifier // if any other reference exists (without RelationshipIdentifier) // B/ Rename existing TableName to TableName + " " + RelationshipIdentifier // if NO other reference exists (without RelationshipIdentifier or any other RelationshipIdentifier) // same target on this table if (!toTables.ContainsKey(rr.ToKey)) { toTables.Add(rr.ToKey, 1); } else { toTables[rr.ToKey]++; var numberOfReferencesToTheSameTable = toTables[rr.ToKey]; if (numberOfReferencesToTheSameTable > 1) { CreateTableCopyForReference(rr, model, numberOfReferencesToTheSameTable); } } model.Relationships.Add(GenerateRelationship(rr)); } } }