Skip to content

Commit

Permalink
0003899: When changing just a LOB field on SQL Server, the change is not
Browse files Browse the repository at this point in the history
captured by the trigger
  • Loading branch information
philipmarzullo64 committed Apr 5, 2019
1 parent 2139c4d commit 6aba9f6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
Expand Up @@ -63,6 +63,7 @@
import org.jumpmind.symmetric.db.informix.InformixSymmetricDialect;
import org.jumpmind.symmetric.db.interbase.InterbaseSymmetricDialect;
import org.jumpmind.symmetric.db.mariadb.MariaDBSymmetricDialect;
import org.jumpmind.symmetric.db.mssql.MsSql2008SymmetricDialect;
import org.jumpmind.symmetric.db.mssql.MsSqlSymmetricDialect;
import org.jumpmind.symmetric.db.mssql2000.MsSql2000SymmetricDialect;
import org.jumpmind.symmetric.db.mysql.MySqlSymmetricDialect;
Expand Down Expand Up @@ -108,7 +109,7 @@ public ISymmetricDialect create() {
} else if (platform instanceof OracleDatabasePlatform) {
dialect = new OracleSymmetricDialect(parameterService, platform);
} else if (platform instanceof MsSql2008DatabasePlatform) {
dialect = new MsSqlSymmetricDialect(parameterService, platform);
dialect = new MsSql2008SymmetricDialect(parameterService, platform);
} else if (platform instanceof MsSql2005DatabasePlatform) {
dialect = new MsSqlSymmetricDialect(parameterService, platform);
} else if (platform instanceof MsSql2000DatabasePlatform) {
Expand Down
@@ -0,0 +1,22 @@
package org.jumpmind.symmetric.db.mssql;

import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.symmetric.model.Trigger;
import org.jumpmind.symmetric.service.IParameterService;

public class MsSql2008SymmetricDialect extends MsSqlSymmetricDialect {
public MsSql2008SymmetricDialect() {
super();
}

public MsSql2008SymmetricDialect(IParameterService parameterService, IDatabasePlatform platform) {
super(parameterService, platform);
this.triggerTemplate = new MsSql2008TriggerTemplate(this);
}

@Override
protected String getDbSpecificDataHasChangedCondition(Trigger trigger) {
/* gets filled/replaced by trigger template as it will compare by each column */
return "$(anyColumnChanged)";
}
}
@@ -0,0 +1,50 @@
package org.jumpmind.symmetric.db.mssql;

import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Table;
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 MsSql2008TriggerTemplate extends MsSqlTriggerTemplate {
public MsSql2008TriggerTemplate(ISymmetricDialect symmetricDialect) {
super(symmetricDialect);
}

@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 = super.replaceTemplateVariables(dml, trigger, history, channel, tablePrefix, originalTable, table, defaultCatalog, defaultSchema, ddl);
ddl = FormatUtils.replace("anyColumnChanged",
buildColumnsAreNotEqualString(table, newTriggerValue, oldTriggerValue), ddl);
return ddl;
}

private String buildColumnsAreNotEqualString(Table table, String table1Name, String table2Name){
StringBuilder builder = new StringBuilder();

for(Column column : table.getColumns()){
if (builder.length() > 0) {
builder.append(" or ");
}

if(isNotComparable(column)) {
// Can't compare the value.
// Let's use the UPDATE() function to see if it showed up in the SET list of the update statement
builder.append(String.format("UPDATE(\"%1$s\")", column.getName()));
} else {
builder.append(String.format("((%1$s.\"%2$s\" IS NOT NULL AND %3$s.\"%2$s\" IS NOT NULL AND %1$s.\"%2$s\"<>%3$s.\"%2$s\") or "
+ "(%1$s.\"%2$s\" IS NULL AND %3$s.\"%2$s\" IS NOT NULL) or "
+ "(%1$s.\"%2$s\" IS NOT NULL AND %3$s.\"%2$s\" IS NULL))", table1Name, column.getName(), table2Name));
}
}
if (builder.length() == 0) {
builder.append("1=1");
}
return builder.toString();
}
}
Expand Up @@ -284,7 +284,7 @@ protected String replaceTemplateVariables(DataEventType dml, Trigger trigger,
return ddl;
}

private boolean isNotComparable(Column column) {
protected boolean isNotComparable(Column column) {
String columnType = column.getJdbcTypeName();
return StringUtils.equalsIgnoreCase(columnType, "IMAGE")
|| StringUtils.equalsIgnoreCase(columnType, "TEXT")
Expand Down

0 comments on commit 6aba9f6

Please sign in to comment.