Hi,
So if the parameters change, you have to drop and re-create the
trigger. That sounds wrong. Supporting special constructors is a
workaround, the real problem is: 'how to pass data to a trigger'. What
about:
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import org.h2.api.Trigger;
public class TriggerPassData implements Trigger {
public static void main(String... args) throws Exception {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:h2:mem:", "sa", "");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT)");
stat.execute("CREATE ALIAS TRIGGER_SET FOR \"" +
TriggerPassData.class.getName() +
".setTriggerData\"");
stat.execute("CREATE TRIGGER T1 " +
"BEFORE INSERT ON TEST " +
"FOR EACH ROW CALL \"" +
TriggerPassData.class.getName() + "\"");
stat.execute("CALL TRIGGER_SET('T1', 'Hello')");
stat.execute("INSERT INTO TEST VALUES(1)");
stat.execute("CALL TRIGGER_SET('T1', 'World')");
stat.execute("INSERT INTO TEST VALUES(2)");
conn.close();
}
private static final Map<String, TriggerPassData> TRIGGERS =
new HashMap<String, TriggerPassData>();
private String triggerData;
public void init(Connection conn, String schemaName,
String triggerName, String tableName, boolean before,
int type) {
TRIGGERS.put(triggerName, this);
}
public void fire(Connection conn, Object[] old, Object[] row) {
System.out.println(triggerData + ": " + row[0]);
}
public static void setTriggerData(String trigger, String data) {
TRIGGERS.get(trigger).triggerData = data;
}
}
On Tue, Sep 15, 2009 at 9:09 PM, Sergi Vladykin
<[email protected]> wrote:
>
> The use case is that I need to transfer some information from one
> table to other tables in aggregated form on the fly while the data
> arrives. The logic of aggregation is mainly the same but depends on
> destination table and also can vary with parameters. So now I need to
> have at least one trigger class for each destination table instead of
> heaving one parametrized. Also every time I need to change parameters
> I need to recompile trigger in java and restart database instead of
> using "drop trigger" and "create trigger". Though now its not the case
> but may be in future I will also need to do this all (create
> destination tables and triggers) at runtime with unpredictable
> parameters.
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/h2-database?hl=en
-~----------~----~----~----~------~----~------~--~---