/
AseTriggerTemplate.java
452 lines (446 loc) · 43.9 KB
/
AseTriggerTemplate.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/**
* 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.ase;
import java.sql.Types;
import java.util.HashMap;
import org.apache.commons.lang3.NotImplementedException;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Table;
import org.jumpmind.symmetric.db.AbstractTriggerTemplate;
import org.jumpmind.symmetric.db.ISymmetricDialect;
import org.jumpmind.symmetric.io.data.DataEventType;
import org.jumpmind.symmetric.model.Channel;
import org.jumpmind.symmetric.model.Trigger;
import org.jumpmind.symmetric.model.TriggerHistory;
import org.jumpmind.util.FormatUtils;
public class AseTriggerTemplate extends AbstractTriggerTemplate {
public AseTriggerTemplate(ISymmetricDialect symmetricDialect) {
super(symmetricDialect);
String quote = symmetricDialect.getPlatform()
.getDatabaseInfo().getDelimiterToken();
quote = quote == null ? "\"" : quote;
emptyColumnTemplate = "null";
stringColumnTemplate = "case when $(tableAlias)." + quote + "$(columnName)" + quote
+ " is null then null else '\"' + str_replace(str_replace($(tableAlias)." + quote + "$(columnName)" + quote
+ ",'\\','\\\\'),'\"','\\\"') + '\"' end";
numberColumnTemplate = "case when $(tableAlias)." + quote + "$(columnName)" + quote + " is null then null else ('\"' + convert(varchar,$(tableAlias)."
+ quote + "$(columnName)" + quote + ") + '\"') end";
datetimeColumnTemplate = "case when $(tableAlias)." + quote + "$(columnName)" + quote
+ " is null then null else ('\"' + str_replace(convert(varchar,$(tableAlias)." + quote + "$(columnName)" + quote
+ ",102),'.','-') + ' ' + right('00'+convert(varchar,datepart(HOUR,$(tableAlias)." + quote + "$(columnName)" + quote
+ ")),2)+':'+right('00'+convert(varchar,datepart(MINUTE,$(tableAlias)." + quote + "$(columnName)" + quote
+ ")),2)+':'+right('00'+convert(varchar,datepart(SECOND,$(tableAlias)." + quote + "$(columnName)" + quote
+ ")),2)+'.'+right('000'+convert(varchar,datepart(MILLISECOND,$(tableAlias)." + quote + "$(columnName)" + quote + ")),3) + '\"') end";
timeColumnTemplate = "case when $(tableAlias)." + quote + "$(columnName)" + quote
+ " is null then null else ('\"' + right('00'+convert(varchar,datepart(HOUR,$(tableAlias)." + quote + "$(columnName)" + quote
+ ")),2)+':'+right('00'+convert(varchar,datepart(MINUTE,$(tableAlias)." + quote + "$(columnName)" + quote
+ ")),2)+':'+right('00'+convert(varchar,datepart(SECOND,$(tableAlias)." + quote + "$(columnName)" + quote
+ ")),2)+'.'+right('000'+convert(varchar,datepart(MILLISECOND,$(tableAlias)." + quote + "$(columnName)" + quote + ")),3) + '\"') end";
clobColumnTemplate = "case when datalength($(origTableAlias)." + quote + "$(columnName)" + quote + ") is null or datalength($(origTableAlias)." + quote
+ "$(columnName)" + quote + ")=0 then null when char_length($(origTableAlias)." + quote + "$(columnName)" + quote
+ ") > 16384 then '\"\\b\"' else '\"' + str_replace(str_replace(cast($(origTableAlias)." + quote + "$(columnName)" + quote
+ " as varchar(16384)),'\\','\\\\'),'\"','\\\"') + '\"' end";
blobColumnTemplate = "case when $(origTableAlias)." + quote + "$(columnName)" + quote
+ " is null then null else '\"' + bintostr(convert(varbinary(16384),$(origTableAlias)." + quote + "$(columnName)" + quote + ")) + '\"' end";
binaryColumnTemplate = "case when $(tableAlias)." + quote + "$(columnName)" + quote
+ " is null then null else '\"' + bintostr(convert(varbinary(16384),$(tableAlias)." + quote + "$(columnName)" + quote + ")) + '\"' end";
imageColumnTemplate = "case when datalength($(origTableAlias)." + quote + "$(columnName)" + quote + ") is null or datalength($(origTableAlias)." + quote
+ "$(columnName)" + quote + ")=0 then null else '\"' + bintostr(convert(varbinary(16384),$(origTableAlias)." + quote + "$(columnName)" + quote
+ ")) + '\"' end";
booleanColumnTemplate = "case when $(tableAlias)." + quote + "$(columnName)" + quote + " is null then null when $(tableAlias)." + quote
+ "$(columnName)" + quote + " = 1 then '\"1\"' else '\"0\"' end";
triggerConcatCharacter = "+";
newTriggerValue = "inserted";
oldTriggerValue = "deleted";
oldColumnPrefix = "";
newColumnPrefix = "";
sqlTemplates = new HashMap<String, String>();
sqlTemplates.put("insertTriggerTemplate",
"create trigger $(triggerName) on $(schemaName)$(tableName) for insert " + getOrderClause() + " as\n" +
" begin \n"
+
" set nocount on \n" +
" declare @txid varchar(50) \n" +
" if (@@TRANCOUNT > 0) begin \n"
+
" select @txid = $(txIdExpression) \n" +
" end \n"
+
" declare @clientapplname varchar(50) \n" +
" declare @clientname varchar(50) \n" +
" select @clientapplname = clientapplname, @clientname = case when clientapplname = 'SymmetricDS' then clientname else null end from master.dbo.sysprocesses where spid = @@spid \n"
+
" declare @DataRow varchar(16384) \n" +
" declare @ChannelId varchar(128) \n" +
" $(declareNewKeyVariables) \n"
+
" $(custom_before_insert_text) \n" +
" if ($(syncOnIncomingBatchCondition)) begin \n"
+
" declare DataCursor cursor for \n"
+
" $(if:containsBlobClobColumns) \n"
+
" select $(columns) $(newKeyNames), $(channelExpression) from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) where $(syncOnInsertCondition)\n"
+
" $(else:containsBlobClobColumns) \n"
+
" select $(columns) $(newKeyNames), $(channelExpression) from inserted where $(syncOnInsertCondition) \n"
+
" $(end:containsBlobClobColumns) \n"
+
" open DataCursor \n"
+
" fetch DataCursor into @DataRow $(newKeyVariables), @ChannelId \n"
+
" while @@sqlstatus = 0 begin \n"
+
" insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, channel_id, transaction_id, source_node_id, external_data, create_time) \n"
+
" values('$(targetTableName)','I', $(triggerHistoryId), @DataRow, @ChannelId, @txid, @clientname, $(externalSelect), getdate()) \n"
+
" fetch DataCursor into @DataRow $(newKeyVariables), @ChannelId \n"
+
" end \n"
+
" close DataCursor \n"
+
" deallocate cursor DataCursor \n"
+
" end \n"
+
" $(custom_on_insert_text) \n" +
" set nocount off \n" +
" end ");
sqlTemplates.put("updateTriggerTemplate",
"create trigger $(triggerName) on $(schemaName)$(tableName) for update " + getOrderClause() + " as\n" +
" begin \n"
+
" set nocount on \n" +
" declare @LOCALROWCOUNT int \n"
+
" declare @DataRow varchar(16384) \n"
+
" declare @OldPk varchar(2000) \n"
+
" declare @OldDataRow varchar(16384) \n"
+
" declare @ChannelId varchar(128) \n" +
" select @LOCALROWCOUNT = count(*) from inserted \n" +
" declare @txid varchar(50) \n"
+
" if (@@TRANCOUNT > 0) begin \n"
+
" select @txid = $(txIdExpression) \n" +
" end \n"
+
" declare @clientapplname varchar(50) \n" +
" declare @clientname varchar(50) \n" +
" select @clientapplname = clientapplname, @clientname = case when clientapplname = 'SymmetricDS' then clientname else null end from master.dbo.sysprocesses where spid = @@spid \n"
+
" $(declareOldKeyVariables) \n"
+
" $(declareNewKeyVariables) \n"
+
" $(custom_before_update_text) \n" +
" if ($(syncOnIncomingBatchCondition)) begin \n"
+
" if ($(hasPrimaryKeysDefined) $(primaryKeysUpdated) ) \n" +
" begin \n" +
" if (@LOCALROWCOUNT = 1) \n"
+
" begin \n"
+
" declare DataCursor cursor for \n"
+
" $(if:containsBlobClobColumns) \n"
+
" select $(columns), $(oldKeys), $(oldColumns) $(oldKeyNames) $(newKeyNames), $(channelExpression) from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) inner join deleted on 1=1 where $(syncOnUpdateCondition)\n"
+
" $(else:containsBlobClobColumns) \n"
+
" select $(columns), $(oldKeys), $(oldColumns) $(oldKeyNames) $(newKeyNames), $(channelExpression) from inserted inner join deleted on 1=1 where $(syncOnUpdateCondition) \n"
+
" $(end:containsBlobClobColumns) \n"
+
" open DataCursor \n"
+
" fetch DataCursor into @DataRow, @OldPk, @OldDataRow $(oldKeyVariables) $(newKeyVariables), @ChannelId \n"
+
" while @@sqlstatus = 0 begin \n"
+
" insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, pk_data, old_data, channel_id, transaction_id, source_node_id, external_data, create_time) \n"
+
" values('$(targetTableName)','U', $(triggerHistoryId), @DataRow, @OldPk, @OldDataRow, @ChannelId, @txid, @clientname, $(externalSelect), getdate())\n"
+
" fetch DataCursor into @DataRow, @OldPk, @OldDataRow $(oldKeyVariables) $(newKeyVariables), @ChannelId \n"
+
" end \n"
+
" close DataCursor \n"
+
" deallocate cursor DataCursor \n"
+
" end \n"
+
" else \n"
+
" begin \n"
+
" declare DeleteDataCursor cursor for \n"
+
" select $(oldKeys), $(oldColumns) $(oldKeyNames), $(specialSqlServerSybaseChannelExpression) from deleted where $(syncOnDeleteCondition) \n"
+
" open DeleteDataCursor \n"
+
" fetch DeleteDataCursor into @OldPk, @OldDataRow $(oldKeyVariables), @ChannelId \n"
+
" while @@sqlstatus = 0 begin \n"
+
" insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, pk_data, old_data, channel_id, transaction_id, source_node_id, external_data, create_time) \n"
+
" values('$(targetTableName)','D', $(triggerHistoryId), @OldPk, @OldDataRow, @ChannelId, @txid, @clientname, $(externalSelectForDelete), getdate())\n"
+
" fetch DeleteDataCursor into @OldPk,@OldDataRow $(oldKeyVariables), @ChannelId \n"
+
" end \n"
+
" close DeleteDataCursor \n"
+
" deallocate cursor DeleteDataCursor \n"
+
" declare InsertDataCursor cursor for \n"
+
" $(if:containsBlobClobColumns) \n"
+
" select $(columns) $(newKeyNames), $(channelExpression) from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) where $(syncOnInsertCondition)\n"
+
" $(else:containsBlobClobColumns) \n"
+
" select $(columns) $(newKeyNames), $(channelExpression) from inserted where $(syncOnInsertCondition) \n"
+
" $(end:containsBlobClobColumns) \n"
+
" open InsertDataCursor \n"
+
" fetch InsertDataCursor into @DataRow $(newKeyVariables), @ChannelId \n"
+
" while @@sqlstatus = 0 begin \n"
+
" insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, channel_id, transaction_id, source_node_id, external_data, create_time) \n"
+
" values('$(targetTableName)','I', $(triggerHistoryId), @DataRow, @ChannelId, @txid, @clientname, $(externalSelectForInsert), getdate()) \n"
+
" fetch InsertDataCursor into @DataRow $(newKeyVariables), @ChannelId \n"
+
" end \n"
+
" close InsertDataCursor \n"
+
" deallocate cursor InsertDataCursor \n"
+
" end \n" +
" end \n"
+
" else \n" +
" begin \n"
+
" declare DataCursor cursor for \n"
+
" $(if:containsBlobClobColumns) \n"
+
" select $(columns), $(oldKeys), $(oldColumns) $(oldKeyNames) $(newKeyNames), $(channelExpression) from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) inner join deleted on $(oldNewPrimaryKeyJoin) where $(syncOnUpdateCondition)\n"
+
" $(else:containsBlobClobColumns) \n"
+
" select $(columns), $(oldKeys), $(oldColumns) $(oldKeyNames) $(newKeyNames), $(channelExpression) from inserted inner join deleted on $(oldNewPrimaryKeyJoin) where $(syncOnUpdateCondition) \n"
+
" $(end:containsBlobClobColumns) \n"
+
" open DataCursor \n"
+
" fetch DataCursor into @DataRow, @OldPk, @OldDataRow $(oldKeyVariables) $(newKeyVariables), @ChannelId \n"
+
" while @@sqlstatus = 0 begin \n"
+
" insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, pk_data, old_data, channel_id, transaction_id, source_node_id, external_data, create_time) \n"
+
" values('$(targetTableName)','U', $(triggerHistoryId), @DataRow, @OldPk, @OldDataRow, @ChannelId, @txid, @clientname, $(externalSelect), getdate())\n"
+
" fetch DataCursor into @DataRow, @OldPk, @OldDataRow $(oldKeyVariables) $(newKeyVariables), @ChannelId \n"
+
" end \n"
+
" close DataCursor \n"
+
" deallocate cursor DataCursor \n"
+
" end \n"
+
" end \n"
+
" $(custom_on_update_text) \n"
+
" set nocount off \n"
+
" end \n");
sqlTemplates.put("deleteTriggerTemplate",
"create trigger $(triggerName) on $(schemaName)$(tableName) for delete " + getOrderClause() + " as\n" +
" begin \n"
+
" set nocount on \n" +
" declare @OldPk varchar(2000) \n"
+
" declare @OldDataRow varchar(16384) \n"
+
" declare @ChannelId varchar(128) \n" +
" declare @txid varchar(50) \n"
+
" if (@@TRANCOUNT > 0) begin \n"
+
" select @txid = $(txIdExpression) \n" +
" end \n"
+
" declare @clientapplname varchar(50) \n" +
" declare @clientname varchar(50) \n" +
" select @clientapplname = clientapplname, @clientname = case when clientapplname = 'SymmetricDS' then clientname else null end from master.dbo.sysprocesses where spid = @@spid \n"
+
" $(declareOldKeyVariables) \n"
+
" $(custom_before_delete_text) \n" +
" if ($(syncOnIncomingBatchCondition)) begin \n"
+
" declare DataCursor cursor for \n"
+
" select $(oldKeys), $(oldColumns) $(oldKeyNames), $(channelExpression) from deleted where $(syncOnDeleteCondition) \n"
+
" open DataCursor \n"
+
" fetch DataCursor into @OldPk, @OldDataRow $(oldKeyVariables), @ChannelId \n"
+
" while @@sqlstatus = 0 begin \n"
+
" insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, pk_data, old_data, channel_id, transaction_id, source_node_id, external_data, create_time) \n"
+
" values('$(targetTableName)','D', $(triggerHistoryId), @OldPk, @OldDataRow, @ChannelId, @txid, @clientname, $(externalSelect), getdate())\n"
+
" fetch DataCursor into @OldPk,@OldDataRow $(oldKeyVariables), @ChannelId \n"
+
" end \n"
+
" close DataCursor \n"
+
" deallocate cursor DataCursor \n"
+
" end \n"
+
" $(custom_on_delete_text) \n" +
" set nocount off \n" +
" end ");
sqlTemplates.put("initialLoadSqlTemplate",
"select $(columns) from $(schemaName)$(tableName) t where $(whereClause) ");
}
protected String getOrderClause() {
return "";
}
@Override
protected String replaceTemplateVariables(DataEventType dml, Trigger trigger,
TriggerHistory history, Channel channel, String tablePrefix, Table originalTable, Table table,
String defaultCatalog, String defaultSchema, String ddl) {
ddl = FormatUtils.replace("oldColumns", trigger.isUseCaptureOldData() ? super.buildColumnsString(ORIG_TABLE_ALIAS, oldTriggerValue, oldColumnPrefix,
table, table.getColumns(), dml, true, channel, trigger).toString() : "convert(VARCHAR,null)", ddl);
ddl = super.replaceTemplateVariables(dml, trigger, history, channel, tablePrefix, originalTable, table,
defaultCatalog, defaultSchema, ddl);
Column[] columns = table.getPrimaryKeyColumns();
ddl = FormatUtils.replace("declareOldKeyVariables",
buildKeyVariablesDeclare(columns, "old"), ddl);
ddl = FormatUtils.replace("declareNewKeyVariables",
buildKeyVariablesDeclare(columns, "new"), ddl);
return ddl;
}
@Override
protected String buildKeyVariablesDeclare(Column[] columns, String prefix) {
String text = "";
for (int i = 0; i < columns.length; i++) {
text += "declare @" + prefix + "pk" + i + " ";
switch (columns[i].getMappedTypeCode()) {
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
case Types.BIGINT:
// ASE does not support bigint
text += "NUMERIC(18,0)\n";
break;
case Types.NUMERIC:
case Types.DECIMAL:
// Use same default scale and precision used by Sybase ASA
// for a decimal with unspecified scale and precision.
text += "decimal(30,6)\n";
break;
case Types.FLOAT:
case Types.REAL:
case Types.DOUBLE:
text += "float\n";
break;
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
text += "varchar(1000)\n";
break;
case Types.DATE:
text += "date\n";
break;
case Types.TIME:
text += "time\n";
break;
case Types.TIMESTAMP:
text += "datetime\n";
break;
case Types.BOOLEAN:
case Types.BIT:
text += "bit\n";
break;
case Types.CLOB:
text += "varchar(32767)\n";
break;
case Types.BLOB:
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
case -10: // SQL-Server ntext binary type
text += "varbinary(32767)\n";
break;
case Types.OTHER:
text += "varbinary(32767)\n";
break;
default:
if (columns[i].getJdbcTypeName() != null
&& columns[i].getJdbcTypeName().equalsIgnoreCase("interval")) {
text += "interval";
break;
}
throw new NotImplementedException(columns[i] + " is of type "
+ columns[i].getMappedType());
}
}
return text;
}
}