/
PostgreSqlDbDialect.java
190 lines (158 loc) · 6.52 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
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Eric Long <erilong@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.postgresql;
import java.sql.Types;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.common.logging.Log;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.db.AbstractDbDialect;
import org.jumpmind.symmetric.db.BinaryEncoding;
import org.jumpmind.symmetric.db.IDbDialect;
import org.jumpmind.symmetric.model.Trigger;
import org.jumpmind.symmetric.model.TriggerHistory;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
public class PostgreSqlDbDialect extends AbstractDbDialect implements IDbDialect {
static final Log logger = LogFactory.getLog(PostgreSqlDbDialect.class);
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
protected void initForSpecificDialect() {
if (getMajorVersion() >= 8 && getMinorVersion() >= 3) {
logger.info("TransactionIDSupportEnabling");
supportsTransactionId = true;
transactionIdExpression = TRANSACTION_ID_EXPRESSION;
}
try {
enableSyncTriggers();
} catch (Exception e) {
logger.error("PostgreSqlCustomVariableMissing");
throw new RuntimeException("PostgreSqlCustomVariableMissing", e);
}
}
@SuppressWarnings("unchecked")
@Override
protected Integer overrideJdbcTypeForColumn(Map values) {
String typeName = (String) values.get("TYPE_NAME");
if (typeName != null && typeName.equalsIgnoreCase("ABSTIME")) {
return Types.TIMESTAMP;
} else {
return super.overrideJdbcTypeForColumn(values);
}
}
@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) {
logger.warn("TriggerDoesNotExist");
}
}
}
public void disableSyncTriggers(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() {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus transactionstatus) {
if (!transactionstatus.isRollbackOnly()) {
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)fn_sym_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 isCharSpacePadded() {
return true;
}
public boolean isCharSpaceTrimmed() {
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;
}
}