using System; using System.Collections.Generic; using System.Linq; using OpenDBDiff.Schema.Model; namespace OpenDBDiff.Schema.SQLServer.Generates.Model { internal class Dependencies : List { public Database Database { get; private set; } public void Add(Database database, int tableId, int columnId, int ownerTableId, int typeId, ISchemaBase constraint) { Dependency dependency = new Dependency(); dependency.SubObjectId = columnId; dependency.ObjectId = tableId; dependency.OwnerTableId = ownerTableId; dependency.FullName = constraint.FullName; dependency.Type = constraint.ObjectType; dependency.DataTypeId = typeId; this.Database = database; base.Add(dependency); } public void Add(Database database, int objectId, ISchemaBase objectSchema) { Dependency dependency = new Dependency(); dependency.ObjectId = objectId; dependency.FullName = objectSchema.FullName; dependency.Type = objectSchema.ObjectType; this.Database = database; base.Add(dependency); } /// /// Devuelve todos las constraints dependientes de una tabla. /// public List FindNotOwner(int tableId, ObjectType type) { try { List cons = new List(); this.ForEach(dependency => { if (dependency.Type == type) { ISchemaBase item = (ISchemaBase)Database.Find(dependency.FullName); if (dependency.Type == ObjectType.Constraint) { if ((dependency.ObjectId == tableId) && (((Constraint)item).Type == Constraint.ConstraintType.ForeignKey)) cons.Add(item); } else if (dependency.ObjectId == tableId) cons.Add(item); } }); return cons; } catch (Exception ex) { throw ex; } } /// /// Devuelve todos las constraints dependientes de una tabla. /// /*public void Set(int tableId, Constraint constraint) { this.ForEach(item => { if (item.Type == ObjectType.Constraint) if ((item.ObjectId == tableId) && (item.ObjectSchema.Name.Equals(constraint.Name))) item.ObjectSchema = constraint; }); }*/ /// /// Devuelve todos las constraints dependientes de una tabla. /// public List Find(int tableId) { return Find(tableId, 0, 0); } public int DependenciesCount(int objectId, ObjectType type) { Dictionary depencyTracker = new Dictionary(); return DependenciesCount(objectId, type, depencyTracker); } private int DependenciesCount(int tableId, ObjectType type, Dictionary depencyTracker) { int count = 0; bool putItem = false; int relationalTableId; List constraints = this.FindNotOwner(tableId, type); for (int index = 0; index < constraints.Count; index++) { ISchemaBase cons = constraints[index]; if (cons.ObjectType == type) { if (type == ObjectType.Constraint) { relationalTableId = ((Constraint)cons).RelationalTableId; putItem = (relationalTableId == tableId); } } if (putItem) { if (!depencyTracker.ContainsKey(tableId)) { depencyTracker.Add(tableId, true); count += 1 + DependenciesCount(cons.Parent.Id, type, depencyTracker); } } } return count; } /// /// Devuelve todos las constraints dependientes de una tabla y una columna. /// public List Find(int tableId, int columnId, int dataTypeId) { List cons = new List(); List real = new List(); cons = (from depends in this where (depends.Type == ObjectType.Constraint || depends.Type == ObjectType.Index) && ((depends.DataTypeId == dataTypeId || dataTypeId == 0) && (depends.SubObjectId == columnId || columnId == 0) && (depends.ObjectId == tableId)) select depends.FullName) .Concat(from depends in this where (depends.Type == ObjectType.View || depends.Type == ObjectType.Function) && (depends.ObjectId == tableId) select depends.FullName).ToList(); cons.ForEach(item => { ISchemaBase schema = Database.Find(item); if (schema != null) real.Add(schema); } ); return real; } } }