01: package com.vividsolutions.jump.datastore.postgis;
02:
03: import java.sql.*;
04:
05: import org.postgresql.PGConnection;
06:
07: import com.vividsolutions.jump.datastore.AdhocQuery;
08: import com.vividsolutions.jump.datastore.DataStoreConnection;
09: import com.vividsolutions.jump.datastore.DataStoreException;
10: import com.vividsolutions.jump.datastore.DataStoreMetadata;
11: import com.vividsolutions.jump.datastore.FilterQuery;
12: import com.vividsolutions.jump.datastore.Query;
13: import com.vividsolutions.jump.datastore.SpatialReferenceSystemID;
14: import com.vividsolutions.jump.io.FeatureInputStream;
15:
16: /**
17: */
18: public class PostgisDSConnection implements DataStoreConnection {
19: private PostgisDSMetadata dbMetadata;
20: private Connection connection;
21:
22: public PostgisDSConnection(Connection conn) {
23: connection = conn;
24: dbMetadata = new PostgisDSMetadata(this );
25: }
26:
27: public Connection getConnection() {
28: return connection;
29: }
30:
31: public DataStoreMetadata getMetadata() {
32: return dbMetadata;
33: }
34:
35: public FeatureInputStream execute(Query query) {
36: if (query instanceof FilterQuery) {
37: try {
38: return executeFilterQuery((FilterQuery) query);
39: } catch (SQLException e) {
40: throw new RuntimeException(e);
41: }
42: }
43: if (query instanceof AdhocQuery) {
44: return executeAdhocQuery((AdhocQuery) query);
45: }
46: throw new IllegalArgumentException("Unsupported Query type");
47: }
48:
49: /**
50: * Executes a filter query.
51: *
52: * The SRID is optional for queries - it will be determined automatically
53: * from the table metadata if not supplied.
54: *
55: * @param query the query to execute
56: * @return the results of the query
57: * @throws SQLException
58: */
59: public FeatureInputStream executeFilterQuery(FilterQuery query)
60: throws SQLException {
61: SpatialReferenceSystemID srid = dbMetadata.getSRID(query
62: .getDatasetName(), query.getGeometryAttributeName());
63: String[] colNames = dbMetadata.getColumnNames(query
64: .getDatasetName());
65:
66: PostgisSQLBuilder builder = new PostgisSQLBuilder(srid,
67: colNames);
68: String queryString = builder.getSQL(query);
69:
70: PostgisFeatureInputStream ifs = new PostgisFeatureInputStream(
71: connection, queryString);
72: return ifs;
73: }
74:
75: public FeatureInputStream executeAdhocQuery(AdhocQuery query) {
76: String queryString = query.getQuery();
77: PostgisFeatureInputStream ifs = new PostgisFeatureInputStream(
78: connection, queryString);
79: return ifs;
80: }
81:
82: public void close() throws DataStoreException {
83: try {
84: connection.close();
85: } catch (Exception ex) {
86: throw new DataStoreException(ex);
87: }
88: }
89:
90: public boolean isClosed() throws DataStoreException {
91: try {
92: return connection.isClosed();
93: } catch (SQLException e) {
94: throw new DataStoreException(e);
95: }
96: }
97:
98: }
|