/
Db2DbDialect.java
163 lines (133 loc) · 4.77 KB
/
Db2DbDialect.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
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Eric Long <erilong@users.sourceforge.net>,
* Eric Weimer <eweimer@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/
package org.jumpmind.symmetric.db.db2;
import java.net.URL;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.db.AbstractDbDialect;
import org.jumpmind.symmetric.db.BinaryEncoding;
import org.jumpmind.symmetric.db.IDbDialect;
import org.jumpmind.symmetric.db.SqlScript;
import org.jumpmind.symmetric.model.Trigger;
public class Db2DbDialect extends AbstractDbDialect implements IDbDialect {
static final String SYNC_TRIGGERS_DISABLED_USER_VARIABLE = "sync_triggers_disabled";
static final String SYNC_TRIGGERS_DISABLED_NODE_VARIABLE = "sync_node_disabled";
@Override
protected void initForSpecificDialect() {
try {
enableSyncTriggers();
} catch (Exception e) {
try {
log.info("EnvironmentVariablesCreating", SYNC_TRIGGERS_DISABLED_USER_VARIABLE,
SYNC_TRIGGERS_DISABLED_NODE_VARIABLE);
new SqlScript(getSqlScriptUrl(), getPlatform().getDataSource(), ";").execute();
} catch (Exception ex) {
log.error("DB2DialectInitializingError", ex);
}
}
}
private URL getSqlScriptUrl() {
return getClass().getResource("/dialects/db2.sql");
}
@Override
protected boolean doesTriggerExistOnPlatform(String catalog, String schema, String tableName, String triggerName) {
schema = schema == null ? (getDefaultSchema() == null ? null : getDefaultSchema()) : schema;
return jdbcTemplate.queryForInt("select count(*) from syscat.triggers where trigname = ?",
new Object[] { triggerName.toUpperCase() }) > 0;
}
@Override
public boolean isBlobSyncSupported() {
return true;
}
@Override
public boolean isClobSyncSupported() {
return true;
}
@Override
public BinaryEncoding getBinaryEncoding() {
return BinaryEncoding.HEX;
}
public void disableSyncTriggers(String nodeId) {
jdbcTemplate.update("set " + SYNC_TRIGGERS_DISABLED_USER_VARIABLE + "=1");
if (nodeId != null) {
jdbcTemplate.update("set " + SYNC_TRIGGERS_DISABLED_NODE_VARIABLE + "='" + nodeId + "'");
}
}
public void enableSyncTriggers() {
jdbcTemplate.update("set " + SYNC_TRIGGERS_DISABLED_USER_VARIABLE + "=null");
jdbcTemplate.update("set " + SYNC_TRIGGERS_DISABLED_NODE_VARIABLE + "=null");
}
public String getSyncTriggersExpression() {
return SYNC_TRIGGERS_DISABLED_USER_VARIABLE + " is null";
}
@Override
public String getTransactionTriggerExpression(String defaultCatalog, String defaultSchema, Trigger trigger) {
return "null";
}
@Override
public String getSelectLastInsertIdSql(String sequenceName) {
return "values IDENTITY_VAL_LOCAL()";
}
public boolean isCharSpacePadded() {
return true;
}
public boolean isCharSpaceTrimmed() {
return false;
}
public boolean isEmptyStringNulled() {
return false;
}
@Override
public boolean supportsTransactionId() {
return false;
}
@Override
public boolean storesUpperCaseNamesInCatalog() {
return true;
}
@Override
public boolean supportsGetGeneratedKeys() {
return false;
}
@Override
protected boolean allowsNullForIdentityColumn() {
return false;
}
public void purge() {
}
public String getDefaultCatalog() {
return null;
}
@Override
public String getDefaultSchema() {
if (StringUtils.isBlank(this.defaultSchema)) {
this.defaultSchema = (String) jdbcTemplate.queryForObject("values CURRENT SCHEMA", String.class);
}
return this.defaultSchema;
}
@Override
public String getIdentifierQuoteString() {
return "";
}
@Override
public void truncateTable(String tableName) {
jdbcTemplate.update("delete from " + tableName);
}
}