/
TriggerHistory.java
291 lines (225 loc) · 8.28 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
/*
* 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 Lesser General Public License (the
* "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU Lesser General Public
* License 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.util.Date;
import org.jumpmind.db.model.Table;
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 sourceSchemaName;
private String sourceCatalogName;
private Date createTime;
private String columnNames;
private String[] parsedColumnNames;
private String pkColumnNames;
private String[] parsedPkColumnNames;
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;
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;
}
public TriggerHistory(Table table, Trigger trigger) {
this(table, trigger, null);
}
public TriggerHistory(Table table, Trigger trigger, TriggerReBuildReason reason) {
this();
this.lastTriggerBuildReason = reason;
this.sourceTableName = trigger.getSourceTableName();
this.columnNames = Table.getCommaDeliminatedColumns(trigger.orderColumnsForTable(table));
this.sourceSchemaName = trigger.getSourceSchemaName();
this.sourceCatalogName = trigger.getSourceCatalogName();
this.triggerId = trigger.getTriggerId();
this.pkColumnNames = Table.getCommaDeliminatedColumns(table.getPrimaryKeyColumns());
this.triggerRowHash = trigger.toHashedValue();
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();
}
throw new IllegalStateException();
}
public String[] getParsedColumnNames() {
if (parsedColumnNames == null && columnNames != null) {
parsedColumnNames = columnNames.split(",");
}
return parsedColumnNames;
}
public String[] getParsedPkColumnNames() {
if (parsedPkColumnNames == null && pkColumnNames != null) {
parsedPkColumnNames = pkColumnNames.split(",");
}
return parsedPkColumnNames;
}
public int getTableHash() {
return tableHash;
}
public void setTableHash(int tableHash) {
this.tableHash = tableHash;
}
public String getSourceTableName() {
return sourceTableName;
}
public void setSourceTableName(String tableName) {
this.sourceTableName = tableName;
}
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 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;
}
}