Search in sources :

Example 21 with Trigger

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

the class DropTrigger method update.

@Override
public int update() {
    session.commit(true);
    Database db = session.getDatabase();
    TriggerObject trigger = getSchema().findTrigger(triggerName);
    if (trigger == null) {
        if (!ifExists) {
            throw DbException.get(ErrorCode.TRIGGER_NOT_FOUND_1, triggerName);
        }
    } else {
        Table table = trigger.getTable();
        session.getUser().checkRight(table, Right.ALL);
        db.removeSchemaObject(session, trigger);
    }
    return 0;
}
Also used : Table(org.h2.table.Table) Database(org.h2.engine.Database) TriggerObject(org.h2.schema.TriggerObject)

Example 22 with Trigger

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

the class FullText method removeAllTriggers.

/**
 * Remove all triggers that start with the given prefix.
 *
 * @param conn the database connection
 * @param prefix the prefix
 */
protected static void removeAllTriggers(Connection conn, String prefix) throws SQLException {
    Statement stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("SELECT * FROM INFORMATION_SCHEMA.TRIGGERS");
    Statement stat2 = conn.createStatement();
    while (rs.next()) {
        String schema = rs.getString("TRIGGER_SCHEMA");
        String name = rs.getString("TRIGGER_NAME");
        if (name.startsWith(prefix)) {
            name = StringUtils.quoteIdentifier(schema) + "." + StringUtils.quoteIdentifier(name);
            stat2.execute("DROP TRIGGER " + name);
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 23 with Trigger

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

the class TriggerObject method load.

private synchronized void load() {
    if (triggerCallback != null) {
        return;
    }
    try {
        Session sysSession = database.getSystemSession();
        Connection c2 = sysSession.createConnection(false);
        Object obj;
        if (triggerClassName != null) {
            obj = JdbcUtils.loadUserClass(triggerClassName).newInstance();
        } else {
            obj = loadFromSource();
        }
        triggerCallback = (Trigger) obj;
        triggerCallback.init(c2, getSchema().getName(), getName(), table.getName(), before, typeMask);
    } catch (Throwable e) {
        // try again later
        triggerCallback = null;
        throw DbException.get(ErrorCode.ERROR_CREATING_TRIGGER_OBJECT_3, e, getName(), triggerClassName != null ? triggerClassName : "..source..", e.toString());
    }
}
Also used : Connection(java.sql.Connection) DbObject(org.h2.engine.DbObject) Session(org.h2.engine.Session)

Example 24 with Trigger

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

the class TriggerObject method fireRow.

/**
 * Call the fire method of the user-defined trigger class if required. This
 * method does nothing if the trigger is not defined for the given action.
 * This method is called before or after a row is processed, possibly many
 * times for each statement.
 *
 * @param session the session
 * @param table the table
 * @param oldRow the old row
 * @param newRow the new row
 * @param beforeAction true if this method is called before the operation is
 *            applied
 * @param rollback when the operation occurred within a rollback
 * @return true if no further action is required (for 'instead of' triggers)
 */
public boolean fireRow(Session session, Table table, Row oldRow, Row newRow, boolean beforeAction, boolean rollback) {
    if (!rowBased || before != beforeAction) {
        return false;
    }
    if (rollback && !onRollback) {
        return false;
    }
    load();
    Object[] oldList;
    Object[] newList;
    boolean fire = false;
    if ((typeMask & Trigger.INSERT) != 0) {
        if (oldRow == null && newRow != null) {
            fire = true;
        }
    }
    if ((typeMask & Trigger.UPDATE) != 0) {
        if (oldRow != null && newRow != null) {
            fire = true;
        }
    }
    if ((typeMask & Trigger.DELETE) != 0) {
        if (oldRow != null && newRow == null) {
            fire = true;
        }
    }
    if (!fire) {
        return false;
    }
    oldList = convertToObjectList(oldRow);
    newList = convertToObjectList(newRow);
    Object[] newListBackup;
    if (before && newList != null) {
        newListBackup = Arrays.copyOf(newList, newList.length);
    } else {
        newListBackup = null;
    }
    Connection c2 = session.createConnection(false);
    boolean old = session.getAutoCommit();
    boolean oldDisabled = session.setCommitOrRollbackDisabled(true);
    Value identity = session.getLastScopeIdentity();
    try {
        session.setAutoCommit(false);
        triggerCallback.fire(c2, oldList, newList);
        if (newListBackup != null) {
            for (int i = 0; i < newList.length; i++) {
                Object o = newList[i];
                if (o != newListBackup[i]) {
                    Value v = DataType.convertToValue(session, o, Value.UNKNOWN);
                    session.getGeneratedKeys().add(table.getColumn(i));
                    newRow.setValue(i, v);
                }
            }
        }
    } catch (Exception e) {
        if (onRollback) {
        // ignore
        } else {
            throw DbException.convert(e);
        }
    } finally {
        if (session.getLastTriggerIdentity() != null) {
            session.setLastScopeIdentity(session.getLastTriggerIdentity());
            session.setLastTriggerIdentity(null);
        } else {
            session.setLastScopeIdentity(identity);
        }
        session.setCommitOrRollbackDisabled(oldDisabled);
        session.setAutoCommit(old);
    }
    return insteadOf;
}
Also used : Connection(java.sql.Connection) Value(org.h2.value.Value) DbObject(org.h2.engine.DbObject) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 25 with Trigger

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

the class TestTriggersConstraints method testTriggerAdapter.

private void testTriggerAdapter() throws SQLException {
    Connection conn;
    Statement stat;
    conn = getConnection("trigger");
    stat = conn.createStatement();
    stat.execute("drop table if exists test");
    stat.execute("create table test(id int, c clob, b blob)");
    stat.execute("create table message(name varchar)");
    stat.execute("create trigger test_insert before insert, update, delete on test " + "for each row call \"" + TestTriggerAdapter.class.getName() + "\"");
    stat.execute("insert into test values(1, 'hello', 'abcd')");
    ResultSet rs;
    rs = stat.executeQuery("select * from test");
    rs.next();
    assertEquals(10, rs.getInt(1));
    stat.execute("update test set id = 2");
    rs = stat.executeQuery("select * from test");
    rs.next();
    assertEquals(20, rs.getInt(1));
    stat.execute("delete from test");
    rs = stat.executeQuery("select * from message");
    assertTrue(rs.next());
    assertEquals("+1;", rs.getString(1));
    assertTrue(rs.next());
    assertEquals("-10;+2;", rs.getString(1));
    assertTrue(rs.next());
    assertEquals("-20;", rs.getString(1));
    assertFalse(rs.next());
    stat.execute("drop table test, message");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

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