Пример #1
0
        private void ProcessCreateTable(Tokenizer tokenizer, Channel channel, bool cached)
        {
            Table  t;
            string sToken = tokenizer.GetName();

            if (cached && _log != null)
            {
                t = new Table(this, true, sToken, true);
            }
            else
            {
                t = new Table(this, true, sToken, false);
            }

            tokenizer.GetThis("(");

            int     primarykeycolumn = -1;
            int     column = 0;
            bool constraint = false;

            while (true)
            {
                bool identity = false;

                sToken = tokenizer.GetString();

                if (sToken.Equals("CONSTRAINT") || sToken.Equals("PRIMARY")
                    || sToken.Equals("FOREIGN") || sToken.Equals("UNIQUE"))
                {
                    tokenizer.Back();

                    constraint = true;

                    break;
                }

                string sColumn = sToken;
                ColumnType iType = Column.GetColumnType(tokenizer.GetString());

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

                sToken = tokenizer.GetString();

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

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

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

                    sToken = tokenizer.GetString();
                }

                bool nullable = true;

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

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

                if (sToken.Equals("IDENTITY"))
                {
                    identity = true;

                    TracingHelper.Check(primarykeycolumn == -1, TracingHelper.SECOND_PRIMARY_KEY,
                        sColumn);

                    sToken = tokenizer.GetString();
                    primarykeycolumn = column;
                }

                if (sToken.Equals("PRIMARY"))
                {
                    tokenizer.GetThis("KEY");
                    TracingHelper.Check(identity || primarykeycolumn == -1,
                        TracingHelper.SECOND_PRIMARY_KEY, sColumn);

                    primarykeycolumn = column;
                    sToken = tokenizer.GetString();
                }

                t.AddColumn(sColumn, iType, nullable, identity);

                if (sToken.Equals(")"))
                {
                    break;
                }

                if (!sToken.Equals(","))
                {
                    throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
                }

                column++;
            }

            if (primarykeycolumn != -1)
            {
                t.CreatePrimaryKey(primarykeycolumn);
            }
            else
            {
                t.CreatePrimaryKey();
            }

            if (constraint)
            {
                int i = 0;

                while (true)
                {
                    sToken = tokenizer.GetString();

                    string name = "SYSTEM_CONSTRAINT" + i;

                    i++;

                    if (sToken.Equals("CONSTRAINT"))
                    {
                        name = tokenizer.GetString();
                        sToken = tokenizer.GetString();
                    }

                    if (sToken.Equals("PRIMARY"))
                    {
                        tokenizer.GetThis("KEY");
                        AddUniqueConstraintOn(tokenizer, channel, name, t);
                    }
                    else if (sToken.Equals("UNIQUE"))
                    {
                        AddUniqueConstraintOn(tokenizer, channel, name, t);
                    }
                    else if (sToken.Equals("FOREIGN"))
                    {
                        tokenizer.GetThis("KEY");
                        AddForeignKeyOn(tokenizer, channel, name, t);
                    }

                    sToken = tokenizer.GetString();

                    if (sToken.Equals(")"))
                    {
                        break;
                    }

                    if (!sToken.Equals(","))
                    {
                        throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
                    }
                }
            }

            channel.Commit();
            LinkTable(t);
        }
Пример #2
0
        private Result ProcessConnect(Tokenizer c,
            Channel channel)
        {
            c.GetThis("USER");

            string username = c.GetStringToken();

            c.GetThis("PASSWORD");

            string password = c.GetStringToken();
            User   user = _access.GetUser(username, password);

            channel.Commit();
            channel.SetUser(user);

            return new Result();
        }
Пример #3
0
        private Result ProcessCreate(Tokenizer tokenizer, Channel channel)
        {
            channel.CheckReadWrite();
            channel.CheckAdmin();

            string sToken = tokenizer.GetString();

            switch(sToken)
            {
                case "TABLE":
                    ProcessCreateTable(tokenizer, channel, true);
                    break;
                case "MEMORY":
                    tokenizer.GetThis("TABLE");
                    ProcessCreateTable(tokenizer, channel, false);
                    break;
                case "CACHED":
                    tokenizer.GetThis("TABLE");
                    ProcessCreateTable(tokenizer, channel, true);
                    break;
                case "USER":
                {
                    string u = tokenizer.GetStringToken();

                    tokenizer.GetThis("PASSWORD");

                    string  p = tokenizer.GetStringToken();
                    bool admin;

                    if (tokenizer.GetString().Equals("ADMIN"))
                    {
                        admin = true;
                    }
                    else
                    {
                        admin = false;
                    }

                    _access.CreateUser(u, p, admin);
                }
                    break;
                case "ALIAS":
                {
                    string name = tokenizer.GetString();

                    sToken = tokenizer.GetString();

                    TracingHelper.Check(sToken.Equals("FOR"), TracingHelper.UnexpectedToken, sToken);

                    sToken = tokenizer.GetString();

                    _alias[name] = sToken;
                }
                    break;
                default:
                {
                    bool unique = false;

                    if (sToken.Equals("UNIQUE"))
                    {
                        unique = true;
                        sToken = tokenizer.GetString();
                    }

                    if (!sToken.Equals("INDEX"))
                    {
                        throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
                    }

                    string name = tokenizer.GetName();

                    tokenizer.GetThis("ON");

                    Table t = GetTable(tokenizer.GetString(), channel);

                    AddIndexOn(tokenizer, channel, name, t, unique);
                }
                    break;
            }

            return new Result();
        }
Пример #4
0
        private int[] ProcessColumnList(Tokenizer tokenizer, Table table)
        {
            ArrayList v = new ArrayList();

            tokenizer.GetThis("(");

            while (true)
            {
                v.Add(tokenizer.GetString());

                string sToken = tokenizer.GetString();

                if (sToken.Equals(")"))
                {
                    break;
                }

                if (!sToken.Equals(","))
                {
                    throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
                }
            }

            int s = v.Count;
            int[] col = new int[s];

            for (int i = 0; i < s; i++)
            {
                col[i] = table.GetColumnNumber((string) v[i]);
            }

            return col;
        }
Пример #5
0
        private Result ProcessCommit(Tokenizer c,
            Channel channel)
        {
            string sToken = c.GetString();

            if (!sToken.Equals("WORK"))
            {
                c.Back();
            }

            channel.Commit();

            return new Result();
        }
Пример #6
0
 public Parser(Database db, Tokenizer tokenizer, Channel channel)
 {
     dDatabase = db;
     tTokenizer = tokenizer;
     cChannel = channel;
 }
Пример #7
0
        private Result ProcessRollback(Tokenizer tokenizer,	Channel channel)
        {
            string sToken = tokenizer.GetString();

            if (!sToken.Equals("WORK"))
            {
                tokenizer.Back();
            }

            channel.Rollback();

            return new Result();
        }
Пример #8
0
        /// <summary>
        /// Executes an SQL statement and return the results.
        /// </summary>
        /// <param name="statement">The SQL statement to execute.</param>
        /// <param name="channel">The channel to use.</param>
        /// <returns>The Result object.</returns>
        public Result Execute(string statement, Channel channel)
        {
            if (TracingHelper.TraceEnabled)
            {
                TracingHelper.Write(statement);
            }

            Tokenizer c = new Tokenizer(statement);
            Parser    p = new Parser(this, c, channel);
            Result    rResult = new Result();
            string    newStatement = string.Empty;
            int updateCount = 0;

            try
            {
                if (_log != null && _log.cCache != null)
                {
                    _log.cCache.CleanUp();
                }

                if (TracingHelper.AssertEnabled)
                {
                    TracingHelper.Assert(!channel.IsNestedTransaction);
                }

                TracingHelper.Check(channel != null, TracingHelper.ACCESS_IS_DENIED);
                TracingHelper.Check(!_shutDown, TracingHelper.DATABASE_IS_SHUTDOWN);

                while (true)
                {
                    int     begin = c.Position;
                    bool script = false;
                    string  sToken = c.GetString();

                    if( sToken.Equals("") )
                        break;

                    switch(sToken)
                    {
                        case "SELECT":
                            rResult = p.ProcessSelect();
                            break;
                        case "INSERT":
                            rResult = p.ProcessInsert();
                            break;
                        case "UPDATE":
                            rResult = p.ProcessUpdate();
                            break;
                        case "DELETE":
                            rResult = p.ProcessDelete();
                            break;
                        case "ALTER":
                            rResult=p.ProcessAlter();
                            break;
                        case "CREATE":
                            rResult = ProcessCreate(c, channel);
                            script = true;
                            break;
                        case "DROP":
                            rResult = ProcessDrop(c, channel);
                            script = true;
                            break;
                        case "GRANT":
                            rResult = ProcessGrantOrRevoke(c, channel, true);
                            script = true;
                            break;
                        case "REVOKE":
                            rResult = ProcessGrantOrRevoke(c, channel, false);
                            script = true;
                            break;
                        case "CONNECT":
                            rResult = ProcessConnect(c, channel);
                            break;
                        case "DISCONNECT":
                            rResult = ProcessDisconnect(c, channel);
                            break;
                        case "SET":
                            rResult = ProcessSet(c, channel);
                            script = true;
                            break;
                        case "SCRIPT":
                            rResult = ProcessScript(c, channel);
                            break;
                        case "COMMIT":
                            rResult = ProcessCommit(c, channel);
                            script = true;
                            break;
                        case "ROLLBACK":
                            rResult = ProcessRollback(c, channel);
                            script = true;
                            break;
                        case "SHUTDOWN":
                            rResult = ProcessShutdown(c, channel);
                            break;
                        case "CHECKPOINT":
                            rResult = ProcessCheckpoint(channel);
                            break;
                        case "CALL":
                            rResult = p.ProcessCall();
                            break;
                        case "SHOW":
                            rResult = ProcessShow(c,channel);
                            break;
                        case "DECLARE":
                            rResult = p.ProcessDeclare();
                            script = true;
                            break;
                        case ";":
                            continue;
                        default:
                            throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
                    }

                    if( rResult != null && rResult.UpdateCount > updateCount )
                        updateCount = rResult.UpdateCount;

                    if (script && _log != null)
                    {
                        int end = c.Position;

                        _log.Write(channel, c.GetPart(begin, end));
                    }
                }
            }
            catch (Exception e)
            {
                rResult = new Result(TracingHelper.GetMessage(e) + " in statement [" + statement + "]");
            }

            if( rResult != null && rResult.UpdateCount < updateCount )
                rResult.SetUpdateCount( updateCount );

            return rResult;
        }
Пример #9
0
        private void AddForeignKeyOn(Tokenizer tokenizer, Channel channel, string name, Table table)
        {
            int[] col = ProcessColumnList(tokenizer, table);

            tokenizer.GetThis("REFERENCES");

            Table t2 = GetTable(tokenizer.GetString(), channel);
            int[]   col2 = ProcessColumnList(tokenizer, t2);

            if (table.GetIndexForColumns(col) == null)
            {
                CreateIndex(channel, table, col, "SYSTEM_FOREIGN_KEY_" + name, false);
            }

            if (t2.GetIndexForColumns(col2) == null)
            {
                CreateIndex(channel, t2, col2, "SYSTEM_REFERENCE_" + name, false);
            }

            table.AddConstraint(new Constraint(ConstraintType.ForeignKey, t2, table, col2,	col));
            t2.AddConstraint(new Constraint(ConstraintType.Main, t2, table, col2, col));
        }
Пример #10
0
        private Result ProcessShutdown(Tokenizer tokenizer, Channel channel)
        {
            channel.CheckAdmin();

            // don't disconnect system user; need it to save database
            for (int i = 1; i < _channel.Count; i++)
            {
                Channel d = (Channel) _channel[i];

                if (d != null)
                {
                    d.Disconnect();
                }
            }

            _channel.Clear();

            string token = tokenizer.GetString();

            switch(token)
            {
                case "IMMEDIATELY":
                    Close(-1);
                    break;
                case "COMPACT":
                    Close(1);
                    break;
                default:
                    tokenizer.Back();
                    Close(0);
                    break;
            }

            ProcessDisconnect(tokenizer, channel);

            return new Result();
        }
Пример #11
0
        private bool ProcessTrueOrFalse(Tokenizer tokenizer)
        {
            string sToken = tokenizer.GetString();

            switch(sToken)
            {
                case "TRUE":
                    return true;
                case "FALSE":
                    return false;
                default:
                    throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
            }
        }
Пример #12
0
        private Result ProcessShow(Tokenizer tokenizer, Channel channel)
        {
            Result r = new Result(1);

            string sToken = tokenizer.GetString();

            if (sToken.Equals("TABLES"))
            {
                r.Table[0] = "SYSTEM_TABLES";
                r.Label[0] = "TABLE_NAME";
                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.Table[0] = "SYSTEM_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 TracingHelper.Error(TracingHelper.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 TracingHelper.Error(TracingHelper.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 TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
            }

            return r;
        }
Пример #13
0
        private Result ProcessSet(Tokenizer tokenizer, Channel channel)
        {
            string sToken = tokenizer.GetString();

            switch(sToken)
            {
                case "PASSWORD":
                    channel.CheckReadWrite();
                    channel.SetPassword(tokenizer.GetStringToken());
                    break;
                case "READONLY":
                    channel.Commit();
                    channel.SetReadOnly(ProcessTrueOrFalse(tokenizer));
                    break;
                case "LOGSIZE":
                {
                    channel.CheckAdmin();

                    int i = Int32.Parse(tokenizer.GetString());

                    if (_log != null)
                    {
                        _log.SetLogSize(i);
                    }
                }
                    break;
                case "IGNORECASE":
                    channel.CheckAdmin();
                    _ignoreCase = ProcessTrueOrFalse(tokenizer);
                    break;
                case "MAXROWS":
                {
                    int i = Int32.Parse(tokenizer.GetString());
                    channel.MaxRows = i;
                    break;
                }
                case "AUTOCOMMIT":
                    channel.SetAutoCommit(ProcessTrueOrFalse(tokenizer));
                    break;
                case "TABLE":
                {
                    channel.CheckReadWrite();
                    channel.CheckAdmin();

                    Table t = GetTable(tokenizer.GetString(), channel);

                    tokenizer.GetThis("INDEX");
                    tokenizer.GetString();
                    t.IndexRoots = (string)tokenizer.Value;
                }
                    break;
                case "REFERENCIAL_INTEGRITY":
                case "REFERENTIAL_INTEGRITY":
                    channel.CheckAdmin();
                    _referentialIntegrity = ProcessTrueOrFalse(tokenizer);
                    break;
                case "WRITE_DELAY":
                {
                    channel.CheckAdmin();

                    bool delay = ProcessTrueOrFalse(tokenizer);

                    if (_log != null)
                    {
                        _log.SetWriteDelay(delay);
                    }
                }
                    break;
                default:
                    if( tokenizer.TokenType == TokenType.VARIABLE )
                    {
                        Parser p = new Parser(this, tokenizer, channel);
                        p.ProcessSet( sToken );
                        break;
                    }

                    throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
            }

            return new Result();
        }
Пример #14
0
        private Result ProcessScript(Tokenizer tokenizer, Channel channel)
        {
            string sToken = tokenizer.GetString();

            if (tokenizer.WasValue)
            {
                sToken = (string) tokenizer.Value;

                Log.ScriptToFile(this, sToken, true, channel);

                return new Result();
            }
            else
            {
                tokenizer.Back();

                // try to script all: drop, insert; but no positions for cached tables
                return GetScript(true, true, false, channel);
            }
        }
Пример #15
0
        private Result ProcessDisconnect(Tokenizer tokenizer, Channel channel)
        {
            if (!channel.IsClosed)
            {
                channel.Disconnect();
                _channel.Remove(channel.Id);
            }

            return new Result();
        }
Пример #16
0
        private void AddIndexOn(Tokenizer tokenizer, Channel channel, string name, Table table, bool unique)
        {
            int[] col = ProcessColumnList(tokenizer, table);

            CreateIndex(channel, table, col, name, unique);
        }
Пример #17
0
        private Result ProcessDrop(Tokenizer tokenizer, Channel channel)
        {
            channel.CheckReadWrite();
            channel.CheckAdmin();

            string sToken = tokenizer.GetString();

            if (sToken.Equals("TABLE"))
            {
                sToken = tokenizer.GetString();

                if (sToken.Equals("IF"))
                {
                    sToken = tokenizer.GetString();    // EXISTS
                    sToken = tokenizer.GetString();    // <table>

                    DropTable(sToken, true);
                }
                else
                {
                    DropTable(sToken, false);
                }
                channel.Commit();
            }
            else if (sToken.Equals("USER"))
            {
                _access.DropUser(tokenizer.GetStringToken());
            }
            else if (sToken.Equals("INDEX"))
            {
                sToken = tokenizer.GetString();

                if (!tokenizer.WasLongName)
                {
                    throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
                }

                string table = tokenizer.LongNameFirst;
                string index = tokenizer.LongNameLast;
                Table  t = GetTable(table, channel);

                t.CheckDropIndex(index);

                Table tn = t.MoveDefinition(index);

                tn.MoveData(t);
                DropTable(table);
                LinkTable(tn);
                channel.Commit();
            }
            else
            {
                throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
            }

            return new Result();
        }
Пример #18
0
        private void AddUniqueConstraintOn(Tokenizer tokenizer, Channel channel, string name, Table table)
        {
            int[] col = ProcessColumnList(tokenizer, table);

            CreateIndex(channel, table, col, name, true);
            table.AddConstraint(new Constraint(ConstraintType.Unique, table, col));
        }
Пример #19
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;
        }
Пример #20
0
        private Result ProcessGrantOrRevoke(Tokenizer c, Channel channel, bool grant)
        {
            channel.CheckReadWrite();
            channel.CheckAdmin();

            AccessType    right = AccessType.None;
            string sToken;

            do
            {
                string sRight = c.GetString();

                right |= Access.GetRight(sRight);
                sToken = c.GetString();
            } while (sToken.Equals(","));

            if (!sToken.Equals("ON"))
            {
                throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
            }

            string table = c.GetString();

            if (table.Equals("CLASS"))
            {
                // object is saved as 'CLASS "java.lang.Math"'
                // tables like 'CLASS "xy"' should not be created
                table += " \"" + c.GetString() + "\"";
            }
            else
            {
                GetTable(table, channel);    // to make sure the table exists
            }

            c.GetThis("TO");

            string user = c.GetStringToken();
            //			string command;

            if (grant)
            {
                _access.Grant(user, table, right);

                //				command = "GRANT";
            }
            else
            {
                _access.Revoke(user, table, right);

                //				command = "REVOKE";
            }

            return new Result();
        }