/
MsSqlDbDialect.java
195 lines (164 loc) · 6.56 KB
/
MsSqlDbDialect.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
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Chris Henson <chenson42@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.mssql;
import java.util.ArrayList;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ddlutils.model.Column;
import org.apache.ddlutils.model.Table;
import org.jumpmind.symmetric.db.AbstractDbDialect;
import org.jumpmind.symmetric.db.BinaryEncoding;
import org.jumpmind.symmetric.db.IDbDialect;
import org.jumpmind.symmetric.load.IColumnFilter;
import org.jumpmind.symmetric.load.StatementBuilder.DmlType;
/**
* This dialect was tested with the jTDS JDBC driver on SQL Server 2005.
*
* TODO support text and image fields, they cannot be referenced from the inserted or deleted tables in the triggers. Here is one
* idea we could implement: http://www.devx.com/getHelpOn/10MinuteSolution/16544
*/
public class MsSqlDbDialect extends AbstractDbDialect implements IDbDialect {
static final Log logger = LogFactory.getLog(MsSqlDbDialect.class);
protected void initForSpecificDialect() {
}
protected boolean allowsNullForIdentityColumn() {
return false;
}
@Override
public IColumnFilter getDatabaseColumnFilter() {
return new IColumnFilter () {
int[] indexesToRemove = null;
public String[] filterColumnsNames(DmlType dml, Table table, String[] columnNames) {
ArrayList<String> columns = new ArrayList<String>();
CollectionUtils.addAll(columns, columnNames);
if (dml == DmlType.UPDATE) {
Column[] autoIncrementColumns = table.getAutoIncrementColumns();
indexesToRemove = new int[autoIncrementColumns.length];
int i = 0;
for (Column column : autoIncrementColumns) {
indexesToRemove[i++] = columns.indexOf(column.getName());
columns.remove(column.getName());
}
}
return columns.toArray(new String[columns.size()]);
}
public Object[] filterColumnsValues(DmlType dml, Table table, Object[] columnValues) {
if (dml == DmlType.UPDATE && indexesToRemove != null) {
ArrayList<Object> values = new ArrayList<Object>();
CollectionUtils.addAll(values, columnValues);
for (int index : indexesToRemove) {
values.remove(index);
}
return values.toArray(new Object[values.size()]);
}
return columnValues;
}
};
}
@Override
public void prepareTableForDataLoad(Table table) {
if (table != null && table.getAutoIncrementColumns().length > 0) {
jdbcTemplate.execute("SET IDENTITY_INSERT " + table.getName() + " ON");
}
}
@Override
public void cleanupAfterDataLoad(Table table) {
if (table != null && table.getAutoIncrementColumns().length > 0) {
jdbcTemplate.execute("SET IDENTITY_INSERT " + table.getName() + " OFF");
}
}
@Override
public BinaryEncoding getBinaryEncoding() {
return BinaryEncoding.BASE64;
}
@Override
protected boolean doesTriggerExistOnPlatform(String schema, String tableName, String triggerName) {
return jdbcTemplate.queryForInt("select count(*) from sysobjects where type = 'TR' AND name = ?",
new Object[] { triggerName }) > 0;
}
public void disableSyncTriggers() {
jdbcTemplate.update("set context_info 0x1");
}
public void enableSyncTriggers() {
jdbcTemplate.update("set context_info 0x0");
}
public String getSyncTriggersExpression() {
return "select @SyncEnabled = context_info from master.dbo.sysprocesses where spid=@@SPID";
}
public String getTransactionTriggerExpression() {
return "@TransactionId";
}
public boolean supportsTransactionId() {
return true;
}
/**
* SQL Server always pads character fields out to the right to fill out field with space characters.
* @return true always
*/
public boolean isCharSpacePadded() {
return true;
}
/**
* @return false always
*/
public boolean isCharSpaceTrimmed() {
return false;
}
public boolean isTransactionIdOverrideSupported() {
return false;
}
/**
* SQL Server pads an empty string with spaces.
* @return false always
*/
public boolean isEmptyStringNulled() {
return false;
}
/**
* Nothing to do for SQL Server
*/
public void purge() {
}
public String getDefaultCatalog() {
return (String) jdbcTemplate.queryForObject("select DB_NAME()", String.class);
}
public String getDefaultSchema() {
return (String) jdbcTemplate.queryForObject("select SCHEMA_NAME()", String.class);
}
public void removeTrigger(String schemaName, String triggerName, String tableName) {
schemaName = schemaName == null ? "" : (schemaName + ".");
try {
jdbcTemplate.update("drop trigger " + schemaName + triggerName);
} catch (Exception e) {
logger.warn("Trigger does not exist");
}
}
public void removeTrigger(String schemaName, String triggerName) {
removeTrigger(schemaName, triggerName, null);
}
/**
* SQL Server is case insensitive.
* @return false always
*/
public boolean supportsMixedCaseNamesInCatalog() {
return false;
}
}