| 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.samples; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.DriverManager; |
| 10 |
import java.sql.ResultSet; |
| 11 |
import java.sql.SQLException; |
| 12 |
import java.sql.Statement; |
| 13 |
import java.util.concurrent.ConcurrentHashMap; |
| 14 |
|
| 15 |
import org.h2.api.Trigger; |
| 16 |
|
| 17 |
/** |
| 18 |
* This sample application shows how to pass data to a trigger. Trigger data can |
| 19 |
* be persisted by storing it in the database. |
| 20 |
*/ |
| 21 |
public class TriggerPassData implements Trigger { |
| 22 |
|
| 23 |
private static final ConcurrentHashMap<String, TriggerPassData> TRIGGERS = new ConcurrentHashMap<>(); |
| 24 |
private String triggerData; |
| 25 |
|
| 26 |
/** |
| 27 |
* This method is called when executing this sample application from the |
| 28 |
* command line. |
| 29 |
* |
| 30 |
* @param args the command line parameters |
| 31 |
* @throws Exception on failure |
| 32 |
*/ |
| 33 |
public static void main(String... args) throws Exception { |
| 34 |
Class.forName("org.h2.Driver"); |
| 35 |
Connection conn = DriverManager.getConnection( |
| 36 |
"jdbc:h2:mem:test", "sa", ""); |
| 37 |
Statement stat = conn.createStatement(); |
| 38 |
stat.execute("CREATE TABLE TEST(ID INT)"); |
| 39 |
stat.execute("CREATE ALIAS TRIGGER_SET FOR '" + |
| 40 |
TriggerPassData.class.getName() + |
| 41 |
".setTriggerData'"); |
| 42 |
stat.execute("CREATE TRIGGER T1 " + |
| 43 |
"BEFORE INSERT ON TEST " + |
| 44 |
"FOR EACH ROW CALL \"" + |
| 45 |
TriggerPassData.class.getName() + "\""); |
| 46 |
stat.execute("CALL TRIGGER_SET('T1', 'Hello')"); |
| 47 |
stat.execute("INSERT INTO TEST VALUES(1)"); |
| 48 |
stat.execute("CALL TRIGGER_SET('T1', 'World')"); |
| 49 |
stat.execute("INSERT INTO TEST VALUES(2)"); |
| 50 |
stat.close(); |
| 51 |
conn.close(); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void init(Connection conn, String schemaName, |
| 56 |
String triggerName, String tableName, boolean before, |
| 57 |
int type) throws SQLException { |
| 58 |
TRIGGERS.put(getPrefix(conn) + triggerName, this); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void fire(Connection conn, Object[] old, Object[] row) { |
| 63 |
System.out.println(triggerData + ": " + row[0]); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Call this method to change a specific trigger. |
| 68 |
* |
| 69 |
* @param conn the connection |
| 70 |
* @param trigger the trigger name |
| 71 |
* @param data the data |
| 72 |
* @throws SQLException on failure |
| 73 |
*/ |
| 74 |
public static void setTriggerData(Connection conn, String trigger, |
| 75 |
String data) throws SQLException { |
| 76 |
TRIGGERS.get(getPrefix(conn) + trigger).triggerData = data; |
| 77 |
} |
| 78 |
|
| 79 |
private static String getPrefix(Connection conn) throws SQLException { |
| 80 |
Statement stat = conn.createStatement(); |
| 81 |
ResultSet rs = stat.executeQuery( |
| 82 |
"call coalesce(database_path() || '_', '') || database() || '_'"); |
| 83 |
rs.next(); |
| 84 |
return rs.getString(1); |
| 85 |
} |
| 86 |
|
| 87 |
} |