Search in sources :

Example 16 with Trigger

use of org.h2.api.Trigger in project h2database by h2database.

the class Database method close.

/**
 * Close the database.
 *
 * @param fromShutdownHook true if this method is called from the shutdown
 *            hook
 */
void close(boolean fromShutdownHook) {
    try {
        synchronized (this) {
            if (closing) {
                return;
            }
            throwLastBackgroundException();
            if (fileLockMethod == FileLockMethod.SERIALIZED && !reconnectChangePending) {
                // another connection may have written something - don't write
                try {
                    closeOpenFilesAndUnlock(false);
                } catch (DbException e) {
                // ignore
                }
                traceSystem.close();
                return;
            }
            closing = true;
            stopServer();
            if (!userSessions.isEmpty()) {
                if (!fromShutdownHook) {
                    return;
                }
                trace.info("closing {0} from shutdown hook", databaseName);
                closeAllSessionsException(null);
            }
            trace.info("closing {0}", databaseName);
            if (eventListener != null) {
                // allow the event listener to connect to the database
                closing = false;
                DatabaseEventListener e = eventListener;
                // set it to null, to make sure it's called only once
                eventListener = null;
                e.closingDatabase();
                if (!userSessions.isEmpty()) {
                    // if a connection was opened, we can't close the database
                    return;
                }
                closing = true;
            }
        }
        if (!this.isReadOnly()) {
            removeOrphanedLobs();
        }
        try {
            if (systemSession != null) {
                if (powerOffCount != -1) {
                    for (Table table : getAllTablesAndViews(false)) {
                        if (table.isGlobalTemporary()) {
                            table.removeChildrenAndResources(systemSession);
                        } else {
                            table.close(systemSession);
                        }
                    }
                    for (SchemaObject obj : getAllSchemaObjects(DbObject.SEQUENCE)) {
                        Sequence sequence = (Sequence) obj;
                        sequence.close();
                    }
                }
                for (SchemaObject obj : getAllSchemaObjects(DbObject.TRIGGER)) {
                    TriggerObject trigger = (TriggerObject) obj;
                    try {
                        trigger.close();
                    } catch (SQLException e) {
                        trace.error(e, "close");
                    }
                }
                if (powerOffCount != -1) {
                    meta.close(systemSession);
                    systemSession.commit(true);
                }
            }
        } catch (DbException e) {
            trace.error(e, "close");
        }
        tempFileDeleter.deleteAll();
        try {
            closeOpenFilesAndUnlock(true);
        } catch (DbException e) {
            trace.error(e, "close");
        }
        trace.info("closed");
        traceSystem.close();
        if (closeOnExit != null) {
            closeOnExit.reset();
            try {
                Runtime.getRuntime().removeShutdownHook(closeOnExit);
            } catch (IllegalStateException e) {
            // ignore
            } catch (SecurityException e) {
            // applets may not do that - ignore
            }
            closeOnExit = null;
        }
        if (deleteFilesOnDisconnect && persistent) {
            deleteFilesOnDisconnect = false;
            try {
                String directory = FileUtils.getParent(databaseName);
                String name = FileUtils.getName(databaseName);
                DeleteDbFiles.execute(directory, name, true);
            } catch (Exception e) {
            // ignore (the trace is closed already)
            }
        }
    } finally {
        Engine.getInstance().close(databaseName);
    }
}
Also used : SchemaObject(org.h2.schema.SchemaObject) DatabaseEventListener(org.h2.api.DatabaseEventListener) MetaTable(org.h2.table.MetaTable) Table(org.h2.table.Table) SQLException(java.sql.SQLException) TriggerObject(org.h2.schema.TriggerObject) Sequence(org.h2.schema.Sequence) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException) IOException(java.io.IOException) DbException(org.h2.message.DbException)

Example 17 with Trigger

use of org.h2.api.Trigger in project h2database by h2database.

the class Table method removeChildrenAndResources.

@Override
public void removeChildrenAndResources(Session session) {
    while (!dependentViews.isEmpty()) {
        TableView view = dependentViews.get(0);
        dependentViews.remove(0);
        database.removeSchemaObject(session, view);
    }
    while (synonyms != null && !synonyms.isEmpty()) {
        TableSynonym synonym = synonyms.remove(0);
        database.removeSchemaObject(session, synonym);
    }
    while (triggers != null && !triggers.isEmpty()) {
        TriggerObject trigger = triggers.remove(0);
        database.removeSchemaObject(session, trigger);
    }
    while (constraints != null && !constraints.isEmpty()) {
        Constraint constraint = constraints.remove(0);
        database.removeSchemaObject(session, constraint);
    }
    for (Right right : database.getAllRights()) {
        if (right.getGrantedObject() == this) {
            database.removeDatabaseObject(session, right);
        }
    }
    database.removeMeta(session, getId());
    // before removing the table object)
    while (sequences != null && !sequences.isEmpty()) {
        Sequence sequence = sequences.remove(0);
        // this is possible when calling ALTER TABLE ALTER COLUMN
        if (database.getDependentTable(sequence, this) == null) {
            database.removeSchemaObject(session, sequence);
        }
    }
}
Also used : Constraint(org.h2.constraint.Constraint) Right(org.h2.engine.Right) TriggerObject(org.h2.schema.TriggerObject) Sequence(org.h2.schema.Sequence)

Example 18 with Trigger

use of org.h2.api.Trigger in project h2database by h2database.

the class Parser method parseComment.

private Prepared parseComment() {
    int type = 0;
    read("ON");
    boolean column = false;
    if (readIf("TABLE") || readIf("VIEW")) {
        type = DbObject.TABLE_OR_VIEW;
    } else if (readIf("COLUMN")) {
        column = true;
        type = DbObject.TABLE_OR_VIEW;
    } else if (readIf("CONSTANT")) {
        type = DbObject.CONSTANT;
    } else if (readIf("CONSTRAINT")) {
        type = DbObject.CONSTRAINT;
    } else if (readIf("ALIAS")) {
        type = DbObject.FUNCTION_ALIAS;
    } else if (readIf("INDEX")) {
        type = DbObject.INDEX;
    } else if (readIf("ROLE")) {
        type = DbObject.ROLE;
    } else if (readIf("SCHEMA")) {
        type = DbObject.SCHEMA;
    } else if (readIf("SEQUENCE")) {
        type = DbObject.SEQUENCE;
    } else if (readIf("TRIGGER")) {
        type = DbObject.TRIGGER;
    } else if (readIf("USER")) {
        type = DbObject.USER;
    } else if (readIf("DOMAIN")) {
        type = DbObject.USER_DATATYPE;
    } else {
        throw getSyntaxError();
    }
    SetComment command = new SetComment(session);
    String objectName;
    if (column) {
        // can't use readIdentifierWithSchema() because
        // it would not read schema.table.column correctly
        // if the db name is equal to the schema name
        ArrayList<String> list = New.arrayList();
        do {
            list.add(readUniqueIdentifier());
        } while (readIf("."));
        schemaName = session.getCurrentSchemaName();
        if (list.size() == 4) {
            if (!equalsToken(database.getShortName(), list.remove(0))) {
                throw DbException.getSyntaxError(sqlCommand, parseIndex, "database name");
            }
        }
        if (list.size() == 3) {
            schemaName = list.remove(0);
        }
        if (list.size() != 2) {
            throw DbException.getSyntaxError(sqlCommand, parseIndex, "table.column");
        }
        objectName = list.get(0);
        command.setColumn(true);
        command.setColumnName(list.get(1));
    } else {
        objectName = readIdentifierWithSchema();
    }
    command.setSchemaName(schemaName);
    command.setObjectName(objectName);
    command.setObjectType(type);
    read("IS");
    command.setCommentExpression(readExpression());
    return command;
}
Also used : SetComment(org.h2.command.ddl.SetComment) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 19 with Trigger

use of org.h2.api.Trigger 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 20 with Trigger

use of org.h2.api.Trigger in project h2database by h2database.

the class CreateTrigger method update.

@Override
public int update() {
    session.commit(true);
    Database db = session.getDatabase();
    if (getSchema().findTrigger(triggerName) != null) {
        if (ifNotExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.TRIGGER_ALREADY_EXISTS_1, triggerName);
    }
    if ((typeMask & Trigger.SELECT) == Trigger.SELECT && rowBased) {
        throw DbException.get(ErrorCode.TRIGGER_SELECT_AND_ROW_BASED_NOT_SUPPORTED, triggerName);
    }
    int id = getObjectId();
    Table table = getSchema().getTableOrView(session, tableName);
    TriggerObject trigger = new TriggerObject(getSchema(), id, triggerName, table);
    trigger.setInsteadOf(insteadOf);
    trigger.setBefore(before);
    trigger.setNoWait(noWait);
    trigger.setQueueSize(queueSize);
    trigger.setRowBased(rowBased);
    trigger.setTypeMask(typeMask);
    trigger.setOnRollback(onRollback);
    if (this.triggerClassName != null) {
        trigger.setTriggerClassName(triggerClassName, force);
    } else {
        trigger.setTriggerSource(triggerSource, force);
    }
    db.addSchemaObject(session, trigger);
    table.addTrigger(trigger);
    return 0;
}
Also used : Table(org.h2.table.Table) Database(org.h2.engine.Database) TriggerObject(org.h2.schema.TriggerObject)

Aggregations

Statement (java.sql.Statement)20 Connection (java.sql.Connection)17 PreparedStatement (java.sql.PreparedStatement)16 JdbcConnection (org.h2.jdbc.JdbcConnection)14 ResultSet (java.sql.ResultSet)13 SimpleResultSet (org.h2.tools.SimpleResultSet)8 SQLException (java.sql.SQLException)7 TriggerObject (org.h2.schema.TriggerObject)6 IOException (java.io.IOException)5 ValueString (org.h2.value.ValueString)5 DbObject (org.h2.engine.DbObject)4 Session (org.h2.engine.Session)4 DbException (org.h2.message.DbException)4 Sequence (org.h2.schema.Sequence)4 Constraint (org.h2.constraint.Constraint)3 Schema (org.h2.schema.Schema)3 Table (org.h2.table.Table)3 Analyzer (org.apache.lucene.analysis.Analyzer)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 Document (org.apache.lucene.document.Document)2