svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.mdb / src / main / java / org / gvsig / fmap / dal / store / mdb / operations / MDBFetchFeatureTypeOperation.java @ 44918
History | View | Annotate | Download (10.7 KB)
1 |
|
---|---|
2 |
package org.gvsig.fmap.dal.store.mdb.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.cresques.cts.IProjection; |
13 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
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.resource.exception.AccessResourceException; |
19 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
20 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
21 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
22 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
23 |
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolver; |
24 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation; |
25 |
import org.gvsig.fmap.geom.Geometry; |
26 |
import org.gvsig.fmap.geom.GeometryLocator; |
27 |
import org.gvsig.fmap.geom.GeometryManager; |
28 |
import org.gvsig.fmap.geom.type.GeometryType; |
29 |
import org.gvsig.tools.dataTypes.DataType; |
30 |
|
31 |
@SuppressWarnings("UseSpecificCatch") |
32 |
public class MDBFetchFeatureTypeOperation extends FetchFeatureTypeOperation { |
33 |
|
34 |
private static Map<String,GeometryType>mdbGeometryTypes = null; |
35 |
private Map<String,GeometryColumnInfo> geometry_column; |
36 |
|
37 |
private static class GeometryColumnInfo { |
38 |
|
39 |
public String columnName; |
40 |
public int geometryType; |
41 |
public String geometryTypeName; |
42 |
public int dimensions; |
43 |
public int srid; |
44 |
|
45 |
public GeometryColumnInfo(String columnName, int geometryType, String geometryTypeName, int dimensions, int srid) { |
46 |
this.columnName = columnName;
|
47 |
this.geometryType = geometryType;
|
48 |
this.geometryTypeName = geometryTypeName;
|
49 |
this.dimensions = dimensions;
|
50 |
this.srid = srid;
|
51 |
} |
52 |
} |
53 |
|
54 |
|
55 |
public MDBFetchFeatureTypeOperation(
|
56 |
JDBCHelper helper |
57 |
) { |
58 |
super(helper);
|
59 |
} |
60 |
|
61 |
private GeometryType getGT(
|
62 |
GeometryManager manager, |
63 |
int type,
|
64 |
int subtype
|
65 |
) { |
66 |
try {
|
67 |
return manager.getGeometryType(type, subtype);
|
68 |
} catch (Exception ex) { |
69 |
return null; |
70 |
} |
71 |
} |
72 |
|
73 |
public MDBFetchFeatureTypeOperation(
|
74 |
JDBCHelper helper, |
75 |
EditableFeatureType featureType, |
76 |
TableReference table, |
77 |
List<String> primaryKeys, |
78 |
String defaultGeometryColumn,
|
79 |
IProjection crs |
80 |
) { |
81 |
super(helper, featureType, table, primaryKeys, defaultGeometryColumn, crs);
|
82 |
} |
83 |
|
84 |
private GeometryColumnInfo getGeometryColumnInfo(String name) { |
85 |
if( geometry_column==null ) { |
86 |
geometry_column = new HashMap<>(); |
87 |
try {
|
88 |
//
|
89 |
// https://github.com/orbisgis/h2gis/wiki/1.-Spatial-data#geometry-columns-view
|
90 |
//
|
91 |
StringBuilder where = null; |
92 |
if( table.hasDatabase() ) {
|
93 |
if( where == null ) { |
94 |
where = new StringBuilder(); |
95 |
} else {
|
96 |
where.append(" AND ");
|
97 |
} |
98 |
where.append("UPPER(F_TABLE_CATALOG) = '");
|
99 |
where.append(table.getDatabase().toUpperCase()); |
100 |
where.append("'");
|
101 |
} |
102 |
if( table.hasSchema()) {
|
103 |
if( where == null ) { |
104 |
where = new StringBuilder(); |
105 |
} else {
|
106 |
where.append(" AND ");
|
107 |
} |
108 |
where.append("UPPER(F_TABLE_SCHEMA) = '");
|
109 |
where.append(table.getSchema().toUpperCase()); |
110 |
where.append("'");
|
111 |
} |
112 |
if( table.hasTable()) {
|
113 |
if( where == null ) { |
114 |
where = new StringBuilder(); |
115 |
} else {
|
116 |
where.append(" AND ");
|
117 |
} |
118 |
where.append("UPPER(F_TABLE_NAME) = '");
|
119 |
where.append(table.getTable().toUpperCase()); |
120 |
where.append("'");
|
121 |
} |
122 |
String sql = "SELECT F_GEOMETRY_COLUMN, GEOMETRY_TYPE, COORD_DIMENSION, SRID, TYPE FROM GEOMETRY_COLUMNS WHERE " + where; |
123 |
Statement st = this.getConnection().createStatement(); |
124 |
ResultSet rs = JDBCUtils.executeQuery(st,sql);
|
125 |
while( rs.next() ) {
|
126 |
geometry_column.put( |
127 |
rs.getString("F_GEOMETRY_COLUMN"),
|
128 |
new GeometryColumnInfo(
|
129 |
rs.getString("F_GEOMETRY_COLUMN"),
|
130 |
rs.getInt("GEOMETRY_TYPE"),
|
131 |
rs.getString("TYPE"),
|
132 |
rs.getInt("COORD_DIMENSION"),
|
133 |
rs.getInt("SRID")
|
134 |
) |
135 |
); |
136 |
} |
137 |
} catch (SQLException | AccessResourceException ex) { |
138 |
LOGGER.warn("Can't read metadata from table '"+table+"'.",ex); |
139 |
} |
140 |
} |
141 |
return this.geometry_column.get(name); |
142 |
} |
143 |
|
144 |
@Override
|
145 |
public void fetch(Connection conn) throws DataException { |
146 |
super.fetch(conn);
|
147 |
} |
148 |
|
149 |
@Override
|
150 |
protected void fetchGeometryTypeAndSRS( |
151 |
EditableFeatureAttributeDescriptor attr, |
152 |
ResultSetMetaData rsMetadata,
|
153 |
int colIndex
|
154 |
) { |
155 |
try {
|
156 |
if( attr.getType()==DataTypes.GEOMETRY ) {
|
157 |
GeometryColumnInfo column_info = this.getGeometryColumnInfo(attr.getName());
|
158 |
String type = "GEOMETRY"; |
159 |
if( column_info!=null ) { |
160 |
// H2GIS solo soporte 2D y 3D no soporta Ms
|
161 |
if( column_info.dimensions==3 ) { |
162 |
type = column_info.geometryTypeName+"Z";
|
163 |
} else {
|
164 |
type = column_info.geometryTypeName; |
165 |
} |
166 |
SRSSolver solver = this.helper.getSRSSolver();
|
167 |
attr.setSRS( |
168 |
solver.getProjection(getConnection(), column_info.srid) |
169 |
); |
170 |
} |
171 |
GeometryType gt = getGeometryTypeFromMDBSpatialType(type); |
172 |
if( gt != null ) { |
173 |
attr.setGeometryType(gt); |
174 |
} |
175 |
} |
176 |
} catch(Exception ex) { |
177 |
throw new RuntimeException("Can't fetch geometry type and SRS.", ex); |
178 |
} |
179 |
} |
180 |
|
181 |
private GeometryType getGeometryTypeFromMDBSpatialType(String typeName) { |
182 |
if( mdbGeometryTypes==null ) { |
183 |
//
|
184 |
// https://github.com/orbisgis/h2gis/wiki/1.-Spatial-data#geometry-columns-view
|
185 |
//
|
186 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
187 |
mdbGeometryTypes = new HashMap<>(); |
188 |
mdbGeometryTypes.put("POINT", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM2D));
|
189 |
mdbGeometryTypes.put("POINTZ", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM3D));
|
190 |
|
191 |
mdbGeometryTypes.put("LINESTRING", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM2D));
|
192 |
mdbGeometryTypes.put("LINESTRINGZ", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM3D));
|
193 |
|
194 |
mdbGeometryTypes.put("POLYGON", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM2D));
|
195 |
mdbGeometryTypes.put("POLYGONZ", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM3D));
|
196 |
|
197 |
mdbGeometryTypes.put("MULTIPOINT", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM2D));
|
198 |
mdbGeometryTypes.put("MULTIPOINTZ", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM3D));
|
199 |
|
200 |
mdbGeometryTypes.put("MULTILINESTRING", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM2D));
|
201 |
mdbGeometryTypes.put("MULTILINESTRINGZ", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM3D));
|
202 |
|
203 |
mdbGeometryTypes.put("MULTIPOLYGON", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM2D));
|
204 |
mdbGeometryTypes.put("MULTIPOLYGONZ", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM3D));
|
205 |
|
206 |
mdbGeometryTypes.put("GEOMETRY", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2D));
|
207 |
mdbGeometryTypes.put("GEOMETRYZ", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3D));
|
208 |
|
209 |
mdbGeometryTypes.put("GEOMCOLLECTION", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2D));
|
210 |
mdbGeometryTypes.put("GEOMCOLLECTIONZ", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3D));
|
211 |
} |
212 |
return mdbGeometryTypes.get(typeName);
|
213 |
} |
214 |
|
215 |
@Override
|
216 |
public String getSQLToRetrievePrimaryKeysFromInformationSchema() throws SQLException { |
217 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
218 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
219 |
|
220 |
sqlbuilder.select().column().name("COLUMN_LIST");
|
221 |
sqlbuilder.select().column().name("CONSTRAINT_TYPE");
|
222 |
sqlbuilder.select().from().table().schema("INFORMATION_SCHEMA").name("CONSTRAINTS"); |
223 |
sqlbuilder.select().where().set( |
224 |
expbuilder.like( |
225 |
expbuilder.column("TABLE_NAME"),
|
226 |
expbuilder.constant(table.getTable()) |
227 |
) |
228 |
); |
229 |
if( table.hasSchema() ) {
|
230 |
sqlbuilder.select().where().and( |
231 |
expbuilder.like( |
232 |
expbuilder.column("TABLE_SCHEMA"),
|
233 |
expbuilder.constant(table.getSchema()) |
234 |
) |
235 |
); |
236 |
} |
237 |
// if (catalog != null) {
|
238 |
// sqlbuilder.select().where().and(
|
239 |
// expbuilder.like(
|
240 |
// expbuilder.column("CONSTRAINT_CATALOG"),
|
241 |
// expbuilder.constant(catalog)
|
242 |
// )
|
243 |
// );
|
244 |
// }
|
245 |
sqlbuilder.select().where().and( |
246 |
expbuilder.eq( |
247 |
expbuilder.column("CONSTRAINT_TYPE"),
|
248 |
expbuilder.constant("PRIMARY KEY")
|
249 |
) |
250 |
); |
251 |
return sqlbuilder.toString();
|
252 |
} |
253 |
|
254 |
|
255 |
} |