Exemplo n.º 1
0
        /**
         * Method declaration
         *
         *
         * @return
         *
         * @throws Exception
         */
        public bool next()
        {
            if (bOuterJoin && nCurrent == null)
            {
                return(false);
            }

            nCurrent = iIndex.next(nCurrent);

            while (nCurrent != null)
            {
                oCurrentData = nCurrent.getData();

                if (!test(eEnd))
                {
                    break;
                }

                if (test(eAnd))
                {
                    return(true);
                }

                nCurrent = iIndex.next(nCurrent);
            }

            oCurrentData = oEmptyData;

            return(false);
        }
Exemplo n.º 2
0
        /**
         * Method declaration
         *
         *
         * @param d
         *
         * @return
         *
         * @throws Exception
         */
        private Node search(object[] d)
        {
            Node x = root;

            while (x != null)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                int c = compareRow(d, x.getData());

                if (c == 0)
                {
                    return(x);
                }
                else if (c < 0)
                {
                    x = x.getLeft();
                }
                else
                {
                    x = x.getRight();
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        /**
         * Method declaration
         *
         *
         * @param from
         *
         * @throws Exception
         */
        public void moveData(Table from)
        {
            Index index = from.getPrimaryIndex();
            Node  n     = index.first();

            while (n != null)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                object[] o = n.getData();

                insertNoCheck(o, null);

                n = index.next(n);
            }

            index = getPrimaryIndex();
            n     = index.first();

            while (n != null)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                object[] o = n.getData();

                from.deleteNoCheck(o, null);

                n = index.next(n);
            }
        }
Exemplo n.º 4
0
        /**
         * Method declaration
         *
         *
         * @return
         *
         * @throws Exception
         */
        public bool findFirst()
        {
            if (iIndex == null)
            {
                iIndex = tTable.getPrimaryIndex();
            }

            if (eStart == null)
            {
                nCurrent = iIndex.first();
            }
            else
            {
                int    type = eStart.getArg().getDataType();
                object o    = eStart.getArg2().getValue(type);

                nCurrent = iIndex.findFirst(o, eStart.getType());
            }

            while (nCurrent != null)
            {
                oCurrentData = nCurrent.getData();

                if (!test(eEnd))
                {
                    break;
                }

                if (test(eAnd))
                {
                    return(true);
                }

                nCurrent = iIndex.next(nCurrent);
            }

            oCurrentData = oEmptyData;

            if (bOuterJoin)
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 5
0
        /**
         * Method declaration
         *
         *
         * @param data
         *
         * @return
         *
         * @throws Exception
         */
        public Node find(object[] data)
        {
            Node x = root, n;

            while (x != null)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                int i = compareRowNonUnique(data, x.getData());

                if (i == 0)
                {
                    return(x);
                }
                else if (i > 0)
                {
                    n = x.getRight();
                }
                else
                {
                    n = x.getLeft();
                }

                if (n == null)
                {
                    return(null);
                }

                x = n;
            }

            return(null);
        }
Exemplo n.º 6
0
        /**
         * Method declaration
         *
         *
         * @param value
         * @param compare
         *
         * @return
         *
         * @throws Exception
         */
        public Node findFirst(object value, int compare)
        {
            Trace.assert(compare == Expression.BIGGER ||
                         compare == Expression.EQUAL ||
                         compare == Expression.BIGGER_EQUAL,
                         "Index.findFirst");

            Node x     = root;
            int  iTest = 1;

            if (compare == Expression.BIGGER)
            {
                iTest = 0;
            }

            while (x != null)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                bool t = compareValue(value, x.getData()[iColumn_0]) >= iTest;

                if (t)
                {
                    Node r = x.getRight();

                    if (r == null)
                    {
                        break;
                    }

                    x = r;
                }
                else
                {
                    Node l = x.getLeft();

                    if (l == null)
                    {
                        break;
                    }

                    x = l;
                }
            }

            while (x != null &&
                   compareValue(value, x.getData()[iColumn_0]) >= iTest)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                x = next(x);
            }

            return(x);
        }
Exemplo n.º 7
0
        /**
         * Method declaration
         *
         *
         * @param i
         *
         * @throws Exception
         */
        public void insert(Node i)
        {
            object[] data = i.getData();
            Node     n = root, x = n;
            bool     way     = true;
            int      compare = -1;

            while (true)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                if (n == null)
                {
                    if (x == null)
                    {
                        root = i;

                        return;
                    }

                    set(x, way, i);

                    break;
                }

                x       = n;
                compare = compareRow(data, x.getData());

                Trace.check(compare != 0, Trace.VIOLATION_OF_UNIQUE_INDEX);

                way = compare < 0;
                n   = child(x, way);
            }

            while (true)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                int sign = way ? 1 : -1;

                switch (x.getBalance() * sign)
                {
                case 1:
                    x.setBalance(0);

                    return;

                case 0:
                    x.setBalance(-sign);

                    break;

                case -1:
                    Node l = child(x, way);

                    if (l.getBalance() == -sign)
                    {
                        replace(x, l);
                        set(x, way, child(l, !way));
                        set(l, !way, x);
                        x.setBalance(0);
                        l.setBalance(0);
                    }
                    else
                    {
                        Node r = child(l, !way);

                        replace(x, r);
                        set(l, !way, child(r, way));
                        set(r, way, l);
                        set(x, way, child(r, !way));
                        set(r, !way, x);

                        int rb = r.getBalance();

                        x.setBalance((rb == -sign) ? sign : 0);
                        l.setBalance((rb == sign) ? -sign : 0);
                        r.setBalance(0);
                    }

                    return;
                }

                if (x.Equals(root))
                {
                    return;
                }

                way = from(x);
                x   = x.getParent();
            }
        }
Exemplo n.º 8
0
        /**
         * Method declaration
         *
         *
         * @param db
         * @param file
         * @param full
         * @param channel
         *
         * @throws Exception
         */
        public static void scriptToFile(Database db, string file, bool full,
                                        Channel channel)
        {
            if ((new File(file)).Exists)
            {
                // there must be no such file; overwriting not allowed for security
                throw Trace.error(Trace.FILE_IO_ERROR, file);
            }

            try
            {
                DateTime time = DateTime.Now;

                // only ddl commands; needs not so much memory
                Result r;

                if (full)
                {
                    // no drop, no insert, and no positions for cached tables
                    r = db.getScript(false, false, false, channel);
                }
                else
                {
                    // no drop, no insert, but positions for cached tables
                    r = db.getScript(false, false, true, channel);
                }

                Record       n = r.rRoot;
                StreamWriter w = new StreamWriter(file);

                while (n != null)
                {
                    writeLine(w, (string)n.data[0]);

                    n = n.next;
                }

                // inserts are done separetely to save memory
                ArrayList tables = db.getTables();

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

                    // cached tables have the index roots set in the ddl script
                    if (full || !t.isCached())
                    {
                        Index primary = t.getPrimaryIndex();
                        Node  x       = primary.first();

                        while (x != null)
                        {
                            writeLine(w, t.getInsertStatement(x.getData()));

                            x = primary.next(x);
                        }
                    }
                }

                w.Close();

                TimeSpan execution = DateTime.Now.Subtract(time);

                if (Trace.TRACE)
                {
                    Trace.trace(execution.TotalMilliseconds.ToInt64());
                }
            }
            catch (IOException e)
            {
                Trace.error(Trace.FILE_IO_ERROR, file + " " + e);
            }
        }
Exemplo n.º 9
0
        /**
         * Method declaration
         *
         *
         * @return
         *
         * @throws Exception
         */
        public bool next()
        {
            if (bOuterJoin && nCurrent == null)
            {
                return false;
            }

            nCurrent = iIndex.next(nCurrent);

            while (nCurrent != null)
            {
                oCurrentData = nCurrent.getData();

                if (!test(eEnd))
                {
                    break;
                }

                if (test(eAnd))
                {
                    return true;
                }

                nCurrent = iIndex.next(nCurrent);
            }

            oCurrentData = oEmptyData;

            return false;
        }
Exemplo n.º 10
0
        /**
         * Method declaration
         *
         *
         * @param i
         *
         * @throws Exception
         */
        public void insert(Node i)
        {
            object[] data = i.getData();
            Node    n = root, x = n;
            bool way = true;
            int     compare = -1;

            while (true)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                if (n == null)
                {
                    if (x == null)
                    {
                        root = i;

                        return;
                    }

                    set(x, way, i);

                    break;
                }

                x = n;
                compare = compareRow(data, x.getData());

                Trace.check(compare != 0, Trace.VIOLATION_OF_UNIQUE_INDEX);

                way = compare < 0;
                n = child(x, way);
            }

            while (true)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                int sign = way ? 1 : -1;

                switch (x.getBalance() * sign)
                {

                    case 1:
                        x.setBalance(0);

                        return;

                    case 0:
                        x.setBalance(-sign);

                        break;

                    case -1:
                        Node l = child(x, way);

                        if (l.getBalance() == -sign)
                        {
                            replace(x, l);
                            set(x, way, child(l, !way));
                            set(l, !way, x);
                            x.setBalance(0);
                            l.setBalance(0);
                        }
                        else
                        {
                            Node r = child(l, !way);

                            replace(x, r);
                            set(l, !way, child(r, way));
                            set(r, way, l);
                            set(x, way, child(r, !way));
                            set(r, !way, x);

                            int rb = r.getBalance();

                            x.setBalance((rb == -sign) ? sign : 0);
                            l.setBalance((rb == sign) ? -sign : 0);
                            r.setBalance(0);
                        }

                        return;
                }

                if (x.Equals(root))
                {
                    return;
                }

                way = from(x);
                x = x.getParent();
            }
        }
Exemplo n.º 11
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);
        }