Пример #1
0
        public static Table NewLookupTable(Database database)
        {
            Table table;

            QNameManager.QName subqueryTableName = database.NameManager.GetSubqueryTableName();
            ColumnSchema       column            = new ColumnSchema(QNameManager.GetAutoColumnName(0), SqlType.SqlInteger, false, true, null);
            TableDerived       derived1          = new TableDerived(database, subqueryTableName, 2, null, null);

            derived1.AddColumn(column);
            TableDerived derived2 = table = derived1;

            QNameManager.QName name = derived2.GetName();
            int[] columns           = new int[1];
            table.CreatePrimaryKeyConstraint(name, columns, true);
            return(derived2);
        }
Пример #2
0
 public void CheckAddColumn(ColumnSchema col)
 {
     this.CheckModifyTable();
     if (this._table.FindColumn(col.GetName().Name) != -1)
     {
         throw Error.GetError(0x1580);
     }
     if (col.IsPrimaryKey() && this._table.HasPrimaryKey())
     {
         throw Error.GetError(0x159a);
     }
     if (col.IsIdentity() && this._table.HasIdentityColumn())
     {
         throw Error.GetError(0x1595);
     }
     if (((!this._table.IsEmpty(this._session) && !col.HasDefault) && (!col.IsNullable() || col.IsPrimaryKey())) && !col.IsIdentity())
     {
         throw Error.GetError(0x159b);
     }
 }
Пример #3
0
 public void SetType(ColumnSchema other)
 {
     base.Nullability = other.Nullability;
     base.DataType    = other.DataType;
 }
Пример #4
0
        public void RetypeColumn(ColumnSchema oldCol, ColumnSchema newCol)
        {
            bool flag     = true;
            int  typeCode = oldCol.GetDataType().TypeCode;
            int  num2     = newCol.GetDataType().TypeCode;

            this.CheckModifyTable();
            if (!this._table.IsEmpty(this._session) && (typeCode != num2))
            {
                flag = newCol.GetDataType().CanConvertFrom(oldCol.GetDataType());
                switch (typeCode)
                {
                case 0x457:
                case 0x7d0:
                    flag = false;
                    break;
                }
            }
            if (!flag)
            {
                throw Error.GetError(0x15b9);
            }
            int columnIndex = this._table.GetColumnIndex(oldCol.GetName().Name);

            if ((newCol.IsIdentity() && this._table.HasIdentityColumn()) && (this._table.IdentityColumn != columnIndex))
            {
                throw Error.GetError(0x1595);
            }
            if (this._table.GetPrimaryKey().Length > 1)
            {
                newCol.SetPrimaryKey(oldCol.IsPrimaryKey());
                if (ArrayUtil.Find(this._table.GetPrimaryKey(), columnIndex) == -1)
                {
                }
            }
            else if (this._table.HasPrimaryKey())
            {
                if (!oldCol.IsPrimaryKey())
                {
                    if (newCol.IsPrimaryKey())
                    {
                        throw Error.GetError(0x159c);
                    }
                }
                else
                {
                    newCol.SetPrimaryKey(true);
                }
            }
            else if (newCol.IsPrimaryKey())
            {
                throw Error.GetError(0x159a);
            }
            if (((((num2 == typeCode) & (oldCol.IsNullable() == newCol.IsNullable())) & (oldCol.GetDataType().Scale == newCol.GetDataType().Scale)) & (oldCol.IsIdentity() == newCol.IsIdentity())) & ((oldCol.GetDataType().Precision == newCol.GetDataType().Precision) || ((oldCol.GetDataType().Precision < newCol.GetDataType().Precision) && ((typeCode == 12) || (typeCode == 0x3d)))))
            {
                oldCol.SetType(newCol);
                oldCol.SetDefaultExpression(newCol.GetDefaultExpression());
                if (newCol.IsIdentity())
                {
                    oldCol.SetIdentity(newCol.GetIdentitySequence());
                }
                this._table.SetColumnTypeVars(columnIndex);
                this._table.ResetDefaultsFlag();
            }
            else
            {
                this._database.schemaManager.CheckColumnIsReferenced(this._table.GetName(), this._table.GetColumn(columnIndex).GetName());
                this._table.CheckColumnInCheckConstraint(columnIndex);
                this._table.CheckColumnInFkConstraint(columnIndex);
                this.CheckConvertColDataType(oldCol, newCol);
                this.RetypeColumn(newCol, columnIndex);
            }
        }
Пример #5
0
        public void AddColumn(ColumnSchema column, int colIndex, List <Constraint> constraints)
        {
            Table      table = this._table;
            Constraint c     = null;
            bool       flag  = false;
            bool       flag2 = false;
            bool       flag3 = false;

            this.CheckAddColumn(column);
            Constraint constraint = constraints[0];

            if (constraint.GetConstraintType() == 4)
            {
                if (column.GetDataType().IsLobType())
                {
                    throw Error.GetError(0x159e);
                }
                constraint.Core.MainCols = new int[] { colIndex };
                this._database.schemaManager.CheckSchemaObjectNotExists(constraint.GetName());
                if (this._table.HasPrimaryKey())
                {
                    throw Error.GetError(0x159a);
                }
                flag2 = true;
            }
            else
            {
                constraint = null;
            }
            this._table = this._table.MoveDefinition(this._session, this._table.TableType, column, constraint, null, colIndex, 1, this.EmptySetQName, this.EmptySetQName);
            for (int i = 1; i < constraints.Count; i++)
            {
                constraint = constraints[i];
                switch (constraint.ConstType)
                {
                case 0:
                    if (flag)
                    {
                        throw Error.GetError(0x1598);
                    }
                    break;

                case 1:
                {
                    continue;
                }

                case 2:
                    if (flag2)
                    {
                        throw Error.GetError(0x1592);
                    }
                    goto Label_036B;

                case 3:
                    if (flag3)
                    {
                        throw Error.GetError(0x1598);
                    }
                    goto Label_044C;

                default:
                {
                    continue;
                }
                }
                flag = true;
                constraint.Core.RefCols  = new int[] { colIndex };
                constraint.Core.RefTable = this._table;
                bool flag1 = table.GetName() == constraint.Core.MainTableName;
                if (flag1)
                {
                    constraint.Core.MainTable = this._table;
                }
                else
                {
                    constraint.Core.MainTable = this._database.schemaManager.GetTable(this._session, constraint.Core.MainTableName.Name, constraint.Core.MainTableName.schema.Name);
                }
                constraint.SetColumnsIndexes(this._table);
                this.CheckCreateForeignKey(constraint);
                Constraint uniqueConstraintForColumns = constraint.Core.MainTable.GetUniqueConstraintForColumns(constraint.Core.MainCols, constraint.Core.RefCols);
                bool       forward    = constraint.Core.MainTable.GetSchemaName() != this._table.GetSchemaName();
                int        tableIndex = this._database.schemaManager.GetTableIndex(table);
                if (!flag1 && (tableIndex < this._database.schemaManager.GetTableIndex(constraint.Core.MainTable)))
                {
                    forward = true;
                }
                QNameManager.QName name = this._database.NameManager.NewAutoName("IDX", constraint.GetName().Name, this._table.GetSchemaName(), this._table.GetName(), 20);
                Index index             = this._table.CreateAndAddIndexStructure(this._session, name, constraint.GetRefColumns(), null, null, false, true, forward);
                constraint.Core.RefName    = constraint.GetName();
                constraint.Core.UniqueName = uniqueConstraintForColumns.GetName();
                constraint.Core.MainName   = this._database.NameManager.NewAutoName("REF", constraint.Core.RefName.Name, this._table.GetSchemaName(), this._table.GetName(), 20);
                constraint.Core.MainIndex  = uniqueConstraintForColumns.GetMainIndex();
                constraint.Core.RefIndex   = index;
                constraint.IsForward       = forward;
                this._table.AddConstraint(constraint);
                c = new Constraint(constraint.Core.MainName, constraint);
                continue;
Label_036B:
                if (column.GetDataType().IsLobType())
                {
                    throw Error.GetError(0x159e);
                }
                flag2 = true;
                constraint.Core.MainCols = new int[] { colIndex };
                this._database.schemaManager.CheckSchemaObjectNotExists(constraint.GetName());
                QNameManager.QName name2 = this._database.NameManager.NewAutoName("IDX", constraint.GetName().Name, this._table.GetSchemaName(), this._table.GetName(), 20);
                Index index2             = this._table.CreateAndAddIndexStructure(this._session, name2, constraint.GetMainColumns(), null, null, true, true, false);
                constraint.Core.MainTable = this._table;
                constraint.Core.MainIndex = index2;
                this._table.AddConstraint(constraint);
                continue;
Label_044C:
                flag3 = true;
                constraint.PrepareCheckConstraint(this._session, this._table, false);
                this._table.AddConstraint(constraint);
                if (constraint.IsNotNull())
                {
                    column.SetNullable(false);
                    this._table.SetColumnTypeVars(colIndex);
                }
            }
            column.Compile(this._session, this._table);
            this.MoveData(table, this._table, colIndex, 1);
            if (c != null)
            {
                c.GetMain().AddConstraint(c);
            }
            this.RegisterConstraintNames(constraints);
            this.SetNewTableInSchema(this._table);
            this.UpdateConstraints(this._table, this.EmptySetQName);
            this._database.schemaManager.AddSchemaObject(column);
            this._database.schemaManager.RecompileDependentObjects(this._table);
            this._table.Compile(this._session, null);
        }
Пример #6
0
        public void DropColumn(int colIndex, bool cascade)
        {
            OrderedHashSet <QNameManager.QName> dropConstraintSet     = new OrderedHashSet <QNameManager.QName>();
            OrderedHashSet <Constraint>         dependentConstraints  = this._table.GetDependentConstraints(colIndex);
            OrderedHashSet <Constraint>         containingConstraints = this._table.GetContainingConstraints(colIndex);
            OrderedHashSet <QNameManager.QName> containingIndexNames  = this._table.GetContainingIndexNames(colIndex);
            ColumnSchema column = this._table.GetColumn(colIndex);

            QNameManager.QName name = column.GetName();
            OrderedHashSet <QNameManager.QName> referencingObjectNames = this._database.schemaManager.GetReferencingObjectNames(this._table.GetName(), name);

            this.CheckModifyTable();
            if (!cascade)
            {
                if (!containingConstraints.IsEmpty())
                {
                    QNameManager.QName name2 = containingConstraints.Get(0).GetName();
                    throw Error.GetError(0x15a0, name2.GetSchemaQualifiedStatementName());
                }
                if (!referencingObjectNames.IsEmpty())
                {
                    for (int j = 0; j < referencingObjectNames.Size(); j++)
                    {
                        QNameManager.QName name3 = referencingObjectNames.Get(j);
                        if (name3 != name)
                        {
                            for (int k = 0; k < dependentConstraints.Size(); k++)
                            {
                                if (dependentConstraints.Get(k).GetName() == name3)
                                {
                                    continue;
                                }
                            }
                            throw Error.GetError(0x15a0, name3.GetSchemaQualifiedStatementName());
                        }
                    }
                }
            }
            dependentConstraints.AddAll(containingConstraints);
            containingConstraints.Clear();
            OrderedHashSet <Table> tableSet = new OrderedHashSet <Table>();

            for (int i = 0; i < dependentConstraints.Size(); i++)
            {
                Constraint constraint = dependentConstraints.Get(i);
                if (constraint.ConstType == 0)
                {
                    tableSet.Add(constraint.GetMain());
                    dropConstraintSet.Add(constraint.GetMainName());
                    dropConstraintSet.Add(constraint.GetRefName());
                    containingIndexNames.Add(constraint.GetRefIndex().GetName());
                }
                if (constraint.ConstType == 1)
                {
                    tableSet.Add(constraint.GetRef());
                    dropConstraintSet.Add(constraint.GetMainName());
                    dropConstraintSet.Add(constraint.GetRefName());
                    containingIndexNames.Add(constraint.GetRefIndex().GetName());
                }
                dropConstraintSet.Add(constraint.GetName());
            }
            tableSet = this.MakeNewTables(tableSet, dropConstraintSet, containingIndexNames);
            Table newTable = this._table.MoveDefinition(this._session, this._table.TableType, null, null, null, colIndex, -1, dropConstraintSet, containingIndexNames);

            this.MoveData(this._table, newTable, colIndex, -1);
            this._database.schemaManager.RemoveSchemaObjects(referencingObjectNames);
            this._database.schemaManager.RemoveSchemaObjects(dropConstraintSet);
            this._database.schemaManager.RemoveSchemaObject(name);
            this.SetNewTableInSchema(newTable);
            this.SetNewTablesInSchema(tableSet);
            this.UpdateConstraints(newTable, this.EmptySetQName);
            this.UpdateConstraints(tableSet, dropConstraintSet);
            this._database.schemaManager.RecompileDependentObjects(tableSet);
            this._database.schemaManager.RecompileDependentObjects(newTable);
            newTable.Compile(this._session, null);
            if (column.GetDataType().IsLobType())
            {
                IRowIterator rowIterator = this._table.GetRowIterator(this._session);
                while (rowIterator.HasNext())
                {
                    object[] rowData = rowIterator.GetNextRow().RowData;
                    if (rowData[colIndex] != null)
                    {
                        this._session.sessionData.AdjustLobUsageCount(rowData[colIndex], -1);
                    }
                }
            }
            this._table = newTable;
        }
Пример #7
0
 public void CheckCreateForeignKey(Constraint c)
 {
     if ((((c.Core.UpdateAction == 4) || (c.Core.UpdateAction == 2)) || ((c.Core.UpdateAction == 0) || (c.Core.DeleteAction == 4))) || (c.Core.DeleteAction == 2))
     {
         for (int i = 0; i < c.Core.RefCols.Length; i++)
         {
             ColumnSchema column = this._table.GetColumn(c.Core.RefCols[i]);
             if (column.IsGenerated())
             {
                 throw Error.GetError(0x1594, column.GetNameString());
             }
         }
     }
     if ((c.Core.MainName == this._table.GetName()) && ArrayUtil.HaveCommonElement(c.Core.RefCols, c.Core.MainCols))
     {
         throw Error.GetError(0x1597);
     }
     if ((c.Core.UpdateAction == 4) || (c.Core.DeleteAction == 4))
     {
         for (int i = 0; i < c.Core.RefCols.Length; i++)
         {
             ColumnSchema column = this._table.GetColumn(c.Core.RefCols[i]);
             if (column.GetDefaultExpression() == null)
             {
                 string statementName = column.GetName().StatementName;
                 throw Error.GetError(0x1591, statementName);
             }
         }
     }
     if (((c.Core.UpdateAction == 2) || (c.Core.DeleteAction == 2)) && !this._session.IsProcessingScript())
     {
         for (int i = 0; i < c.Core.RefCols.Length; i++)
         {
             ColumnSchema column = this._table.GetColumn(c.Core.RefCols[i]);
             if (!column.IsNullable())
             {
                 string statementName = column.GetName().StatementName;
                 throw Error.GetError(0x1590, statementName);
             }
         }
     }
     this._database.schemaManager.CheckSchemaObjectNotExists(c.GetName());
     if (this._table.GetConstraint(c.GetName().Name) != null)
     {
         throw Error.GetError(0x1580, c.GetName().StatementName);
     }
     if (this._table.GetFkConstraintForColumns(c.Core.MainTable, c.Core.MainCols, c.Core.RefCols) != null)
     {
         throw Error.GetError(0x1598, c.GetName().StatementName);
     }
     if (c.Core.MainTable.IsTemp() != this._table.IsTemp())
     {
         throw Error.GetError(0x1594, c.GetName().StatementName);
     }
     if (c.Core.MainTable.GetUniqueConstraintForColumns(c.Core.MainCols, c.Core.RefCols) == null)
     {
         throw Error.GetError(0x1599, c.GetMain().GetName().StatementName);
     }
     c.Core.MainTable.CheckColumnsMatch(c.Core.MainCols, this._table, c.Core.RefCols);
     bool[] columnCheckList = c.Core.MainTable.GetColumnCheckList(c.Core.MainCols);
     this._session.GetGrantee().CheckReferences(c.Core.MainTable, columnCheckList);
 }