| 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.tools; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.ResultSet; |
| 10 |
import java.sql.SQLException; |
| 11 |
import org.h2.api.Trigger; |
| 12 |
import org.h2.message.DbException; |
| 13 |
|
| 14 |
/** |
| 15 |
* An adapter for the trigger interface that allows to use the ResultSet |
| 16 |
* interface instead of a row array. |
| 17 |
*/ |
| 18 |
public abstract class TriggerAdapter implements Trigger { |
| 19 |
|
| 20 |
/** |
| 21 |
* The schema name. |
| 22 |
*/ |
| 23 |
protected String schemaName; |
| 24 |
|
| 25 |
/** |
| 26 |
* The name of the trigger. |
| 27 |
*/ |
| 28 |
protected String triggerName; |
| 29 |
|
| 30 |
/** |
| 31 |
* The name of the table. |
| 32 |
*/ |
| 33 |
protected String tableName; |
| 34 |
|
| 35 |
/** |
| 36 |
* Whether the fire method is called before or after the operation is |
| 37 |
* performed. |
| 38 |
*/ |
| 39 |
protected boolean before; |
| 40 |
|
| 41 |
/** |
| 42 |
* The trigger type: INSERT, UPDATE, DELETE, SELECT, or a combination (a bit |
| 43 |
* field). |
| 44 |
*/ |
| 45 |
protected int type; |
| 46 |
|
| 47 |
/** |
| 48 |
* This method is called by the database engine once when initializing the |
| 49 |
* trigger. It is called when the trigger is created, as well as when the |
| 50 |
* database is opened. The default implementation initialized the result |
| 51 |
* sets. |
| 52 |
* |
| 53 |
* @param conn a connection to the database |
| 54 |
* @param schemaName the name of the schema |
| 55 |
* @param triggerName the name of the trigger used in the CREATE TRIGGER |
| 56 |
* statement |
| 57 |
* @param tableName the name of the table |
| 58 |
* @param before whether the fire method is called before or after the |
| 59 |
* operation is performed |
| 60 |
* @param type the operation type: INSERT, UPDATE, DELETE, SELECT, or a |
| 61 |
* combination (this parameter is a bit field) |
| 62 |
*/ |
| 63 |
@Override |
| 64 |
public void init(Connection conn, String schemaName, |
| 65 |
String triggerName, String tableName, |
| 66 |
boolean before, int type) throws SQLException { |
| 67 |
this.schemaName = schemaName; |
| 68 |
this.triggerName = triggerName; |
| 69 |
this.tableName = tableName; |
| 70 |
this.before = before; |
| 71 |
this.type = type; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public final void fire(Connection conn, Object[] oldRow, Object[] newRow) throws SQLException { |
| 76 |
throw DbException.getInternalError(); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* This method is called for each triggered action by the default |
| 81 |
* fire(Connection conn, Object[] oldRow, Object[] newRow) method. |
| 82 |
* <p> |
| 83 |
* For "before" triggers, the new values of the new row may be changed |
| 84 |
* using the ResultSet.updateX methods. |
| 85 |
* </p> |
| 86 |
* |
| 87 |
* @param conn a connection to the database |
| 88 |
* @param oldRow the old row, or null if no old row is available (for |
| 89 |
* INSERT) |
| 90 |
* @param newRow the new row, or null if no new row is available (for |
| 91 |
* DELETE) |
| 92 |
* @throws SQLException if the operation must be undone |
| 93 |
*/ |
| 94 |
public abstract void fire(Connection conn, ResultSet oldRow, |
| 95 |
ResultSet newRow) throws SQLException; |
| 96 |
|
| 97 |
} |