root / trunk / org.gvsig.postgresql / org.gvsig.postgresql.provider / src / main / java / org / gvsig / postgresql / dal / operations / PostgreSQLFetchFeatureTypeOperation.java @ 510
History | View | Annotate | Download (11.1 KB)
1 |
|
---|---|
2 |
package org.gvsig.postgresql.dal.operations; |
3 |
|
4 |
import java.sql.Connection; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.ResultSetMetaData; |
7 |
import java.sql.SQLException; |
8 |
import java.sql.Statement; |
9 |
import java.util.HashMap; |
10 |
import java.util.List; |
11 |
import java.util.Map; |
12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.cresques.cts.IProjection; |
14 |
import org.gvsig.fmap.dal.DataTypes; |
15 |
import org.gvsig.fmap.dal.exception.DataException; |
16 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
17 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
18 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
19 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
20 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
21 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
22 |
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolver; |
23 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation; |
24 |
import org.gvsig.fmap.geom.Geometry; |
25 |
import org.gvsig.fmap.geom.GeometryLocator; |
26 |
import org.gvsig.fmap.geom.GeometryManager; |
27 |
import org.gvsig.fmap.geom.type.GeometryType; |
28 |
import org.gvsig.postgresql.dal.PostgreSQLBuilder; |
29 |
|
30 |
@SuppressWarnings("UseSpecificCatch") |
31 |
public class PostgreSQLFetchFeatureTypeOperation extends FetchFeatureTypeOperation { |
32 |
|
33 |
private static Map<String,GeometryType>databaseGeometryTypes = null; |
34 |
|
35 |
public PostgreSQLFetchFeatureTypeOperation(
|
36 |
JDBCHelper helper |
37 |
) { |
38 |
super(helper);
|
39 |
} |
40 |
|
41 |
private GeometryType getGT(
|
42 |
GeometryManager manager, |
43 |
int type,
|
44 |
int subtype
|
45 |
) { |
46 |
try {
|
47 |
return manager.getGeometryType(type, subtype);
|
48 |
} catch (Exception ex) { |
49 |
return null; |
50 |
} |
51 |
} |
52 |
|
53 |
public PostgreSQLFetchFeatureTypeOperation(
|
54 |
JDBCHelper helper, |
55 |
EditableFeatureType featureType, |
56 |
TableReference table, |
57 |
List<String> primaryKeys, |
58 |
String defaultGeometryColumn,
|
59 |
IProjection crs |
60 |
) { |
61 |
super(helper, featureType, table, primaryKeys, defaultGeometryColumn, crs);
|
62 |
} |
63 |
|
64 |
@Override
|
65 |
public void fetch(EditableFeatureType featureType, Connection conn, TableReference table, List<String> pks, String defaultGeometryColumn, IProjection crs) throws DataException { |
66 |
super.fetch(featureType, conn, table, pks, defaultGeometryColumn, crs);
|
67 |
} |
68 |
|
69 |
@Override
|
70 |
protected int getDataTypeFromMetadata( |
71 |
ResultSetMetaData rsMetadata,
|
72 |
int colIndex
|
73 |
) throws SQLException { |
74 |
|
75 |
return super.getDataTypeFromMetadata(rsMetadata, colIndex); |
76 |
} |
77 |
protected String getSQLToRetrievePrimaryKeysFromInformationSchema( |
78 |
String catalog,
|
79 |
String schema,
|
80 |
String table
|
81 |
) throws SQLException { |
82 |
PostgreSQLBuilder sqlbuilder = (PostgreSQLBuilder) this.createSQLBuilder();
|
83 |
|
84 |
String column_COLUMN_NAME = "column_name"; |
85 |
String column_CONSTRAINT_TYPE = "constraint_type"; |
86 |
|
87 |
if( sqlbuilder.getDatabaseVersion().getMajor()<10 ) { |
88 |
column_COLUMN_NAME = "COLUMN_NAME";
|
89 |
column_CONSTRAINT_TYPE = "CONSTRAINT_TYPE";
|
90 |
} |
91 |
sqlbuilder.select().column().name(column_COLUMN_NAME); |
92 |
sqlbuilder.select().column().name(column_CONSTRAINT_TYPE); |
93 |
sqlbuilder.select().from().custom( |
94 |
"INFORMATION_SCHEMA.table_constraints t_cons "
|
95 |
+ "inner join INFORMATION_SCHEMA.key_column_usage c on "
|
96 |
+ "c.constraint_catalog = t_cons.constraint_catalog and "
|
97 |
+ "c.table_schema = t_cons.table_schema and "
|
98 |
+ "c.table_name = t_cons.table_name and "
|
99 |
+ "c.constraint_name = t_cons.constraint_name "
|
100 |
); |
101 |
sqlbuilder.select().where().set( |
102 |
sqlbuilder.like( |
103 |
sqlbuilder.custom("c.TABLE_NAME"),
|
104 |
sqlbuilder.constant(table) |
105 |
) |
106 |
); |
107 |
if (schema != null) { |
108 |
sqlbuilder.select().where().and( |
109 |
sqlbuilder.like( |
110 |
sqlbuilder.custom("c.TABLE_SCHEMA"),
|
111 |
sqlbuilder.constant(schema) |
112 |
) |
113 |
); |
114 |
} |
115 |
if (catalog != null) { |
116 |
sqlbuilder.select().where().and( |
117 |
sqlbuilder.like( |
118 |
sqlbuilder.custom("c.CONSTRAINT_CATALOG"),
|
119 |
sqlbuilder.constant(catalog) |
120 |
) |
121 |
); |
122 |
} |
123 |
sqlbuilder.select().where().and( |
124 |
sqlbuilder.eq( |
125 |
sqlbuilder.column(column_CONSTRAINT_TYPE), |
126 |
sqlbuilder.constant("PRIMARY KEY")
|
127 |
) |
128 |
); |
129 |
return sqlbuilder.toString();
|
130 |
} |
131 |
|
132 |
@Override
|
133 |
protected void fetchGeometryTypeAndSRS( |
134 |
EditableFeatureAttributeDescriptor attr, |
135 |
ResultSetMetaData rsMetadata,
|
136 |
int colIndex
|
137 |
) { |
138 |
if( attr.getType()!=DataTypes.GEOMETRY ) {
|
139 |
return;
|
140 |
} |
141 |
try {
|
142 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
143 |
sqlbuilder.select().column().name("f_table_catalog");
|
144 |
sqlbuilder.select().column().name("f_table_schema");
|
145 |
sqlbuilder.select().column().name("f_table_name");
|
146 |
sqlbuilder.select().column().name("f_geometry_column");
|
147 |
sqlbuilder.select().column().name("coord_dimension");
|
148 |
sqlbuilder.select().column().name("srid");
|
149 |
sqlbuilder.select().column().name("type");
|
150 |
sqlbuilder.select().where().set( |
151 |
sqlbuilder.eq( |
152 |
sqlbuilder.column("f_table_name"),
|
153 |
sqlbuilder.constant(this.getTable().getTable())
|
154 |
) |
155 |
); |
156 |
sqlbuilder.select().where().and( |
157 |
sqlbuilder.eq( |
158 |
sqlbuilder.column("f_geometry_column"),
|
159 |
sqlbuilder.constant(attr.getName()) |
160 |
) |
161 |
); |
162 |
sqlbuilder.select().from().table().name("geometry_columns");
|
163 |
Statement st = null; |
164 |
ResultSet rs = null; |
165 |
|
166 |
Integer srsid = null; |
167 |
String geometryTypeName = null; |
168 |
try {
|
169 |
st = this.getConnection().createStatement();
|
170 |
rs = JDBCUtils.executeQuery(st, sqlbuilder.toString()); |
171 |
if (rs.next()) {
|
172 |
srsid = rs.getInt("srid");
|
173 |
geometryTypeName = rs.getString("type");
|
174 |
} |
175 |
} finally {
|
176 |
JDBCUtils.closeQuietly(rs); |
177 |
JDBCUtils.closeQuietly(st); |
178 |
} |
179 |
if( !StringUtils.isEmpty(geometryTypeName) ) {
|
180 |
GeometryType gt = getGeometryTypeFromDatabaseTypeName(geometryTypeName); |
181 |
attr.setGeometryType(gt); |
182 |
} |
183 |
if( srsid!=null ) { |
184 |
SRSSolver srssolver = this.helper.getSRSSolver();
|
185 |
attr.setSRS(srssolver.getProjection(this.getConnection(),srsid));
|
186 |
} |
187 |
} catch (Exception ex) { |
188 |
LOGGER.debug("Can't get geometry type and srs from column '"+attr.getName()+"'.",ex); |
189 |
} |
190 |
} |
191 |
|
192 |
private GeometryType getGeometryTypeFromDatabaseTypeName(String typeName) { |
193 |
if( databaseGeometryTypes==null ) { |
194 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
195 |
databaseGeometryTypes = new HashMap<>(); |
196 |
databaseGeometryTypes.put("POINT", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM2D));
|
197 |
databaseGeometryTypes.put("POINTZ", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM3D));
|
198 |
databaseGeometryTypes.put("POINTM", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM2DM));
|
199 |
databaseGeometryTypes.put("POINTZM", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM3DM));
|
200 |
|
201 |
databaseGeometryTypes.put("LINESTRING", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM2D));
|
202 |
databaseGeometryTypes.put("LINESTRINGZ", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM3D));
|
203 |
databaseGeometryTypes.put("LINESTRINGM", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM2DM));
|
204 |
databaseGeometryTypes.put("LINESTRINGZM", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM3DM));
|
205 |
|
206 |
databaseGeometryTypes.put("POLYGON", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM2D));
|
207 |
databaseGeometryTypes.put("POLYGONZ", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM3D));
|
208 |
databaseGeometryTypes.put("POLYGONM", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM2DM));
|
209 |
databaseGeometryTypes.put("POLYGONZM", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM3DM));
|
210 |
|
211 |
databaseGeometryTypes.put("MULTIPOINT", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM2D));
|
212 |
databaseGeometryTypes.put("MULTIPOINTZ", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM3D));
|
213 |
databaseGeometryTypes.put("MULTIPOINTM", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM2DM));
|
214 |
databaseGeometryTypes.put("MULTIPOINTZM", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM3DM));
|
215 |
|
216 |
databaseGeometryTypes.put("MULTILINESTRING", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM2D));
|
217 |
databaseGeometryTypes.put("MULTILINESTRINGZ", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM3D));
|
218 |
databaseGeometryTypes.put("MULTILINESTRINGM", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM2DM));
|
219 |
databaseGeometryTypes.put("MULTILINESTRINGZM", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM3DM));
|
220 |
|
221 |
databaseGeometryTypes.put("MULTIPOLYGON", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM2D));
|
222 |
databaseGeometryTypes.put("MULTIPOLYGONZ", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM3D));
|
223 |
databaseGeometryTypes.put("MULTIPOLYGONM", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM2DM));
|
224 |
databaseGeometryTypes.put("MULTIPOLYGONZM", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM3DM));
|
225 |
|
226 |
databaseGeometryTypes.put("GEOMETRY", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2D));
|
227 |
databaseGeometryTypes.put("GEOMETRYZ", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3D));
|
228 |
databaseGeometryTypes.put("GEOMETRYM", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2DM));
|
229 |
databaseGeometryTypes.put("GEOMETRYZM", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3DM));
|
230 |
} |
231 |
return databaseGeometryTypes.get(typeName);
|
232 |
} |
233 |
|
234 |
} |