Search in sources :

Example 6 with Index

use of com.wplatform.ddal.dbobject.index.Index in project jdbc-shards by wplatform.

the class Schema method removeChildrenAndResources.

@Override
public void removeChildrenAndResources(Session session) {
    // There can be dependencies between tables e.g. using computed columns,
    // so we might need to loop over them multiple times.
    boolean runLoopAgain = false;
    do {
        runLoopAgain = false;
        if (tablesAndViews != null) {
            // Loop over a copy because the map is modified underneath us.
            for (Table obj : New.arrayList(tablesAndViews.values())) {
                // in one go underneath us.
                if (obj.getName() != null) {
                    if (database.getDependentTable(obj, obj) == null) {
                        database.removeSchemaObject(session, obj);
                    } else {
                        runLoopAgain = true;
                    }
                }
            }
        }
    } while (runLoopAgain);
    while (indexes != null && indexes.size() > 0) {
        Index obj = (Index) indexes.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    while (sequences != null && sequences.size() > 0) {
        Sequence obj = (Sequence) sequences.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    while (constants != null && constants.size() > 0) {
        Constant obj = (Constant) constants.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    while (functions != null && functions.size() > 0) {
        FunctionAlias obj = (FunctionAlias) functions.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    owner = null;
    invalidate();
}
Also used : Table(com.wplatform.ddal.dbobject.table.Table) FunctionAlias(com.wplatform.ddal.dbobject.FunctionAlias) Index(com.wplatform.ddal.dbobject.index.Index)

Example 7 with Index

use of com.wplatform.ddal.dbobject.index.Index in project jdbc-shards by wplatform.

the class Database method removeSchemaObject.

/**
 * Remove an object from the system table.
 *
 * @param session the session
 * @param obj the object to be removed
 */
public synchronized void removeSchemaObject(Session session, SchemaObject obj) {
    int type = obj.getType();
    if (type == DbObject.TABLE_OR_VIEW) {
        Table table = (Table) obj;
        if (table.isTemporary() && !table.isGlobalTemporary()) {
            session.removeLocalTempTable(table);
            return;
        }
    } else if (type == DbObject.INDEX) {
        Index index = (Index) obj;
        Table table = index.getTable();
        if (table.isTemporary() && !table.isGlobalTemporary()) {
            session.removeLocalTempTableIndex(index);
            return;
        }
    }
    Comment comment = findComment(obj);
    if (comment != null) {
        removeDatabaseObject(session, comment);
    }
    obj.getSchema().remove(obj);
}
Also used : Comment(com.wplatform.ddal.dbobject.Comment) Table(com.wplatform.ddal.dbobject.table.Table) Index(com.wplatform.ddal.dbobject.index.Index)

Example 8 with Index

use of com.wplatform.ddal.dbobject.index.Index in project jdbc-shards by wplatform.

the class Table method getBestPlanItem.

/**
 * Get the best plan for the given search mask.
 *
 * @param session the session
 * @param masks per-column comparison bit masks, null means 'always false',
 *            see constants in IndexCondition
 * @param filter the table filter
 * @param sortOrder the sort order
 * @return the plan item
 */
public PlanItem getBestPlanItem(Session session, int[] masks, TableFilter filter, SortOrder sortOrder) {
    PlanItem item = new PlanItem();
    item.setIndex(getScanIndex(session));
    item.cost = item.getIndex().getCost(session, null, null, null);
    ArrayList<Index> indexes = getIndexes();
    if (indexes != null && masks != null) {
        for (int i = 1, size = indexes.size(); i < size; i++) {
            Index index = indexes.get(i);
            double cost = index.getCost(session, masks, filter, sortOrder);
            if (cost < item.cost) {
                item.cost = cost;
                item.setIndex(index);
            }
        }
    }
    return item;
}
Also used : Index(com.wplatform.ddal.dbobject.index.Index)

Example 9 with Index

use of com.wplatform.ddal.dbobject.index.Index in project jdbc-shards by wplatform.

the class TableMate method addIndex.

private Index addIndex(ArrayList<Column> list, IndexType indexType) {
    Column[] cols = new Column[list.size()];
    list.toArray(cols);
    Index index = new IndexMate(this, 0, null, IndexColumn.wrap(cols), indexType);
    indexes.add(index);
    return index;
}
Also used : RuleColumn(com.wplatform.ddal.dispatch.rule.RuleColumn) Index(com.wplatform.ddal.dbobject.index.Index) IndexMate(com.wplatform.ddal.dbobject.index.IndexMate)

Example 10 with Index

use of com.wplatform.ddal.dbobject.index.Index in project jdbc-shards by wplatform.

the class Parser method parseAlterTable.

private Prepared parseAlterTable() {
    Table table = readTableOrView();
    if (readIf("ADD")) {
        Prepared command = parseAlterTableAddConstraintIf(table.getName(), table.getSchema());
        if (command != null) {
            return command;
        }
        return parseAlterTableAddColumn(table);
    } else if (readIf("SET")) {
        read("REFERENTIAL_INTEGRITY");
        int type = CommandInterface.ALTER_TABLE_SET_REFERENTIAL_INTEGRITY;
        boolean value = readBooleanSetting();
        AlterTableSet command = new AlterTableSet(session, table.getSchema(), type, value);
        command.setTableName(table.getName());
        if (readIf("CHECK")) {
            command.setCheckExisting(true);
        } else if (readIf("NOCHECK")) {
            command.setCheckExisting(false);
        }
        return command;
    } else if (readIf("RENAME")) {
        read("TO");
        String newName = readIdentifierWithSchema(table.getSchema().getName());
        checkSchema(table.getSchema());
        AlterTableRename command = new AlterTableRename(session, getSchema());
        command.setOldTable(table);
        command.setNewTableName(newName);
        command.setHidden(readIf("HIDDEN"));
        return command;
    } else if (readIf("DROP")) {
        if (readIf("CONSTRAINT")) {
            boolean ifExists = readIfExists(false);
            String constraintName = readIdentifierWithSchema(table.getSchema().getName());
            ifExists = readIfExists(ifExists);
            checkSchema(table.getSchema());
            AlterTableDropConstraint command = new AlterTableDropConstraint(session, getSchema(), ifExists);
            command.setConstraintName(constraintName);
            return command;
        } else if (readIf("FOREIGN")) {
            // MySQL compatibility
            read("KEY");
            String constraintName = readIdentifierWithSchema(table.getSchema().getName());
            checkSchema(table.getSchema());
            AlterTableDropConstraint command = new AlterTableDropConstraint(session, getSchema(), false);
            command.setConstraintName(constraintName);
            return command;
        } else if (readIf("INDEX")) {
            // MySQL compatibility
            String indexName = readIdentifierWithSchema();
            DropIndex command = new DropIndex(session, getSchema());
            command.setIndexName(indexName);
            return command;
        } else if (readIf("PRIMARY")) {
            read("KEY");
            Index idx = table.getPrimaryKey();
            DropIndex command = new DropIndex(session, table.getSchema());
            command.setIndexName(idx.getName());
            return command;
        } else {
            readIf("COLUMN");
            boolean ifExists = readIfExists(false);
            AlterTableAlterColumn command = new AlterTableAlterColumn(session, table.getSchema());
            command.setType(CommandInterface.ALTER_TABLE_DROP_COLUMN);
            String columnName = readColumnIdentifier();
            command.setTable(table);
            if (ifExists && !table.doesColumnExist(columnName)) {
                return new NoOperation(session);
            }
            command.setOldColumn(table.getColumn(columnName));
            return command;
        }
    } else if (readIf("CHANGE")) {
        // MySQL compatibility
        readIf("COLUMN");
        String columnName = readColumnIdentifier();
        Column column = table.getColumn(columnName);
        String newColumnName = readColumnIdentifier();
        // new column type ignored. RENAME and MODIFY are
        // a single command in MySQL but two different commands in H2.
        parseColumnForTable(newColumnName, column.isNullable());
        AlterTableRenameColumn command = new AlterTableRenameColumn(session);
        command.setTable(table);
        command.setColumn(column);
        command.setNewColumnName(newColumnName);
        return command;
    } else if (readIf("MODIFY")) {
        // MySQL compatibility
        readIf("COLUMN");
        String columnName = readColumnIdentifier();
        Column column = table.getColumn(columnName);
        return parseAlterTableAlterColumnType(table, columnName, column);
    } else if (readIf("ALTER")) {
        readIf("COLUMN");
        String columnName = readColumnIdentifier();
        Column column = table.getColumn(columnName);
        if (readIf("RENAME")) {
            read("TO");
            AlterTableRenameColumn command = new AlterTableRenameColumn(session);
            command.setTable(table);
            command.setColumn(column);
            String newName = readColumnIdentifier();
            command.setNewColumnName(newName);
            return command;
        } else if (readIf("DROP")) {
            // PostgreSQL compatibility
            if (readIf("DEFAULT")) {
                AlterTableAlterColumn command = new AlterTableAlterColumn(session, table.getSchema());
                command.setTable(table);
                command.setOldColumn(column);
                command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT);
                command.setDefaultExpression(null);
                return command;
            }
            read("NOT");
            read("NULL");
            AlterTableAlterColumn command = new AlterTableAlterColumn(session, table.getSchema());
            command.setTable(table);
            command.setOldColumn(column);
            command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
            return command;
        } else if (readIf("TYPE")) {
            // PostgreSQL compatibility
            return parseAlterTableAlterColumnType(table, columnName, column);
        } else if (readIf("SET")) {
            if (readIf("DATA")) {
                // Derby compatibility
                read("TYPE");
                return parseAlterTableAlterColumnType(table, columnName, column);
            }
            AlterTableAlterColumn command = new AlterTableAlterColumn(session, table.getSchema());
            command.setTable(table);
            command.setOldColumn(column);
            if (readIf("NULL")) {
                command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
                return command;
            } else if (readIf("NOT")) {
                read("NULL");
                command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL);
                return command;
            } else if (readIf("DEFAULT")) {
                Expression defaultExpression = readExpression();
                command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT);
                command.setDefaultExpression(defaultExpression);
                return command;
            }
        } else if (readIf("RESTART")) {
            readIf("WITH");
            Expression start = readExpression();
            AlterSequence command = new AlterSequence(session, table.getSchema());
            command.setColumn(column);
            command.setStartWith(start);
            return command;
        } else if (readIf("SELECTIVITY")) {
            AlterTableAlterColumn command = new AlterTableAlterColumn(session, table.getSchema());
            command.setTable(table);
            command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY);
            command.setOldColumn(column);
            command.setSelectivity(readExpression());
            return command;
        } else {
            return parseAlterTableAlterColumnType(table, columnName, column);
        }
    }
    throw getSyntaxError();
}
Also used : Index(com.wplatform.ddal.dbobject.index.Index)

Aggregations

Index (com.wplatform.ddal.dbobject.index.Index)12 Table (com.wplatform.ddal.dbobject.table.Table)4 Column (com.wplatform.ddal.dbobject.table.Column)3 Expression (com.wplatform.ddal.command.expression.Expression)2 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)2 Comment (com.wplatform.ddal.dbobject.Comment)1 DbObject (com.wplatform.ddal.dbobject.DbObject)1 FunctionAlias (com.wplatform.ddal.dbobject.FunctionAlias)1 Right (com.wplatform.ddal.dbobject.Right)1 IndexMate (com.wplatform.ddal.dbobject.index.IndexMate)1 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)1 TableMate (com.wplatform.ddal.dbobject.table.TableMate)1 RuleColumn (com.wplatform.ddal.dispatch.rule.RuleColumn)1