Пример #1
0
        public virtual TableStructure ReadCreateTable()
        {
            SkipKeyword("create");
            SkipTokenIf("temporary");
            SkipTokenIf("temp");
            SkipKeyword("table");
            TableStructure res = new TableStructure();

            res.FullName = ReadFullName();
            SkipSymbol("(");
            bool was = false;

            while (!IsSymbol(")"))
            {
                if (was)
                {
                    SkipKeyword(",");
                }
                Constraint cnt = ReadConstraint();
                if (cnt != null)
                {
                    res._Constraints.Add(cnt);
                }
                else
                {
                    ColumnStructure col = new ColumnStructure();
                    res._Columns.Add(col);
                    ReadColumn(col);
                }
                was = true;
            }
            SkipSymbol(")");
            return(res);
        }
Пример #2
0
 public EditorColumnStructure(TableEditFrame frame, ColumnStructure src, ISqlDialect dialect)
 {
     m_frame = frame;
     if (src.Domain != null)
     {
         Domain = m_frame.Domains[src.Domain];
     }
     ColumnName   = src.ColumnName;
     IsNullable   = src.IsNullable;
     CharacterSet = src.CharacterSet;
     Collation    = src.Collation;
     if (src.DefaultValue != null)
     {
         DefaultValue = src.DefaultValue.GenerateSql(dialect, src.DataType, null);
     }
     m_originalGroupId = src.GroupId;
     if (dialect == null)
     {
         DataType = src.DataType;
     }
     else
     {
         DataType = dialect.GenericTypeToSpecific(src.DataType);
     }
     Source = src;
 }
Пример #3
0
        private ColumnStructure GetColumnWithDummyTable()
        {
            var c = new ColumnStructure(Column);

            c.SetDummyTable(DbObjectName);
            return(c);
        }
Пример #4
0
        public void CreateColumn(ITableStructure table, ColumnStructure newcol)
        {
            TableStructure tbl = Structure.FindOrCreateTable(table.FullName);

            AddOperation(new AlterOperation_CreateColumn {
                ParentTable = tbl, NewObject = new ColumnStructure(newcol)
            });
        }
Пример #5
0
        public void ChangeColumn(IColumnStructure column, IColumnStructure newcolumn)
        {
            ColumnStructure col = Structure.FindOrCreateColumn(column);

            AddOperation(new AlterOperation_ChangeColumn {
                OldObject = col, ParentTable = (TableStructure)col.Table, NewObject = new ColumnStructure(newcolumn)
            });
        }
Пример #6
0
        public void RenameColumn(IColumnStructure column, string name)
        {
            ColumnStructure col = Structure.FindOrCreateColumn(column);

            AddOperation(new AlterOperation_RenameColumn {
                OldObject = col, ParentTable = (TableStructure)col.Table, NewName = new NameWithSchema(name)
            });
        }
Пример #7
0
        public void DropColumn(IColumnStructure column)
        {
            ColumnStructure col = Structure.FindOrCreateColumn(column);

            AddOperation(new AlterOperation_DropColumn {
                ParentTable = (TableStructure)col.Table, OldObject = col
            });
        }
Пример #8
0
        //public NameWithSchema FullName
        //{
        //    get { return new NameWithSchema(SchemaName, Name); }
        //    set { SchemaName = value.Schema; Name = value.Name; }
        //}

        //#region ITableStructure Members

        //IColumnCollection ITableStructure.Columns
        //{
        //    get { return Columns; }
        //}

        //IList<IConstraint> ITableStructure.Constraints
        //{
        //    get { return Constraints.ToTypedList<IConstraint>(); }
        //}

        //IList<IForeignKey> ITableStructure.ReferencedFrom
        //{
        //    get { return ReferencedFrom.ToTypedList<IForeignKey>(); }
        //}

        ////TableStructureMembers ITableStructure.FilledMembers
        ////{
        ////    get { return FilledMembers; }
        ////}

        //#endregion

        public ColumnStructure AddColumn(string name, DbTypeBase datatype)
        {
            ColumnStructure col = new ColumnStructure();

            col.ColumnName = name;
            col.DataType   = datatype;
            _Columns.Add(col);
            return(col);
        }
Пример #9
0
 protected virtual void ProcessTableColumnsTable(TableStructure table, DataTable columns)
 {
     foreach (DataRow row in columns.Rows.SortedByKey <DataRow, int>(row => Int32.Parse(row["ORDINAL_POSITION"].ToString())))
     {
         //NameWithSchema table = NewNameWithSchema(row["TABLE_SCHEMA"].SafeToString(), row["TABLE_NAME"].ToString());
         ColumnStructure col = table.AddColumn(row["COLUMN_NAME"].ToString(), AnalyseType(new DataRowAdapter(row), m_conn, false));
         LoadTableColumn(col, row);
     }
     //table.FilledMembers |= TableStructureMembers.ColumnNames | TableStructureMembers.ColumnTypes;
 }
Пример #10
0
 protected void CopyFromTable(ITableStructure src, bool hardAssign)
 {
     if (hardAssign || ShouldCopy(src, TableStructureMembers.SpecificDetails))
     {
         base.AssignFrom(src);
     }
     if (hardAssign || ShouldCopy(src, TableStructureMembers.ColumnNames))
     {
         var oldcols = new List <ColumnStructure>();
         foreach (ColumnStructure c in _Columns)
         {
             oldcols.Add(c);
         }
         _Columns.Clear();
         foreach (var col in src.Columns)
         {
             ColumnStructure scol = (from c in oldcols where c.ColumnName == col.ColumnName select c).FirstOrDefault();
             if (scol != null)
             {
                 scol.AssignFrom(col);
             }
             else
             {
                 scol = new ColumnStructure(col);
             }
             _Columns.Add(scol);
         }
     }
     if (hardAssign || ShouldCopy(src, TableStructureMembers.ColumnTypes))
     {
         foreach (var col in src.Columns)
         {
             ColumnStructure scol = this.FindColumn(col.ColumnName) as ColumnStructure;
             if (scol != null)
             {
                 scol.AssignFrom(col);
             }
         }
     }
     if (hardAssign || ShouldCopy(src, TableStructureMembers.Constraints))
     {
         foreach (var cnt in src.Constraints)
         {
             Constraint scnt = this.FindConstraint(cnt) as Constraint;
             if (scnt != null)
             {
                 scnt.AssignFrom(scnt);
             }
             else
             {
                 _Constraints.Add(Constraint.CreateCopy(cnt));
             }
         }
     }
 }
Пример #11
0
        public ColumnStructure AddColumn(IColumnStructure source, bool reuseGrouId)
        {
            ColumnStructure col = new ColumnStructure(source);

            if (!reuseGrouId)
            {
                col.GroupId = Guid.NewGuid().ToString();
            }
            _Columns.Add(col);
            return(col);
        }
Пример #12
0
        private TableStructure CreateStructure()
        {
            TableStructure ts = new TableStructure();

            foreach (string colname in m_columnNames)
            {
                ColumnStructure col = new ColumnStructure();
                col.ColumnName = colname;
                col.DataType   = new DbTypeString();
                ts._Columns.Add(col);
            }
            return(ts);
        }
Пример #13
0
        protected virtual void LoadTableColumn(ColumnStructure col, DataRow row)
        {
            col.IsNullable = IsColumnNullable(new DataRowAdapter(row));
            string def = row.ColumnDefault();

            col.DefaultValue = SqlExpression.ParseDefaultValue(GetDefaultValueExpression(def), m_dialect);
            col.Collation    = row.SafeString("COLLATION_NAME");
            col.CharacterSet = row.SafeString("CHARACTER_SET_NAME");
            string dschema = row.SafeString("DOMAIN_SCHEMA"), dname = row.SafeString("DOMAIN_NAME");

            if (dname != null && dname.Trim() != "")
            {
                col.Domain = NewNameWithSchema(dschema, dname);
            }
        }
Пример #14
0
        public void DropColumn(IColumnStructure column)
        {
            ColumnStructure colcopy = new ColumnStructure(column);

            colcopy.SetDummyTable(column.Table.FullName);
            // we must send copy of column, because when dropping column, column.Table will be null
            foreach (AbstractObjectStructure obj in this.GetAllObjects())
            {
                obj.NotifyDropColumn(colcopy);
            }
            foreach (TableStructure tbl in Tables)
            {
                tbl.DropEmptyConstraints();
            }
        }
Пример #15
0
        public static TableStructure SchemaTableToStructure(DataTable schemaTable, ISqlDialect dialect)
        {
            TableStructure res = new TableStructure();
            PrimaryKey     pk  = new PrimaryKey();

            foreach (DataRow row in schemaTable.Rows.SortedByKey <DataRow, int>(row => Int32.Parse(row["ColumnOrdinal"].ToString())))
            {
                if (row.SafeBool("IsHidden", false))
                {
                    continue;
                }

                var col = new ColumnStructure();
                col.ColumnName = row.SafeString("ColumnName");
                col.IsNullable = (bool)row["AllowDBNull"];
                if (dialect != null)
                {
                    col.DataType = dialect.ReaderDataType(row);
                }
                else
                {
                    col.DataType = ReaderDataType(row);
                }

                string schema = row.SafeString("BaseSchemaName");
                string table  = row.SafeString("BaseTableName");
                if (table != null && res.FullName == null)
                {
                    res.FullName = new NameWithSchema(schema, table);
                }
                if (row.SafeBool("IsAutoIncrement", false))
                {
                    col.DataType.SetAutoincrement(true);
                }

                if (row.SafeBool("IsKey", false))
                {
                    pk.Columns.Add(new ColumnReference(col.ColumnName));
                }
                res._Columns.Add(col);
            }
            if (pk.Columns.Count > 0)
            {
                res._Constraints.Add(pk);
            }
            return(res);
        }
Пример #16
0
        private ITableStructure CreateTargetStructure()
        {
            TableStructure table = new TableStructure();

            foreach (DataGridViewRow row in lbtarget.Rows)
            {
                var type = GetExprType(row);
                if (type == null)
                {
                    continue;
                }
                if (type is GenericTransform.ColumnColExprType)
                {
                    string oldcol = row.Cells[2].Value.ToString();
                    if (m_srcformat.Columns.GetIndex(oldcol) < 0)
                    {
                        throw new IncorrectObjectReferenceError("DAE-00185", "s_column", oldcol);
                    }
                    var coldef = new ColumnStructure(m_srcformat.Columns[oldcol]);
                    coldef.ColumnName = row.Cells[0].Value.ToString();
                    table.AddColumn(coldef, true);
                }
                else if (type is GenericTransform.RowNumberColExprType)
                {
                    var col = new ColumnStructure();
                    col.ColumnName = row.Cells[0].Value.ToString();
                    col.DataType   = new DbTypeInt();
                    table._Columns.Add(col);
                    if (table.FindConstraint <IPrimaryKey>() == null && table.FindAutoIncrementColumn() == null)
                    {
                        var pk = new PrimaryKey();
                        pk.Columns.Add(new ColumnReference(col.ColumnName));
                        table._Constraints.Add(pk);
                    }
                }
                else
                {
                    var col = new ColumnStructure();
                    col.ColumnName = row.Cells[0].Value.ToString();
                    col.DataType   = new DbTypeString(250);
                    table._Columns.Add(col);
                }
            }
            return(table);
        }
Пример #17
0
        public override void AddLogicalDependencies(AlterProcessorCaps caps, DbDiffOptions opts, List <AlterOperation> before, List <AlterOperation> after, AlterPlan plan, IDatabaseSource targetDb)
        {
            ParentTable.LoadStructure(TableStructureMembers.ReferencedFrom, targetDb);
            var oldcol = OldObject as ColumnStructure;
            var newcol = NewObject as ColumnStructure;

            List <IForeignKey> recreateFks = new List <IForeignKey>();
            var changeCols = new List <Tuple <IColumnStructure, IColumnStructure> >();

            foreach (ForeignKey fk in ParentTable.GetReferencedFrom())
            {
                for (int i = 0; i < fk.PrimaryKeyColumns.Count; i++)
                {
                    if (fk.PrimaryKeyColumns[i].ColumnName == oldcol.ColumnName)
                    {
                        //plan.RecreateObject(fk, null);
                        TableStructure table = (TableStructure)fk.Table;
                        table.LoadStructure(TableStructureMembers.Columns, targetDb);
                        ColumnStructure othercol = table.Columns[fk.Columns[i].ColumnName] as ColumnStructure;

                        // compare types with ignoring autoincrement flag
                        // HACK: ignore specific attributes
                        var opts2 = opts.Clone();
                        opts2.IgnoreSpecificData = true;
                        DbTypeBase dt1 = othercol.DataType.Clone(), dt2 = newcol.DataType.Clone();
                        dt1.SetAutoincrement(false);
                        dt2.SetAutoincrement(false);
                        if (!DbDiffTool.EqualTypes(dt1, dt2, opts2))
                        {
                            after.Add(new AlterOperation_ChangeColumn
                            {
                                ParentTable = table,
                                OldObject   = othercol,
                                NewObject   = new ColumnStructure(othercol)
                                {
                                    DataType = dt2
                                }
                            });
                        }
                        opts.AlterLogger.Warning(Texts.Get("s_changed_referenced_column$table$column", "table", fk.Table.FullName, "column", othercol.ColumnName));
                    }
                }
            }
        }
Пример #18
0
 protected virtual void ReadColumn(ColumnStructure col)
 {
     col.ColumnName = ReadName();
     col.DataType   = ReadDataType();
     col.IsNullable = true;
     if (SkipMultiIf("not", "null"))
     {
         col.IsNullable = false;
     }
     if (SkipMultiIf("null"))
     {
         col.IsNullable = true;
     }
     if (SkipMultiIf("default"))
     {
         col.DefaultValue = ParseExpression();
     }
     ReadColumnSpecific(col);
 }
Пример #19
0
        protected virtual void ReadCharSetCollate(ColumnStructure col)
        {
            bool any = true;

            while (any)
            {
                any = false;
                if (SkipTokenIf("collate"))
                {
                    col.Collation = SkipToken();
                    any           = true;
                }
                if (SkipMultiIf("character", "set"))
                {
                    col.CharacterSet = SkipToken();
                    any = true;
                }
            }
        }
Пример #20
0
        public ColumnStructure FindOrCreateColumn(IColumnStructure col)
        {
            var t = FindTable(col.Table.FullName);

            if (t == null)
            {
                t = AddTable(new TableStructure {
                    FullName = col.Table.FullName
                }, false);
            }
            ColumnStructure res = t.FindColumn(col.ColumnName) as ColumnStructure;

            if (res == null)
            {
                res         = new ColumnStructure(col);
                res.GroupId = Guid.NewGuid().ToString();
                t._Columns.Add(res);
            }
            return(res);
        }
Пример #21
0
 public override void LoadFromXml(XmlElement xml, ITableStructure source, ITableStructure target)
 {
     m_source = source;
     m_target = target;
     if (m_target == null)
     {
         var t = new TableStructure();
         foreach (XmlElement e in xml.SelectNodes("Column"))
         {
             int colindex = m_source.Columns.GetIndex(e.GetAttribute("src"));
             m_dstIndexes.Add(colindex);
             var newcol = new ColumnStructure(m_source.Columns[colindex]);
             newcol.ColumnName = e.GetAttribute("dst");
             t.AddColumn(newcol, true);
         }
         m_target = t;
     }
     else
     {
         var t = new TableStructure();
         foreach (XmlElement e in xml.SelectNodes("Column"))
         {
             string colname    = e.GetAttribute("src");
             string dstcolname = e.GetAttribute("dst");
             int    pos        = m_source.Columns.GetIndex(colname);
             if (pos < 0)
             {
                 throw new InternalError(String.Format("DAE-00025 Error transforming column {0}, column not found in source table", colname));
             }
             IColumnStructure dstcol = m_target.Columns.FirstOrDefault(col => col.ColumnName == dstcolname);
             if (dstcol == null)
             {
                 throw new InternalError(String.Format("DAE-00026 Error transforming column {0}, column not found in destination table", dstcolname));
             }
             m_dstIndexes.Add(pos);
             t.AddColumn(dstcol, true);
         }
         m_target = t;
     }
 }
Пример #22
0
        public static void AlterTable(AlterPlan plan, ITableStructure oldTable, ITableStructure newTable, DbDiffOptions opts, DbObjectPairing pairing)
        {
            //plan.BeginFixedOrder();
            if (oldTable == null)
            {
                throw new ArgumentNullException("oldTable", "DAE-00240 oldTable is null");
            }
            if (newTable == null)
            {
                throw new ArgumentNullException("newTable", "DAE-00241 newTable is null");
            }

            //bool processed;
            //proc.AlterTable(oldTable, newTable, out processed);
            //if (processed) return;

            InMemoryTableOperation dataOps = null;

            if (oldTable.FixedData != null)
            {
                dataOps = new InMemoryTableOperation(oldTable.FixedData.Structure);
            }

            NameWithSchema newTableName = GenerateNewName(oldTable.FullName, newTable.FullName, opts);

            bool permuteColumns = false;
            bool insertColumns  = false;
            //bool renameColumns = false;

            List <int> columnMap     = new List <int>();
            List <int> constraintMap = new List <int>();

            foreach (var col in newTable.Columns)
            {
                columnMap.Add(oldTable.Columns.IndexOfIf(c => c.GroupId == col.GroupId));
            }
            foreach (var cnt in newTable.Constraints)
            {
                int cindex = oldTable.Constraints.IndexOfIf(c => c.GroupId == cnt.GroupId);
                if (cindex < 0 && cnt is IPrimaryKey)
                {
                    // primary keys for one table are equal
                    cindex = oldTable.Constraints.IndexOfIf(c => c is IPrimaryKey);
                }
                constraintMap.Add(cindex);
            }

            if (!opts.IgnoreColumnOrder)
            {
                // count alter requests
                int lastcol = -1;
                foreach (int col in columnMap)
                {
                    if (col < 0)
                    {
                        continue;
                    }
                    if (col < lastcol)
                    {
                        permuteColumns = true;
                    }
                    lastcol = col;
                }

                bool wasins = false;
                foreach (int col in columnMap)
                {
                    if (col < 0)
                    {
                        wasins = true;
                    }
                    if (col >= 0 && wasins)
                    {
                        insertColumns = true;
                    }
                }
            }

            int index;

            // drop constraints
            index = 0;

            foreach (IConstraint cnt in oldTable.Constraints)
            {
                if (constraintMap.IndexOf(index) < 0)
                {
                    plan.DropConstraint(cnt);
                }
                index++;
            }

            // drop columns
            index = 0;
            foreach (IColumnStructure col in oldTable.Columns)
            {
                if (columnMap.IndexOf(index) < 0)
                {
                    plan.DropColumn(col);
                    if (dataOps != null)
                    {
                        dataOps.DropColumn(col.ColumnName);
                    }
                }
                index++;
            }

            if (!DbDiffTool.EqualFullNames(oldTable.FullName, newTable.FullName, opts))
            {
                plan.RenameTable(oldTable, newTable.FullName);
            }

            // create columns
            index = 0;
            foreach (IColumnStructure col in newTable.Columns)
            {
                if (columnMap[index] < 0)
                {
                    ColumnStructure newcol = new ColumnStructure(col);
                    plan.CreateColumn(oldTable, newcol);
                    if (dataOps != null)
                    {
                        dataOps.CreateColumn(newcol);
                    }
                }
                index++;
            }

            // change columns
            index = 0;
            foreach (IColumnStructure col in newTable.Columns)
            {
                if (columnMap[index] >= 0)
                {
                    IColumnStructure src = oldTable.Columns[columnMap[index]];
                    if (!DbDiffTool.EqualsColumns(src, col, true, opts, pairing))
                    {
                        using (var ctx = new DbDiffChangeLoggerContext(opts, NopLogger.Instance, DbDiffOptsLogger.DiffLogger))
                        {
                            if (DbDiffTool.EqualsColumns(src, col, false, opts, pairing))
                            {
                                plan.RenameColumn(src, col.ColumnName);
                            }
                            else
                            {
                                plan.ChangeColumn(src, col);
                            }
                            if (dataOps != null && src.ColumnName != col.ColumnName)
                            {
                                dataOps.RenameColumn(src.ColumnName, col.ColumnName);
                            }
                        }
                    }
                }
                index++;
            }

            // create fixed data script
            var script = AlterFixedData(oldTable.FixedData, newTable.FixedData, dataOps, opts);

            if (script != null)
            {
                plan.UpdateData(oldTable.FullName, script);
            }

            // change constraints
            index = 0;
            foreach (IConstraint cnt in newTable.Constraints)
            {
                if (constraintMap[index] >= 0)
                {
                    IConstraint src = oldTable.Constraints[constraintMap[index]];
                    if (DbDiffTool.EqualsConstraints(src, cnt, opts, false, pairing) && src.Name != cnt.Name)
                    {
                        if (cnt is IPrimaryKey && (pairing.Source.Dialect.DialectCaps.AnonymousPrimaryKey || pairing.Target.Dialect.DialectCaps.AnonymousPrimaryKey))
                        {
                            // do nothing
                        }
                        else
                        {
                            plan.RenameConstraint(src, cnt.Name);
                        }
                    }
                    else
                    {
                        if (!DbDiffTool.EqualsConstraints(src, cnt, opts, true, pairing))
                        {
                            plan.ChangeConstraint(src, cnt);
                        }
                    }
                }
                index++;
            }

            // create constraints
            index = 0;
            foreach (IConstraint cnt in newTable.Constraints)
            {
                if (constraintMap[index] < 0)
                {
                    plan.CreateConstraint(oldTable, cnt);
                }
                index++;;
            }

            if (permuteColumns || insertColumns)
            {
                plan.ReorderColumns(oldTable, new List <string>((from c in newTable.Columns select c.ColumnName)));
            }

            var alteredOptions = GetTableAlteredOptions(oldTable, newTable, opts);

            if (alteredOptions.Count > 0)
            {
                plan.ChangeTableOptions(oldTable, alteredOptions);
            }
            //plan.EndFixedOrder();
        }
Пример #23
0
        public static void LoadQueue(Stream fr, IDataQueue queue)
        {
            try
            {
                BinaryReader br  = new BinaryReader(fr);
                int          sgn = br.ReadInt32();
                if (sgn != SIGNATURE)
                {
                    throw new InternalError("DAE-00021 Bad BED file");
                }
                int ver = br.ReadInt32();
                if (ver != 1 && ver != 2)
                {
                    throw new InternalError("DAE-00022 Bad BED file");
                }
                int            colcnt = br.Read7BitEncodedInt();
                TableStructure ts     = new TableStructure();
                PrimaryKey     pk     = new PrimaryKey();
                for (int i = 0; i < colcnt; i++)
                {
                    ColumnStructure col = new ColumnStructure();
                    col.ColumnName = br.ReadString();
                    if (ver >= 2)
                    {
                        var type = (TypeStorage)br.ReadByte();
                        col.DataType = type.GetDatAdminType();
                        var flags = (ColFlags)br.ReadByte();
                        if ((flags & ColFlags.ISPK) != 0)
                        {
                            pk.Columns.Add(new ColumnReference(col.ColumnName));
                        }
                    }
                    ts._Columns.Add(col);
                }
                if (pk.Columns.Count > 0)
                {
                    ts._Constraints.Add(pk);
                }

                for (; ;)
                {
                    int len = br.Read7BitEncodedInt();
                    if (len < 0)
                    {
                        break;
                    }
                    var rec = LoadRecord(br, ts);
                    queue.PutRecord(rec);
                }

                queue.PutEof();
            }
            catch (Exception e)
            {
                Errors.Report(e);
                queue.PutError(e);
            }
            finally
            {
                queue.CloseWriting();
            }
        }
Пример #24
0
 protected virtual void ReadColumnSpecific(ColumnStructure col)
 {
     SkipToOneOf(",", ")");
 }