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

📄 tableworks.java

📁 一个用java写的开源的数据库系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param  col     * @param  name     * @param  unique     * @return  new index     * @throws  SQLException normally for lack of resources     */    Index createIndex(int col[], HsqlName name,                      boolean unique) throws SQLException {        if (table.isEmpty()) {            return table.createIndexPrivate(col, name, unique);        }        Table tn = table.moveDefinition(null, null, table.getColumnCount(),                                        0);        Index newindex = tn.createIndexPrivate(col, name, unique);        tn.moveData(table, table.getColumnCount(), 0);        tn.updateConstraints(table, table.getColumnCount(), 0);        int index = table.dDatabase.getTableIndex(table);        table.dDatabase.getTables().setElementAt(tn, index);        table = tn;        return newindex;    }// fredt@users 20020225 - avoid duplicate constraints    /**     *  A unique constraint relies on a unique indexe on the table. It can     *  cover a single column or multiple columns.     *  <p>     *  All unique constraint names are generated by Database.java as unique     *  within the database. Duplicate constraints (more than one unique     *  constriant on the same set of columns are still allowed but the     *  names will be different. (fredt@users)     *     * @param  col     * @param  name     * @throws  SQLException     */    void createUniqueConstraint(int[] col,                                HsqlName name) throws SQLException {        Vector constraints = table.getConstraints();        for (int i = 0; i < constraints.size(); i++) {            Constraint c = (Constraint) constraints.elementAt(i);            if (c.isEquivalent(col, Constraint.UNIQUE)                    || c.getName().name.equals(name.name)) {                throw Trace.error(Trace.CONSTRAINT_ALREADY_EXISTS);            }        }        // create an autonamed index        HsqlName   indexname     = HsqlName.makeAutoName("IDX");        Index      index         = createIndex(col, indexname, true);        Constraint newconstraint = new Constraint(name, table, index);        table.addConstraint(newconstraint);    }// fredt@users 20020315 - patch 1.7.0 - drop index bug    /**     *  Because of the way indexes and column data are held in memory and     *  on disk, it is necessary to recreate the table when an index is added     *  to a non-empty table.<p>     *  Originally, this method would break existing foreign keys as the     *  table order in the DB was changed. The new table is now linked     *  in place of the old table (fredt@users)     *     * @param  indexname     * @throws  SQLException     */    void dropIndex(String indexname) throws SQLException {        Table tn = table.moveDefinition(indexname, null,                                        table.getColumnCount(), 0);        tn.moveData(table, table.getColumnCount(), 0);        tn.updateConstraints(table, table.getColumnCount(), 0);        int i = table.dDatabase.getTableIndex(table);        table.dDatabase.getTables().setElementAt(tn, i);        table = tn;    }    /**     *     * @param  column     * @param  colindex     * @param  adjust +1 or -1     * @throws  SQLException     */    void addOrDropColumn(Column column, int colindex,                         int adjust) throws SQLException {        Table tn = table.moveDefinition(null, column, colindex, adjust);        tn.moveData(table, colindex, adjust);        tn.updateConstraints(table, colindex, adjust);        int i = table.dDatabase.getTableIndex(table);        table.dDatabase.getTables().setElementAt(tn, i);        table = tn;    }    /**     *  Method declaration     *     */    void dropConstraint(String name) throws SQLException {        int        j    = table.getConstraintIndex(name);        Constraint c    = table.getConstraint(name);        Hashtable  cmap = new Hashtable();        cmap.put(c, c);        if (c == null) {            throw Trace.error(Trace.CONSTRAINT_NOT_FOUND,                              name + " in table: " + table.getName().name);        }        if (c.getType() == c.MAIN) {            throw Trace.error(Trace.DROP_SYSTEM_CONSTRAINT);        }        if (c.getType() == c.FOREIGN_KEY) {            Table mainTable = c.getMain();            Constraint mainConstraint =                mainTable.getConstraint(c.getPkName());            cmap.put(mainConstraint, mainConstraint);            int   k         = mainTable.getConstraintIndex(c.getPkName());            Index mainIndex = mainConstraint.getMainIndex();            // never drop user defined indexes            // fredt - todo - use of auto main indexes for FK's will be            // deprecated so that there is no need to drop an index on the            // main (pk) table            if (mainIndex.getName().isReservedName()) {                boolean candrop = false;                try {                    // check if the index is used by other constraints otherwise drop                    mainTable.checkDropIndex(mainIndex.getName().name, cmap);                    candrop = true;                    TableWorks tw = new TableWorks(mainTable);                    tw.dropIndex(mainIndex.getName().name);                    // update this.table if self referencing FK                    if (mainTable == table) {                        table = tw.getTable();                    }                } catch (SQLException e) {                    if (candrop) {                        throw e;                    }                }            }            // drop the reference index if automatic and unused elsewhere            Index refIndex = c.getRefIndex();            if (refIndex.getName().isReservedName()) {                try {                    // check if the index is used by other constraints otherwise drop                    table.checkDropIndex(refIndex.getName().name, cmap);                    dropIndex(refIndex.getName().name);                } catch (SQLException e) {}            }            mainTable.vConstraint.removeElementAt(k);            table.vConstraint.removeElementAt(j);        } else if (c.getType() == c.UNIQUE) {            // throw if the index for unique constraint is shared            table.checkDropIndex(c.getMainIndex().getName().name, cmap);            // all is well if dropIndex throws for lack of resources            dropIndex(c.getMainIndex().getName().name);            table.vConstraint.removeElementAt(j);        }    }}

⌨️ 快捷键说明

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