Пример #1
0
 public void GetIndexes(MetaInfoIndexes indexes, MetaInfoTables tables)
 {
     //table.Indexes.Clear();
     foreach (DataRow row in SelectData(GetIndexesSql()).Rows)
     {
         string        persistentSchema = row[SqlColName_IndexOwner].ToString();
         string        persistentName   = row[SqlColName_IndexName].ToString();
         bool          isUnique         = int.Parse(row[SqlColName_IsUnique].ToString()) == 1 ? true : false;
         MetaInfoIndex ix          = indexes.FindByPersistentName(persistentSchema, persistentName, false);
         bool          indexExists = ix != null;
         if (!indexExists)
         {
             ix = new MetaInfoIndex();
             ix.PersistentSchema = persistentSchema;
             ix.PersistentName   = persistentName;
             ix.IsUnique         = isUnique;
         }
         string        tableSchema = row[SqlColName_TableSchema].ToString();
         string        tableName   = row[SqlColName_TableName].ToString();
         MetaInfoTable table       = tables.FindByPersistentName(tableSchema, tableName, true);
         if (table != null)
         {
             ix.Table = table;
             ix.Columns.Add(table.Columns.FindByPersistentName(row[SqlColName_ColumnName].ToString(), true));
             if (!indexExists)
             {
                 table.Indexes.Add(ix);
                 indexes.Add(ix);
             }
         }
         Logger.ProgressStep();
     }
 }
Пример #2
0
        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++);
                        }
                    }
                }
            }
        }