Revision 44058 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 | ||
---|---|---|
18 | 18 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
19 | 19 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
20 | 20 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
21 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.AbstractConnectionOperation.logger;
|
|
21 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
|
|
22 | 22 |
import org.gvsig.fmap.geom.Geometry; |
23 | 23 |
import org.gvsig.fmap.geom.GeometryLocator; |
24 | 24 |
import org.gvsig.fmap.geom.type.GeometryType; |
25 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.AbstractConnectionOperation.LOGGER; |
|
25 | 26 |
|
26 | 27 |
public class FetchFeatureTypeOperation extends AbstractConnectionOperation { |
27 | 28 |
private final EditableFeatureType featureType; |
28 |
private final String dbname; |
|
29 |
private final String schema; |
|
30 |
private final String table; |
|
29 |
private final TableReference table; |
|
31 | 30 |
private final List<String> primaryKeys; |
32 | 31 |
private final String defaultGeometryColumn; |
33 | 32 |
private final IProjection crs; |
... | ... | |
35 | 34 |
public FetchFeatureTypeOperation( |
36 | 35 |
JDBCHelper helper |
37 | 36 |
) { |
38 |
this(helper, null, null, null, null, null, null, null);
|
|
37 |
this(helper, null, null, null, null, null); |
|
39 | 38 |
} |
40 | 39 |
|
41 | 40 |
public FetchFeatureTypeOperation( |
... | ... | |
44 | 43 |
String defaultGeometryColumn, |
45 | 44 |
IProjection crs |
46 | 45 |
) { |
47 |
this(helper, featureType, null, null, null, null, defaultGeometryColumn, crs);
|
|
46 |
this(helper, featureType, null, null, defaultGeometryColumn, crs); |
|
48 | 47 |
} |
49 | 48 |
|
50 | 49 |
public FetchFeatureTypeOperation( |
51 | 50 |
JDBCHelper helper, |
52 | 51 |
EditableFeatureType featureType, |
53 |
String dbname, |
|
54 |
String schema, |
|
55 |
String table, |
|
52 |
TableReference table, |
|
56 | 53 |
List<String> primaryKeys, |
57 | 54 |
String defaultGeometryColumn, |
58 | 55 |
IProjection crs |
59 | 56 |
) { |
60 | 57 |
super(helper); |
61 | 58 |
this.featureType = featureType; |
62 |
this.dbname = dbname; |
|
63 |
this.schema = schema; |
|
64 | 59 |
this.table = table; |
65 | 60 |
this.primaryKeys = primaryKeys; |
66 | 61 |
this.defaultGeometryColumn = defaultGeometryColumn; |
... | ... | |
69 | 64 |
|
70 | 65 |
@Override |
71 | 66 |
public final Object perform(Connection conn) throws DataException { |
72 |
this.fetch(featureType, conn, dbname, schema, table,
|
|
67 |
this.fetch(featureType, conn, table, |
|
73 | 68 |
primaryKeys, defaultGeometryColumn, crs |
74 | 69 |
); |
75 | 70 |
return true; |
76 | 71 |
} |
77 | 72 |
|
78 |
protected String getDatabase() { |
|
79 |
return this.dbname; |
|
80 |
} |
|
81 |
|
|
82 |
protected String getSchema() { |
|
83 |
return this.schema; |
|
84 |
} |
|
85 |
|
|
86 |
protected String getTablename() { |
|
73 |
protected TableReference getTable() { |
|
87 | 74 |
return this.table; |
88 | 75 |
} |
89 | 76 |
|
90 | 77 |
public void fetch( |
91 | 78 |
EditableFeatureType featureType, |
92 | 79 |
Connection conn, |
93 |
String database, |
|
94 |
String schema, |
|
95 |
String table, |
|
80 |
TableReference table, |
|
96 | 81 |
List<String> pks, |
97 | 82 |
String defaultGeometryColumn, |
98 | 83 |
IProjection crs |
... | ... | |
102 | 87 |
ResultSet rs = null; |
103 | 88 |
try { |
104 | 89 |
if (CollectionUtils.isEmpty(pks)) { |
105 |
if (!StringUtils.isEmpty(table)) {
|
|
106 |
pks = this.getPrimaryKeysFromMetadata(conn, null, schema, table);
|
|
90 |
if (!table.hasSubquery()) {
|
|
91 |
pks = this.getPrimaryKeysFromMetadata(conn, null, table.getSchema(), table.getTable());
|
|
107 | 92 |
if (CollectionUtils.isEmpty(pks)) { |
108 |
pks = getPrimaryKeysFromInformationSchema(conn, null, schema, table);
|
|
93 |
pks = getPrimaryKeysFromInformationSchema(conn, null, table.getSchema(), table.getTable());
|
|
109 | 94 |
} |
110 | 95 |
} |
111 | 96 |
} |
112 | 97 |
|
113 | 98 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder(); |
114 | 99 |
sqlbuilder.select().column().all(); |
115 |
sqlbuilder.select().from().table().database(database).schema(schema).name(table); |
|
100 |
sqlbuilder.select().from().table() |
|
101 |
.database(this.table.getDatabase()) |
|
102 |
.schema(this.table.getSchema()) |
|
103 |
.name(this.table.getTable()); |
|
104 |
sqlbuilder.select().from().subquery(this.table.getSubquery()); |
|
116 | 105 |
sqlbuilder.select().limit(1); |
117 | 106 |
|
118 | 107 |
st = conn.createStatement(); |
... | ... | |
236 | 225 |
String schema, |
237 | 226 |
String table) throws SQLException { |
238 | 227 |
|
228 |
String sql = getSQLToRetrievePrimaryKeysFromInformationSchema(catalog, schema, table); |
|
229 |
|
|
230 |
Statement st = null; |
|
231 |
ResultSet rs = null; |
|
232 |
List<String> pks = new ArrayList(); |
|
233 |
try { |
|
234 |
st = conn.createStatement(); |
|
235 |
rs = JDBCUtils.executeQuery(st, sql); |
|
236 |
while (rs.next()) { |
|
237 |
pks.add(rs.getString(1)); |
|
238 |
} |
|
239 |
if (pks.isEmpty()) { |
|
240 |
return null; |
|
241 |
} |
|
242 |
return pks; |
|
243 |
|
|
244 |
} catch (Exception ex) { |
|
245 |
return pks; |
|
246 |
|
|
247 |
} finally { |
|
248 |
JDBCUtils.closeQuietly(rs); |
|
249 |
JDBCUtils.closeQuietly(st); |
|
250 |
} |
|
251 |
} |
|
252 |
|
|
253 |
protected String getSQLToRetrievePrimaryKeysFromInformationSchema( |
|
254 |
String catalog, |
|
255 |
String schema, |
|
256 |
String table |
|
257 |
) throws SQLException { |
|
239 | 258 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder(); |
240 | 259 |
|
241 | 260 |
sqlbuilder.select().column().name("COLUMN_NAME"); |
... | ... | |
276 | 295 |
sqlbuilder.constant("PRIMARY KEY") |
277 | 296 |
) |
278 | 297 |
); |
279 |
|
|
280 |
Statement st = null; |
|
281 |
ResultSet rs = null; |
|
282 |
List<String> pks = new ArrayList(); |
|
283 |
try { |
|
284 |
st = conn.createStatement(); |
|
285 |
rs = JDBCUtils.executeQuery(st, sqlbuilder.toString()); |
|
286 |
while (rs.next()) { |
|
287 |
pks.add(rs.getString(1)); |
|
288 |
} |
|
289 |
if (pks.isEmpty()) { |
|
290 |
return null; |
|
291 |
} |
|
292 |
return pks; |
|
293 |
|
|
294 |
} catch (Exception ex) { |
|
295 |
return pks; |
|
296 |
|
|
297 |
} finally { |
|
298 |
JDBCUtils.closeQuietly(rs); |
|
299 |
JDBCUtils.closeQuietly(st); |
|
300 |
} |
|
298 |
return sqlbuilder.toString(); |
|
301 | 299 |
} |
302 |
|
|
300 |
|
|
301 |
|
|
303 | 302 |
protected EditableFeatureAttributeDescriptor getAttributeFromMetadata( |
304 | 303 |
EditableFeatureType type, |
305 | 304 |
Connection conn, |
... | ... | |
420 | 419 |
attr.setGeometryType(geomType); |
421 | 420 |
attr.setSRS(null); |
422 | 421 |
} catch (Exception ex) { |
423 |
logger.warn("Can't get default geometry type.",ex);
|
|
422 |
LOGGER.warn("Can't get default geometry type.",ex);
|
|
424 | 423 |
} |
425 | 424 |
} |
426 | 425 |
|
Also available in: Unified diff