private static void CopyKeys(string sourceTable, string destTable, FkDicts dicts) { if (dicts.relationDict.ContainsKey(sourceTable)) //получаем исходник { var sourceList = dicts.relationDict[sourceTable]; List <Relation> destList; if (dicts.relationDict.ContainsKey(destTable)) { destList = dicts.relationDict[destTable]; } else { destList = new List <Relation>(); dicts.relationDict.Add(destTable, destList); } Key refKey; string key; foreach (var relation in sourceList) { if (destList.Contains(relation) == false) { destList.Add(relation); } key = CreateFkDictKey(destTable, relation.columnName); refKey = new Key { tableName = relation.referencedTable, columnName = relation.referencedColumn }; if (dicts.keyDict.ContainsKey(key) == false) { dicts.keyDict.Add(key, refKey); } } } }
private static FkDicts GetForeignKeysFromBase() { var fkDicts = new FkDicts(); fkDicts.keyDict = new Dictionary <string, Key>(); fkDicts.relationDict = new Dictionary <string, List <Relation> >(); fkDicts.columnDict = new Dictionary <string, List <string> >(); string query = @"SELECT (detail_relation_constraints.rdb$relation_name) AS table_name, (detail_index_segments.rdb$field_name) AS field_name, (master_relation_constraints.rdb$relation_name) AS reference_table, (master_index_segments.rdb$field_name) AS fk_field FROM rdb$relation_constraints detail_relation_constraints JOIN rdb$index_segments detail_index_segments ON detail_relation_constraints.rdb$index_name = detail_index_segments.rdb$index_name JOIN rdb$ref_constraints ON detail_relation_constraints.rdb$constraint_name = rdb$ref_constraints.rdb$constraint_name -- Master indeksas JOIN rdb$relation_constraints master_relation_constraints ON rdb$ref_constraints.rdb$const_name_uq = master_relation_constraints.rdb$constraint_name JOIN rdb$index_segments master_index_segments ON master_relation_constraints.rdb$index_name = master_index_segments.rdb$index_name WHERE detail_relation_constraints.rdb$constraint_type = 'FOREIGN KEY'"; var fkTable = DataBaseConnector.GetTableByQuery(query); foreach (DataRow row in fkTable.Rows) { string table = row[0].ToString().Replace(" ", ""); string column = row[1].ToString().Replace(" ", ""); string refTable = row[2].ToString().Replace(" ", ""); string refColumn = row[3].ToString().Replace(" ", ""); AddValuesToFkDics(table, column, refTable, refColumn, fkDicts); } return(fkDicts); }
private static void AddValuesToFkDics(string tableName, string columnName, string refTable, string refColumn, FkDicts dicts) { string key = CreateFkDictKey(tableName, columnName); Key refKey = new Key { tableName = refTable, columnName = refColumn }; Relation relation = new Relation { columnName = columnName, referencedTable = refTable, referencedColumn = refColumn }; List <Relation> relationList; List <string> columnList; //Relation list if (dicts.relationDict.ContainsKey(tableName)) { relationList = dicts.relationDict[tableName]; } else { relationList = new List <Relation>(); dicts.relationDict.Add(tableName, relationList); } //Column List if (dicts.columnDict.ContainsKey(tableName)) { columnList = dicts.columnDict[tableName]; } else { columnList = new List <string>(); dicts.columnDict.Add(tableName, columnList); } //Relation List if (relationList.Contains(relation) == false) { relationList.Add(relation); } //Keys list if (dicts.keyDict.ContainsKey(key) == false) { dicts.keyDict.Add(key, refKey); } //Column List if (columnList.Contains(columnName) == false) { columnList.Add(columnName); } }