/
FirebirdSymmetricDialect.java
177 lines (155 loc) · 7.32 KB
/
FirebirdSymmetricDialect.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
/**
* 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.firebird;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
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.common.ParameterConstants;
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.firebirdsql.org/">Firebird</a>.
*/
public class FirebirdSymmetricDialect extends AbstractSymmetricDialect implements ISymmetricDialect {
static final String SQL_DROP_FUNCTION = "DROP EXTERNAL FUNCTION $(functionName)";
static final String SQL_FUNCTION_INSTALLED = "select count(*) from rdb$functions where rdb$function_name = upper('$(functionName)')";
static final String SYNC_TRIGGERS_DISABLED_USER_VARIABLE = "sync_triggers_disabled";
static final String SYNC_TRIGGERS_DISABLED_NODE_VARIABLE = "sync_node_disabled";
public FirebirdSymmetricDialect(IParameterService parameterService, IDatabasePlatform platform) {
super(parameterService, platform);
this.triggerTemplate = new FirebirdTriggerTemplate(this);
}
@Override
public void createRequiredDatabaseObjects() {
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);
}
try {
platform.getSqlTemplate().queryForInt("select char_length(" + escape + "('')) from rdb$database");
} catch (UncategorizedSQLException e) {
if (e.getSQLException().getErrorCode() == -804) {
log.error("Please install the sym_udf.so/dll to your {firebird_home}/UDF folder");
}
throw new RuntimeException("Function " + 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);
}
}
@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) {
}
public void enableSyncTriggers(ISqlTransaction transaction) {
}
public String getSyncTriggersExpression() {
return "1=1";
}
@Override
public String getTransactionTriggerExpression(String defaultCatalog, String defaultSchema, Trigger trigger) {
return "current_transaction||''";
}
@Override
public boolean isBlobSyncSupported() {
return true;
}
@Override
public BinaryEncoding getBinaryEncoding() {
return BinaryEncoding.HEX;
}
public void cleanDatabase() {
}
@Override
public String getName() {
return StringUtils.left(super.getName(), 50);
}
@Override
public boolean supportsOpenCursorsAcrossCommit() {
return false;
}
@Override
public boolean supportsTransactionId() {
return true;
}
@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());
int count = 0;
for (String name : names) {
count += platform.getSqlTemplate().update("drop trigger " + name);
}
if (count > 0) {
log.info("Remove {} triggers", count);
}
}
@Override
public String massageDataExtractionSql(String sql, boolean isContainsBigLob) {
if (!isContainsBigLob) {
String[] sizes = parameterService.getString(ParameterConstants.FIREBIRD_EXTRACT_VARCHAR_ROW_OLD_PK_DATA, "20000,20000,1000").split(",");
sql = StringUtils.replace(sql, "d.row_data", "cast(d.row_data as varchar(" + sizes[0] + "))");
sql = StringUtils.replace(sql, "d.old_data", "cast(d.old_data as varchar(" + sizes[1] + "))");
sql = StringUtils.replace(sql, "d.pk_data", "cast(d.pk_data as varchar(" + sizes[2] + "))");
}
return sql;
}
@Override
public long getCurrentSequenceValue(SequenceIdentifier identifier) {
return platform.getSqlTemplate().queryForLong("select gen_id(GEN_" + getSequenceName(identifier) + ", 0) from rdb$database");
}
@Override
public String getDatabaseTimeSQL() {
return "select current_timestamp from rdb$database";
}
}