提交 39b11771 authored 作者: Thomas Mueller's avatar Thomas Mueller

New sample application that shows how to pass data to a trigger.

上级 0d3a6cc7
......@@ -18,7 +18,8 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>More bugs in the server-less multi-connection mode have been fixed:
<ul><li>New sample application that shows how to pass data to a trigger.
</li><li>More bugs in the server-less multi-connection mode have been fixed:
On Windows, two processes could write to the same database at the same time.
</li><li>When loading triggers or other client classes
(static functions, database event listener, user aggregate functions, other JDBC drivers),
......
/*
* Copyright 2004-2009 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.samples;
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;
/**
* This sample application shows how to pass data to a trigger.
*/
public class TriggerPassData implements Trigger {
private static final Map<String, TriggerPassData> TRIGGERS =
new HashMap<String, TriggerPassData>();
private String triggerData;
/**
* This method is called when executing this sample application from the
* command line.
*
* @param args the command line parameters
*/
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();
}
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]);
}
/**
* Call this method to change a specific trigger.
*
* @param trigger the trigger name
* @param data the data
*/
public static void setTriggerData(String trigger, String data) {
TRIGGERS.get(trigger).triggerData = data;
}
}
......@@ -7,6 +7,7 @@
package org.h2.test.unit;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
......@@ -35,6 +36,7 @@ public class TestSampleApps extends TestBase {
public void test() throws Exception {
deleteDb("optimizations");
InputStream in = getClass().getClassLoader().getResourceAsStream("org/h2/samples/optimizations.sql");
new File(baseDir).mkdirs();
FileOutputStream out = new FileOutputStream(baseDir + "/optimizations.sql");
IOUtils.copyAndClose(in, out);
String url = "jdbc:h2:" + baseDir + "/optimizations";
......@@ -54,6 +56,7 @@ public class TestSampleApps extends TestBase {
// TODO test ShutdownServer (server needs to be started in a separate
// process)
testApp("The sum is 20.00", org.h2.samples.TriggerSample.class);
testApp("Hello: 1\nWorld: 2", org.h2.samples.TriggerPassData.class);
// tools
testApp("Allows changing the database file encryption password or algorithm*",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论