/
PostgreSqlDbDialect.java
290 lines (238 loc) · 9.44 KB
/
PostgreSqlDbDialect.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* 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 Lesser General Public License (the
* "License"); you may not use this file except in compliance
* with the License.
*
* 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/>.
*
* 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.postgresql;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;
import javax.sql.rowset.serial.SerialBlob;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.SymmetricException;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.db.AbstractDbDialect;
import org.jumpmind.symmetric.db.BinaryEncoding;
import org.jumpmind.symmetric.db.IDbDialect;
import org.jumpmind.symmetric.ddl.Platform;
import org.jumpmind.symmetric.ddl.model.Column;
import org.jumpmind.symmetric.model.Trigger;
import org.jumpmind.symmetric.model.TriggerHistory;
import org.springframework.jdbc.core.JdbcTemplate;
/**
*
*/
public class PostgreSqlDbDialect extends AbstractDbDialect implements IDbDialect {
static final String TRANSACTION_ID_EXPRESSION = "txid_current()";
static final String SYNC_TRIGGERS_DISABLED_VARIABLE = "symmetric.triggers_disabled";
static final String SYNC_NODE_DISABLED_VARIABLE = "symmetric.node_disabled";
private boolean supportsTransactionId = false;
private String transactionIdExpression = "null";
@Override
public void init(Platform pf, int queryTimeout, JdbcTemplate jdbcTemplate) {
super.init(pf, 0, jdbcTemplate);
}
@Override
protected void initTablesAndFunctionsForSpecificDialect() {
if (getMajorVersion() > 8 || (getMajorVersion() == 8 && getMinorVersion() >= 3)) {
log.info("TransactionIDSupportEnabling");
supportsTransactionId = true;
transactionIdExpression = TRANSACTION_ID_EXPRESSION;
}
try {
enableSyncTriggers(jdbcTemplate);
} catch (Exception e) {
log.error("PostgreSqlCustomVariableMissing");
throw new SymmetricException("PostgreSqlCustomVariableMissing", e);
}
}
@Override
public boolean requiresAutoCommitFalseToSetFetchSize() {
return true;
}
@Override
protected Integer overrideJdbcTypeForColumn(Map<Object,Object> values) {
String typeName = (String) values.get("TYPE_NAME");
if (typeName != null && typeName.equalsIgnoreCase("ABSTIME")) {
return Types.TIMESTAMP;
} else if (typeName != null && typeName.equalsIgnoreCase("OID")) {
return Types.BLOB;
} else {
return super.overrideJdbcTypeForColumn(values);
}
}
@Override
public Object[] getObjectValues(BinaryEncoding encoding, String[] values,
Column[] orderedMetaData) {
Object[] objectValues = super.getObjectValues(encoding, values, orderedMetaData);
for (int i = 0; i < orderedMetaData.length; i++) {
if (orderedMetaData[i] != null && orderedMetaData[i].getTypeCode() == Types.BLOB) {
try {
objectValues[i] = new SerialBlob((byte[]) objectValues[i]);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return objectValues;
}
@Override
protected boolean doesTriggerExistOnPlatform(String catalogName, String schema, String tableName, String triggerName) {
return jdbcTemplate.queryForInt("select count(*) from information_schema.triggers where trigger_name = ? "
+ "and event_object_table = ? and trigger_schema = ?", new Object[] { triggerName.toLowerCase(),
tableName.toLowerCase(), schema == null ? defaultSchema : schema }) > 0;
}
@Override
public void removeTrigger(StringBuilder sqlBuffer, String catalogName, String schemaName, String triggerName,
String tableName, TriggerHistory oldHistory) {
schemaName = schemaName == null ? "" : (schemaName + ".");
final String dropSql = "drop trigger " + triggerName + " on " + schemaName + tableName;
logSql(dropSql, sqlBuffer);
final String dropFunction = "drop function " + schemaName + "f" + triggerName + "()";
logSql(dropFunction, sqlBuffer);
if (parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
try {
jdbcTemplate.update(dropSql);
jdbcTemplate.update(dropFunction);
} catch (Exception e) {
log.warn("TriggerDoesNotExist");
}
}
}
public void disableSyncTriggers(JdbcTemplate jdbcTemplate, String nodeId) {
jdbcTemplate.queryForList("select set_config('" + SYNC_TRIGGERS_DISABLED_VARIABLE + "', '1', false)");
if (nodeId == null) {
nodeId = "";
}
jdbcTemplate.queryForList("select set_config('" + SYNC_NODE_DISABLED_VARIABLE + "', '" + nodeId + "', false)");
}
public void enableSyncTriggers(JdbcTemplate jdbcTemplate) {
jdbcTemplate.queryForList("select set_config('" + SYNC_TRIGGERS_DISABLED_VARIABLE
+ "', '', false)");
jdbcTemplate.queryForList("select set_config('" + SYNC_NODE_DISABLED_VARIABLE
+ "', '', false)");
}
public String getSyncTriggersExpression() {
return "$(defaultSchema)" + tablePrefix + "_triggers_disabled() = 0";
}
@Override
public String getTransactionTriggerExpression(String defaultCatalog, String defaultSchema, Trigger trigger) {
return transactionIdExpression;
}
@Override
public String getSelectLastInsertIdSql(String sequenceName) {
return "select currval('" + sequenceName + "_seq')";
}
@Override
public boolean requiresSavepointForFallback() {
return true;
}
public boolean isNonBlankCharColumnSpacePadded() {
return true;
}
public boolean isCharColumnSpaceTrimmed() {
return false;
}
public boolean isEmptyStringNulled() {
return false;
}
@Override
public boolean storesLowerCaseNamesInCatalog() {
return true;
}
@Override
protected boolean allowsNullForIdentityColumn() {
return false;
}
@Override
public boolean supportsTransactionId() {
return supportsTransactionId;
}
public void purge() {
}
public String getDefaultCatalog() {
return null;
}
@Override
public String getDefaultSchema() {
if (StringUtils.isBlank(this.defaultSchema)) {
defaultSchema = (String) jdbcTemplate.queryForObject("select current_schema()", String.class);
}
return super.getDefaultSchema();
}
@Override
public BinaryEncoding getBinaryEncoding() {
return BinaryEncoding.BASE64;
}
@Override
protected Array createArray(Column column, final String value) {
if (StringUtils.isNotBlank(value)) {
String jdbcTypeName = column.getJdbcTypeName();
if (jdbcTypeName.startsWith("_")) {
jdbcTypeName = jdbcTypeName.substring(1);
}
int jdbcBaseType = Types.VARCHAR;
if (jdbcTypeName.toLowerCase().contains("int")) {
jdbcBaseType = Types.INTEGER;
}
final String baseTypeName = jdbcTypeName;
final int baseType = jdbcBaseType;
return new Array() {
public String getBaseTypeName() {
return baseTypeName;
}
public void free() throws SQLException {
}
public int getBaseType() {
return baseType;
}
public Object getArray() {
return null;
}
public Object getArray(Map<String, Class<?>> map) {
return null;
}
public Object getArray(long index, int count) {
return null;
}
public Object getArray(long index, int count, Map<String, Class<?>> map) {
return null;
}
public ResultSet getResultSet() {
return null;
}
public ResultSet getResultSet(Map<String, Class<?>> map) {
return null;
}
public ResultSet getResultSet(long index, int count) {
return null;
}
public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map) {
return null;
}
public String toString() {
return value;
}
};
} else {
return null;
}
}
}