Пример #1
0
        internal static CreateTable FromSQL(Table table)
        {
            var createTable = new CreateTable(SqlUtil.GetQuotedObjectIdentifierString(table.name, table.Schema.name), table.Columns.Select(c => ColumnInfo.FromSQL(c)), $"Table {SqlUtil.GetQuotedObjectIdentifierString(table.name, table.Schema.name)} was reverse engineered from schema inspection.");

            createTable.Constraints.AddRange(table.Indexes.Where(x => x.is_primary_key).Select(index => new PrimaryKeyInfo
            {
                PrimaryKeyName = index.name,
                IsNonClustered = index.type_desc == "NONCLUSTERED",
                Columns = index.Columns.OrderBy(x => x.key_ordinal).Select(x => "[" + x.Column.Name + "]" + (x.is_descending_key ? " DESC" : "")),
            }));

            createTable.Constraints.AddRange(table.Indexes.Where(x => x.is_unique_constraint).Select(index => new UniqueConstraintInfo
            {
                ConstraintName = index.name,
                IsSystemNamed = index.is_system_named,
                IsNonClustered = index.type_desc == "NONCLUSTERED",
                Columns = index.Columns.OrderBy(x => x.key_ordinal).Select(x => "[" + x.Column.Name + "]" + (x.is_descending_key ? " DESC" : "")),
            }));

            createTable.Constraints.AddRange(table.ForeignKeys.Where(fk => !fk.IsCircularDependency).Select(fk => new ForeignKeyInfo
            {
                QuotedForeignKeyName = SqlUtil.GetQuotedObjectIdentifierString(fk.ForeignKeyName),
                ParentObjectIdentifier = SqlUtil.GetQuotedObjectIdentifierString(fk.Parent.name, fk.Parent.Schema.name),
                ParentObjectColumns = fk.Columns.OrderBy(c => c.constraint_object_id).Select(c => c.ParentColumnName),
                ReferencedObjectIdentifier = SqlUtil.GetQuotedObjectIdentifierString(fk.Referenced.name, fk.Referenced.Schema.name),
                ReferencedObjectColumns = fk.Columns.OrderBy(c => c.constraint_object_id).Select(c => c.ReferencedColumnName),
                CascadeDelete = fk.delete_referential_action == 1,
            }));

            createTable.Constraints.AddRange(table.TableCheckConstraints.Select(x => new TableCheckConstraintInfo
            {
                ConstraintName = x.name,
                IsSystemNamed = x.is_system_named,
                Definition = x.definition,
            }));
            return createTable;
        }
Пример #2
0
 private bool IsCircularDependencyRecurisve(Table rootTable, ForeignKey fk)
 {
     if (visitedFK.Contains(fk))
     {
         return false;
     }
     visitedFK.Add(fk);
     if (fk.Referenced == rootTable)
     {
         return true;
     }
     return fk.Referenced.ForeignKeys.Any(_fk => IsCircularDependencyRecurisve(rootTable, _fk));
 }
Пример #3
0
 private bool IsCircularDependency(Table rootTable, ForeignKey fk)
 {
     visitedFK.Clear();
     return IsCircularDependencyRecurisve(rootTable, fk);
 }