⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 table.java

📁 一个用java写的开源的数据库系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            Index newidx = tn.createAdjustedIndex(idx, colindex, adjust);            if (newidx == null) {                // fredt - todo - better error message                throw Trace.error(Trace.INDEX_ALREADY_EXISTS);            }        }        return tn;    }    void updateConstraints(Table to, int colindex,                           int adjust) throws SQLException {        for (int j = 0; j < vConstraint.size(); j++) {            Constraint c = (Constraint) vConstraint.elementAt(j);            c.replaceTable(to, this, colindex, adjust);        }    }    /**     *  Method declaration     *     * @return     */    int getColumnCount() {        return iVisibleColumns;    }    /**     *  Method declaration     *     * @return     */    int getIndexCount() {        return iIndexCount;    }    /**     *  Method declaration     *     * @return     */    int getIdentityColumn() {        return iIdentityColumn;    }    /**     *  Method declaration     *     * @param  c     * @return     * @throws  SQLException     */    int getColumnNr(String c) throws SQLException {        int i = searchColumn(c);        if (i == -1) {            throw Trace.error(Trace.COLUMN_NOT_FOUND, c);        }        return i;    }    /**     *  Method declaration     *     * @param  c     * @return     */    int searchColumn(String c) {        for (int i = 0; i < iColumnCount; i++) {            if (c.equals(((Column) vColumn.elementAt(i)).columnName.name)) {                return i;            }        }        return -1;    }    /**     *  Method declaration     *     * @return     * @throws  SQLException     */    Index getPrimaryIndex() {        if (iPrimaryKey == null) {            return null;        }        return getIndex(0);    }    /**     *  Method declaration     *     * @param  column     * @return     * @throws  SQLException     */    Index getIndexForColumn(int column) throws SQLException {        for (int i = 0; i < iIndexCount; i++) {            Index h = getIndex(i);            if (h.getColumns()[0] == column) {                return h;            }        }        return null;    }    /**     *  Finds an existing index for a foreign key column group     *     * @param  col     * @return     * @throws  SQLException     */    Index getIndexForColumns(int col[], boolean unique) throws SQLException {        for (int i = 0; i < iIndexCount; i++) {            Index currentindex = getIndex(i);            int   indexcol[]   = currentindex.getColumns();            if (ArrayUtil.haveEquality(indexcol, col, col.length, unique)) {                if (!unique || currentindex.isUnique()) {                    return currentindex;                }            }        }        return null;    }    /**     *  Return the list of file pointers to root nodes for this table's     *  indexes.     */    int[] getIndexRootsArray() throws SQLException {        int[] roots = new int[iIndexCount];        for (int i = 0; i < iIndexCount; i++) {            Node f = getIndex(i).getRoot();            roots[i] = (f != null) ? f.getKey()                                   : -1;        }        return roots;    }    /**     *  Method declaration     *     * @return     * @throws  SQLException     */    String getIndexRoots() throws SQLException {        Trace.doAssert(isCached, "Table.getIndexRootData");        String roots   = StringUtil.getList(getIndexRootsArray(), " ", "");        StringBuffer s = new StringBuffer(roots);        s.append(' ');        s.append(iIdentityId);        return s.toString();    }    /**     *  Method declaration     *     * @param  s     * @throws  SQLException     */    void setIndexRoots(int[] roots) throws SQLException {        Trace.check(isCached, Trace.TABLE_NOT_FOUND);        for (int i = 0; i < iIndexCount; i++) {            int p = roots[i];            Row r = null;            if (p != -1) {                r = cCache.getRow(p, this);            }            Node f = null;            if (r != null) {                f = r.getNode(i);            }            getIndex(i).setRoot(f);        }    }    /**     *  Method declaration     *     * @param  s     * @throws  SQLException     */    void setIndexRoots(String s) throws SQLException {        // the user may try to set this; this is not only internal problem        Trace.check(isCached, Trace.TABLE_NOT_FOUND);        int[] roots = new int[iIndexCount];        int   j     = 0;        for (int i = 0; i < iIndexCount; i++) {            int n = s.indexOf(' ', j);            int p = Integer.parseInt(s.substring(j, n));            roots[i] = p;            j        = n + 1;        }        setIndexRoots(roots);        iIdentityId = Integer.parseInt(s.substring(j));    }    /**     *  Method declaration     *     * @param  index     * @return     */    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    }    /**     *  Shortcut for creating default PK's     *     * @throws  SQLException     */    void createPrimaryKey() throws SQLException {// tony_lai@users 20020820 - patch 595099        createPrimaryKey(null, null);    }    /**     *  Adds the SYSTEM_ID column if no primary key is specified in DDL.     *  Creates a single or multi-column primary key and index. sets the     *  colTypes array. Finalises the creation of the table. (fredt@users)     *     * @param columns primary key column(s) or null if no primary key in DDL     * @throws  SQLException     */// tony_lai@users 20020820 - patch 595099    void createPrimaryKey(HsqlName pkName,                          int[] columns) throws SQLException {        Trace.doAssert(iPrimaryKey == null, "Table.createPrimaryKey(column)");        iVisibleColumns = iColumnCount;        if (columns == null) {            columns = new int[]{ iColumnCount };            Column column = new Column(new HsqlName(DEFAULT_PK, false),                                       false, Types.INTEGER, 0, 0, true,                                       true, null);            addColumn(column);        } else {            for (int i = 0; i < columns.length; i++) {                getColumn(columns[i]).setNullable(false);                getColumn(columns[i]).setPrimaryKey(true);            }        }        iPrimaryKey = columns;// tony_lai@users 20020820 - patch 595099        HsqlName name = pkName != null ? pkName                                       : new HsqlName("SYS_PK",                                           tableName.name,                                           tableName.isNameQuoted);        createIndexPrivate(columns, name, true);        colTypes = new int[iColumnCount];        for (int i = 0; i < iColumnCount; i++) {            colTypes[i] = getColumn(i).getType();        }    }    /**     *  Create new index taking into account removal or addition a column of     *  the table.     *     * @param  index     * @param  colindex     * @param  ajdust -1 or 0 or 1     * @return new index or null if a column is removed from index     * @throws  SQLException     */    private Index createAdjustedIndex(Index index, int colindex,                                      int adjust) throws SQLException {        int[] colarr = ArrayUtil.getAdjustedColumnArray(index.getColumns(),            index.getVisibleColumns(), colindex, adjust);        if (colarr.length != index.getVisibleColumns()) {            return null;        }        return createIndexPrivate(colarr, index.getName(), index.isUnique());    }    /**     *  Method declaration     *     * @param  column     * @param  name     * @param  unique     * @return                Description of the Return Value     * @throws  SQLException     */    Index createIndexPrivate(int column[], HsqlName name,                             boolean unique) throws SQLException {        Trace.doAssert(iPrimaryKey != null, "createIndex");        int s = column.length;        int t = iPrimaryKey.length;        // The primary key field is added for non-unique indexes        // making all indexes unique        int col[]  = new int[unique ? s                                    : s + t];        int type[] = new int[unique ? s                                    : s + t];        for (int j = 0; j < s; j++) {            col[j]  = column[j];            type[j] = getColumn(col[j]).getType();        }        if (!unique) {            for (int j = 0; j < t; j++) {                col[s + j]  = iPrimaryKey[j];                type[s + j] = getColumn(iPrimaryKey[j]).getType();            }        }        // fredt - visible columns of index is 0 for system generated PK        if (col[0] == iVisibleColumns) {            s = 0;        }        Index newindex = new Index(name, this, col, type, unique, s);// fredt@users 20020225 - comment// in future we can avoid duplicate indexes/*        for (int i = 0; i < iIndexCount; i++) {            if ( newindex.isEquivalent(getIndex(i))){                return;            }        }*/        Trace.doAssert(isEmpty(), "createIndex");        vIndex.addElement(newindex);        iIndexCount++;        return newindex;    }// fredt@users 20020315 - patch 1.7.0 - drop index bug// don't drop an index used for a foreign key    /**     *  Checks for use of a named index in table constraints     *     * @param  indexname     * @param ignore null or a set of constraints that should be ignored in checks     * @throws  SQLException if index is used in a constraint     */    void checkDropIndex(String indexname,                        Hashtable ignore) throws SQLException {        Index index = this.getIndex(indexname);        if (index == null) {            throw Trace.error(Trace.INDEX_NOT_FOUND, indexname);        }        if (index.equals(getIndex(0))) {            throw Trace.error(Trace.DROP_PRIMARY_KEY, indexname);        }        for (int i = 0; i < vConstraint.size(); i++) {            Constraint c = (Constraint) vConstraint.elementAt(i);            if (ignore.get(c) != null) {                continue;            }            if (c.isIndexFK(index)) {                throw Trace.error(Trace.DROP_FK_INDEX, indexname);            }            if (c.isIndexUnique(index)) {                throw Trace.error(Trace.SYSTEM_INDEX, indexname);            }        }        return;    }    /**     *  Method declaration     *     * @return     */    boolean isEmpty() {        if (iIndexCount == 0) {            return true;        }        return getIndex(0).getRoot() == null;    }    /**     *  Method declaration     *     * @return     */    Object[] getNewRow() {        return new Object[iColumnCount];    }    /**     *  Method declaration     *     * @param  from     * @param  colindex index of the column that was added or removed     * @throws  SQLException normally for lack of resources     */    void moveData(Table from, int colindex, int adjust) throws SQLException {        Object colvalue = null;        if (adjust > 0) {            Column column = getColumn(colindex);            colvalue = Column.convertObject(column.getDefaultString(),                                            column.getType());        }        Index index = from.getPrimaryIndex();        Node  n     = index.first();        while (n != null) {            if (Trace.STOP) {                Trace.stop();            }            Object o[]      = n.getData();            Object newrow[] = this.getNewRow();            ArrayUtil.copyAdjustArray(o, newrow, colvalue, colindex, adjust);            insertNoCheck(newrow, null, false);            n = index.next(n);        }        index = from.getPrimaryIndex();        n     = index.first();        while (n != null) {            if (Trace.STOP) {                Trace.stop();            }            Node   nextnode = index.next(n);            Object o[]      = n.getData();            from.deleteNoCheck(o, null, false);            n = nextnode;        }    }    /**     *  Method declaration     *     * @param  col     * @param  deleted     * @param  inserted     * @throws  SQLException     */    void checkUpdate(int col[], Result deleted,                     Result inserted) throws SQLException {        Trace.check(!isReadOnly, Trace.DATA_IS_READONLY);        if (dDatabase.isReferentialIntegrity()) {            for (int i = 0; i < vConstraint.size(); i++) {                Constraint v = (Constraint) vConstraint.elementAt(i);                v.checkUpdate(col, deleted, inserted);            }        }    }    /**     *  Method declaration

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -