Debian Codegraph @

src/test/org/h2/samples/TriggerSample.java

raw
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.math.BigDecimal;
9 import java.sql.Connection;
10 import java.sql.DriverManager;
11 import java.sql.PreparedStatement;
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.sql.Statement;
15
16 import org.h2.api.Trigger;
17
18 /**
19 * This sample application shows how to use database triggers.
20 */
21 public class TriggerSample {
22
23 /**
24 * This method is called when executing this sample application from the
25 * command line.
26 *
27 * @param args the command line parameters
28 * @throws Exception on failure
29 */
30 public static void main(String... args) throws Exception {
31 Class.forName("org.h2.Driver");
32 Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", "");
33 Statement stat = conn.createStatement();
34 stat.execute("CREATE TABLE INVOICE(ID INT PRIMARY KEY, AMOUNT DECIMAL(10, 2))");
35 stat.execute("CREATE TABLE INVOICE_SUM(AMOUNT DECIMAL(10, 2))");
36 stat.execute("INSERT INTO INVOICE_SUM VALUES(0.0)");
37
38 stat.execute("CREATE TRIGGER INV_INS " +
39 "AFTER INSERT ON INVOICE FOR EACH ROW " +
40 "CALL \"org.h2.samples.TriggerSample$MyTrigger\" ");
41 stat.execute("CREATE TRIGGER INV_UPD " +
42 "AFTER UPDATE ON INVOICE FOR EACH ROW " +
43 "CALL \"org.h2.samples.TriggerSample$MyTrigger\" ");
44 stat.execute("CREATE TRIGGER INV_DEL " +
45 "AFTER DELETE ON INVOICE FOR EACH ROW " +
46 "CALL \"org.h2.samples.TriggerSample$MyTrigger\" ");
47
48 stat.execute("INSERT INTO INVOICE VALUES(1, 10.0)");
49 stat.execute("INSERT INTO INVOICE VALUES(2, 19.95)");
50 stat.execute("UPDATE INVOICE SET AMOUNT=20.0 WHERE ID=2");
51 stat.execute("DELETE FROM INVOICE WHERE ID=1");
52
53 ResultSet rs;
54 rs = stat.executeQuery("SELECT AMOUNT FROM INVOICE_SUM");
55 rs.next();
56 System.out.println("The sum is " + rs.getBigDecimal(1));
57 rs.close();
58 stat.close();
59 conn.close();
60 }
61
62 /**
63 * This class is a simple trigger implementation.
64 */
65 public static class MyTrigger implements Trigger {
66
67 /**
68 * Initializes the trigger.
69 *
70 * @param conn a connection to the database
71 * @param schemaName the name of the schema
72 * @param triggerName the name of the trigger used in the CREATE TRIGGER
73 * statement
74 * @param tableName the name of the table
75 * @param before whether the fire method is called before or after the
76 * operation is performed
77 * @param type the operation type: INSERT, UPDATE, or DELETE
78 */
79 @Override
80 public void init(Connection conn, String schemaName,
81 String triggerName, String tableName, boolean before, int type) {
82 // initialize the trigger object is necessary
83 }
84
85 /**
86 * This method is called for each triggered action.
87 *
88 * @param conn a connection to the database
89 * @param oldRow the old row, or null if no old row is available (for
90 * INSERT)
91 * @param newRow the new row, or null if no new row is available (for
92 * DELETE)
93 * @throws SQLException if the operation must be undone
94 */
95 @Override
96 public void fire(Connection conn,
97 Object[] oldRow, Object[] newRow)
98 throws SQLException {
99 BigDecimal diff = null;
100 if (newRow != null) {
101 diff = (BigDecimal) newRow[1];
102 }
103 if (oldRow != null) {
104 BigDecimal m = (BigDecimal) oldRow[1];
105 diff = diff == null ? m.negate() : diff.subtract(m);
106 }
107 PreparedStatement prep = conn.prepareStatement(
108 "UPDATE INVOICE_SUM SET AMOUNT=AMOUNT+?");
109 prep.setBigDecimal(1, diff);
110 prep.execute();
111 }
112
113 @Override
114 public void close() {
115 // ignore
116 }
117
118 @Override
119 public void remove() {
120 // ignore
121 }
122
123 }
124
125 }