/
MsSql2008TriggerTemplate.java
50 lines (44 loc) · 2.25 KB
/
MsSql2008TriggerTemplate.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
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();
}
}