/
MsSql2000TriggerTemplate.java
203 lines (190 loc) · 28.3 KB
/
MsSql2000TriggerTemplate.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
/**
* 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.mssql2000;
import java.util.HashMap;
import org.jumpmind.symmetric.db.ISymmetricDialect;
import org.jumpmind.symmetric.db.mssql.MsSqlTriggerTemplate;
public class MsSql2000TriggerTemplate extends MsSqlTriggerTemplate {
public MsSql2000TriggerTemplate(ISymmetricDialect symmetricDialect) {
super(symmetricDialect);
this.symmetricDialect = symmetricDialect;
// @formatter:off
emptyColumnTemplate = "''" ;
stringColumnTemplate = "case when $(tableAlias).\"$(columnName)\" is null then '' else '\"' + replace(replace(convert(varchar(8000),$(tableAlias).\"$(columnName)\") $(masterCollation),'\\','\\\\'),'\"','\\\"') + '\"' end" ;
geometryColumnTemplate = "case when $(tableAlias).\"$(columnName)\" is null then '' else '\"' + replace(replace(convert(varchar(8000),$(tableAlias).\"$(columnName)\".STAsText()) $(masterCollation),'\\','\\\\'),'\"','\\\"') + '\"' end" ;
numberColumnTemplate = "case when $(tableAlias).\"$(columnName)\" is null then '' else ('\"' + convert(varchar(40), $(tableAlias).\"$(columnName)\",2) + '\"') end" ;
datetimeColumnTemplate = "case when $(tableAlias).\"$(columnName)\" is null then '' else ('\"' + convert(varchar,$(tableAlias).\"$(columnName)\",121) + '\"') end" ;
clobColumnTemplate = "case when $(origTableAlias).\"$(columnName)\" is null then '' else '\"' + replace(replace(cast($(origTableAlias).\"$(columnName)\" as varchar(8000)),'\\','\\\\'),'\"','\\\"') + '\"' end" ;
blobColumnTemplate = "case when $(origTableAlias).\"$(columnName)\" is null then '' else '\"' + replace(replace($(defaultCatalog)dbo.sym_base64_encode(CONVERT(VARBINARY(8000), $(origTableAlias).\"$(columnName)\")),'\\','\\\\'),'\"','\\\"') + '\"' end" ;
binaryColumnTemplate = "case when $(tableAlias).\"$(columnName)\" is null then '' else '\"' + replace(replace($(defaultCatalog)dbo.sym_base64_encode(CONVERT(VARBINARY(8000), $(tableAlias).\"$(columnName)\")),'\\','\\\\'),'\"','\\\"') + '\"' end" ;
booleanColumnTemplate = "case when $(tableAlias).\"$(columnName)\" is null then '' when $(tableAlias).\"$(columnName)\" = 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) after insert as \n" +
" begin \n" +
" declare @NCT int \n" +
" set @NCT = @@OPTIONS & 512 \n" +
" set nocount on; \n" +
" declare @TransactionId varchar(1000) \n" +
" declare @DataRow varchar(8000) \n" +
" declare @ChannelId varchar(128) \n" +
" $(declareNewKeyVariables) \n" +
" if (@@TRANCOUNT > 0) begin \n" +
" execute sp_getbindtoken @TransactionId output; \n" +
" end \n" +
" if ($(syncOnIncomingBatchCondition)) begin \n" +
" declare DataCursor cursor local 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 next from DataCursor into @DataRow $(newKeyVariables), @ChannelId \n" +
" while @@FETCH_STATUS = 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, $(txIdExpression), $(defaultCatalog)dbo.sym_node_disabled(), $(externalSelect), current_timestamp) \n" +
" fetch next from DataCursor into @DataRow $(newKeyVariables), @ChannelId \n" +
" end \n" +
" close DataCursor \n" +
" deallocate DataCursor \n" +
" end \n" +
" $(custom_on_insert_text) \n" +
" if (@NCT = 0) set nocount off \n" +
" end " );
sqlTemplates.put("updateTriggerTemplate" ,
"create trigger $(triggerName) on $(schemaName)$(tableName) after update as \n" +
" begin \n" +
" declare @NCT int \n" +
" set @NCT = @@OPTIONS & 512 \n" +
" set nocount on; \n" +
" declare @TransactionId varchar(1000) \n" +
" declare @DataRow varchar(8000) \n" +
" declare @OldPk varchar(2000) \n" +
" declare @OldDataRow varchar(8000) \n" +
" declare @ChannelId varchar(128) \n" +
" $(declareOldKeyVariables) \n" +
" $(declareNewKeyVariables) \n" +
" if (@@TRANCOUNT > 0) begin \n" +
" execute sp_getbindtoken @TransactionId output; \n" +
" end \n" +
" if ($(syncOnIncomingBatchCondition)) begin \n" +
" declare DataCursor cursor local 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 next from DataCursor into @DataRow, @OldPk, @OldDataRow $(oldKeyVariables) $(newKeyVariables), @ChannelId \n" +
" while @@FETCH_STATUS = 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, $(txIdExpression), $(defaultCatalog)dbo.sym_node_disabled(), $(externalSelect), current_timestamp)\n" +
" fetch next from DataCursor into @DataRow, @OldPk, @OldDataRow $(oldKeyVariables) $(newKeyVariables), @ChannelId \n" +
" end \n" +
" close DataCursor \n" +
" deallocate DataCursor \n" +
" end \n" +
" $(custom_on_update_text) \n" +
" if (@NCT = 0) set nocount off \n" +
" end " );
sqlTemplates.put("updateHandleKeyUpdatesTriggerTemplate" ,
"create trigger $(triggerName) on $(schemaName)$(tableName) after update as \n" +
" begin \n" +
" declare @NCT int \n" +
" set @NCT = @@OPTIONS & 512 \n" +
" set nocount on; \n" +
" declare @TransactionId varchar(1000) \n" +
" declare @OldPk varchar(2000) \n" +
" declare @OldDataRow varchar(8000) \n" +
" declare @DataRow varchar(8000) \n" +
" declare @ChannelId varchar(128) \n" +
" $(declareOldKeyVariables) \n" +
" $(declareNewKeyVariables) \n" +
" if (@@TRANCOUNT > 0) begin \n" +
" execute sp_getbindtoken @TransactionId output; \n" +
" end \n" +
" if ($(syncOnIncomingBatchCondition)) begin \n" +
" declare DeleteCursor cursor local for \n" +
" select $(oldKeys), $(oldColumns) $(oldKeyNames) from deleted where $(syncOnDeleteCondition) \n" +
" declare InsertCursor cursor local 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 DeleteCursor \n" +
" open InsertCursor \n" +
" fetch next from DeleteCursor into @OldPk, @OldDataRow $(oldKeyVariables) \n" +
" fetch next from InsertCursor into @DataRow $(newKeyVariables), @ChannelId \n" +
" while @@FETCH_STATUS = 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, $(txIdExpression), $(defaultCatalog)dbo.sym_node_disabled(), $(externalSelect), current_timestamp)\n" +
" fetch next from DeleteCursor into @OldPk, @OldDataRow $(oldKeyVariables) \n" +
" fetch next from InsertCursor into @DataRow $(newKeyVariables), @ChannelId \n" +
" end \n" +
" close DeleteCursor \n" +
" close InsertCursor \n" +
" deallocate DeleteCursor \n" +
" deallocate InsertCursor \n" +
" end \n" +
" $(custom_on_update_text) \n" +
" if (@NCT = 0) set nocount off \n" +
" end " );
sqlTemplates.put("deleteTriggerTemplate" ,
"create trigger $(triggerName) on $(schemaName)$(tableName) after delete as \n" +
" begin \n" +
" declare @NCT int \n" +
" set @NCT = @@OPTIONS & 512 \n" +
" set nocount on; \n" +
" declare @TransactionId varchar(1000) \n" +
" declare @OldPk varchar(2000) \n" +
" declare @OldDataRow varchar(8000) \n" +
" declare @ChannelId varchar(128) \n" +
" $(declareOldKeyVariables) \n" +
" if (@@TRANCOUNT > 0) begin \n" +
" execute sp_getbindtoken @TransactionId output; \n" +
" end \n" +
" if ($(syncOnIncomingBatchCondition)) begin \n" +
" declare DataCursor cursor local for \n" +
" select $(oldKeys), $(oldColumns) $(oldKeyNames), $(channelExpression) from deleted where $(syncOnDeleteCondition) \n" +
" open DataCursor \n" +
" fetch next from DataCursor into @OldPk, @OldDataRow $(oldKeyVariables), @ChannelId \n" +
" while @@FETCH_STATUS = 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, $(txIdExpression), $(defaultCatalog)dbo.sym_node_disabled(), $(externalSelect), current_timestamp)\n" +
" fetch next from DataCursor into @OldPk,@OldDataRow $(oldKeyVariables), @ChannelId \n" +
" end \n" +
" close DataCursor \n" +
" deallocate DataCursor \n" +
" end \n" +
" $(custom_on_delete_text) \n" +
" if (@NCT = 0) set nocount off \n" +
" end " );
sqlTemplates.put("initialLoadSqlTemplate" ,
"select $(columns) from $(schemaName)$(tableName) t where $(whereClause) " );
}
}