Example #1
0
        public Table MoveDefinition(string withoutIndex)
        {
            Table tn = new Table(dDatabase, true, Name, IsCached);

            for (int i = 0; i < ColumnCount; i++)
            {
                tn.AddColumn(GetColumn(i));
            }

            // todo: there should be nothing special with the primary key!
            if (iVisibleColumns < iColumnCount)
            {
                tn.CreatePrimaryKey();
            }
            else
            {
                tn.CreatePrimaryKey(PrimaryIndex.Columns[0]);
            }

            Index idx = null;

            while (true)
            {
                idx = GetNextIndex(idx);

                if (idx == null)
                {
                    break;
                }

                if (withoutIndex != null && idx.Name.Equals(withoutIndex))
                {
                    continue;
                }

                if (idx == PrimaryIndex)
                {
                    continue;
                }

                tn.CreateIndex(idx);
            }

            for (int i = 0; i < iConstraintCount; i++)
            {
                Constraint c = (Constraint)vConstraint[i];

                c.ReplaceTable(this, tn);
            }

            tn.vConstraint = vConstraint;

            return(tn);
        }
Example #2
0
        private void CreateIndex(Channel channel, Table t, int[] col, string name, bool unique)
        {
            channel.Commit();

            if (t.IsEmpty)
            {
                t.CreateIndex(col, name, unique);
            }
            else
            {
                Table tn = t.MoveDefinition(null);

                tn.CreateIndex(col, name, unique);
                tn.MoveData(t);
                DropTable(t.Name);
                LinkTable(tn);
            }
        }
Example #3
0
        /// <summary>
        /// Process ALTER TABLE statements.
        /// 
        /// ALTER TABLE tableName ADD COLUMN columnName columnType;
        /// ALTER TABLE tableName DELETE COLUMN columnName;
        /// </summary>
        /// <remarks>
        /// The only change I've made to Sergio's original code was
        /// changing the insert's to call insertNoCheck to bypass the trigger
        /// mechanism that is a part of hsqldb 1.60 and beyond. - Mark Tutt
        /// </remarks>
        /// <returns></returns>
        public Result ProcessAlter()
        {
            tTokenizer.GetThis ("TABLE");

            string token = tTokenizer.GetString ();

            cChannel.CheckReadWrite ();

            // cChannel.check(token,Access.ALTER); --> Accessul nu-l inca controleaza...
            string tName = token;
            string swap = tName + "SWAP";

            // nimicirea swapului...
            dDatabase.Execute ("DROP TABLE " + swap, cChannel);

            Table initialTable = dDatabase.GetTable (token, cChannel);
            int count = 0;

            token = tTokenizer.GetString ();

            if (token.Equals ("ADD")) {
                token = tTokenizer.GetString ();

                if (token.Equals ("COLUMN")) {
                    Table swapTable = new Table (dDatabase, true, swap,
                                          initialTable.IsCached);

                    // copiem coloanele (fara date) din tabelul initial in swap
                    for (int i = 0; i < initialTable.ColumnCount; i++) {
                        Column aColumn = initialTable.GetColumn (i);

                        swapTable.AddColumn (aColumn);
                    }

                    // end Of copiem coloanele...
                    // aflam daca are PrimaryKey & o cream...
                    string cName = tTokenizer.GetString ();
                    string cType = tTokenizer.GetString ();
                    ColumnType iType = Column.GetColumnType (cType);
                    string sToken = cType;
                    //					int     primarykeycolumn = -1;
                    bool identity = false;
                    int column = initialTable.ColumnCount + 1;

                    // !--
                    // stolen from CREATE TABLE...
                    string sColumn = cName;

                    if (iType == ColumnType.VarChar && dDatabase.IsIgnoreCase) {
                        iType = ColumnType.VarCharIgnoreCase;
                    }

                    sToken = tTokenizer.GetString ();

                    if (iType == ColumnType.DbDouble && sToken.Equals ("PRECISION")) {
                        sToken = tTokenizer.GetString ();
                    }

                    if (sToken.Equals ("(")) {

                        // overread length
                        do {
                            sToken = tTokenizer.GetString ();
                        } while (!sToken.Equals (")"));

                        sToken = tTokenizer.GetString ();
                    }

                    // !--
                    bool nullable = true;

                    if (sToken.Equals ("NULL")) {
                        sToken = tTokenizer.GetString ();
                    } else if (sToken.Equals ("NOT")) {
                        tTokenizer.GetThis ("NULL");

                        nullable = false;
                        sToken = tTokenizer.GetString ();
                    }

                    /*
                     * if(sToken.Equals("IDENTITY")) {
                     * identity=true;
                     * Trace.check(primarykeycolumn==-1,Trace.SECOND_PRIMARY_KEY,sColumn);
                     * sToken=tTokenizer.getstring();
                     * primarykeycolumn=column;
                     * }
                     *
                     * if(sToken.Equals("PRIMARY")) {
                     * tTokenizer.getThis("KEY");
                     * Trace.check(identity || primarykeycolumn==-1,
                     * Trace.SECOND_PRIMARY_KEY,sColumn);
                     * primarykeycolumn=column;
                     * //sToken=tTokenizer.getstring();
                     * }
                     * //end of STOLEN...
                     */
                    swapTable.AddColumn (cName, iType, nullable,
                        identity);    // under construction...

                    if (initialTable.ColumnCount
                        < initialTable.InternalColumnCount) {
                        swapTable.CreatePrimaryKey ();
                    } else {
                        swapTable.CreatePrimaryKey (initialTable.PrimaryIndex.Columns [0]);
                    }

                    // endof PrimaryKey...
                    // sa ne farimam cu indicii... ;-((
                    Index idx = null;

                    while (true) {
                        idx = initialTable.GetNextIndex (idx);

                        if (idx == null) {
                            break;
                        }

                        if (idx == initialTable.PrimaryIndex) {
                            continue;
                        }

                        swapTable.CreateIndex (idx);
                    }

                    // end of Index...
                    cChannel.Commit ();
                    dDatabase.LinkTable (swapTable);

                    Tokenizer tmpTokenizer = new Tokenizer ("SELECT * FROM "
                                             + tName);
                    Parser pp = new Parser (dDatabase, tmpTokenizer, cChannel);
                    string ff = tmpTokenizer.GetString ();

                    if (!initialTable.IsEmpty) {
                        Record n = ((Result)pp.ProcessSelect ()).Root;

                        do {
                            object[] row = swapTable.NewRow;
                            object[] row1 = n.Data;

                            for (int i = 0; i < initialTable.ColumnCount;
                                i++) {
                                row [i] = row1 [i];
                            }

                            swapTable.InsertNoCheck (row, cChannel);

                            n = n.Next;
                        } while (n != null);
                    }

                    dDatabase.Execute ("DROP TABLE " + tName, cChannel);

                    // cream tabelul vechi cu proprietatile celui nou...
                    initialTable = new Table (dDatabase, true, tName,
                        swapTable.IsCached);

                    for (int i = 0; i < swapTable.ColumnCount; i++) {
                        Column aColumn = swapTable.GetColumn (i);

                        initialTable.AddColumn (aColumn);
                    }

                    if (swapTable.ColumnCount
                        < swapTable.InternalColumnCount) {
                        initialTable.CreatePrimaryKey ();
                    } else {
                        initialTable.CreatePrimaryKey (swapTable.PrimaryIndex.Columns [0]);
                    }

                    // endof PrimaryKey...
                    // sa ne farimam cu indicii... ;-((
                    idx = null;

                    while (true) {
                        idx = swapTable.GetNextIndex (idx);

                        if (idx == null) {
                            break;
                        }

                        if (idx == swapTable.PrimaryIndex) {
                            continue;
                        }

                        initialTable.CreateIndex (idx);
                    }

                    // end of Index...
                    cChannel.Commit ();
                    dDatabase.LinkTable (initialTable);

                    // end of cream...
                    // copiem datele din swap in tabel...
                    tmpTokenizer = new Tokenizer ("SELECT * FROM " + swap);
                    pp = new Parser (dDatabase, tmpTokenizer, cChannel);
                    ff = tmpTokenizer.GetString ();

                    if (!swapTable.IsEmpty) {
                        Record n = ((Result)pp.ProcessSelect ()).Root;

                        do {
                            object[] row = initialTable.NewRow;
                            object[] row1 = n.Data;

                            for (int i = 0; i < swapTable.ColumnCount; i++) {
                                row [i] = row1 [i];
                            }

                            initialTable.InsertNoCheck (row, cChannel);

                            n = n.Next;
                        } while (n != null);

                        // end of copiem...
                    }

                    dDatabase.Execute ("DROP TABLE " + swap, cChannel);

                    count = 4;
                } else {
                    throw TracingHelper.Error (TracingHelper.UnexpectedToken, token);
                }
            } else if (token.Equals ("DELETE")) {
                token = tTokenizer.GetString ();

                if (token.Equals ("COLUMN")) {
                    Table swapTable = new Table (dDatabase, true, swap,
                                          initialTable.IsCached);
                    string cName = tTokenizer.GetString ();
                    int undesired = initialTable.GetColumnNumber (cName);

                    for (int i = 0; i < initialTable.ColumnCount; i++) {
                        Column aColumn = initialTable.GetColumn (i);

                        if (i != undesired) {
                            swapTable.AddColumn (aColumn);
                        }
                    }

                    int pKey = -1;

                    // !--
                    if (initialTable.ColumnCount
                        < initialTable.InternalColumnCount) {
                        swapTable.CreatePrimaryKey ();
                    } else {
                        int[] cols = initialTable.PrimaryIndex.Columns;

                        pKey = cols [0];

                        if ((cols [0] > undesired)
                            || (cols [0] + cols.Length < undesired)) {
                            if (undesired
                                < initialTable.PrimaryIndex.Columns [0]) {

                                // reindexarea...
                                for (int i = 0; i < cols.Length; i++) {
                                    cols [i]--;
                                }

                                // endOf reindexarea...
                            }
                            // MT: This initially wouldn't compile, missing the array index on cols[]
                            swapTable.CreatePrimaryKey (cols [0]);
                        } else {
                            swapTable.CreatePrimaryKey ();
                        }
                    }

                    // endof PrimaryKey...
                    // sa ne farimam cu indicii... ;-((
                    Index idx = null;

                    while (true) {
                        idx = initialTable.GetNextIndex (idx);

                        if (idx == null) {
                            break;
                        }

                        if (idx == initialTable.PrimaryIndex) {
                            continue;
                        }

                        bool flag = true;
                        int[] cols = idx.Columns;

                        for (int i = 0; i < cols.Length; i++) {
                            if (cols [i] == undesired) {
                                flag = false;
                            }
                        }

                        if (flag) {
                            Index tIdx;

                            for (int i = 0; i < cols.Length; i++) {
                                if (cols [i] > undesired) {
                                    cols [i]--;
                                }
                            }

                            tIdx = new Index (idx.Name, idx.Columns, idx.ColumnType, idx.IsUnique);

                            swapTable.CreateIndex (tIdx);
                        }
                    }

                    // !--
                    cChannel.Commit ();
                    dDatabase.LinkTable (swapTable);

                    Tokenizer tmpTokenizer = new Tokenizer ("SELECT * FROM "
                                             + tName);
                    Parser pp = new Parser (dDatabase, tmpTokenizer, cChannel);
                    string ff = tmpTokenizer.GetString ();

                    if (!initialTable.IsEmpty) {
                        Record n = ((Result)pp.ProcessSelect ()).Root;

                        do {
                            object[] row = swapTable.NewRow;
                            object[] row1 = n.Data;
                            int j = 0;

                            for (int i = 0; i < initialTable.ColumnCount;
                                i++) {
                                if (i != undesired) {
                                    row [j] = row1 [i];
                                    j++;
                                }
                            }

                            swapTable.InsertNoCheck (row, cChannel);

                            n = n.Next;
                        } while (n != null);
                    }

                    dDatabase.Execute ("DROP TABLE " + tName, cChannel);

                    // cream tabelul vechi cu proprietatile celui nou...
                    initialTable = new Table (dDatabase, true, tName,
                        swapTable.IsCached);

                    for (int i = 0; i < swapTable.ColumnCount; i++) {
                        Column aColumn = swapTable.GetColumn (i);

                        initialTable.AddColumn (aColumn);
                    }

                    // !--
                    if (swapTable.ColumnCount
                        < swapTable.InternalColumnCount) {
                        initialTable.CreatePrimaryKey ();
                    } else {
                        initialTable.CreatePrimaryKey (swapTable.PrimaryIndex.Columns [0]);
                    }

                    // endof PrimaryKey...
                    // sa ne farimam cu indicii... ;-((
                    idx = null;

                    while (true) {
                        idx = swapTable.GetNextIndex (idx);

                        if (idx == null) {
                            break;
                        }

                        if (idx == swapTable.PrimaryIndex) {
                            continue;
                        }

                        initialTable.CreateIndex (idx);
                    }

                    // end of Index...
                    // !--
                    cChannel.Commit ();
                    dDatabase.LinkTable (initialTable);

                    // end of cream...
                    // copiem datele din swap in tabel...
                    tmpTokenizer = new Tokenizer ("SELECT * FROM " + swap);
                    pp = new Parser (dDatabase, tmpTokenizer, cChannel);
                    ff = tmpTokenizer.GetString ();

                    if (!swapTable.IsEmpty) {
                        Record n = ((Result)pp.ProcessSelect ()).Root;

                        do {
                            object[] row = initialTable.NewRow;
                            object[] row1 = n.Data;

                            for (int i = 0; i < swapTable.ColumnCount; i++) {
                                row [i] = row1 [i];
                            }

                            initialTable.InsertNoCheck (row, cChannel);

                            n = n.Next;
                        } while (n != null);

                        // end of copiem...
                    }

                    dDatabase.Execute ("DROP TABLE " + swap, cChannel);

                    count = 3;
                } else {
                    throw TracingHelper.Error (TracingHelper.UnexpectedToken, token);
                }

                count = 3;
            }

            Result r = new Result ();

            r.SetUpdateCount (count);

            return r;
        }
Example #4
0
        public Table MoveDefinition(string withoutIndex)
        {
            Table tn = new Table(dDatabase, true, Name, IsCached);

            for (int i = 0; i < ColumnCount; i++)
            {
                tn.AddColumn(GetColumn(i));
            }

            // todo: there should be nothing special with the primary key!
            if (iVisibleColumns < iColumnCount)
            {
                tn.CreatePrimaryKey();
            }
            else
            {
                tn.CreatePrimaryKey(PrimaryIndex.Columns[0]);
            }

            Index idx = null;

            while (true)
            {
                idx = GetNextIndex(idx);

                if (idx == null)
                {
                    break;
                }

                if (withoutIndex != null && idx.Name.Equals(withoutIndex))
                {
                    continue;
                }

                if (idx == PrimaryIndex)
                {
                    continue;
                }

                tn.CreateIndex(idx);
            }

            for (int i = 0; i < iConstraintCount; i++)
            {
                Constraint c = (Constraint) vConstraint[i];

                c.ReplaceTable(this, tn);
            }

            tn.vConstraint = vConstraint;

            return tn;
        }
Example #5
0
        private void CreateIndex(Channel channel, Table t, int[] col, string name, bool unique)
        {
            channel.Commit();

            if (t.IsEmpty)
            {
                t.CreateIndex(col, name, unique);
            }
            else
            {
                Table tn = t.MoveDefinition(null);

                tn.CreateIndex(col, name, unique);
                tn.MoveData(t);
                DropTable(t.Name);
                LinkTable(tn);
            }
        }