/
AbstractEmbeddedTrigger.java
270 lines (251 loc) · 10.2 KB
/
AbstractEmbeddedTrigger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.db;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.jumpmind.symmetric.db.h2.H2Trigger;
import org.jumpmind.symmetric.db.hsqldb.HsqlDbTrigger;
/**
* An implementation of logic that can be used in Java database triggers to
* capture data for SymmetricDS.
*
* @see H2Trigger
* @see HsqlDbTrigger
*
*
*/
abstract public class AbstractEmbeddedTrigger {
protected static final char[] HEX = "0123456789abcdef".toCharArray();
protected static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
protected static final String KEY_CONDITION_SQL = "CONDITION_SQL";
protected static final String KEY_INSERT_DATA_SQL = "INSERT_DATA_SQL";
protected static final String TEMPLATE_TABLE_SUFFIX = "_CONFIG";
protected String triggerName;
protected String schemaName;
protected Map<String, String> templates = null;
/**
* This method should be called by the database engine once when
* initializing the trigger.
*
* @param conn
* a connection to the database
* @param triggerName
* the name of the trigger used in the CREATE TRIGGER statement
* @param schemaName TODO
* @param tableName
* the name of the table
*/
protected void init(Connection conn, String triggerName, String schemaName, String tableName) throws SQLException {
if (this.templates == null) {
this.schemaName = schemaName;
this.triggerName = triggerName;
this.templates = getTemplates(conn);
}
if (templates == null || templates.size() == 0) {
throw new IllegalStateException(String.format(
"The '%s' SymmetricDS trigger is in an invalid state. It needs to be dropped.", triggerName));
}
}
/**
* This method is called for each triggered action.
*
* @param conn
* a connection to the database
* @param oldRow
* the old row, or null if no old row is available (for INSERT)
* @param newRow
* the new row, or null if no new row is available (for DELETE)
* @throws SQLException
* if the operation must be undone
*/
public void fire(Connection conn, Object[] oldRow, Object[] newRow) throws SQLException {
String sql = null;
try {
Statement stmt = conn.createStatement();
sql = fillVirtualTableSql(templates.get(KEY_CONDITION_SQL), oldRow, newRow);
ResultSet rs = stmt.executeQuery(sql);
if (rs.next() && rs.getInt(1) > 0) {
rs.close();
sql = fillVirtualTableSql(templates.get(KEY_INSERT_DATA_SQL), oldRow, newRow);
stmt.executeUpdate(sql);
}
stmt.close();
} catch (SQLException ex) {
System.err.println("This sql failed: " + sql);
Throwable rootException = ex;
while (rootException.getCause() != null && !rootException.getCause().equals(ex)) {
rootException = ex.getCause();
}
rootException.printStackTrace();
throw ex;
}
}
protected String fillVirtualTableSql(String sql, Object[] oldRow, Object[] newRow) throws SQLException {
int columnCount = oldRow != null ? oldRow.length : newRow.length;
StringBuilder out = new StringBuilder();
String[] tokens = sql.split("\\?");
int tokenIndex = 0;
tokenIndex = forEachColumn(columnCount, newRow, out, tokenIndex, tokens);
tokenIndex = forEachColumn(columnCount, oldRow, out, tokenIndex, tokens);
out.append(tokens[tokenIndex]);
return out.toString();
}
private int forEachColumn(int columnCount, Object[] data, StringBuilder out, int tokenIndex, String[] tokens) {
for (int i = 0; i < columnCount; i++) {
out.append(tokens[tokenIndex++]);
if (data != null) {
data[i] = appendVirtualTableStringValue(data[i], out);
} else {
out.append("null");
}
}
return tokenIndex;
}
protected Object appendVirtualTableStringValue(Object value, StringBuilder out) {
if (value == null) {
out.append("null");
} else if (value instanceof String || value instanceof Reader) {
if (value instanceof Reader) {
try {
value = readStringAndClose((Reader) value, -1);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
out.append("'");
out.append(escapeString(value));
out.append("'");
} else if (value instanceof Number) {
out.append(value);
} else if (value instanceof Boolean) {
out.append(value);
} else if (value instanceof InputStream) {
out.append("'");
try {
value = convertBytesToString(readBytesAndClose((InputStream) value, -1));
} catch (IOException e) {
throw new RuntimeException(e);
}
out.append(escapeString(value));
out.append("'");
} else if (value instanceof Date) {
out.append("'");
synchronized (DATE_FORMATTER) {
out.append(DATE_FORMATTER.format(value));
}
out.append("'");
} else {
throw new IllegalStateException(String.format("Type not supported: %s", value.getClass().getName()));
}
return value;
}
protected String readStringAndClose(Reader paramReader, int paramInt) throws IOException {
try {
if (paramInt <= 0) {
paramInt = 2147483647;
}
int i = Math.min(4096, paramInt);
StringWriter localStringWriter = new StringWriter((paramInt == 2147483647) ? i : paramInt);
char[] arrayOfChar = new char[i];
while (paramInt > 0) {
int j = Math.min(i, paramInt);
j = paramReader.read(arrayOfChar, 0, j);
if (j < 0) {
break;
}
localStringWriter.write(arrayOfChar, 0, j);
paramInt -= j;
}
String str = localStringWriter.toString();
return str;
} finally {
paramReader.close();
}
}
public static byte[] readBytesAndClose(InputStream paramInputStream, int paramInt) throws IOException {
try {
if (paramInt <= 0) {
paramInt = 2147483647;
}
int i = Math.min(4096, paramInt);
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(i);
byte[] arrayOfByte1 = new byte[i];
while (paramInt > 0) {
int j = Math.min(i, paramInt);
j = paramInputStream.read(arrayOfByte1, 0, j);
if (j < 0) {
break;
}
localByteArrayOutputStream.write(arrayOfByte1, 0, j);
paramInt -= j;
}
byte[] arrayOfByte2 = localByteArrayOutputStream.toByteArray();
return arrayOfByte2;
} finally {
paramInputStream.close();
}
}
public static String convertBytesToString(byte[] paramArrayOfByte) {
return convertBytesToString(paramArrayOfByte, paramArrayOfByte.length);
}
public static String convertBytesToString(byte[] paramArrayOfByte, int paramInt) {
char[] arrayOfChar1 = new char[paramInt + paramInt];
char[] arrayOfChar2 = HEX;
for (int i = 0; i < paramInt; ++i) {
int j = paramArrayOfByte[i] & 0xFF;
arrayOfChar1[(i + i)] = arrayOfChar2[(j >> 4)];
arrayOfChar1[(i + i + 1)] = arrayOfChar2[(j & 0xF)];
}
return new String(arrayOfChar1);
}
protected String escapeString(Object val) {
return val == null ? null : val.toString().replaceAll("'", "''");
}
protected Map<String, String> getTemplates(Connection conn) throws SQLException {
Map<String, String> templates = new HashMap<String, String>();
Statement stmt = conn.createStatement();
String schemaPrefix = schemaName != null && schemaName.length() > 0 ? "\"" + schemaName+"\"." : "";
ResultSet rs = stmt.executeQuery(String.format("select * from %s%s%s", schemaPrefix, triggerName, TEMPLATE_TABLE_SUFFIX));
if (rs.next()) {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
templates.put(metaData.getColumnName(i), rs.getString(i));
}
return templates;
} else {
throw new SQLException(String.format("%s is in an invalid state. %s%s did not return a row.", triggerName,
triggerName, TEMPLATE_TABLE_SUFFIX));
}
}
}