// object[] getCurrent() {
 // return oCurrentData;
 // }
 /**
  * Constructor declaration
  *
  *
  * @param t
  * @param alias
  * @param outerjoin
  */
 public TableFilter(Table t, string alias, bool outerjoin)
 {
     tTable = t;
     iIndex = null;
     sAlias = alias != null ? alias : t.getName();
     bOuterJoin = outerjoin;
     oEmptyData = tTable.getNewRow();
 }
Beispiel #2
0
        /**
         * Constructor declaration
         *
         *
         * @param type
         * @param main
         * @param ref
         * @param cmain
         * @param cref
         */
        public Constraint(int type, Table main, Table child, int[] cmain,
			int[] cref)
        {
            iType = type;
            tMain = main;
            tRef = child;
            iColMain = cmain;
            iColRef = cref;
            iLen = cmain.Length;

            if (Trace.ASSERT)
            {
                Trace.assert(cmain.Length == cref.Length);
            }

            oMain = tMain.getNewRow();
            oRef = tRef.getNewRow();
            iMain = tMain.getIndexForColumns(cmain);
            iRef = tRef.getIndexForColumns(cref);
        }
        /**
         * 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;
        }
        /**
         * Method declaration
         *
         *
         * @param e
         *
         * @throws Exception
         */
        public void setCondition(Expression e)
        {
            int	   type = e.getType();
            Expression e1 = e.getArg();
            Expression e2 = e.getArg2();

            if (type == Expression.AND)
            {
                setCondition(e1);
                setCondition(e2);

                return;
            }

            int candidate;

            switch (type)
            {

                case Expression.NOT_EQUAL:

                case Expression.LIKE:    // todo: maybe use index

                case Expression.IN:
                    candidate = 0;

                    break;

                case Expression.EQUAL:
                    candidate = 1;

                    break;

                case Expression.BIGGER:

                case Expression.BIGGER_EQUAL:
                    candidate = 2;

                    break;

                case Expression.SMALLER:

                case Expression.SMALLER_EQUAL:
                    candidate = 3;

                    break;

                default:

                    // not a condition so forget it
                    return;
            }

            if (e1.getFilter() == this)
            {

                // ok include this
            }
            else if (e2.getFilter() == this && candidate != 0)
            {

                // swap and try again to allow index usage
                e.swapCondition();
                setCondition(e);

                return;
            }
            else
            {

                // unrelated: don't include
                return;
            }

            Trace.assert(e1.getFilter() == this, "setCondition");

            if (!e2.isResolved())
            {
                return;
            }

            if (candidate == 0)
            {
                addAndCondition(e);

                return;
            }

            int   i = e1.getColumnNr();
            Index index = tTable.getIndexForColumn(i);

            if (index == null || (iIndex != index && iIndex != null))
            {

                // no index or already another index is used
                addAndCondition(e);

                return;
            }

            iIndex = index;

            if (candidate == 1)
            {

                // candidate for both start & end
                if (eStart != null || eEnd != null)
                {
                    addAndCondition(e);

                    return;
                }

                eStart = new Expression(e);
                eEnd = eStart;
            }
            else if (candidate == 2)
            {

                // candidate for start
                if (eStart != null)
                {
                    addAndCondition(e);

                    return;
                }

                eStart = new Expression(e);
            }
            else if (candidate == 3)
            {

                // candidate for end
                if (eEnd != null)
                {
                    addAndCondition(e);

                    return;
                }

                eEnd = new Expression(e);
            }

            e.setTrue();
        }
Beispiel #5
0
        /**
         * Method declaration
         *
         *
         * @param index
         *
         * @return
         */
        public Index getNextIndex(Index index)
        {
            int i = 0;

            if (index != null)
            {
                for (; i < iIndexCount && getIndex(i) != index; i++);

                i++;
            }

            if (i < iIndexCount)
            {
                return getIndex(i);
            }

            return null;    // no more indexes
        }
Beispiel #6
0
        /**
         * Method declaration
         *
         *
         * @param column
         * @param name
         * @param unique
         *
         * @throws Exception
         */
        public void createIndex(int[] column, string name,
			bool unique)
        {
            Trace.assert(iPrimaryKey != -1, "createIndex");

            for (int i = 0; i < iIndexCount; i++)
            {
                Index index = getIndex(i);

                if (name.Equals(index.getName()))
                {
                    throw Trace.error(Trace.INDEX_ALREADY_EXISTS);
                }
            }

            int s = column.Length;

            // The primary key field is added for non-unique indexes
            // making all indexes unique
            int[] col = new int[unique ? s : s + 1];
            int[] type = new int[unique ? s : s + 1];

            for (int j = 0; j < s; j++)
            {
                col[j] = column[j];
                type[j] = getColumn(col[j]).iType;
            }

            if (!unique)
            {
                col[s] = iPrimaryKey;
                type[s] = getColumn(iPrimaryKey).iType;
            }

            Index newindex = new Index(name, col, type, unique);

            if (iIndexCount != 0)
            {
                Trace.assert(isEmpty(), "createIndex");
            }

            vIndex.Add(newindex);

            iIndexCount++;
        }
Beispiel #7
0
 /**
  * Method declaration
  *
  *
  * @param index
  *
  * @throws Exception
  */
 public void createIndex(Index index)
 {
     createIndex(index.getColumns(), index.getName(), index.isUnique());
 }
Beispiel #8
0
        /**
         * Method declaration
         *
         * ALTER TABLE tableName ADD COLUMN columnName columnType;
         * ALTER TABLE tableName DELETE COLUMN columnName;
         *
         * <B>Note: </B>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
         *
         * @return
         *
         * @throws Exception
         */
        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.getColumnCount(); 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();
                    int     iType = Column.getTypeNr(cType);
                    string  sToken = cType;
                    //					int     primarykeycolumn = -1;
                    bool identity = false;
                    int     column = initialTable.getColumnCount() + 1;

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

                    if (iType == Column.VARCHAR && dDatabase.isIgnoreCase())
                    {
                        iType = Column.VARCHAR_IGNORECASE;
                    }

                    sToken = tTokenizer.getstring();

                    if (iType == Column.DOUBLE && 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.getColumnCount()
                        < initialTable.getInternalColumnCount())
                    {
                        swapTable.createPrimaryKey();
                    }
                    else
                    {
                        swapTable.createPrimaryKey(initialTable.getPrimaryIndex().getColumns()[0]);
                    }

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

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

                        if (idx == null)
                        {
                            break;
                        }

                        if (idx == initialTable.getPrimaryIndex())
                        {
                            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()).rRoot;

                        do
                        {
                            object[] row = swapTable.getNewRow();
                            object[] row1 = n.data;

                            for (int i = 0; i < initialTable.getColumnCount();
                                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.getColumnCount(); i++)
                    {
                        Column aColumn = swapTable.getColumn(i);

                        initialTable.addColumn(aColumn);
                    }

                    if (swapTable.getColumnCount()
                        < swapTable.getInternalColumnCount())
                    {
                        initialTable.createPrimaryKey();
                    }
                    else
                    {
                        initialTable.createPrimaryKey(swapTable.getPrimaryIndex().getColumns()[0]);
                    }

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

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

                        if (idx == null)
                        {
                            break;
                        }

                        if (idx == swapTable.getPrimaryIndex())
                        {
                            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()).rRoot;

                        do
                        {
                            object[] row = initialTable.getNewRow();
                            object[] row1 = n.data;

                            for (int i = 0; i < swapTable.getColumnCount(); 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 Trace.error(Trace.UNEXPECTED_TOKEN, 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.getColumnNr(cName);

                    for (int i = 0; i < initialTable.getColumnCount(); i++)
                    {
                        Column aColumn = initialTable.getColumn(i);

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

                    int pKey = -1;

                    // !--
                    if (initialTable.getColumnCount()
                        < initialTable.getInternalColumnCount())
                    {
                        swapTable.createPrimaryKey();
                    }
                    else
                    {
                        int[] cols = initialTable.getPrimaryIndex().getColumns();

                        pKey = cols[0];

                        if ((cols[0] > undesired)
                            || (cols[0] + cols.Length < undesired))
                        {
                            if (undesired
                                < initialTable.getPrimaryIndex().getColumns()[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.getPrimaryIndex())
                        {
                            continue;
                        }

                        bool flag = true;
                        int[]   cols = idx.getColumns();

                        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.getName(), idx.getColumns(),
                                idx.getType(), 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()).rRoot;

                        do
                        {
                            object[] row = swapTable.getNewRow();
                            object[] row1 = n.data;
                            int    j = 0;

                            for (int i = 0; i < initialTable.getColumnCount();
                                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.getColumnCount(); i++)
                    {
                        Column aColumn = swapTable.getColumn(i);

                        initialTable.addColumn(aColumn);
                    }

                    // !--
                    if (swapTable.getColumnCount()
                        < swapTable.getInternalColumnCount())
                    {
                        initialTable.createPrimaryKey();
                    }
                    else
                    {
                        initialTable.createPrimaryKey(swapTable.getPrimaryIndex().getColumns()[0]);
                    }

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

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

                        if (idx == null)
                        {
                            break;
                        }

                        if (idx == swapTable.getPrimaryIndex())
                        {
                            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()).rRoot;

                        do
                        {
                            object[] row = initialTable.getNewRow();
                            object[] row1 = n.data;

                            for (int i = 0; i < swapTable.getColumnCount(); 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 Trace.error(Trace.UNEXPECTED_TOKEN, token);
                }

                count = 3;
            }

            Result r = new Result();

            r.iUpdateCount = count;    // --> nu tre inca...??

            return r;
        }