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;
}
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);
}
}
}
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());
}
}
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;
}
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();
}
Aggregations