Example #1
0
        /**
         * Method declaration
         *
         *
         * @param c
         * @param channel
         *
         * @return
         *
         * @throws Exception
         */
        private Result processDrop(Tokenizer c,
                                   Channel channel)
        {
            channel.checkReadWrite();
            channel.checkAdmin();

            string sToken = c.getstring();

            if (sToken.Equals("TABLE"))
            {
                sToken = c.getstring();

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

                    dropTable(sToken, true);
                }
                else
                {
                    dropTable(sToken, false);
                }
                channel.commit();
            }
            else if (sToken.Equals("USER"))
            {
                aAccess.dropUser(c.getstringToken());
            }
            else if (sToken.Equals("INDEX"))
            {
                sToken = c.getstring();

                if (!c.wasLongName())
                {
                    throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
                }

                string table = c.getLongNameFirst();
                string index = c.getLongNameLast();
                Table  t     = getTable(table, channel);

                t.checkDropIndex(index);

                Table tn = t.moveDefinition(index);

                tn.moveData(t);
                dropTable(table);
                linkTable(tn);
                channel.commit();
            }
            else
            {
                throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
            }

            return(new Result());
        }
Example #2
0
        /**
         * Method declaration
         *
         *
         * @param c
         * @param channel
         * @param grant
         *
         * @return
         *
         * @throws Exception
         */
        private Result processGrantOrRevoke(Tokenizer c, Channel channel,
                                            bool grant)
        {
            channel.checkReadWrite();
            channel.checkAdmin();

            int    right = 0;
            string sToken;

            do
            {
                string sRight = c.getstring();

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

            if (!sToken.Equals("ON"))
            {
                throw Trace.error(Trace.UNEXPECTED_TOKEN, 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)
            {
                aAccess.grant(user, table, right);

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

                //				command = "REVOKE";
            }

            return(new Result());
        }
Example #3
0
        /**
         * Method declaration
         *
         *
         * @param channel
         *
         * @return
         *
         * @throws Exception
         */
        private Result processCheckpoint(Channel channel)
        {
            channel.checkAdmin();

            if (lLog != null)
            {
                lLog.checkpoint();
            }

            return(new Result());
        }
Example #4
0
        /**
         * Method declaration
         *
         *
         * @param c
         * @param channel
         *
         * @return
         *
         * @throws Exception
         */
        private Result processShutdown(Tokenizer c,
                                       Channel channel)
        {
            channel.checkAdmin();

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

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

            cChannel.Clear();

            string token = c.getstring();

            if (token.Equals("IMMEDIATELY"))
            {
                close(-1);
            }
            else if (token.Equals("COMPACT"))
            {
                close(1);
            }
            else
            {
                c.back();
                close(0);
            }

            processDisconnect(c, channel);

            return(new Result());
        }
        /**
         * Method declaration
         *
         *
         * @param bDrop
         * @param bInsert
         * @param bCached
         * @param channel
         *
         * @return
         *
         * @throws Exception
         */
        public Result getScript(bool bDrop, bool bInsert, bool bCached,
			Channel channel)
        {
            channel.checkAdmin();

            Result r = new Result(1);

            r.iType[0] = Column.VARCHAR;
            r.sTable[0] = "SYSTEM_SCRIPT";
            r.sLabel[0] = "COMMAND";
            r.sName[0] = "COMMAND";

            StringBuilder a = new StringBuilder();

            for (int i = 0; i < tTable.Count; i++)
            {
                Table t = (Table) tTable[i];

                if (bDrop)
                {
                    addRow(r, "DROP TABLE " + t.getName());
                }

                a.Remove(0,a.Length);
                a.Append("CREATE ");

                if (t.isCached())
                {
                    a.Append("CACHED ");
                }

                a.Append("TABLE ");
                a.Append(t.getName());
                a.Append("(");

                int   columns = t.getColumnCount();
                Index pki = t.getIndex("SYSTEM_PK");
                int   pk = (pki == null) ? -1 : pki.getColumns()[0];

                for (int j = 0; j < columns; j++)
                {
                    a.Append(t.getColumnName(j));
                    a.Append(" ");
                    a.Append(Column.getType(t.getType(j)));

                    if (!t.getColumnIsNullable(j))
                    {
                        a.Append(" NOT NULL");
                    }

                    if (j == t.getIdentityColumn())
                    {
                        a.Append(" IDENTITY");
                    }

                    if (j == pk)
                    {
                        a.Append(" PRIMARY KEY");
                    }

                    if (j < columns - 1)
                    {
                        a.Append(",");
                    }
                }

                ArrayList v = t.getConstraints();

                for (int j = 0; j < v.Count; j++)
                {
                    Constraint c = (Constraint) v[j];

                    if (c.getType() == Constraint.FOREIGN_KEY)
                    {
                        a.Append(",FOREIGN KEY");

                        int[] col = c.getRefColumns();

                        a.Append(getColumnList(c.getRef(), col, col.Length));
                        a.Append("REFERENCES ");
                        a.Append(c.getMain().getName());

                        col = c.getMainColumns();

                        a.Append(getColumnList(c.getMain(), col, col.Length));
                    }
                    else if (c.getType() == Constraint.UNIQUE)
                    {
                        a.Append(",UNIQUE");

                        int[] col = c.getMainColumns();

                        a.Append(getColumnList(c.getMain(), col, col.Length));
                    }
                }

                a.Append(")");
                addRow(r, a.ToString());

                Index index = null;

                while (true)
                {
                    index = t.getNextIndex(index);

                    if (index == null)
                    {
                        break;
                    }

                    string indexname = index.getName();

                    if (indexname.Equals("SYSTEM_PK"))
                    {
                        continue;
                    }
                    else if (indexname.StartsWith("SYSTEM_FOREIGN_KEY"))
                    {

                        // foreign keys where created in the 'create table'
                        continue;
                    }
                    else if (indexname.StartsWith("SYSTEM_CONSTRAINT"))
                    {

                        // constraints where created in the 'create table'
                        continue;
                    }

                    a.Remove(0,a.Length);
                    a.Append("CREATE ");

                    if (index.isUnique())
                    {
                        a.Append("UNIQUE ");
                    }

                    a.Append("INDEX ");
                    a.Append(indexname);
                    a.Append(" ON ");
                    a.Append(t.getName());

                    int[] col = index.getColumns();
                    int len = col.Length;

                    if (!index.isUnique())
                    {
                        len--;
                    }

                    a.Append(getColumnList(t, col, len));
                    addRow(r, a.ToString());
                }

                if (bInsert)
                {
                    Index   primary = t.getPrimaryIndex();
                    Node    x = primary.first();
                    bool integrity = true;

                    if (x != null)
                    {
                        integrity = false;

                        addRow(r, "SET REFERENTIAL_INTEGRITY FALSE");
                    }

                    while (x != null)
                    {
                        addRow(r, t.getInsertStatement(x.getData()));

                        x = primary.next(x);
                    }

                    if (!integrity)
                    {
                        addRow(r, "SET REFERENTIAL_INTEGRITY TRUE");
                    }
                }

                if (bCached && t.isCached())
                {
                    a.Remove(0,a.Length);
                    a.Append("SET TABLE ");

                    a.Append(t.getName());
                    a.Append(" INDEX '");
                    a.Append(t.getIndexRoots());
                    a.Append("'");
                    addRow(r, a.ToString());
                }

            }

            ArrayList uList = aAccess.getUsers();

            for (int i = 0; i < uList.Count; i++)
            {
                User u = (User) uList[i];

                // todo: this is not a nice implementation
                if (u == null)
                {
                    continue;
                }

                string name = u.getName();

                if (!name.Equals("PUBLIC"))
                {
                    a.Remove(0,a.Length);
                    a.Append("CREATE USER ");

                    a.Append(name);
                    a.Append(" PASSWORD ");
                    a.Append("\"" + u.getPassword() + "\"");

                    if (u.isAdmin())
                    {
                        a.Append(" ADMIN");
                    }

                    addRow(r, a.ToString());
                }

                Hashtable rights = u.getRights();

                if (rights == null)
                {
                    continue;
                }

                foreach( string dbObject in rights.Keys)
                {
                    int    right = (int) rights[dbObject];

                    if (right == 0)
                    {
                        continue;
                    }

                    a.Remove(0,a.Length);
                    a.Append("GRANT ");

                    a.Append(Access.getRight(right));
                    a.Append(" ON ");
                    a.Append(dbObject);
                    a.Append(" TO ");
                    a.Append(u.getName());
                    addRow(r, a.ToString());
                }
            }

            if (dDatabase.isIgnoreCase())
            {
                addRow(r, "SET IGNORECASE TRUE");
            }

            Hashtable   h = dDatabase.getAlias();

            foreach(string alias in h.Keys)
            {
                string java = (string) h[alias];
                addRow(r, "CREATE ALIAS " + alias + " FOR \"" + java + "\"");
            }

            return r;
        }
Example #6
0
        /**
         * Method declaration
         *
         *
         * @param c
         * @param channel
         *
         * @return
         *
         * @throws Exception
         */
        private Result processCreate(Tokenizer c,
                                     Channel channel)
        {
            channel.checkReadWrite();
            channel.checkAdmin();

            string sToken = c.getstring();

            if (sToken.Equals("TABLE"))
            {
                processCreateTable(c, channel, false);
            }
            else if (sToken.Equals("MEMORY"))
            {
                c.getThis("TABLE");
                processCreateTable(c, channel, false);
            }
            else if (sToken.Equals("CACHED"))
            {
                c.getThis("TABLE");
                processCreateTable(c, channel, true);
            }
            else if (sToken.Equals("USER"))
            {
                string u = c.getstringToken();

                c.getThis("PASSWORD");

                string p = c.getstringToken();
                bool   admin;

                if (c.getstring().Equals("ADMIN"))
                {
                    admin = true;
                }
                else
                {
                    admin = false;
                }

                aAccess.createUser(u, p, admin);
            }
            else if (sToken.Equals("ALIAS"))
            {
                string name = c.getstring();

                sToken = c.getstring();

                Trace.check(sToken.Equals("FOR"), Trace.UNEXPECTED_TOKEN, sToken);

                sToken = c.getstring();

                hAlias.Add(name, sToken);
            }
            else
            {
                bool unique = false;

                if (sToken.Equals("UNIQUE"))
                {
                    unique = true;
                    sToken = c.getstring();
                }

                if (!sToken.Equals("INDEX"))
                {
                    throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
                }

                string name = c.getName();

                c.getThis("ON");

                Table t = getTable(c.getstring(), channel);

                addIndexOn(c, channel, name, t, unique);
            }

            return(new Result());
        }
Example #7
0
        /**
         * Method declaration
         *
         *
         * @param c
         * @param channel
         *
         * @return
         *
         * @throws Exception
         */
        private Result processSet(Tokenizer c,
                                  Channel channel)
        {
            string sToken = c.getstring();

            if (sToken.Equals("PASSWORD"))
            {
                channel.checkReadWrite();
                channel.setPassword(c.getstringToken());
            }
            else if (sToken.Equals("READONLY"))
            {
                channel.commit();
                channel.setReadOnly(processTrueOrFalse(c));
            }
            else if (sToken.Equals("LOGSIZE"))
            {
                channel.checkAdmin();

                int i = Int32.FromString(c.getstring());

                if (lLog != null)
                {
                    lLog.setLogSize(i);
                }
            }
            else if (sToken.Equals("IGNORECASE"))
            {
                channel.checkAdmin();

                bIgnoreCase = processTrueOrFalse(c);
            }
            else if (sToken.Equals("MAXROWS"))
            {
                int i = Int32.FromString(c.getstring());

                channel.setMaxRows(i);
            }
            else if (sToken.Equals("AUTOCOMMIT"))
            {
                channel.setAutoCommit(processTrueOrFalse(c));
            }
            else if (sToken.Equals("TABLE"))
            {
                channel.checkReadWrite();
                channel.checkAdmin();

                Table t = getTable(c.getstring(), channel);

                c.getThis("INDEX");
                c.getstring();
                t.setIndexRoots((string)c.getAsValue());
            }
            else if (sToken.Equals("REFERENCIAL_INTEGRITY") ||
                     sToken.Equals("REFERENTIAL_INTEGRITY"))
            {
                channel.checkAdmin();

                bReferentialIntegrity = processTrueOrFalse(c);
            }
            else if (sToken.Equals("WRITE_DELAY"))
            {
                channel.checkAdmin();

                bool delay = processTrueOrFalse(c);

                if (lLog != null)
                {
                    lLog.setWriteDelay(delay);
                }
            }
            else
            {
                throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
            }

            return(new Result());
        }
Example #8
0
        /**
         * Method declaration
         *
         *
         * @param bDrop
         * @param bInsert
         * @param bCached
         * @param channel
         *
         * @return
         *
         * @throws Exception
         */
        public Result getScript(bool bDrop, bool bInsert, bool bCached,
                                Channel channel)
        {
            channel.checkAdmin();

            Result r = new Result(1);

            r.iType[0]  = Column.VARCHAR;
            r.sTable[0] = "SYSTEM_SCRIPT";
            r.sLabel[0] = "COMMAND";
            r.sName[0]  = "COMMAND";

            StringBuilder a = new StringBuilder();

            for (int i = 0; i < tTable.Count; i++)
            {
                Table t = (Table)tTable[i];

                if (bDrop)
                {
                    addRow(r, "DROP TABLE " + t.getName());
                }

                a.Remove(0, a.Length);
                a.Append("CREATE ");

                if (t.isCached())
                {
                    a.Append("CACHED ");
                }

                a.Append("TABLE ");
                a.Append(t.getName());
                a.Append("(");

                int   columns = t.getColumnCount();
                Index pki     = t.getIndex("SYSTEM_PK");
                int   pk      = (pki == null) ? -1 : pki.getColumns()[0];

                for (int j = 0; j < columns; j++)
                {
                    a.Append(t.getColumnName(j));
                    a.Append(" ");
                    a.Append(Column.getType(t.getType(j)));

                    if (!t.getColumnIsNullable(j))
                    {
                        a.Append(" NOT NULL");
                    }

                    if (j == t.getIdentityColumn())
                    {
                        a.Append(" IDENTITY");
                    }

                    if (j == pk)
                    {
                        a.Append(" PRIMARY KEY");
                    }

                    if (j < columns - 1)
                    {
                        a.Append(",");
                    }
                }

                ArrayList v = t.getConstraints();

                for (int j = 0; j < v.Count; j++)
                {
                    Constraint c = (Constraint)v[j];

                    if (c.getType() == Constraint.FOREIGN_KEY)
                    {
                        a.Append(",FOREIGN KEY");

                        int[] col = c.getRefColumns();

                        a.Append(getColumnList(c.getRef(), col, col.Length));
                        a.Append("REFERENCES ");
                        a.Append(c.getMain().getName());

                        col = c.getMainColumns();

                        a.Append(getColumnList(c.getMain(), col, col.Length));
                    }
                    else if (c.getType() == Constraint.UNIQUE)
                    {
                        a.Append(",UNIQUE");

                        int[] col = c.getMainColumns();

                        a.Append(getColumnList(c.getMain(), col, col.Length));
                    }
                }

                a.Append(")");
                addRow(r, a.ToString());

                Index index = null;

                while (true)
                {
                    index = t.getNextIndex(index);

                    if (index == null)
                    {
                        break;
                    }

                    string indexname = index.getName();

                    if (indexname.Equals("SYSTEM_PK"))
                    {
                        continue;
                    }
                    else if (indexname.StartsWith("SYSTEM_FOREIGN_KEY"))
                    {
                        // foreign keys where created in the 'create table'
                        continue;
                    }
                    else if (indexname.StartsWith("SYSTEM_CONSTRAINT"))
                    {
                        // constraints where created in the 'create table'
                        continue;
                    }

                    a.Remove(0, a.Length);
                    a.Append("CREATE ");

                    if (index.isUnique())
                    {
                        a.Append("UNIQUE ");
                    }

                    a.Append("INDEX ");
                    a.Append(indexname);
                    a.Append(" ON ");
                    a.Append(t.getName());

                    int[] col = index.getColumns();
                    int   len = col.Length;

                    if (!index.isUnique())
                    {
                        len--;
                    }

                    a.Append(getColumnList(t, col, len));
                    addRow(r, a.ToString());
                }

                if (bInsert)
                {
                    Index primary   = t.getPrimaryIndex();
                    Node  x         = primary.first();
                    bool  integrity = true;

                    if (x != null)
                    {
                        integrity = false;

                        addRow(r, "SET REFERENTIAL_INTEGRITY FALSE");
                    }

                    while (x != null)
                    {
                        addRow(r, t.getInsertStatement(x.getData()));

                        x = primary.next(x);
                    }

                    if (!integrity)
                    {
                        addRow(r, "SET REFERENTIAL_INTEGRITY TRUE");
                    }
                }

                if (bCached && t.isCached())
                {
                    a.Remove(0, a.Length);
                    a.Append("SET TABLE ");

                    a.Append(t.getName());
                    a.Append(" INDEX '");
                    a.Append(t.getIndexRoots());
                    a.Append("'");
                    addRow(r, a.ToString());
                }
            }

            ArrayList uList = aAccess.getUsers();

            for (int i = 0; i < uList.Count; i++)
            {
                User u = (User)uList[i];

                // todo: this is not a nice implementation
                if (u == null)
                {
                    continue;
                }

                string name = u.getName();

                if (!name.Equals("PUBLIC"))
                {
                    a.Remove(0, a.Length);
                    a.Append("CREATE USER ");

                    a.Append(name);
                    a.Append(" PASSWORD ");
                    a.Append("\"" + u.getPassword() + "\"");

                    if (u.isAdmin())
                    {
                        a.Append(" ADMIN");
                    }

                    addRow(r, a.ToString());
                }

                Hashtable rights = u.getRights();

                if (rights == null)
                {
                    continue;
                }

                foreach (string dbObject in rights.Keys)
                {
                    int right = (int)rights[dbObject];

                    if (right == 0)
                    {
                        continue;
                    }

                    a.Remove(0, a.Length);
                    a.Append("GRANT ");

                    a.Append(Access.getRight(right));
                    a.Append(" ON ");
                    a.Append(dbObject);
                    a.Append(" TO ");
                    a.Append(u.getName());
                    addRow(r, a.ToString());
                }
            }

            if (dDatabase.isIgnoreCase())
            {
                addRow(r, "SET IGNORECASE TRUE");
            }

            Hashtable h = dDatabase.getAlias();

            foreach (string alias in h.Keys)
            {
                string java = (string)h[alias];
                addRow(r, "CREATE ALIAS " + alias + " FOR \"" + java + "\"");
            }

            return(r);
        }