Revision 44678 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc2/spi/operations/FetchFeatureTypeOperation.java
FetchFeatureTypeOperation.java | ||
---|---|---|
24 | 24 |
import org.gvsig.fmap.geom.GeometryLocator; |
25 | 25 |
import org.gvsig.fmap.geom.type.GeometryType; |
26 | 26 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.AbstractConnectionOperation.LOGGER; |
27 |
import org.gvsig.tools.dataTypes.DataType; |
|
27 | 28 |
|
28 | 29 |
@SuppressWarnings("UseSpecificCatch") |
29 | 30 |
public class FetchFeatureTypeOperation extends AbstractConnectionOperation { |
... | ... | |
66 | 67 |
|
67 | 68 |
@Override |
68 | 69 |
public final Object perform(Connection conn) throws DataException { |
69 |
this.fetch(featureType, conn, table, |
|
70 |
primaryKeys, defaultGeometryColumn, crs |
|
71 |
); |
|
70 |
this.fetch(conn); |
|
72 | 71 |
return true; |
73 | 72 |
} |
74 | 73 |
|
... | ... | |
76 | 75 |
return this.table; |
77 | 76 |
} |
78 | 77 |
|
79 |
public void fetch( |
|
80 |
EditableFeatureType featureType, |
|
81 |
Connection conn, |
|
82 |
TableReference table, |
|
83 |
List<String> pks, |
|
84 |
String defaultGeometryColumn, |
|
85 |
IProjection crs |
|
86 |
) throws DataException { |
|
87 |
|
|
78 |
public void fetch(Connection conn) throws DataException { |
|
79 |
List<String> pks = this.primaryKeys; |
|
88 | 80 |
Statement st = null; |
89 | 81 |
ResultSet rs = null; |
90 | 82 |
try { |
... | ... | |
94 | 86 |
} |
95 | 87 |
pks = this.getPrimaryKeysFromMetadata(conn, null, table.getSchema(), table.getTable()); |
96 | 88 |
if (CollectionUtils.isEmpty(pks)) { |
97 |
pks = getPrimaryKeysFromInformationSchema(conn, null, table.getSchema(), table.getTable());
|
|
89 |
pks = getPrimaryKeysFromInformationSchema(conn); |
|
98 | 90 |
} |
99 | 91 |
} |
100 |
|
|
101 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder(); |
|
102 |
sqlbuilder.select().column().all(); |
|
103 |
sqlbuilder.select().from().table() |
|
104 |
.database(this.table.getDatabase()) |
|
105 |
.schema(this.table.getSchema()) |
|
106 |
.name(this.table.getTable()); |
|
107 |
sqlbuilder.select().from().subquery(this.table.getSubquery()); |
|
108 |
sqlbuilder.select().limit(1); |
|
109 |
|
|
110 | 92 |
st = conn.createStatement(); |
111 | 93 |
st.setFetchSize(1); |
112 |
rs = JDBCUtils.executeQuery(st, sqlbuilder.toString());
|
|
94 |
rs = JDBCUtils.executeQuery(st, this.getSQLToRetrieveFirstRowOfTable());
|
|
113 | 95 |
ResultSetMetaData rsMetadata = rs.getMetaData(); |
114 | 96 |
|
115 | 97 |
fetchFeatureTypeFromMetadata(conn, rsMetadata, pks); |
... | ... | |
122 | 104 |
} |
123 | 105 |
} |
124 | 106 |
|
107 |
public String getSQLToRetrieveFirstRowOfTable() { |
|
108 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder(); |
|
109 |
sqlbuilder.select().column().all(); |
|
110 |
sqlbuilder.select().from().table() |
|
111 |
.database(this.table.getDatabase()) |
|
112 |
.schema(this.table.getSchema()) |
|
113 |
.name(this.table.getTable()); |
|
114 |
sqlbuilder.select().from().subquery(this.table.getSubquery()); |
|
115 |
sqlbuilder.select().limit(1); |
|
116 |
|
|
117 |
String sql = sqlbuilder.toString(); |
|
118 |
return sql; |
|
119 |
} |
|
120 |
|
|
121 |
|
|
125 | 122 |
public void fetchFeatureTypeFromMetadata(Connection conn, ResultSetMetaData rsMetadata) throws SQLException { |
126 | 123 |
this.fetchFeatureTypeFromMetadata(conn, rsMetadata, new ArrayList<String>()); |
127 | 124 |
} |
... | ... | |
222 | 219 |
|
223 | 220 |
} |
224 | 221 |
|
225 |
protected List<String> getPrimaryKeysFromInformationSchema( |
|
226 |
Connection conn, |
|
227 |
String catalog, |
|
228 |
String schema, |
|
229 |
String table) throws SQLException { |
|
222 |
protected List<String> getPrimaryKeysFromInformationSchema(Connection conn) throws SQLException { |
|
230 | 223 |
|
231 |
String sql = getSQLToRetrievePrimaryKeysFromInformationSchema(catalog, schema, table);
|
|
224 |
String sql = getSQLToRetrievePrimaryKeysFromInformationSchema(); |
|
232 | 225 |
|
233 | 226 |
Statement st = null; |
234 | 227 |
ResultSet rs = null; |
... | ... | |
253 | 246 |
} |
254 | 247 |
} |
255 | 248 |
|
256 |
protected String getSQLToRetrievePrimaryKeysFromInformationSchema( |
|
257 |
String catalog, |
|
258 |
String schema, |
|
259 |
String table |
|
260 |
) throws SQLException { |
|
249 |
public String getSQLToRetrievePrimaryKeysFromInformationSchema() throws SQLException { |
|
261 | 250 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder(); |
262 | 251 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
263 | 252 |
|
... | ... | |
274 | 263 |
sqlbuilder.select().where().set( |
275 | 264 |
expbuilder.like( |
276 | 265 |
expbuilder.custom("c.TABLE_NAME"), |
277 |
expbuilder.constant(table) |
|
266 |
expbuilder.constant(table.getTable())
|
|
278 | 267 |
) |
279 | 268 |
); |
280 |
if (schema != null) {
|
|
269 |
if( table.hasSchema() ) {
|
|
281 | 270 |
sqlbuilder.select().where().and( |
282 | 271 |
expbuilder.like( |
283 | 272 |
expbuilder.custom("c.TABLE_SCHEMA"), |
284 |
expbuilder.constant(schema)
|
|
273 |
expbuilder.constant(table.getSchema())
|
|
285 | 274 |
) |
286 | 275 |
); |
287 | 276 |
} |
288 |
if (catalog != null) { |
|
289 |
sqlbuilder.select().where().and( |
|
290 |
expbuilder.like( |
|
291 |
expbuilder.custom("c.CONSTRAINT_CATALOG"), |
|
292 |
expbuilder.constant(catalog) |
|
293 |
) |
|
294 |
); |
|
295 |
} |
|
277 |
// if (catalog != null) {
|
|
278 |
// sqlbuilder.select().where().and(
|
|
279 |
// expbuilder.like(
|
|
280 |
// expbuilder.custom("c.CONSTRAINT_CATALOG"),
|
|
281 |
// expbuilder.constant(catalog)
|
|
282 |
// )
|
|
283 |
// );
|
|
284 |
// }
|
|
296 | 285 |
sqlbuilder.select().where().and( |
297 | 286 |
expbuilder.eq( |
298 | 287 |
expbuilder.column("CONSTRAINT_TYPE"), |
... | ... | |
319 | 308 |
); |
320 | 309 |
attr.setIsAutomatic(rsMetadata.isAutoIncrement(colIndex)); |
321 | 310 |
attr.setIsReadOnly(rsMetadata.isReadOnly(colIndex)); |
311 |
attr.setSize(rsMetadata.getColumnDisplaySize(colIndex)); |
|
312 |
attr.setScale(rsMetadata.getScale(colIndex)); |
|
322 | 313 |
attr.setPrecision(rsMetadata.getPrecision(colIndex)); |
323 |
attr.setScale(rsMetadata.getScale(colIndex)); |
|
324 |
attr.setSize(rsMetadata.getColumnDisplaySize(colIndex)); |
|
325 | 314 |
switch(attr.getType()) { |
315 |
case DataTypes.BYTE: |
|
316 |
attr.setPrecision(DataType.BYTE_DEFAULT_PRECISION); |
|
317 |
attr.setScale(DataType.SCALE_NONE); |
|
318 |
break; |
|
319 |
case DataTypes.INT: |
|
320 |
attr.setPrecision(DataType.INT_DEFAULT_PRECISION); |
|
321 |
attr.setScale(DataType.SCALE_NONE); |
|
322 |
break; |
|
323 |
case DataTypes.LONG: |
|
324 |
attr.setPrecision(DataType.LONG_DEFAULT_PRECISION); |
|
325 |
attr.setScale(DataType.SCALE_NONE); |
|
326 |
break; |
|
327 |
case DataTypes.FLOAT: |
|
328 |
attr.setPrecision(DataType.FLOAT_DEFAULT_PRECISION); |
|
329 |
attr.setScale(DataType.FLOAT_DEFAULT_SCALE); |
|
330 |
break; |
|
331 |
case DataTypes.DOUBLE: |
|
332 |
attr.setPrecision(DataType.DOUBLE_DEFAULT_PRECISION); |
|
333 |
attr.setScale(DataType.DOUBLE_DEFAULT_SCALE); |
|
334 |
break; |
|
326 | 335 |
case DataTypes.OBJECT: |
327 | 336 |
attr.setAdditionalInfo( |
328 | 337 |
"SQLType", |
... | ... | |
346 | 355 |
) throws SQLException { |
347 | 356 |
|
348 | 357 |
switch (rsMetadata.getColumnType(colIndex)) { |
358 |
case java.sql.Types.TINYINT: |
|
359 |
return DataTypes.BYTE; |
|
360 |
|
|
349 | 361 |
case java.sql.Types.INTEGER: |
350 | 362 |
return DataTypes.INT; |
351 | 363 |
|
... | ... | |
353 | 365 |
return DataTypes.LONG; |
354 | 366 |
|
355 | 367 |
case java.sql.Types.REAL: |
356 |
return DataTypes.DOUBLE; |
|
368 |
case java.sql.Types.FLOAT: |
|
369 |
return DataTypes.FLOAT; |
|
357 | 370 |
|
358 | 371 |
case java.sql.Types.DOUBLE: |
359 | 372 |
return DataTypes.DOUBLE; |
360 | 373 |
|
374 |
case java.sql.Types.NUMERIC: |
|
375 |
case java.sql.Types.DECIMAL: |
|
376 |
return DataTypes.DECIMAL; |
|
377 |
|
|
361 | 378 |
case java.sql.Types.CHAR: |
362 |
return DataTypes.STRING; |
|
363 |
|
|
364 | 379 |
case java.sql.Types.VARCHAR: |
365 | 380 |
case java.sql.Types.LONGVARCHAR: |
366 | 381 |
return DataTypes.STRING; |
367 | 382 |
|
368 |
case java.sql.Types.FLOAT: |
|
369 |
return DataTypes.DOUBLE; |
|
370 |
|
|
371 |
case java.sql.Types.NUMERIC: |
|
372 |
return DataTypes.DOUBLE; |
|
373 |
|
|
374 |
case java.sql.Types.DECIMAL: |
|
375 |
return DataTypes.FLOAT; |
|
376 |
|
|
377 | 383 |
case java.sql.Types.DATE: |
378 | 384 |
return DataTypes.DATE; |
379 | 385 |
|
Also available in: Unified diff