/
OracleJdbcModelReader.java
345 lines (304 loc) · 12.9 KB
/
OracleJdbcModelReader.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package org.jumpmind.symmetric.jdbc.db.oracle;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jumpmind.symmetric.core.common.LogLevel;
import org.jumpmind.symmetric.core.model.Column;
import org.jumpmind.symmetric.core.model.Index;
import org.jumpmind.symmetric.core.model.Table;
import org.jumpmind.symmetric.jdbc.db.DatabaseMetaDataWrapper;
import org.jumpmind.symmetric.jdbc.db.IJdbcDbPlatform;
import org.jumpmind.symmetric.jdbc.db.JdbcModelReader;
import org.jumpmind.symmetric.jdbc.sql.JdbcSqlConnection;
public class OracleJdbcModelReader extends JdbcModelReader {
/** The regular expression pattern for the Oracle conversion of ISO dates. */
private Pattern oracleIsoDatePattern;
/** The regular expression pattern for the Oracle conversion of ISO times. */
private Pattern oracleIsoTimePattern;
/**
* The regular expression pattern for the Oracle conversion of ISO
* timestamps.
*/
private Pattern oracleIsoTimestampPattern;
/**
* Creates a new model reader for Oracle 8 databases.
*
* @param platform
* The platform that this model reader belongs to
*/
public OracleJdbcModelReader(IJdbcDbPlatform platform) {
super(platform);
setDefaultCatalogPattern(null);
setDefaultSchemaPattern(null);
setDefaultTablePattern("%");
oracleIsoDatePattern = Pattern.compile("TO_DATE\\('([^']*)'\\, 'YYYY\\-MM\\-DD'\\)");
oracleIsoTimePattern = Pattern.compile("TO_DATE\\('([^']*)'\\, 'HH24:MI:SS'\\)");
oracleIsoTimestampPattern = Pattern
.compile("TO_DATE\\('([^']*)'\\, 'YYYY\\-MM\\-DD HH24:MI:SS'\\)");
}
/**
* {@inheritDoc}
*/
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map<String, Object> values)
throws SQLException {
Column column = super.readColumn(metaData, values);
if (column.getDefaultValue() != null) {
// Oracle pads the default value with spaces
column.setDefaultValue(column.getDefaultValue().trim());
}
if (column.getTypeCode() == Types.DECIMAL) {
// We're back-mapping the NUMBER columns returned by Oracle
// Note that the JDBC driver returns DECIMAL for these NUMBER
// columns
switch (column.getSizeAsInt()) {
case 1:
if (column.getScale() == 0) {
column.setTypeCode(Types.BIT);
}
break;
case 3:
if (column.getScale() == 0) {
column.setTypeCode(Types.TINYINT);
}
break;
case 5:
if (column.getScale() == 0) {
column.setTypeCode(Types.SMALLINT);
}
break;
case 18:
column.setTypeCode(Types.REAL);
break;
case 22:
if (column.getScale() == 0) {
column.setTypeCode(Types.INTEGER);
}
break;
case 38:
if (column.getScale() == 0) {
column.setTypeCode(Types.BIGINT);
} else {
column.setTypeCode(Types.DOUBLE);
}
break;
}
} else if (column.getTypeCode() == Types.FLOAT) {
// Same for REAL, FLOAT, DOUBLE PRECISION, which all back-map to
// FLOAT but with
// different sizes (63 for REAL, 126 for FLOAT/DOUBLE PRECISION)
switch (column.getSizeAsInt()) {
case 63:
column.setTypeCode(Types.REAL);
break;
case 126:
column.setTypeCode(Types.DOUBLE);
break;
}
} else if ((column.getTypeCode() == Types.DATE)
|| (column.getTypeCode() == Types.TIMESTAMP)) {
// Oracle has only one DATE/TIME type, so we can't know which it is
// and thus map
// it back to TIMESTAMP
column.setTypeCode(Types.TIMESTAMP);
// we also reverse the ISO-format adaptation, and adjust the default
// value to timestamp
if (column.getDefaultValue() != null) {
Timestamp timestamp = null;
Matcher matcher = oracleIsoTimestampPattern.matcher(column.getDefaultValue());
if (matcher.matches()) {
String timestampVal = matcher.group(1);
timestamp = Timestamp.valueOf(timestampVal);
} else {
matcher = oracleIsoDatePattern.matcher(column.getDefaultValue());
if (matcher.matches()) {
String dateVal = matcher.group(1);
timestamp = new Timestamp(Date.valueOf(dateVal).getTime());
} else {
matcher = oracleIsoTimePattern.matcher(column.getDefaultValue());
if (matcher.matches()) {
String dateVal = matcher.group(1);
timestamp = new Timestamp(Time.valueOf(dateVal).getTime());
}
}
}
if (timestamp != null) {
column.setDefaultValue(timestamp.toString());
}
}
}
return column;
}
/**
* Helper method that determines the auto increment status using Firebird's
* system tables.
*
* @param table
* The table
*/
protected void determineAutoIncrementColumns(Connection c, Table table) throws SQLException {
Column[] columns = table.getColumns();
for (int idx = 0; idx < columns.length; idx++) {
columns[idx].setAutoIncrement(isAutoIncrement(c, table, columns[idx]));
}
}
/**
* Tries to determine whether the given column is an identity column.
*
* @param table
* The table
* @param column
* The column
* @return <code>true</code> if the column is an identity column
*/
protected boolean isAutoIncrement(Connection c, Table table, Column column) throws SQLException {
// TODO: For now, we only check whether there is a sequence & trigger as
// generated by DdlUtils
// But once sequence/trigger support is in place, it might be possible
// to 'parse' the
// trigger body (via SELECT trigger_name, trigger_body FROM
// user_triggers) in order to
// determine whether it fits our auto-increment definition
PreparedStatement prepStmt = null;
ResultSet resultSet = null;
String triggerName = getPlatform().getConstraintName("trg", table, column.getName(), null);
String seqName = getPlatform().getConstraintName("seq", table, column.getName(), null);
if (!getPlatform().getPlatformInfo().isDelimitedIdentifierModeOn()) {
triggerName = triggerName.toUpperCase();
seqName = seqName.toUpperCase();
}
try {
prepStmt = c.prepareStatement("SELECT * FROM user_triggers WHERE trigger_name = ?");
prepStmt.setString(1, triggerName);
resultSet = prepStmt.executeQuery();
if (!resultSet.next()) {
return false;
}
// we have a trigger, so lets check the sequence
JdbcSqlConnection.close(resultSet);
JdbcSqlConnection.close(prepStmt);
prepStmt = c.prepareStatement("SELECT * FROM user_sequences WHERE sequence_name = ?");
prepStmt.setString(1, seqName);
resultSet = prepStmt.executeQuery();
return resultSet.next();
} finally {
JdbcSqlConnection.close(resultSet);
JdbcSqlConnection.close(prepStmt);
}
}
/**
* {@inheritDoc}
*/
protected Collection<Index> readIndices(Connection c, DatabaseMetaDataWrapper metaData,
String tableName) throws SQLException {
// Oracle bug 4999817 causes a table analyze to execute in response to a
// call to
// DatabaseMetaData#getIndexInfo.
// The bug is fixed in driver version 10.2.0.4. The bug is present in at
// least
// driver versions 10.2.0.1.0, 10.1.0.2.0, and 9.2.0.5.
// To avoid this bug, we will access user_indexes view.
// This also allows us to filter system-generated indices which are
// identified by either
// having GENERATED='Y' in the query result, or by their index names
// being equal to the
// name of the primary key of the table
StringBuffer query = new StringBuffer();
query.append("SELECT a.INDEX_NAME, a.INDEX_TYPE, a.UNIQUENESS, b.COLUMN_NAME, b.COLUMN_POSITION FROM USER_INDEXES a, USER_IND_COLUMNS b WHERE ");
query.append("a.TABLE_NAME=? AND a.GENERATED=? AND a.TABLE_TYPE=? AND a.TABLE_NAME=b.TABLE_NAME AND a.INDEX_NAME=b.INDEX_NAME AND ");
query.append("a.INDEX_NAME NOT IN (SELECT DISTINCT c.CONSTRAINT_NAME FROM USER_CONSTRAINTS c WHERE c.CONSTRAINT_TYPE=? AND c.TABLE_NAME=a.TABLE_NAME");
if (metaData.getSchemaPattern() != null) {
query.append(" AND c.OWNER LIKE ?) AND a.TABLE_OWNER LIKE ?");
} else {
query.append(")");
}
Map<String, Index> indices = new LinkedHashMap<String, Index>();
PreparedStatement stmt = null;
try {
stmt = c.prepareStatement(query.toString());
stmt.setString(1,
getPlatform().getPlatformInfo().isDelimitedIdentifierModeOn() ? tableName
: tableName.toUpperCase());
stmt.setString(2, "N");
stmt.setString(3, "TABLE");
stmt.setString(4, "P");
if (metaData.getSchemaPattern() != null) {
stmt.setString(5, metaData.getSchemaPattern().toUpperCase());
stmt.setString(6, metaData.getSchemaPattern().toUpperCase());
}
ResultSet rs = stmt.executeQuery();
Map<String, Object> values = new HashMap<String, Object>();
while (rs.next()) {
String name = rs.getString(1);
String type = rs.getString(2);
// Only read in normal oracle indexes
if (type.startsWith("NORMAL")) {
values.put("INDEX_TYPE", new Short(DatabaseMetaData.tableIndexOther));
values.put("INDEX_NAME", name);
values.put("NON_UNIQUE",
"UNIQUE".equalsIgnoreCase(rs.getString(3)) ? Boolean.FALSE
: Boolean.TRUE);
values.put("COLUMN_NAME", rs.getString(4));
values.put("ORDINAL_POSITION", new Short(rs.getShort(5)));
readIndex(metaData, values, indices);
} else {
log.log(LogLevel.WARN, "Skipping index %s of type %s", name, type);
}
}
} finally {
if (stmt != null) {
stmt.close();
}
}
return indices.values();
}
@Override
protected Table readTable(Connection c, DatabaseMetaDataWrapper metaData,
Map<String, Object> values) throws SQLException {
// Oracle 10 added the recycle bin which contains dropped database
// objects not yet purged
// Since we don't want entries from the recycle bin, we filter them out
PreparedStatement stmt = null;
boolean deletedObj = false;
try {
stmt = c.prepareStatement("SELECT * FROM RECYCLEBIN WHERE OBJECT_NAME=?");
stmt.setString(1, (String) values.get("TABLE_NAME"));
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
// we found the table in the recycle bin, so its a deleted one
// which we ignore
deletedObj = true;
}
rs.close();
} finally {
if (stmt != null) {
stmt.close();
}
}
if (!deletedObj) {
String tableName = (String) values.get("TABLE_NAME");
// system table ?
if (tableName.indexOf('$') > 0) {
return null;
}
Table table = super.readTable(c, metaData, values);
if (table != null) {
determineAutoIncrementColumns(c, table);
}
return table;
} else {
return null;
}
}
}