public virtual void GetForeignKeys(MetaInfoTables tables) { foreach (MetaInfoTable table in tables) { table.ForeignKeys.Clear(); foreach (DataRow row in SelectData(GetForeignKeysSql(table)).Rows) { string persistentSchema = row[SqlColName_FKTableSchema].ToString(); string persistentName = row[SqlColName_ConstraintName].ToString(); MetaInfoForeignKey fk = table.ForeignKeys.FindByPersistentName(persistentSchema, persistentName, false); if (fk == null) { string msg = String.Format("Importing FK: {0}...", MetaInfoBase.MakeFullName(persistentSchema, persistentName)); fk = new MetaInfoForeignKey(); fk.PersistentSchema = persistentSchema; fk.PersistentName = persistentName; fk.Child = table; string parentTableSchema = row[SqlColName_FKParentTableSchema].ToString(); string parentTableName = row[SqlColName_FKParentTableName].ToString(); fk.Parent = tables.FindByPersistentName(parentTableSchema, parentTableName, false); if (fk.Parent != null) { table.ForeignKeys.Add(fk); fk.Name = fk.Parent.Name; Logger.TraceLine("{0}OK", msg); } else { Logger.TraceLine("{0}Ignored (parent table {1} was not imported)", msg, MetaInfoBase.MakeFullName(parentTableSchema, parentTableName)); } } if (fk.Child != null && fk.Parent != null) { MetaInfoColumnsMatch cm = new MetaInfoColumnsMatch(); cm.Child = fk.Child.Columns.FindByPersistentName(row[SqlColName_ColumnName].ToString(), true); cm.Parent = fk.Parent.Columns.FindByPersistentName(row[SqlColName_FKParentColumnName].ToString(), true); fk.ColumnsMatches.Add(cm); } Logger.ProgressStep(); } // Renaming duplicates in FK names foreach (MetaInfoForeignKey fk in table.ForeignKeys) { int count = 1; foreach (MetaInfoForeignKey fk2 in table.ForeignKeys) { if (fk != fk2 && fk.Name == fk2.Name) { fk2.Name = String.Format("{0}{1}", fk.Name, count++); } } } } }
public T FindByPersistentName(string schema, string name, bool throwException) { T o = null; persistentNames.TryGetValue(MetaInfoBase.MakeFullName(schema, name), out o); if (o == null && throwException) { throw new GlException("Item with persistent name not found: {0} ({1})", name, typeof(T).Name); } return(o); }