Exemple #1
0
        private Result ProcessShow(Tokenizer tokenizer, Channel channel)
        {
            Result r = new Result(1);

            string sToken = tokenizer.GetString();

            if (sToken.Equals("TABLES"))
            {
                System.Collections.ArrayList al = channel.Database.Tables;
                r.Label[0] = "TABLE";
                r.Type[0]  = ColumnType.VarChar;
                for (int x = 0; x < al.Count; x++)
                {
                    Table    table     = (Table)al[x];
                    string[] tablename = new string [1];
                    tablename[0] = table.Name;
                    r.Add(tablename);
                }
                channel.Commit();
            }
            else if (sToken.Equals("DATABASES"))
            {
                r.Label[0] = "DATABASE";
                r.Type[0]  = ColumnType.VarChar;

                System.IO.DirectoryInfo di = new
                                             System.IO.DirectoryInfo(System.IO.Directory.GetCurrentDirectory());
                System.IO.FileInfo[] rgFiles = di.GetFiles("*.data");
                foreach (System.IO.FileInfo fi in rgFiles)
                {
                    string[] databaseName = new string [1];
                    databaseName[0] = fi.Name.ToUpper().Replace(".DATA", "");
                    r.Add(databaseName);
                }

                channel.Commit();
            }
            else if (sToken.Equals("ALIAS"))
            {
                r          = new Result(2);
                r.Label[0] = "NAME";
                r.Type[0]  = ColumnType.VarChar;
                r.Label[1] = "LIBRARY";
                r.Type[1]  = ColumnType.VarChar;

                foreach (DictionaryEntry entry in _alias)
                {
                    string[] alias = new string [2];
                    alias[0] = entry.Key.ToString();
                    alias[1] = entry.Value.ToString();
                    r.Add(alias);
                }

                channel.Commit();
            }
            else if (sToken.Equals("PARAMETERS"))
            {
                string alias = tokenizer.GetString().ToUpper();

                if (!_alias.ContainsKey(alias))
                {
                    throw Trace.Error(Trace.UNKNOWN_FUNCTION, alias);
                }

                string fqn = _alias[alias].ToString();

                Function f = new Function(fqn, channel);

                System.Reflection.MethodInfo mi = f.GetMethodInfo(fqn);

                r          = new Result(4);
                r.Label[0] = "ALIAS";
                r.Type[0]  = ColumnType.VarChar;
                r.Label[1] = "PARAMETER";
                r.Type[1]  = ColumnType.VarChar;
                r.Label[2] = "TYPE";
                r.Type[2]  = ColumnType.VarChar;
                r.Label[3] = "POSITION";
                r.Type[3]  = ColumnType.Integer;

                System.Reflection.ParameterInfo[] parms = mi.GetParameters();

                int rt = 0;

                if (mi.ReturnType != null)
                {
                    object[] p = new object[4];
                    p[0] = alias;
                    p[1] = "RETURN_VALUE";
                    p[2] = Column.GetColumnTypeString(Function.GetDataType(mi.ReturnType));
                    p[3] = 0;
                    r.Add(p);
                    rt = 1;
                }

                foreach (System.Reflection.ParameterInfo pi in parms)
                {
                    object[] p = new object[4];
                    p[0] = alias;
                    p[1] = pi.Name;
                    p[2] = Column.GetColumnTypeString(Function.GetDataType(pi.ParameterType));
                    p[3] = (pi.Position + rt);
                    r.Add(p);
                }

                channel.Commit();
            }
            else if (sToken.Equals("COLUMNS"))
            {
                string t        = tokenizer.GetString().ToUpper();
                Table  theTable = null;

                foreach (Table table in channel.Database.Tables)
                {
                    if (table.Name.ToUpper() == t)
                    {
                        theTable = table;
                        break;
                    }
                }

                if (theTable == null)
                {
                    throw Trace.Error(Trace.TABLE_NOT_FOUND, t);
                }

                r          = new Result(7);
                r.Label[0] = "TABLE";
                r.Type[0]  = ColumnType.VarChar;
                r.Label[1] = "COLUMN";
                r.Type[1]  = ColumnType.VarChar;
                r.Label[2] = "NATIVETYPE";
                r.Type[2]  = ColumnType.VarChar;
                r.Label[3] = "DBTYPE";
                r.Type[3]  = ColumnType.Integer;
                r.Label[4] = "POSITION";
                r.Type[4]  = ColumnType.Integer;
                r.Label[5] = "NULLABLE";
                r.Type[5]  = ColumnType.Bit;
                r.Label[6] = "IDENTITY";
                r.Type[6]  = ColumnType.Bit;

                for (int ix = 0; ix < theTable.ColumnCount; ix++)
                {
                    Column   col     = theTable.GetColumn(ix);
                    object[] coldata = new object[7];
                    coldata[0] = theTable.Name;
                    coldata[1] = col.Name;
                    coldata[2] = Column.GetColumnTypeString(col.ColumnType);
                    coldata[3] = Column.GetDbType(col.ColumnType);
                    coldata[4] = ix;
                    coldata[5] = col.IsNullable;
                    coldata[6] = col.IsIdentity;
                    r.Add(coldata);
                }
                channel.Commit();
            }
            else
            {
                throw Trace.Error(Trace.UnexpectedToken, sToken);
            }

            return(r);
        }
Exemple #2
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;
        }