Ejemplo n.º 1
0
        public ConstraintInfo FindOrCreateConstraint(ConstraintInfo cnt)
        {
            var t   = FindOrCreateTable(cnt.OwnerTable.FullName);
            var res = t.FindConstraint(cnt);

            if (res == null)
            {
                t.AddConstraint(cnt);
            }
            return(res);
        }
Ejemplo n.º 2
0
 public static void CreateConstraint(this IAlterProcessor proc, ConstraintInfo cnt)
 {
     var pk = cnt as PrimaryKeyInfo;
     if (pk != null) proc.CreatePrimaryKey(pk);
     var fk = cnt as ForeignKeyInfo;
     if (fk != null) proc.CreateForeignKey(fk);
     var uq = cnt as UniqueInfo;
     if (uq != null) proc.CreateUnique(uq);
     var ix = cnt as IndexInfo;
     if (ix != null) proc.CreateIndex(ix);
     var ch = cnt as CheckInfo;
     if (ch != null) proc.CreateCheck(ch);
 }
Ejemplo n.º 3
0
 public void DropConstraint(ConstraintInfo cnt)
 {
     if (cnt is PrimaryKeyInfo)
     {
         PrimaryKey = null;
     }
     if (cnt is ForeignKeyInfo)
     {
         ForeignKeys.RemoveAll(x => x.ConstraintName == cnt.ConstraintName);
     }
     if (cnt is IndexInfo)
     {
         Indexes.RemoveAll(x => x.ConstraintName == cnt.ConstraintName);
     }
     if (cnt is UniqueInfo)
     {
         Uniques.RemoveAll(x => x.ConstraintName == cnt.ConstraintName);
     }
     if (cnt is CheckInfo)
     {
         Checks.RemoveAll(x => x.ConstraintName == cnt.ConstraintName);
     }
 }
Ejemplo n.º 4
0
 public ConstraintInfo FindConstraint(ConstraintInfo cnt)
 {
     if (cnt is PrimaryKeyInfo)
     {
         return(PrimaryKey);
     }
     if (cnt is ForeignKeyInfo)
     {
         return(ForeignKeys.FirstOrDefault(x => x.ConstraintName == cnt.ConstraintName));
     }
     if (cnt is IndexInfo)
     {
         return(Indexes.FirstOrDefault(x => x.ConstraintName == cnt.ConstraintName));
     }
     if (cnt is UniqueInfo)
     {
         return(Uniques.FirstOrDefault(x => x.ConstraintName == cnt.ConstraintName));
     }
     if (cnt is CheckInfo)
     {
         return(Checks.FirstOrDefault(x => x.ConstraintName == cnt.ConstraintName));
     }
     return(null);
 }
Ejemplo n.º 5
0
 private static int CompareConstraints(ConstraintInfo lft, ConstraintInfo rgt)
 {
     var nl = lft.ConstraintName;
     var nr = rgt.ConstraintName;
     if (lft.GetType() != rgt.GetType()) return String.Compare(lft.GetType().FullName, rgt.GetType().FullName);
     if (nl != null && nr != null) return String.Compare(nl, nr);
     return 0;
 }
Ejemplo n.º 6
0
 public virtual void RenameConstraint(ConstraintInfo constraint, string newname)
 {
     throw new System.NotImplementedException();
 }
Ejemplo n.º 7
0
 protected virtual void DropConstraint(ConstraintInfo cnt)
 {
     PutCmd("^alter ^table %f ^drop ^constraint %i", cnt.OwnerTable.FullName, cnt.ConstraintName);
 }
Ejemplo n.º 8
0
 public void CreateConstraint(TableInfo table, ConstraintInfo newcnt, PlanPosition pos = PlanPosition.End)
 {
     TableInfo tbl = Structure.FindOrCreateTable(table.FullName);
     AddOperation(new AlterOperation_CreateConstraint { ParentTable = tbl, NewObject = newcnt.CloneConstraint() }, pos);
 }
Ejemplo n.º 9
0
 public void ChangeConstraint(ConstraintInfo constraint, ConstraintInfo newconstraint)
 {
     ConstraintInfo cnt = Structure.FindOrCreateConstraint(constraint);
     AddOperation(new AlterOperation_ChangeConstraint { OldObject = cnt, ParentTable = cnt.OwnerTable, NewObject = newconstraint.CloneConstraint() });
 }
Ejemplo n.º 10
0
 public void DropConstraint(ConstraintInfo cnt)
 {
     if (cnt is PrimaryKeyInfo) PrimaryKey = null;
     if (cnt is ForeignKeyInfo) ForeignKeys.RemoveAll(x => x.ConstraintName == cnt.ConstraintName);
     if (cnt is IndexInfo) Indexes.RemoveAll(x => x.ConstraintName == cnt.ConstraintName);
     if (cnt is UniqueInfo) Uniques.RemoveAll(x => x.ConstraintName == cnt.ConstraintName);
     if (cnt is CheckInfo) Checks.RemoveAll(x => x.ConstraintName == cnt.ConstraintName);
 }
Ejemplo n.º 11
0
 public void AddConstraint(ConstraintInfo cnt, bool reuseGrouId = false)
 {
     var primaryKeyInfo = cnt as PrimaryKeyInfo;
     if (primaryKeyInfo != null)
     {
         PrimaryKey = primaryKeyInfo.ClonePrimaryKey(this);
         if (!reuseGrouId) PrimaryKey.GroupId = Guid.NewGuid().ToString();
     }
     var foreignKeyInfo = cnt as ForeignKeyInfo;
     if (foreignKeyInfo != null)
     {
         var fknew = foreignKeyInfo.CloneForeignKey(this);
         if (!reuseGrouId) fknew.GroupId = Guid.NewGuid().ToString();
         ForeignKeys.Add(fknew);
     }
     var indexInfo = cnt as IndexInfo;
     if (indexInfo != null)
     {
         var ixnew = indexInfo.CloneIndex(this);
         if (!reuseGrouId) ixnew.GroupId = Guid.NewGuid().ToString();
         Indexes.Add(ixnew);
     }
     var uniqueInfo = cnt as UniqueInfo;
     if (uniqueInfo != null)
     {
         var uqnew = uniqueInfo.CloneUnique(this);
         if (!reuseGrouId) uqnew.GroupId = Guid.NewGuid().ToString();
         Uniques.Add(uqnew);
     }
     var checkInfo = cnt as CheckInfo;
     if (checkInfo != null)
     {
         var chnew = checkInfo.CloneCheck(this);
         if (!reuseGrouId) chnew.GroupId = Guid.NewGuid().ToString();
         Checks.Add(chnew);
     }
 }
Ejemplo n.º 12
0
 public ConstraintInfo FindConstraint(ConstraintInfo cnt)
 {
     if (cnt is PrimaryKeyInfo) return PrimaryKey;
     if (cnt is ForeignKeyInfo) return ForeignKeys.FirstOrDefault(x => x.ConstraintName == cnt.ConstraintName);
     if (cnt is IndexInfo) return Indexes.FirstOrDefault(x => x.ConstraintName == cnt.ConstraintName);
     if (cnt is UniqueInfo) return Uniques.FirstOrDefault(x => x.ConstraintName == cnt.ConstraintName);
     if (cnt is CheckInfo) return Checks.FirstOrDefault(x => x.ConstraintName == cnt.ConstraintName);
     return null;
 }
Ejemplo n.º 13
0
 public void RenameConstraint(ConstraintInfo constraint, string newname)
 {
     _database.FindConstraint(constraint).ConstraintName = newname;
 }
Ejemplo n.º 14
0
        public void AddConstraint(ConstraintInfo cnt, bool reuseGrouId = false)
        {
            var primaryKeyInfo = cnt as PrimaryKeyInfo;

            if (primaryKeyInfo != null)
            {
                PrimaryKey = primaryKeyInfo.ClonePrimaryKey(this);
                if (!reuseGrouId)
                {
                    PrimaryKey.GroupId = Guid.NewGuid().ToString();
                }
                PrimaryKey.OwnerTable = this;
            }
            var foreignKeyInfo = cnt as ForeignKeyInfo;

            if (foreignKeyInfo != null)
            {
                var fknew = foreignKeyInfo.CloneForeignKey(this);
                if (!reuseGrouId)
                {
                    fknew.GroupId = Guid.NewGuid().ToString();
                }
                ForeignKeys.Add(fknew);
                fknew.OwnerTable = this;
            }
            var indexInfo = cnt as IndexInfo;

            if (indexInfo != null)
            {
                var ixnew = indexInfo.CloneIndex(this);
                if (!reuseGrouId)
                {
                    ixnew.GroupId = Guid.NewGuid().ToString();
                }
                Indexes.Add(ixnew);
                ixnew.OwnerTable = this;
            }
            var uniqueInfo = cnt as UniqueInfo;

            if (uniqueInfo != null)
            {
                var uqnew = uniqueInfo.CloneUnique(this);
                if (!reuseGrouId)
                {
                    uqnew.GroupId = Guid.NewGuid().ToString();
                }
                Uniques.Add(uqnew);
                uqnew.OwnerTable = this;
            }
            var checkInfo = cnt as CheckInfo;

            if (checkInfo != null)
            {
                var chnew = checkInfo.CloneCheck(this);
                if (!reuseGrouId)
                {
                    chnew.GroupId = Guid.NewGuid().ToString();
                }
                Checks.Add(chnew);
                chnew.OwnerTable = this;
            }
        }
Ejemplo n.º 15
0
 public static bool EqualsConstraints(ConstraintInfo csrc, ConstraintInfo cdst, DbDiffOptions options, bool checkNames, DbObjectPairing pairing)
 {
     if (checkNames && !options.IgnoreConstraintNames)
     {
         if (!EqualNames(csrc.ConstraintName, cdst.ConstraintName, options))
         {
             return false;
             //if (csrc is PrimaryKeyInfo && cdst is PrimaryKeyInfo) // && (pairing.Source.Dialect.DialectCaps.AnonymousPrimaryKey || pairing.Target.Dialect.DialectCaps.AnonymousPrimaryKey))
             //{
             //    // do nothing
             //}
             //else
             //{
             //    return false;
             //}
         }
     }
     if (csrc.GetType() != cdst.GetType()) return false;
     if (csrc is ColumnsConstraintInfo)
     {
         TableInfo tsrc = pairing.Source.FindTable(csrc.OwnerTable.FullName);
         TableInfo tdst = pairing.Target.FindTable(cdst.OwnerTable.FullName);
         if (!EqualsColumnRefs(tsrc, tdst, ((ColumnsConstraintInfo)csrc).Columns, ((ColumnsConstraintInfo)cdst).Columns)) return false;
         //if (!((ColumnsConstraint)csrc).Columns.EqualSequence(((ColumnsConstraint)cdst).Columns)) return false;
         if (csrc is ForeignKeyInfo)
         {
             var fsrc = (ForeignKeyInfo)csrc;
             var fdst = (ForeignKeyInfo)cdst;
             if (!EqualFullNames(fsrc.RefTableFullName, fdst.RefTableFullName, options)) return false;
             TableInfo psrc = pairing.Source.FindTable(fsrc.RefTableFullName);
             TableInfo pdst = pairing.Target.FindTable(fdst.RefTableFullName);
             if (!EqualsColumnRefs(psrc, pdst, fsrc.RefColumns, fdst.RefColumns)) return false;
             if (fsrc.OnDeleteAction != fdst.OnDeleteAction) return false;
             if (fsrc.OnUpdateAction != fdst.OnUpdateAction) return false;
         }
         //if (csrc is IIndex)
         //{
         //    var isrc = (IndexConstraint)csrc;
         //    var idst = (IndexConstraint)cdst;
         //    if (isrc.IsUnique != idst.IsUnique) return false;
         //}
     }
     //if (csrc is CheckConstraint)
     //{
     //    if (((CheckConstraint)csrc).Expression != ((CheckConstraint)cdst).Expression) return false;
     //}
     return true;
 }
Ejemplo n.º 16
0
 public override void RenameConstraint(ConstraintInfo cnt, string newname)
 {
     if (cnt.ObjectType == DatabaseObjectType.Index) PutCmd("^execute sp_rename '%f.%i', '%s', 'INDEX'", cnt.OwnerTable.FullName, cnt.ConstraintName, newname);
     else PutCmd("^execute sp_rename '%f', '%s', 'OBJECT'", new NameWithSchema(cnt.OwnerTable.FullName.Schema, cnt.ConstraintName), newname);
 }
Ejemplo n.º 17
0
 public void DropConstraint(ConstraintInfo constraint, PlanPosition pos = PlanPosition.End)
 {
     ConstraintInfo cnt = Structure.FindOrCreateConstraint(constraint);
     AddOperation(new AlterOperation_DropConstraint { ParentTable = cnt.OwnerTable, OldObject = cnt }, pos);
 }
Ejemplo n.º 18
0
 public ConstraintInfo FindConstraint(ConstraintInfo constraint)
 {
     return(FindTable(constraint.OwnerTable.FullName).Constraints.First(c => c.ConstraintName == constraint.ConstraintName));
 }
Ejemplo n.º 19
0
 public void RenameConstraint(ConstraintInfo constraint, string newname)
 {
     ConstraintInfo cnt = Structure.FindOrCreateConstraint(constraint);
     AddOperation(new AlterOperation_RenameConstraint { OldObject = cnt, ParentTable = cnt.OwnerTable, NewName = new NameWithSchema(newname) });
 }