/
TriggerHistory.java
349 lines (295 loc) · 11.7 KB
/
TriggerHistory.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* 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.model;
import java.io.Serializable;
import java.io.StringReader;
import java.util.Date;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.jumpmind.db.model.Table;
import org.jumpmind.symmetric.SymmetricException;
import org.jumpmind.symmetric.csv.CsvReader;
import org.jumpmind.symmetric.db.AbstractTriggerTemplate;
import org.jumpmind.symmetric.io.data.DataEventType;
/**
* Maps to the table sync audit table which tracks the history of sync trigger creation.
* <p/>
* This table also tracks the columns and the primary keys as of the create date so that if the table definition changes while we still have events to process
* (as may be the case when distributing events to remote locations), then we still have the history of what the columns and primary keys were at the time.
*/
public class TriggerHistory implements Serializable {
private static final long serialVersionUID = 1L;
private int triggerHistoryId;
private String triggerId;
private String sourceTableName;
private String sourceTableNameLowerCase;
private String sourceSchemaName;
private String sourceCatalogName;
private Date createTime;
private String columnNames;
private String[] parsedColumnNames;
private String pkColumnNames;
private String[] parsedPkColumnNames;
private boolean isMissingPk;
private String nameForInsertTrigger;
private String nameForUpdateTrigger;
private String nameForDeleteTrigger;
private String errorMessage;
private Date inactiveTime;
/**
* This is a hash based on the tablename, column names, and column data types. It is used to effectively version a table so we know when it changes.
*/
private int tableHash;
/**
* This is a hash based on the values in the trigger configuration table.
*/
private long triggerRowHash;
/**
* This is a hash of the trigger templates used for generating the trigger text.
*/
private long triggerTemplateHash;
private TriggerReBuildReason lastTriggerBuildReason;
public TriggerHistory() {
createTime = new Date();
}
public TriggerHistory(int triggerHistoryId) {
this();
this.triggerHistoryId = triggerHistoryId;
}
public TriggerHistory(String tableName, String pkColumnNames, String columnNames) {
this();
this.sourceTableName = tableName;
this.pkColumnNames = pkColumnNames;
this.columnNames = columnNames;
this.isMissingPk = Objects.equals(pkColumnNames, columnNames) || StringUtils.isBlank(pkColumnNames);
}
public TriggerHistory(Table table, Trigger trigger, AbstractTriggerTemplate triggerTemplate) {
this(table, trigger, triggerTemplate, null);
}
public TriggerHistory(Table table, Trigger trigger, AbstractTriggerTemplate triggerTemplate, TriggerReBuildReason reason) {
this();
if (triggerTemplate == null) {
throw new SymmetricException("triggerTemplate cannot be null. Does the current dialect have a TriggerTemplate?");
}
this.lastTriggerBuildReason = reason;
this.sourceTableName = (trigger.isSourceTableNameWildCarded() || trigger.isSourceTableNameExpanded()) ? table.getName()
: trigger.getSourceTableNameUnescaped();
this.columnNames = Table.getCommaDeliminatedColumns(trigger.orderColumnsForTable(table));
this.sourceSchemaName = trigger.isSourceSchemaNameWildCarded() ? table.getSchema() : trigger.getSourceSchemaNameUnescaped();
this.sourceCatalogName = trigger.isSourceCatalogNameWildCarded() ? table.getCatalog() : trigger.getSourceCatalogNameUnescaped();
this.triggerId = trigger.getTriggerId();
this.pkColumnNames = Table.getCommaDeliminatedColumns(trigger.filterExcludedAndIncludedColumns(trigger
.getSyncKeysColumnsForTable(table)));
this.isMissingPk = !table.hasPrimaryKey() || Objects.equals(pkColumnNames, columnNames) || StringUtils.isBlank(pkColumnNames);
this.triggerRowHash = trigger.toHashedValue();
this.triggerTemplateHash = triggerTemplate.toHashedValue();
this.tableHash = table.calculateTableHashcode();
}
public TriggerHistory(Trigger trigger) {
this.sourceCatalogName = trigger.getSourceCatalogName();
this.sourceSchemaName = trigger.getSourceSchemaName();
this.sourceTableName = trigger.getSourceTableName();
this.triggerId = trigger.getTriggerId();
}
public String getTriggerNameForDmlType(DataEventType type) {
switch (type) {
case INSERT:
return getNameForInsertTrigger();
case UPDATE:
return getNameForUpdateTrigger();
case DELETE:
return getNameForDeleteTrigger();
default:
break;
}
throw new IllegalStateException();
}
public String[] getParsedColumnNames() {
if (parsedColumnNames == null && columnNames != null) {
parsedColumnNames = parseColumnNames(columnNames);
}
return parsedColumnNames;
}
public int indexOfColumnName(String columnName, boolean ignoreCase) {
String[] columnNames = getParsedColumnNames();
int i = 0;
for (String col : columnNames) {
if (ignoreCase && col.equalsIgnoreCase(columnName)) {
return i;
} else if (col.equals(columnName)) {
return i;
}
i++;
}
return -1;
}
public String[] getParsedPkColumnNames() {
if (parsedPkColumnNames == null && pkColumnNames != null) {
parsedPkColumnNames = parseColumnNames(pkColumnNames);
}
return parsedPkColumnNames;
}
public int getTableHash() {
return tableHash;
}
public void setTableHash(int tableHash) {
this.tableHash = tableHash;
}
public String getFullyQualifiedSourceTableName() {
return Table.getFullyQualifiedTableName(sourceCatalogName, sourceSchemaName, sourceTableName);
}
public String getSourceTableName() {
return sourceTableName;
}
public void setSourceTableName(String tableName) {
this.sourceTableName = tableName;
sourceTableNameLowerCase = null;
}
public String getSourceTableNameLowerCase() {
if (sourceTableNameLowerCase == null) {
sourceTableNameLowerCase = sourceTableName.toLowerCase();
}
return sourceTableNameLowerCase;
}
public String getColumnNames() {
return columnNames;
}
public void setColumnNames(String allColumnData) {
this.columnNames = allColumnData;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public TriggerReBuildReason getLastTriggerBuildReason() {
return lastTriggerBuildReason;
}
public void setLastTriggerBuildReason(TriggerReBuildReason lastTriggerBuildReason) {
this.lastTriggerBuildReason = lastTriggerBuildReason;
}
public String getPkColumnNames() {
return pkColumnNames;
}
public void setPkColumnNames(String pkColumnData) {
this.pkColumnNames = pkColumnData;
}
public boolean isMissingPk() {
return isMissingPk;
}
public void setIsMissingPk(boolean isMissingPk) {
this.isMissingPk = isMissingPk;
}
public int getTriggerHistoryId() {
return triggerHistoryId;
}
public void setTriggerHistoryId(int tableSyncAuditId) {
this.triggerHistoryId = tableSyncAuditId;
}
public String getNameForDeleteTrigger() {
return nameForDeleteTrigger;
}
public void setNameForDeleteTrigger(String nameForDeleteTrigger) {
this.nameForDeleteTrigger = nameForDeleteTrigger;
}
public String getNameForInsertTrigger() {
return nameForInsertTrigger;
}
public void setNameForInsertTrigger(String nameForInsertTrigger) {
this.nameForInsertTrigger = nameForInsertTrigger;
}
public String getNameForUpdateTrigger() {
return nameForUpdateTrigger;
}
public void setNameForUpdateTrigger(String nameForUpdateTrigger) {
this.nameForUpdateTrigger = nameForUpdateTrigger;
}
public String getSourceSchemaName() {
return sourceSchemaName;
}
public void setSourceSchemaName(String schemaName) {
this.sourceSchemaName = schemaName;
}
public String getTriggerId() {
return triggerId;
}
public void setTriggerId(String triggerId) {
this.triggerId = triggerId;
}
public Date getInactiveTime() {
return inactiveTime;
}
public void setInactiveTime(Date inactiveTime) {
this.inactiveTime = inactiveTime;
}
public String getSourceCatalogName() {
return sourceCatalogName;
}
public void setSourceCatalogName(String sourceCatalogName) {
this.sourceCatalogName = sourceCatalogName;
}
public long getTriggerRowHash() {
return triggerRowHash;
}
public void setTriggerRowHash(long triggerRowHash) {
this.triggerRowHash = triggerRowHash;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public String getErrorMessage() {
return errorMessage;
}
public int toVirtualTriggerHistId() {
final int prime = 31;
int result = 1;
result = prime * result + ((columnNames == null) ? 0 : columnNames.hashCode());
result = prime * result + ((sourceCatalogName == null) ? 0 : sourceCatalogName.hashCode());
result = prime * result + ((sourceSchemaName == null) ? 0 : sourceSchemaName.hashCode());
result = prime * result + ((sourceTableName == null) ? 0 : sourceTableName.hashCode());
return result;
}
public long getTriggerTemplateHash() {
return triggerTemplateHash;
}
public void setTriggerTemplateHash(long triggerTemplateHash) {
this.triggerTemplateHash = triggerTemplateHash;
}
protected String[] parseColumnNames(String argColumnNames) {
if (argColumnNames.indexOf('"') == -1) {
return argColumnNames.split(",");
}
try (CsvReader reader = new CsvReader(new StringReader(argColumnNames), ',')) {
if (reader.readRecord()) {
return reader.getValues();
} else {
throw new SymmetricException("Failed to read a record from CsvReader.");
}
} catch (Exception ex) {
throw new SymmetricException("Failed to parse columns [" + argColumnNames + "]", ex);
}
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof TriggerHistory && ((TriggerHistory) obj).triggerHistoryId == triggerHistoryId;
}
}