| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.api; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.SQLException; |
| 10 |
|
| 11 |
/** |
| 12 |
* A class that implements this interface can be used as a trigger. |
| 13 |
*/ |
| 14 |
public interface Trigger { |
| 15 |
|
| 16 |
/** |
| 17 |
* The trigger is called for INSERT statements. |
| 18 |
*/ |
| 19 |
int INSERT = 1; |
| 20 |
|
| 21 |
/** |
| 22 |
* The trigger is called for UPDATE statements. |
| 23 |
*/ |
| 24 |
int UPDATE = 2; |
| 25 |
|
| 26 |
/** |
| 27 |
* The trigger is called for DELETE statements. |
| 28 |
*/ |
| 29 |
int DELETE = 4; |
| 30 |
|
| 31 |
/** |
| 32 |
* The trigger is called for SELECT statements. |
| 33 |
*/ |
| 34 |
int SELECT = 8; |
| 35 |
|
| 36 |
/** |
| 37 |
* This method is called by the database engine once when initializing the |
| 38 |
* trigger. It is called when the trigger is created, as well as when the |
| 39 |
* database is opened. The type of operation is a bit field with the |
| 40 |
* appropriate flags set. As an example, if the trigger is of type INSERT |
| 41 |
* and UPDATE, then the parameter type is set to (INSERT | UPDATE). |
| 42 |
* |
| 43 |
* @param conn a connection to the database (a system connection) |
| 44 |
* @param schemaName the name of the schema |
| 45 |
* @param triggerName the name of the trigger used in the CREATE TRIGGER |
| 46 |
* statement |
| 47 |
* @param tableName the name of the table |
| 48 |
* @param before whether the fire method is called before or after the |
| 49 |
* operation is performed |
| 50 |
* @param type the operation type: INSERT, UPDATE, DELETE, SELECT, or a |
| 51 |
* combination (this parameter is a bit field) |
| 52 |
* @throws SQLException on SQL exception |
| 53 |
*/ |
| 54 |
default void init(Connection conn, String schemaName, String triggerName, |
| 55 |
String tableName, boolean before, int type) throws SQLException { |
| 56 |
// Does nothing by default |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* This method is called for each triggered action. The method is called |
| 61 |
* immediately when the operation occurred (before it is committed). A |
| 62 |
* transaction rollback will also rollback the operations that were done |
| 63 |
* within the trigger, if the operations occurred within the same database. |
| 64 |
* If the trigger changes state outside the database, a rollback trigger |
| 65 |
* should be used. |
| 66 |
* <p> |
| 67 |
* The row arrays contain all columns of the table, in the same order |
| 68 |
* as defined in the table. |
| 69 |
* </p> |
| 70 |
* <p> |
| 71 |
* The trigger itself may change the data in the newRow array. |
| 72 |
* </p> |
| 73 |
* |
| 74 |
* @param conn a connection to the database |
| 75 |
* @param oldRow the old row, or null if no old row is available (for |
| 76 |
* INSERT) |
| 77 |
* @param newRow the new row, or null if no new row is available (for |
| 78 |
* DELETE) |
| 79 |
* @throws SQLException if the operation must be undone |
| 80 |
*/ |
| 81 |
void fire(Connection conn, Object[] oldRow, Object[] newRow) |
| 82 |
throws SQLException; |
| 83 |
|
| 84 |
/** |
| 85 |
* This method is called when the database is closed. |
| 86 |
* If the method throws an exception, it will be logged, but |
| 87 |
* closing the database will continue. |
| 88 |
* |
| 89 |
* @throws SQLException on SQL exception |
| 90 |
*/ |
| 91 |
default void close() throws SQLException { |
| 92 |
// Does nothing by default |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* This method is called when the trigger is dropped. |
| 97 |
* |
| 98 |
* @throws SQLException on SQL exception |
| 99 |
*/ |
| 100 |
default void remove() throws SQLException { |
| 101 |
// Does nothing by default |
| 102 |
} |
| 103 |
|
| 104 |
} |