-
Notifications
You must be signed in to change notification settings - Fork 221
/
SqlAnywhereDdlReader.java
234 lines (223 loc) · 9.86 KB
/
SqlAnywhereDdlReader.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
/**
* 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.db.platform.sqlanywhere;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.IIndex;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.model.Trigger;
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.model.Trigger.TriggerType;
import org.jumpmind.db.platform.AbstractJdbcDdlReader;
import org.jumpmind.db.platform.DatabaseMetaDataWrapper;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.sql.ISqlRowMapper;
import org.jumpmind.db.sql.JdbcSqlTemplate;
import org.jumpmind.db.sql.Row;
import org.jumpmind.db.sql.SqlException;
/*
* Reads a database model from a Sybase database.
*/
public class SqlAnywhereDdlReader extends AbstractJdbcDdlReader {
/* The regular expression pattern for the ISO dates. */
private Pattern isoDatePattern = Pattern.compile("'(\\d{4}\\-\\d{2}\\-\\d{2})'");
/* The regular expression pattern for the ISO times. */
private Pattern isoTimePattern = Pattern.compile("'(\\d{2}:\\d{2}:\\d{2})'");
public SqlAnywhereDdlReader(IDatabasePlatform platform) {
super(platform);
setDefaultCatalogPattern(null);
setDefaultSchemaPattern(null);
setDefaultTablePattern("%");
}
@Override
protected Table readTable(Connection connection, DatabaseMetaDataWrapper metaData,
Map<String, Object> values) throws SQLException {
Table table = super.readTable(connection, metaData, values);
if (table != null) {
// Sybase does not return the auto-increment status via the database
// metadata
determineAutoIncrementFromResultSetMetaData(connection, table, table.getColumns());
Column[] columns = table.getColumns();
for (Column column : columns) {
if (column.isAutoIncrement() && "autoincrement".equalsIgnoreCase(column.getDefaultValue())) {
column.setDefaultValue(null);
}
}
}
return table;
}
@Override
protected Integer mapUnknownJdbcTypeForColumn(Map<String, Object> values) {
String typeName = (String) values.get("TYPE_NAME");
if (typeName != null && typeName.toUpperCase().startsWith("TEXT")) {
return Types.LONGVARCHAR;
} else if (typeName != null && typeName.toUpperCase().startsWith("NTEXT")) {
return Types.LONGNVARCHAR;
} else if (typeName != null && typeName.toUpperCase().startsWith("LONG NVARCHAR")) {
return Types.LONGNVARCHAR;
} else {
return super.mapUnknownJdbcTypeForColumn(values);
}
}
@Override
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException {
Column column = super.readColumn(metaData, values);
if (column.getMappedTypeCode() == Types.CHAR) {
column.setMappedTypeCode(Types.VARCHAR);
column.setMappedType("VARCHAR");
}
if ((column.getMappedTypeCode() == Types.NUMERIC) && (column.getSizeAsInt() == 19)
&& (column.getScale() == 0)) {
// Back-mapping to BIGINT
column.setMappedTypeCode(Types.BIGINT);
} else if (column.getDefaultValue() != null) {
if (column.getMappedTypeCode() == Types.TIMESTAMP) {
// Sybase maintains the default values for DATE/TIME jdbc types,
// so we have to
// migrate the default value to TIMESTAMP
Matcher matcher = isoDatePattern.matcher(column.getDefaultValue());
Timestamp timestamp = null;
if (matcher.matches()) {
timestamp = new Timestamp(Date.valueOf(matcher.group(1)).getTime());
} else {
matcher = isoTimePattern.matcher(column.getDefaultValue());
if (matcher.matches()) {
timestamp = new Timestamp(Time.valueOf(matcher.group(1)).getTime());
}
}
if (timestamp != null) {
column.setDefaultValue(timestamp.toString());
}
} else if (TypeMap.isTextType(column.getMappedTypeCode())) {
column.setDefaultValue(unescape(column.getDefaultValue(), "'", "''"));
}
}
return column;
}
@Override
protected void readIndex(DatabaseMetaDataWrapper metaData, Map<String, Object> values, Map<String, IIndex> knownIndices)
throws SQLException {
if (getPlatform().getDdlBuilder().isDelimitedIdentifierModeOn()) {
String indexName = (String) values.get("INDEX_NAME");
// Sometimes, Sybase keeps the delimiter quotes around the index
// names
// when returning them in the metadata, so we strip them
if (indexName != null) {
String delimiter = getPlatformInfo().getDelimiterToken();
if ((indexName != null) && indexName.startsWith(delimiter)
&& indexName.endsWith(delimiter)) {
indexName = indexName.substring(delimiter.length(), indexName.length()
- delimiter.length());
values.put("INDEX_NAME", indexName);
}
}
}
super.readIndex(metaData, values, knownIndices);
}
@Override
protected boolean isInternalPrimaryKeyIndex(Connection connection,
DatabaseMetaDataWrapper metaData, Table table, IIndex index) throws SQLException {
// We can simply check the sysindexes table where a specific flag is set
// for pk indexes
StringBuffer query = new StringBuffer();
query.append("SELECT name = si.name FROM dbo.sysindexes si, dbo.sysobjects so WHERE so.name = '");
query.append(table.getName());
query.append("' AND si.name = '");
query.append(index.getName());
query.append("' AND so.id = si.id AND (si.status & 2048) > 0");
Statement stmt = connection.createStatement();
try {
ResultSet rs = stmt.executeQuery(query.toString());
boolean result = rs.next();
rs.close();
return result;
} finally {
stmt.close();
}
}
@Override
public List<Trigger> getTriggers(final String catalog, final String schema,
final String tableName) throws SqlException {
List<Trigger> triggers = new ArrayList<Trigger>();
log.debug("Reading triggers for: " + tableName);
JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform
.getSqlTemplate();
String sql = "SELECT "
+ "trigname AS trigger_name, "
+ "owner, "
+ "tname AS table_name, "
+ "event AS trigger_type, "
+ "trigtime AS trigger_time, "
+ "trigdefn "
+ "FROM SYS.SYSTRIGGERS "
+ "WHERE tname=? and owner=? ;";
triggers = sqlTemplate.query(sql, new ISqlRowMapper<Trigger>() {
public Trigger mapRow(Row row) {
Trigger trigger = new Trigger();
trigger.setName(row.getString("trigger_name"));
trigger.setSchemaName(row.getString("owner"));
trigger.setTableName(row.getString("table_name"));
trigger.setEnabled(true);
trigger.setSource(row.getString("trigdefn"));
row.remove("trigdefn");
String triggerType = row.getString("trigger_type");
if (triggerType.equals("DELETE")
|| triggerType.equals("INSERT")
|| triggerType.equals("UPDATE")) {
trigger.setTriggerType(TriggerType.valueOf(triggerType));
}
trigger.setMetaData(row);
return trigger;
}
}, tableName, schema);
return triggers;
}
}