/
InterbaseSymmetricDialect.java
250 lines (212 loc) · 10.7 KB
/
InterbaseSymmetricDialect.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.db.interbase;
import java.sql.Types;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.sql.ISqlTransaction;
import org.jumpmind.db.sql.mapper.StringMapper;
import org.jumpmind.db.util.BinaryEncoding;
import org.jumpmind.symmetric.db.AbstractSymmetricDialect;
import org.jumpmind.symmetric.db.ISymmetricDialect;
import org.jumpmind.symmetric.db.SequenceIdentifier;
import org.jumpmind.symmetric.model.Trigger;
import org.jumpmind.symmetric.service.IParameterService;
import org.springframework.jdbc.UncategorizedSQLException;
/*
* Database dialect for <a href="http://www.embarcadero.com/products/interbase/">Interbase</a>.
*/
public class InterbaseSymmetricDialect extends AbstractSymmetricDialect implements ISymmetricDialect {
public static final String CONTEXT_TABLE_NAME = "temp_context";
static final String CONTEXT_TABLE_CREATE = "create global temporary table %s (name varchar(30), context_value varchar(30)) on commit preserve rows";
static final String CONTEXT_TABLE_INSERT = "insert into %s (name, context_value) values (?, ?)";
static final String SYNC_TRIGGERS_DISABLED_USER_VARIABLE = "sync_triggers_disabled";
static final String SYNC_TRIGGERS_DISABLED_NODE_VARIABLE = "sync_node_disabled";
static final String SQL_FUNCTION_INSTALLED = "select count(*) from rdb$functions where rdb$function_name = upper('$(functionName)')" ;
static final String SQL_DROP_FUNCTION = "DROP EXTERNAL FUNCTION $(functionName)";
public InterbaseSymmetricDialect(IParameterService parameterService, IDatabasePlatform platform) {
super(parameterService, platform);
this.triggerTemplate = new InterbaseTriggerTemplate(this);
}
@Override
public void createRequiredDatabaseObjects() {
String contextTableName = parameterService.getTablePrefix() + "_" + CONTEXT_TABLE_NAME;
try {
platform.getSqlTemplate().queryForInt("select count(*) from " + contextTableName);
} catch (Exception e) {
try {
log.info("Creating global temporary table {}", contextTableName);
platform.getSqlTemplate().update(String.format(CONTEXT_TABLE_CREATE, contextTableName));
} catch (Exception ex) {
log.error("Error while initializing Interbase dialect", ex);
}
}
String escape = this.parameterService.getTablePrefix() + "_" + "escape";
if (!installed(SQL_FUNCTION_INSTALLED, escape)) {
String sql = "declare external function $(functionName) cstring(32660) " +
" returns cstring(32660) free_it entry_point 'sym_escape' module_name 'sym_udf' ";
install(sql, escape);
}
String hex = this.parameterService.getTablePrefix() + "_" + "hex";
if (!installed(SQL_FUNCTION_INSTALLED, hex)) {
String sql = "declare external function $(functionName) blob " +
" returns cstring(32660) free_it entry_point 'sym_hex' module_name 'sym_udf' ";
install(sql, hex);
}
String rtrim = this.parameterService.getTablePrefix() + "_" + "rtrim";
if (!installed(SQL_FUNCTION_INSTALLED, rtrim)) {
String sql = "declare external function $(functionName) cstring(32767) " +
" returns cstring(32767) free_it entry_point 'IB_UDF_rtrim' module_name 'ib_udf' ";
install(sql, rtrim);
}
try {
platform.getSqlTemplate().queryForObject("select sym_escape('') from rdb$database", String.class);
} catch (UncategorizedSQLException e) {
if (e.getSQLException().getErrorCode() == -804) {
log.error("Please install the sym_udf.so/dll to your {interbase_home}/UDF folder");
}
throw new RuntimeException("Function SYM_ESCAPE is not installed", e);
}
}
@Override
public void dropRequiredDatabaseObjects() {
String escape = this.parameterService.getTablePrefix() + "_" + "escape";
if (installed(SQL_FUNCTION_INSTALLED, escape)) {
uninstall(SQL_DROP_FUNCTION, escape);
}
String hex = this.parameterService.getTablePrefix() + "_" + "hex";
if (installed(SQL_FUNCTION_INSTALLED, hex)) {
uninstall(SQL_DROP_FUNCTION, hex);
}
String rtrim = this.parameterService.getTablePrefix() + "_" + "rtrim";
if (installed(SQL_FUNCTION_INSTALLED, rtrim)) {
uninstall(SQL_DROP_FUNCTION, rtrim);
}
}
@Override
protected boolean doesTriggerExistOnPlatform(String catalogName, String schema, String tableName, String triggerName) {
return platform.getSqlTemplate().queryForInt("select count(*) from rdb$triggers where rdb$trigger_name = ?",
new Object[] { triggerName.toUpperCase() }) > 0;
}
public void disableSyncTriggers(ISqlTransaction transaction, String nodeId) {
String contextTableName = parameterService.getTablePrefix() + "_" + CONTEXT_TABLE_NAME;
transaction.prepareAndExecute(String.format(CONTEXT_TABLE_INSERT, contextTableName), new Object[] {
SYNC_TRIGGERS_DISABLED_USER_VARIABLE, "1" });
if (nodeId != null) {
transaction.prepareAndExecute(String.format(CONTEXT_TABLE_INSERT, contextTableName), new Object[] {
SYNC_TRIGGERS_DISABLED_NODE_VARIABLE, nodeId });
}
}
public void enableSyncTriggers(ISqlTransaction transaction) {
String contextTableName = parameterService.getTablePrefix() + "_" + CONTEXT_TABLE_NAME;
transaction.prepareAndExecute("delete from " + contextTableName);
}
public String getSyncTriggersExpression() {
return ":" + SYNC_TRIGGERS_DISABLED_USER_VARIABLE + " is null";
}
@Override
public String getSequenceName(SequenceIdentifier identifier) {
switch (identifier) {
case REQUEST:
return "SYM_EXTRACT_RE_ST_REQUEST_ID";
case DATA:
return "SYM_DATA_DATA_ID";
case TRIGGER_HIST:
return "SYM_TRIGGER_TRIGGER_HIST_ID";
}
return null;
}
@Override
public String getTransactionTriggerExpression(String defaultCatalog, String defaultSchema, Trigger trigger) {
return "null";
}
@Override
public boolean isBlobSyncSupported() {
return true;
}
@Override
public BinaryEncoding getBinaryEncoding() {
return BinaryEncoding.HEX;
}
public void cleanDatabase() {
}
@Override
public String getName() {
return super.getName();
}
@Override
public boolean supportsOpenCursorsAcrossCommit() {
return false;
}
@Override
public boolean supportsTransactionId() {
return false;
}
@Override
public boolean supportsBatchUpdates() {
// Interbase and interclient driver do have support for batch updates,
// but we get primary/unique key violation when enabling its use
return false;
}
@Override
public void truncateTable(String tableName) {
platform.getSqlTemplate().update("delete from " + tableName);
}
@Override
public void cleanupTriggers() {
List<String> names = platform.getSqlTemplate().query("select rdb$trigger_name from rdb$triggers where rdb$trigger_name like '"+parameterService.getTablePrefix().toUpperCase()+"_%'", new StringMapper());
for (String name : names) {
platform.getSqlTemplate().update("drop trigger " + name);
log.info("Dropped trigger {}", name);
}
}
@Override
public String massageDataExtractionSql(String sql, boolean isContainsBigLob) {
if (!isContainsBigLob) {
sql = StringUtils.replace(sql, "d.row_data", "cast(d.row_data as varchar(10000))");
sql = StringUtils.replace(sql, "d.old_data", "cast(d.old_data as varchar(10000))");
sql = StringUtils.replace(sql, "d.pk_data", "cast(d.pk_data as varchar(500))");
}
return sql;
}
@Override
public long getCurrentSequenceValue(SequenceIdentifier identifier) {
return platform.getSqlTemplate().queryForLong("select gen_id(GEN_" + getSequenceName(identifier) + ", 0) from rdb$database");
}
@Override
public Database readSymmetricSchemaFromXml() {
Database db = super.readSymmetricSchemaFromXml();
// Change sym_trigger table column description to only allow 1024 characters to make row width less than 64K characters
String prefix = parameterService.getTablePrefix();
if (StringUtils.isNotBlank(prefix) && !prefix.endsWith("_")) {
prefix = prefix + "_";
}
Column description = db.findTable(prefix+"trigger").findColumn("description");
description.setJdbcTypeCode(Types.VARCHAR);
description.setMappedType(TypeMap.VARCHAR);
description.setSize("1024");
return db;
}
}