01: package com.vividsolutions.jump.datastore.postgis;
02:
03: import java.sql.*;
04:
05: import org.postgresql.*;
06:
07: import com.vividsolutions.jump.feature.Feature;
08: import com.vividsolutions.jump.feature.FeatureSchema;
09: import com.vividsolutions.jump.io.BaseFeatureInputStream;
10:
11: /**
12: * Reads features from an Oracle database.
13: */
14: public class PostgisFeatureInputStream extends BaseFeatureInputStream {
15: private FeatureSchema featureSchema;
16: private Connection conn;
17: private String queryString;
18: private boolean initialized = false;
19: private Exception savedException;
20:
21: private Statement stmt = null;
22: private ResultSet rs = null;
23: private PostgisResultSetConverter mapper;
24:
25: int geometryColIndex = -1;
26:
27: public PostgisFeatureInputStream(Connection conn, String queryString) {
28: this .conn = conn;
29: this .queryString = queryString;
30: }
31:
32: /**
33: * @return The underlaying {@link Connection}.
34: */
35: public Connection getConnection() {
36: return conn;
37: }
38:
39: private void init() throws SQLException {
40: if (initialized)
41: return;
42: initialized = true;
43:
44: //conn.setDefaultRowPrefetch(100);
45: stmt = conn.createStatement();
46: String parsedQuery = queryString;
47: //String parsedQuery = QueryUtil.parseQuery(queryString);
48: rs = stmt.executeQuery(parsedQuery);
49: mapper = new PostgisResultSetConverter(conn, rs);
50: featureSchema = mapper.getFeatureSchema();
51: }
52:
53: protected Feature readNext() throws Exception {
54: if (savedException != null)
55: throw savedException;
56: if (!initialized)
57: init();
58: if (rs == null)
59: return null;
60: if (!rs.next())
61: return null;
62: return getFeature();
63: }
64:
65: private Feature getFeature() throws Exception {
66: return mapper.getFeature();
67: }
68:
69: public void close() throws SQLException {
70: if (rs != null) {
71: rs.close();
72: }
73: if (stmt != null) {
74: stmt.close();
75: }
76: }
77:
78: public FeatureSchema getFeatureSchema() {
79: if (featureSchema != null)
80: return featureSchema;
81:
82: try {
83: init();
84: } catch (SQLException ex) {
85: savedException = ex;
86: }
87: if (featureSchema == null)
88: featureSchema = new FeatureSchema();
89: return featureSchema;
90: }
91: }
|