Skip to content

Commit

Permalink
Work on sql server dialect. Use cursor in trigger to support multiple…
Browse files Browse the repository at this point in the history
… row update.
  • Loading branch information
chenson42 committed Apr 22, 2008
1 parent ca7ba9e commit 88ab27e
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 46 deletions.
5 changes: 4 additions & 1 deletion symmetric/src/changes/changes.xml
Expand Up @@ -29,7 +29,10 @@
</action>
<action dev="chenson42" type="fix" issue="aid=1945207&amp;atid=997724">
Remove extra comma in mssql.xml trigger text.
</action>
</action>
<action dev="chenson42" type="fix" issue="aid=todo&amp;atid=997724">
Multi-row updates don't work for the Sql Server Dialect.
</action>
<action dev="erilong" type="fix" issue="aid=1944507&amp;atid=997724">
Bug in selectDataIdSql. data_id was not qualified.
</action>
Expand Down
Expand Up @@ -842,6 +842,10 @@ public boolean isBlobSyncSupported() {
public boolean isClobSyncSupported() {
return true;
}

public boolean isTransactionIdOverrideSupported() {
return true;
}

public void setSqlTemplate(SqlTemplate sqlTemplate) {
this.sqlTemplate = sqlTemplate;
Expand Down
Expand Up @@ -151,6 +151,12 @@ public void initTrigger(DataEventType dml, Trigger config,

public boolean isClobSyncSupported();

/**
* An indicator as to whether the ability to override the default transaction id provided by the
* dialect can be overridden in the trigger configuration.
*/
public boolean isTransactionIdOverrideSupported();

public void createTables(String xml);

public String getSelectLastInsertIdSql(String sequenceName);
Expand Down
Expand Up @@ -176,8 +176,11 @@ public String replaceTemplateVariables(IDbDialect dialect, DataEventType dml, Tr
ddl = replace("targetGroupId", trigger.getTargetGroupId(), ddl);
ddl = replace("channelName", trigger.getChannelId(), ddl);
ddl = replace("triggerHistoryId", Integer.toString(history.getTriggerHistoryId()), ddl);
ddl = replace("txIdExpression", trigger.getTxIdExpression() == null ? dialect.getTransactionTriggerExpression()
: trigger.getTxIdExpression(), ddl);
String triggerExpression = dialect.getTransactionTriggerExpression();
if (dialect.isTransactionIdOverrideSupported() && trigger.getTxIdExpression() != null) {
triggerExpression = trigger.getTxIdExpression();
}
ddl = replace("txIdExpression", triggerExpression, ddl);
ddl = replace("nodeSelectWhere", trigger.getNodeSelect(), ddl);
ddl = replace("nodeSelectWhereEscaped", replace("'", "''", trigger.getNodeSelect()), ddl);
ddl = replace("syncOnInsertCondition", trigger.getSyncOnInsertCondition(), ddl);
Expand Down
Expand Up @@ -143,6 +143,10 @@ public boolean isCharSpacePadded() {
public boolean isCharSpaceTrimmed() {
return false;
}

public boolean isTransactionIdOverrideSupported() {
return false;
}

/**
* SQL Server pads an empty string with spaces.
Expand Down
82 changes: 53 additions & 29 deletions symmetric/src/main/resources/dialects/mssql.xml
Expand Up @@ -78,22 +78,31 @@
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)
insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, create_time)
(select '$(targetTableName)','I', $(triggerHistoryId), $(columns), current_timestamp from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) where $(syncOnInsertCondition));
select $(columns) from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) where $(syncOnInsertCondition)
$(else:containsBlobClobColumns)
insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, create_time)
(select '$(targetTableName)','I', $(triggerHistoryId), $(columns), current_timestamp from inserted where $(syncOnInsertCondition));
$(end:containsBlobClobColumns)
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
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
]]>
Expand All @@ -106,29 +115,34 @@
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)
insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, pk_data, create_time)
(select '$(targetTableName)','U', $(triggerHistoryId), $(columns), $(oldKeys), current_timestamp from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) inner join deleted on $(oldNewPrimaryKeyJoin) where $(syncOnInsertCondition));
select $(columns), $(oldKeys) from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) inner join deleted on $(oldNewPrimaryKeyJoin) where $(syncOnInsertCondition)
$(else:containsBlobClobColumns)
insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, pk_data, create_time)
(select '$(targetTableName)','U', $(triggerHistoryId), $(columns), $(oldKeys), current_timestamp from inserted inner join deleted on $(oldNewPrimaryKeyJoin) where $(syncOnInsertCondition));
select $(columns), $(oldKeys) from inserted inner join deleted on $(oldNewPrimaryKeyJoin) where $(syncOnInsertCondition)
$(end:containsBlobClobColumns)
if (@@ROWCOUNT > 0) begin
if (@@ROWCOUNT = 1) begin
declare @OldKeys varchar(1000)
select @OldKeys=$(oldKeys) from deleted;
update $(defaultSchema)$(prefixName)_data set pk_data=@OldKeys where data_id=@@IDENTITY;
end
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));
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
end
end
]]>
</value>
</entry>
Expand All @@ -139,17 +153,27 @@
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
insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, pk_data, create_time)
(select '$(targetTableName)','D', $(triggerHistoryId), $(oldKeys), current_timestamp from deleted where $(syncOnDeleteCondition));
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
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
]]>
Expand Down

0 comments on commit 88ab27e

Please sign in to comment.