01: package com.vividsolutions.jump.datastore.postgis;
02:
03: import java.sql.*;
04:
05: import org.postgresql.*;
06:
07: import com.vividsolutions.jump.feature.*;
08: import com.vividsolutions.jump.datastore.*;
09: import com.vividsolutions.jump.datastore.jdbc.*;
10:
11: /**
12: * Implements the mapping between a result set and
13: * a {@link FeatureSchema} and {@link Feature} set.
14: *
15: * This is a transient worker class, whose lifetime
16: * should be no longer than the lifetime of the
17: * provided ResultSet
18: */
19: public class PostgisResultSetConverter {
20: private ResultSet rs;
21: private FeatureSchema featureSchema;
22: private int geometryColIndex = -1;
23: private ValueConverter[] mapper;
24: private PostgisValueConverterFactory odm;
25: private boolean isInitialized = false;
26:
27: public PostgisResultSetConverter(Connection conn, ResultSet rs) {
28: odm = new PostgisValueConverterFactory(conn);
29: this .rs = rs;
30: }
31:
32: public FeatureSchema getFeatureSchema() throws SQLException {
33: init();
34: return featureSchema;
35: }
36:
37: public Feature getFeature() throws Exception {
38: init();
39: Feature f = new BasicFeature(featureSchema);
40: for (int i = 0; i < mapper.length; i++) {
41: f.setAttribute(i, mapper[i].getValue(rs, i + 1));
42: }
43: return f;
44: }
45:
46: private void init() throws SQLException {
47: if (isInitialized)
48: return;
49: isInitialized = true;
50:
51: ResultSetMetaData rsmd = rs.getMetaData();
52: int numberOfColumns = rsmd.getColumnCount();
53: //String[] columnNames = new String[numberOfColumns];
54: //String[] columnTypeNames = new String[numberOfColumns];
55: //int[] columnPositions = new int[numberOfColumns];
56: mapper = new ValueConverter[numberOfColumns];
57: featureSchema = new FeatureSchema();
58:
59: for (int i = 0; i < numberOfColumns; i++) {
60: mapper[i] = odm.getConverter(rsmd, i + 1);
61: String colName = rsmd.getColumnName(i + 1);
62: // only handles one geometry col for now [MD ?]
63: // Convert the first geometry into AttributeType.GEOMETRY and the following ones
64: // into AttributeType.STRINGs [mmichaud 2007-05-13]
65: if (mapper[i].getType() == AttributeType.GEOMETRY) {
66: if (featureSchema.getGeometryIndex() == -1) {
67: colName = "GEOMETRY";
68: featureSchema.addAttribute(colName, mapper[i]
69: .getType());
70: } else {
71: mapper[i] = ValueConverterFactory.STRING_MAPPER;
72: featureSchema.addAttribute(colName,
73: AttributeType.STRING);
74: }
75: } else {
76: featureSchema
77: .addAttribute(colName, mapper[i].getType());
78: }
79: }
80: }
81:
82: }
|