/
mssql.xml
193 lines (190 loc) · 13.1 KB
/
mssql.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-lazy-init="true">
<bean id="msSqlStringTemplate" class="java.lang.String">
<constructor-arg>
<value>
<![CDATA[ case when $(tableAlias).$(columnName) is null then '' else '"' + replace(replace($(tableAlias).$(columnName),'\','\\'),'"','\"') + '"' end +','+]]>
</value>
</constructor-arg>
</bean>
<bean id="msSqlDialect" class="org.jumpmind.symmetric.db.mssql.MsSqlDbDialect"
scope="prototype">
<property name="tablePrefix" value="${sync.table.prefix}" />
<property name="engineName" value="${symmetric.runtime.engine.name}"/>
<property name="streamingResultsFetchSize" value="${db.jdbc.streaming.results.fetch.size}" />
<property name="sqlTemplate">
<bean class="org.jumpmind.symmetric.db.SqlTemplate">
<property name="triggerPrefix" value="${symmetric.runtime.trigger.prefix}" />
<property name="functionInstalledSql">
<value>
<![CDATA[select count(object_name(object_id('$(functionName)')))]]>
</value>
</property>
<property name="functionTemplatesToInstall">
<map>
<entry key="fn_sym_base64_encode">
<value>
<![CDATA[
create function dbo.fn_sym_base64_encode (@data varbinary(max)) returns varchar(max)
with schemabinding, returns null on null input
begin
return ( select [text()] = @data for xml path('') )
end
]]>
</value>
</entry>
</map>
</property>
<property name="stringColumnTemplate" ref="msSqlStringTemplate" />
<!-- TODO, We may have to make callbacks to the $(origTableAlias)inal table because SQL Server does not allow access to binary or clob data from a trigger -->
<property name="clobColumnTemplate">
<value>
<![CDATA[ case when $(origTableAlias).$(columnName) is null then '' else '"' + replace(replace(cast($(origTableAlias).$(columnName) as varchar(max)),'\','\\'),'"','\"') + '"' end +','+]]>
</value>
</property>
<property name="blobColumnTemplate">
<value>
<![CDATA[ case when $(origTableAlias).$(columnName) is null then '' else '"' + replace(replace(dbo.fn_sym_base64_encode($(origTableAlias).$(columnName)),'\','\\'),'"','\"') + '"' end +','+]]>
</value>
</property>
<property name="numberColumnTemplate">
<value>
<![CDATA[coalesce(cast($(tableAlias).$(columnName) as char), '') +','+]]>
</value>
</property>
<!-- TODO -->
<property name="datetimeColumnTemplate">
<value>
<![CDATA[case when $(tableAlias).$(columnName) is null then '' else ('"' + convert(varchar,$(tableAlias).$(columnName),121) + '"') end +','+]]>
</value>
</property>
<property name="triggerConcatCharacter" value="+" />
<property name="newTriggerValue" value="inserted" />
<property name="oldTriggerValue" value="deleted" />
<property name="sqlTemplates">
<map>
<entry key="insertTriggerTemplate">
<value>
<![CDATA[
create trigger $(triggerName) on $(schemaName)$(tableName) after insert as
begin
declare @TransactionId varchar(1000)
declare @SyncEnabled varbinary(128)
declare @DataRow varchar(8000)
if (@@TRANCOUNT > 0) begin
execute sp_getbindtoken @TransactionId output;
end
$(syncOnIncomingBatchCondition)
if (@SyncEnabled <> 0x1) begin
declare DataCursor cursor local for
$(if:containsBlobClobColumns)
select $(columns) from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) where $(syncOnInsertCondition)
$(else:containsBlobClobColumns)
select $(columns) from inserted where $(syncOnInsertCondition)
$(end:containsBlobClobColumns)
open DataCursor
fetch next from DataCursor into @DataRow
while @@FETCH_STATUS = 0 begin
insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, create_time)
values('$(targetTableName)','I', $(triggerHistoryId), @DataRow, current_timestamp)
if (@@ROWCOUNT > 0) begin
insert into $(defaultSchema)$(prefixName)_data_event (node_id, data_id, channel_id, transaction_id) (select node_id, @@IDENTITY, '$(channelName)', $(txIdExpression) from $(defaultSchema)$(prefixName)_node c where
c.node_group_id='$(targetGroupId)' and c.sync_enabled=1 $(nodeSelectWhere))
end
fetch next from DataCursor into @DataRow
end
close DataCursor
deallocate DataCursor
end
end
]]>
</value>
</entry>
<entry key="updateTriggerTemplate">
<value>
<![CDATA[
create trigger $(triggerName) on $(schemaName)$(tableName) after update as
begin
declare @TransactionId varchar(1000)
declare @SyncEnabled varbinary(128)
declare @DataRow varchar(8000)
declare @OldPk varchar(2000)
if (@@TRANCOUNT > 0) begin
execute sp_getbindtoken @TransactionId output;
end
$(syncOnIncomingBatchCondition)
if (@SyncEnabled <> 0x1) begin
declare DataCursor cursor local for
$(if:containsBlobClobColumns)
select $(columns), $(oldKeys) from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) inner join deleted on $(oldNewPrimaryKeyJoin) where $(syncOnInsertCondition)
$(else:containsBlobClobColumns)
select $(columns), $(oldKeys) from inserted inner join deleted on $(oldNewPrimaryKeyJoin) where $(syncOnInsertCondition)
$(end:containsBlobClobColumns)
open DataCursor
fetch next from DataCursor into @DataRow, @OldPk
while @@FETCH_STATUS = 0 begin
insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, pk_data, create_time)
values('$(targetTableName)','U', $(triggerHistoryId), @DataRow, @OldPk, current_timestamp)
if (@@ROWCOUNT > 0) begin
insert into $(defaultSchema)$(prefixName)_data_event (node_id, data_id, channel_id, transaction_id) (select node_id, @@IDENTITY, '$(channelName)', $(txIdExpression) from $(defaultSchema)$(prefixName)_node c where
c.node_group_id='$(targetGroupId)' and c.sync_enabled=1 $(nodeSelectWhere))
end
fetch next from DataCursor into @DataRow, @OldPk
end
close DataCursor
deallocate DataCursor
end
end
]]>
</value>
</entry>
<entry key="deleteTriggerTemplate">
<value>
<![CDATA[
create trigger $(triggerName) on $(schemaName)$(tableName) after delete as
begin
declare @TransactionId varchar(1000)
declare @SyncEnabled varbinary(128)
declare @OldPk varchar(2000)
if (@@TRANCOUNT > 0) begin
execute sp_getbindtoken @TransactionId output;
end
$(syncOnIncomingBatchCondition)
if (@SyncEnabled <> 0x1) begin
declare DataCursor cursor local for
select $(oldKeys) from deleted where $(syncOnDeleteCondition)
open DataCursor
fetch next from DataCursor into @OldPk
while @@FETCH_STATUS = 0 begin
insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, pk_data, create_time)
values('$(targetTableName)','D', $(triggerHistoryId), @OldPk, current_timestamp)
if (@@ROWCOUNT > 0) begin
insert into $(defaultSchema)$(prefixName)_data_event (node_id, data_id, channel_id, transaction_id) (select node_id, @@IDENTITY, '$(channelName)', $(txIdExpression) from $(defaultSchema)$(prefixName)_node c where
c.node_group_id='$(targetGroupId)' and c.sync_enabled=1 $(nodeSelectWhere))
end
fetch next from DataCursor into @OldPk
end
close DataCursor
deallocate DataCursor
end
end
]]>
</value>
</entry>
<entry key="initialLoadSqlTemplate">
<value>
<![CDATA[select $(columns) from $(schemaName)$(tableName) t where $(whereClause)]]>
</value>
</entry>
</map>
</property>
</bean>
</property>
</bean>
</beans>