File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\tools\TriggerAdapter.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.tools;
8
9 import java.sql.Connection;
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 import org.h2.api.Trigger;
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 private SimpleResultSet oldResultSet, newResultSet;
48 private TriggerRowSource oldSource, newSource;
49
50 /**
51 * This method is called by the database engine once when initializing the
52 * trigger. It is called when the trigger is created, as well as when the
53 * database is opened. The default implementation initialized the result
54 * sets.
55 *
56 * @param conn a connection to the database
57 * @param schemaName the name of the schema
58 * @param triggerName the name of the trigger used in the CREATE TRIGGER
59 * statement
60 * @param tableName the name of the table
61 * @param before whether the fire method is called before or after the
62 * operation is performed
63 * @param type the operation type: INSERT, UPDATE, DELETE, SELECT, or a
64 * combination (this parameter is a bit field)
65 */
66 public void init(Connection conn, String schemaName,
67 String triggerName, String tableName,
68 boolean before, int type) throws SQLException {
69 ResultSet rs = conn.getMetaData().getColumns(
70 null, schemaName, tableName, null);
71 oldSource = new TriggerRowSource();
72 newSource = new TriggerRowSource();
73 oldResultSet = new SimpleResultSet(oldSource);
74 newResultSet = new SimpleResultSet(newSource);
75 while (rs.next()) {
76 String column = rs.getString("COLUMN_NAME");
77 int dataType = rs.getInt("DATA_TYPE");
78 int precision = rs.getInt("COLUMN_SIZE");
79 int scale = rs.getInt("DECIMAL_DIGITS");
80 oldResultSet.addColumn(column, dataType, precision, scale);
81 newResultSet.addColumn(column, dataType, precision, scale);
82 }
83 this.schemaName = schemaName;
84 this.triggerName = triggerName;
85 this.tableName = tableName;
86 this.before = before;
87 this.type = type;
88 }
89
90 /**
91 * A row source that allows to set the next row.
92 */
93 static class TriggerRowSource implements SimpleRowSource {
94
95 private Object[] row;
96
97 void setRow(Object[] row) {
98 this.row = row;
99 }
100
101 public Object[] readRow() {
102 return row;
103 }
104
105 public void close() {
106 // ignore
107 }
108
109 public void reset() {
110 // ignore
111 }
112
113 }
114
115 /**
116 * This method is called for each triggered action. The method is called
117 * immediately when the operation occurred (before it is committed). A
118 * transaction rollback will also rollback the operations that were done
119 * within the trigger, if the operations occurred within the same database.
120 * If the trigger changes state outside the database, a rollback trigger
121 * should be used.
122 * <p>
123 * The row arrays contain all columns of the table, in the same order
124 * as defined in the table.
125 * </p>
126 * <p>
127 * The default implementation calls the fire method with the ResultSet
128 * parameters.
129 * </p>
130 *
131 * @param conn a connection to the database
132 * @param oldRow the old row, or null if no old row is available (for
133 * INSERT)
134 * @param newRow the new row, or null if no new row is available (for
135 * DELETE)
136 * @throws SQLException if the operation must be undone
137 */
138 public void fire(Connection conn, Object[] oldRow,
139 Object[] newRow) throws SQLException {
140 fire(conn, wrap(oldResultSet, oldSource, oldRow), wrap(newResultSet, newSource, newRow));
141 }
142
143 /**
144 * This method is called for each triggered action by the default
145 * fire(Connection conn, Object[] oldRow, Object[] newRow) method.
146 * ResultSet.next does not need to be called (and calling it has no effect;
147 * it will always return true).
148 * <p>
149 * For "before" triggers, the new values of the new row may be changed
150 * using the ResultSet.updateX methods.
151 * </p>
152 *
153 * @param conn a connection to the database
154 * @param oldRow the old row, or null if no old row is available (for
155 * INSERT)
156 * @param newRow the new row, or null if no new row is available (for
157 * DELETE)
158 * @throws SQLException if the operation must be undone
159 */
160 public abstract void fire(Connection conn, ResultSet oldRow, ResultSet newRow) throws SQLException;
161
162 private static SimpleResultSet wrap(SimpleResultSet rs, TriggerRowSource source, Object[] row) throws SQLException {
163 if (row == null) {
164 return null;
165 }
166 source.setRow(row);
167 rs.next();
168 return rs;
169 }
170
171 /**
172 * This method is called when the database is closed.
173 * If the method throws an exception, it will be logged, but
174 * closing the database will continue.
175 * The default implementation does nothing.
176 *
177 * @throws SQLException
178 */
179 public void remove() throws SQLException {
180 // do nothing by default
181 }
182
183 /**
184 * This method is called when the trigger is dropped.
185 * The default implementation does nothing.
186 *
187 * @throws SQLException
188 */
189 public void close() throws SQLException {
190 // do nothing by default
191 }
192
193 }
194