Search in sources :

Example 6 with DropIndex

use of org.h2.command.ddl.DropIndex in project elastic-core-maven by OrdinaryDude.

the class FullTextTrigger method dropIndex.

/**
 * Drop the fulltext index for a table
 *
 * @param   conn                SQL connection
 * @param   schema              Schema name
 * @param   table               Table name
 * @throws  SQLException        Unable to drop fulltext index
 */
public static void dropIndex(Connection conn, String schema, String table) throws SQLException {
    String upperSchema = schema.toUpperCase();
    String upperTable = table.toUpperCase();
    boolean reindex = false;
    // 
    try (Statement qstmt = conn.createStatement();
        Statement stmt = conn.createStatement()) {
        try (ResultSet rs = qstmt.executeQuery(String.format("SELECT COLUMNS FROM FTL.INDEXES WHERE SCHEMA = '%s' AND TABLE = '%s'", upperSchema, upperTable))) {
            if (rs.next()) {
                stmt.execute("DROP TRIGGER IF EXISTS FTL_" + upperTable);
                stmt.execute(String.format("DELETE FROM FTL.INDEXES WHERE SCHEMA = '%s' AND TABLE = '%s'", upperSchema, upperTable));
                reindex = true;
            }
        }
    }
    // 
    if (reindex) {
        reindex(conn);
    }
}
Also used : Statement(java.sql.Statement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 7 with DropIndex

use of org.h2.command.ddl.DropIndex in project h2database by h2database.

the class Parser method parseDrop.

private Prepared parseDrop() {
    if (readIf("TABLE")) {
        boolean ifExists = readIfExists(false);
        String tableName = readIdentifierWithSchema();
        DropTable command = new DropTable(session, getSchema());
        command.setTableName(tableName);
        while (readIf(",")) {
            tableName = readIdentifierWithSchema();
            DropTable next = new DropTable(session, getSchema());
            next.setTableName(tableName);
            command.addNextDropTable(next);
        }
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
            readIf("CONSTRAINTS");
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        } else if (readIf("IGNORE")) {
            command.setDropAction(ConstraintActionType.SET_DEFAULT);
        }
        return command;
    } else if (readIf("INDEX")) {
        boolean ifExists = readIfExists(false);
        String indexName = readIdentifierWithSchema();
        DropIndex command = new DropIndex(session, getSchema());
        command.setIndexName(indexName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        // Support for MySQL: DROP INDEX index_name ON tbl_name
        if (readIf("ON")) {
            readIdentifierWithSchema();
        }
        return command;
    } else if (readIf("USER")) {
        boolean ifExists = readIfExists(false);
        DropUser command = new DropUser(session);
        command.setUserName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        readIf("CASCADE");
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SEQUENCE")) {
        boolean ifExists = readIfExists(false);
        String sequenceName = readIdentifierWithSchema();
        DropSequence command = new DropSequence(session, getSchema());
        command.setSequenceName(sequenceName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("CONSTANT")) {
        boolean ifExists = readIfExists(false);
        String constantName = readIdentifierWithSchema();
        DropConstant command = new DropConstant(session, getSchema());
        command.setConstantName(constantName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("TRIGGER")) {
        boolean ifExists = readIfExists(false);
        String triggerName = readIdentifierWithSchema();
        DropTrigger command = new DropTrigger(session, getSchema());
        command.setTriggerName(triggerName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("VIEW")) {
        boolean ifExists = readIfExists(false);
        String viewName = readIdentifierWithSchema();
        DropView command = new DropView(session, getSchema());
        command.setViewName(viewName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        ConstraintActionType dropAction = parseCascadeOrRestrict();
        if (dropAction != null) {
            command.setDropAction(dropAction);
        }
        return command;
    } else if (readIf("ROLE")) {
        boolean ifExists = readIfExists(false);
        DropRole command = new DropRole(session);
        command.setRoleName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("ALIAS")) {
        boolean ifExists = readIfExists(false);
        String aliasName = readIdentifierWithSchema();
        DropFunctionAlias command = new DropFunctionAlias(session, getSchema());
        command.setAliasName(aliasName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SCHEMA")) {
        boolean ifExists = readIfExists(false);
        DropSchema command = new DropSchema(session);
        command.setSchemaName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        }
        return command;
    } else if (readIf("ALL")) {
        read("OBJECTS");
        DropDatabase command = new DropDatabase(session);
        command.setDropAllObjects(true);
        if (readIf("DELETE")) {
            read("FILES");
            command.setDeleteFiles(true);
        }
        return command;
    } else if (readIf("DOMAIN")) {
        return parseDropUserDataType();
    } else if (readIf("TYPE")) {
        return parseDropUserDataType();
    } else if (readIf("DATATYPE")) {
        return parseDropUserDataType();
    } else if (readIf("AGGREGATE")) {
        return parseDropAggregate();
    } else if (readIf("SYNONYM")) {
        boolean ifExists = readIfExists(false);
        String synonymName = readIdentifierWithSchema();
        DropSynonym command = new DropSynonym(session, getSchema());
        command.setSynonymName(synonymName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    }
    throw getSyntaxError();
}
Also used : DropConstant(org.h2.command.ddl.DropConstant) DropTrigger(org.h2.command.ddl.DropTrigger) DropView(org.h2.command.ddl.DropView) DropUser(org.h2.command.ddl.DropUser) DropSynonym(org.h2.command.ddl.DropSynonym) DropSequence(org.h2.command.ddl.DropSequence) ValueString(org.h2.value.ValueString) DropTable(org.h2.command.ddl.DropTable) DropSchema(org.h2.command.ddl.DropSchema) DropRole(org.h2.command.ddl.DropRole) DropFunctionAlias(org.h2.command.ddl.DropFunctionAlias) DropDatabase(org.h2.command.ddl.DropDatabase) ConstraintActionType(org.h2.constraint.ConstraintActionType) DropIndex(org.h2.command.ddl.DropIndex)

Example 8 with DropIndex

use of org.h2.command.ddl.DropIndex in project h2database by h2database.

the class FullText method dropIndex.

/**
 * Drop an existing full text index for a table. This method returns
 * silently if no index for this table exists.
 *
 * @param conn the connection
 * @param schema the schema name of the table (case sensitive)
 * @param table the table name (case sensitive)
 */
public static void dropIndex(Connection conn, String schema, String table) throws SQLException {
    init(conn);
    PreparedStatement prep = conn.prepareStatement("SELECT ID FROM " + SCHEMA + ".INDEXES WHERE SCHEMA=? AND TABLE=?");
    prep.setString(1, schema);
    prep.setString(2, table);
    ResultSet rs = prep.executeQuery();
    if (!rs.next()) {
        return;
    }
    int indexId = rs.getInt(1);
    prep = conn.prepareStatement("DELETE FROM " + SCHEMA + ".INDEXES WHERE ID=?");
    prep.setInt(1, indexId);
    prep.execute();
    createOrDropTrigger(conn, schema, table, false);
    prep = conn.prepareStatement("DELETE FROM " + SCHEMA + ".ROWS WHERE INDEXID=? AND ROWNUM<10000");
    while (true) {
        prep.setInt(1, indexId);
        int deleted = prep.executeUpdate();
        if (deleted == 0) {
            break;
        }
    }
    prep = conn.prepareStatement("DELETE FROM " + SCHEMA + ".MAP " + "WHERE NOT EXISTS (SELECT * FROM " + SCHEMA + ".ROWS R WHERE R.ID=ROWID) AND ROWID<10000");
    while (true) {
        int deleted = prep.executeUpdate();
        if (deleted == 0) {
            break;
        }
    }
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

ResultSet (java.sql.ResultSet)6 SimpleResultSet (org.h2.tools.SimpleResultSet)6 PreparedStatement (java.sql.PreparedStatement)4 Statement (java.sql.Statement)4 DropIndex (org.h2.command.ddl.DropIndex)2 DropSchema (org.h2.command.ddl.DropSchema)2 DropTable (org.h2.command.ddl.DropTable)2 ValueString (org.h2.value.ValueString)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)1 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)1 AlterTableRename (org.h2.command.ddl.AlterTableRename)1 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)1 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)1 CreateIndex (org.h2.command.ddl.CreateIndex)1 CreateLinkedTable (org.h2.command.ddl.CreateLinkedTable)1 CreateSchema (org.h2.command.ddl.CreateSchema)1 CreateTable (org.h2.command.ddl.CreateTable)1 DropConstant (org.h2.command.ddl.DropConstant)1