Пример #1
0
        public void SaveConstraints(TableStructure table, DatabaseAnalyser analyser)
        {
            MergeKeys();
            cols.Sort(CompareCols);
            bool refsAdded = false;

            foreach (Key key in keys)
            {
                if (key.IsForTable(table))
                {
                    switch (key.keytype)
                    {
                    case "PRIMARY KEY":
                    {
                        PrimaryKey cnt = new PrimaryKey();
                        cnt.Name = key.keyname;
                        //cnt.Table = new NameWithSchema(key.tblschema, key.tblname);
                        foreach (Col col in GetCols(key))
                        {
                            cnt.Columns.Add(col.getcolref());
                        }
                        if (!analyser.SkipConstraint(cnt))
                        {
                            table._Constraints.Add(cnt);
                        }
                    } break;

                    case "FOREIGN KEY":
                    {
                        ForeignKey cnt = new ForeignKey();
                        FillFk(key, cnt);
                        if (!analyser.SkipConstraint(cnt))
                        {
                            table._Constraints.Add(cnt);
                        }
                    } break;

                    case "UNIQUE":
                    {
                        UniqueConstraint cnt = new UniqueConstraint();
                        cnt.Name = key.keyname;
                        //cnt.Table = new NameWithSchema(key.tblschema, key.tblname);
                        foreach (Col col in GetCols(key))
                        {
                            cnt.Columns.Add(col.getcolref());
                        }
                        if (!analyser.SkipConstraint(cnt))
                        {
                            table._Constraints.Add(cnt);
                        }
                    } break;

                    case "CHECK":
                    {
                        CheckConstraint cnt = new CheckConstraint();
                        cnt.Name = key.keyname;
                        //cnt.Table = new NameWithSchema(key.tblschema, key.tblname);
                        cnt.Expression = key.checkexpr;
                        if (!analyser.SkipConstraint(cnt))
                        {
                            table._Constraints.Add(cnt);
                        }
                    } break;

                    case "INDEX":
                    {
                        IndexConstraint cnt = new IndexConstraint();
                        cnt.Name     = key.keyname;
                        cnt.IsUnique = key.keyisunique ?? false;
                        //cnt.Table = new NameWithSchema(key.tblschema, key.tblname);
                        foreach (Col col in GetCols(key))
                        {
                            cnt.Columns.Add(col.getcolref());
                        }
                        if (!analyser.SkipConstraint(cnt))
                        {
                            table._Constraints.Add(cnt);
                        }
                    } break;
                    }
                }
                if (key.IsForTableDest(table) && key.keytype == "FOREIGN KEY")
                {
                    ForeignKey cnt = new ForeignKey();
                    FillFk(key, cnt);
                    refsAdded = true;
                    table.AddReference(cnt);
                    //table._ReferencedFrom.Add(cnt);
                }
            }

            // deduce keys from columns
            if (keys.Count == 0 && AllowDeduceFromColumns)
            {
                var pks  = new Dictionary <string, PrimaryKey>();
                var fks  = new Dictionary <string, ForeignKey>();
                var uqs  = new Dictionary <string, UniqueConstraint>();
                var idxs = new Dictionary <string, IndexConstraint>();
                foreach (Col col in cols)
                {
                    if (!col.IsForTable(table))
                    {
                        continue;
                    }
                    if (col.keytype == null)
                    {
                        continue;
                    }
                    switch (col.keytype)
                    {
                    case "PRIMARY KEY":
                    {
                        if (!pks.ContainsKey(col.keyname))
                        {
                            pks[col.keyname] = new PrimaryKey();
                            table._Constraints.Add(pks[col.keyname]);
                        }
                        // zde NESMI byt cnt.SetDummyTable
                        //pks[col.keyname].Table = table.FullName;
                        pks[col.keyname].Name = col.keyname;
                        pks[col.keyname].Columns.Add(col.getcolref());
                    } break;

                    case "FOREIGN KEY":
                    {
                        if (!fks.ContainsKey(col.keyname))
                        {
                            fks[col.keyname] = new ForeignKey();
                            table._Constraints.Add(fks[col.keyname]);
                        }
                        // zde NESMI byt cnt.SetDummyTable
                        //fks[col.keyname].Table = table.FullName;
                        fks[col.keyname].Name = col.keyname;
                        fks[col.keyname].Columns.Add(col.getcolref());
                        fks[col.keyname].PrimaryKeyColumns.Add(col.getdstcolref());
                        fks[col.keyname].PrimaryKeyTable = analyser.NewNameWithSchema(col.dsttblschema, col.dsttblname);
                    } break;

                    case "UNIQUE":
                    {
                        if (!uqs.ContainsKey(col.keyname))
                        {
                            uqs[col.keyname] = new UniqueConstraint();
                            table._Constraints.Add(uqs[col.keyname]);
                        }
                        // zde NESMI byt cnt.SetDummyTable
                        //uqs[col.keyname].Table = table.FullName;
                        uqs[col.keyname].Name = col.keyname;
                        uqs[col.keyname].Columns.Add(col.getcolref());
                    } break;

                    case "INDEX":
                    {
                        if (!idxs.ContainsKey(col.keyname))
                        {
                            idxs[col.keyname] = new IndexConstraint();
                            table._Constraints.Add(idxs[col.keyname]);
                        }
                        // zde NESMI byt cnt.SetDummyTable
                        //idxs[col.keyname].Table = table.FullName;
                        idxs[col.keyname].Name = col.keyname;
                        idxs[col.keyname].Columns.Add(col.getcolref());
                        idxs[col.keyname].IsUnique = col.keyisunique;
                    } break;
                    }
                }
            }

            if (!refsAdded)
            {
                Dictionary <string, ForeignKey> refs = new Dictionary <string, ForeignKey>();

                foreach (Col col in cols)
                {
                    if (col.IsForTableDest(table))
                    {
                        if (!refs.ContainsKey(col.keyname))
                        {
                            refs[col.keyname] = new ForeignKey();
                        }
                        ForeignKey cnt = refs[col.keyname];
                        cnt.Name = col.keyname;
                        cnt.SetDummyTable(new NameWithSchema(col.tblschema, col.tblname));
                        cnt.PrimaryKeyTable = new NameWithSchema(col.dsttblschema, col.dsttblname);
                        cnt.Columns.Add(col.getcolref());
                        cnt.PrimaryKeyColumns.Add(col.getdstcolref());
                    }
                }

                foreach (ForeignKey fk in refs.Values)
                {
                    table.AddReference(fk);
                }
            }

            //pkcols.Sort(CompareCols);
            //fkcols.Sort(CompareCols);
            //refcols.Sort(CompareCols);

            //foreach (Pk pk in pks)
            //{
            //    if (pk.tblname != table.Name && pk.tblschema != table.SchemaName) continue;
            //    PrimaryKey cnt = new PrimaryKey();
            //    cnt.Name = pk.pkname;
            //    cnt.Table = new NameWithSchema(pk.tblschema, pk.tblname);
            //    foreach (PkCol col in GetPkCols(pk))
            //    {
            //        cnt.Columns.Add(col.colname);
            //    }
            //    table.Constraints.Add(cnt);
            //}

            //SaveFks(fks, fkcols, table.Constraints, table.FullName, Fk.ExtractTable);
            //SaveFks(fks, fkcols, table.ReferencedFrom, table.FullName, Fk.ExtractDestTable);
        }