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.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / JDBCSQLBuilderBase.java @ 44644
History | View | Annotate | Download (12.3 KB)
1 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2.spi; |
---|---|---|---|
2 | |||
3 | 43687 | jjdelcerro | import java.sql.Connection; |
4 | 43020 | jjdelcerro | import java.sql.PreparedStatement; |
5 | 43629 | jjdelcerro | import java.sql.SQLException; |
6 | 43020 | jjdelcerro | import java.util.ArrayList; |
7 | 44323 | jjdelcerro | import java.util.Date; |
8 | 43020 | jjdelcerro | import java.util.List; |
9 | 43687 | jjdelcerro | import org.cresques.cts.IProjection; |
10 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder.Parameter; |
11 | 44644 | jjdelcerro | import static org.gvsig.expressionevaluator.GeometryExpressionBuilderHelper.GeometrySupportType; |
12 | 43687 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
13 | 43479 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureReference; |
14 | 43687 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
15 | 43093 | jjdelcerro | import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase; |
16 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
17 | 43479 | jjdelcerro | import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
18 | 43687 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
19 | 43629 | jjdelcerro | import org.gvsig.fmap.geom.Geometry; |
20 | 43687 | jjdelcerro | import org.gvsig.fmap.geom.GeometryLocator; |
21 | import org.gvsig.fmap.geom.GeometryManager; |
||
22 | import org.gvsig.fmap.geom.aggregate.MultiLine; |
||
23 | import org.gvsig.fmap.geom.aggregate.MultiPoint; |
||
24 | import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
||
25 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
26 | import org.gvsig.fmap.geom.primitive.Primitive; |
||
27 | import org.gvsig.fmap.geom.type.GeometryType; |
||
28 | 43629 | jjdelcerro | import org.gvsig.tools.dispose.Disposable; |
29 | 43020 | jjdelcerro | |
30 | 44198 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
31 | 43020 | jjdelcerro | public class JDBCSQLBuilderBase extends SQLBuilderBase { |
32 | |||
33 | 44198 | jjdelcerro | public static final String PROP_FEATURE_TYPE = "FeatureType"; |
34 | public static final String PROP_TABLE = "Table"; |
||
35 | |||
36 | 43687 | jjdelcerro | private GeometryManager geometryManager = null; |
37 | protected final JDBCHelper helper; |
||
38 | |||
39 | public JDBCSQLBuilderBase(JDBCHelper helper) {
|
||
40 | 43020 | jjdelcerro | super();
|
41 | 43687 | jjdelcerro | this.helper = helper;
|
42 | 43020 | jjdelcerro | } |
43 | 43687 | jjdelcerro | |
44 | public JDBCHelper getHelper() {
|
||
45 | return helper;
|
||
46 | } |
||
47 | |||
48 | protected GeometryManager getGeometryManager() {
|
||
49 | if (this.geometryManager == null) { |
||
50 | this.geometryManager = GeometryLocator.getGeometryManager();
|
||
51 | } |
||
52 | return this.geometryManager; |
||
53 | } |
||
54 | |||
55 | @Override
|
||
56 | 44198 | jjdelcerro | public Object srs_id(IProjection projection) { |
57 | 43687 | jjdelcerro | Connection conn = null; |
58 | try {
|
||
59 | conn = this.helper.getConnection();
|
||
60 | SRSSolver solver = this.helper.getSRSSolver();
|
||
61 | Object srscode = solver.getDatabaseCode(conn, projection);
|
||
62 | 43737 | jjdelcerro | // logger.debug("database code srs {}, type {}, srssolver {}.",
|
63 | // new Object[] {
|
||
64 | // srscode,
|
||
65 | // srscode==null? "null":srscode.getClass().getSimpleName(),
|
||
66 | // solver
|
||
67 | // }
|
||
68 | // );
|
||
69 | 43687 | jjdelcerro | return srscode;
|
70 | } catch (Exception ex) { |
||
71 | throw new RuntimeException("Can't locate database code for SRS '"+projection.getAbrev()+"'."); |
||
72 | } finally {
|
||
73 | 44191 | jjdelcerro | this.helper.closeConnectionQuietly(conn);
|
74 | 43687 | jjdelcerro | } |
75 | } |
||
76 | |||
77 | 43093 | jjdelcerro | public void setParameters(PreparedStatement st) { |
78 | try {
|
||
79 | int columnIndex = 1; |
||
80 | 44198 | jjdelcerro | for (Parameter parameter : this.parameters()) { |
81 | st.setObject(columnIndex++, parameter.value()); |
||
82 | 43093 | jjdelcerro | } |
83 | } catch (Exception ex) { |
||
84 | String p = "unknow"; |
||
85 | try {
|
||
86 | 44198 | jjdelcerro | p = this.parameters().toString();
|
87 | 43093 | jjdelcerro | } catch (Exception ex2) { |
88 | // Do nothing
|
||
89 | } |
||
90 | throw new RuntimeException("Can't set parameters to prepared statement from parameters (" + p + ")", ex); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | 43629 | jjdelcerro | public Disposable setParameters(PreparedStatement st, FeatureProvider feature) { |
95 | 43020 | jjdelcerro | try {
|
96 | 43687 | jjdelcerro | FeatureType type = feature.getType(); |
97 | 43020 | jjdelcerro | List<Object> values = new ArrayList<>(); |
98 | 43687 | jjdelcerro | Object value;
|
99 | 44198 | jjdelcerro | for (Parameter parameter : this.parameters()) { |
100 | 43093 | jjdelcerro | if (parameter.is_constant()) {
|
101 | 44198 | jjdelcerro | value = parameter.value(); |
102 | 43687 | jjdelcerro | values.add(value); |
103 | 43020 | jjdelcerro | } else {
|
104 | 44198 | jjdelcerro | String name = parameter.name();
|
105 | 43687 | jjdelcerro | value = feature.get(name); |
106 | FeatureAttributeDescriptor attrDesc = type.getAttributeDescriptor(name); |
||
107 | 44323 | jjdelcerro | switch( attrDesc.getType() ) {
|
108 | case org.gvsig.fmap.dal.DataTypes.DATE:
|
||
109 | if( value == null ) { |
||
110 | values.add(null);
|
||
111 | } else {
|
||
112 | values.add(new java.sql.Date(((Date)value).getTime())); |
||
113 | } |
||
114 | break;
|
||
115 | case org.gvsig.fmap.dal.DataTypes.GEOMETRY:
|
||
116 | Geometry geom = this.forceGeometryType(
|
||
117 | attrDesc.getGeomType(), |
||
118 | (Geometry)value |
||
119 | ); |
||
120 | values.add(geom); |
||
121 | break;
|
||
122 | default:
|
||
123 | values.add(value); |
||
124 | break;
|
||
125 | 43687 | jjdelcerro | } |
126 | 43020 | jjdelcerro | } |
127 | } |
||
128 | 43629 | jjdelcerro | return this.setStatementParameters(st, values, this.geometry_support_type()); |
129 | 43020 | jjdelcerro | } catch (Exception ex) { |
130 | 43093 | jjdelcerro | String f = "unknow"; |
131 | try {
|
||
132 | f = feature.toString(); |
||
133 | } catch (Exception ex2) { |
||
134 | // Do nothing
|
||
135 | } |
||
136 | throw new RuntimeException("Can't set parameters to prepared statement from the feature (" + f + ")", ex); |
||
137 | 43020 | jjdelcerro | } |
138 | } |
||
139 | 43687 | jjdelcerro | |
140 | protected Geometry forceGeometryType(GeometryType geomtype, Geometry geom) throws CreateGeometryException { |
||
141 | if( geom == null ) { |
||
142 | return null; |
||
143 | } |
||
144 | switch( geomtype.getType() ) {
|
||
145 | case Geometry.TYPES.MULTIPOLYGON:
|
||
146 | if( geom.getType()==Geometry.TYPES.POLYGON ) {
|
||
147 | MultiPolygon x = getGeometryManager().createMultiPolygon(geomtype.getSubType()); |
||
148 | x.addPrimitive((Primitive) geom); |
||
149 | geom = x; |
||
150 | } |
||
151 | break;
|
||
152 | case Geometry.TYPES.MULTILINE:
|
||
153 | if( geom.getType()==Geometry.TYPES.LINE ) {
|
||
154 | MultiLine x = getGeometryManager().createMultiLine(geomtype.getSubType()); |
||
155 | x.addPrimitive((Primitive) geom); |
||
156 | geom = x; |
||
157 | } |
||
158 | break;
|
||
159 | case Geometry.TYPES.MULTIPOINT:
|
||
160 | if( geom.getType()==Geometry.TYPES.POINT ) {
|
||
161 | MultiLine x = getGeometryManager().createMultiLine(geomtype.getSubType()); |
||
162 | x.addPrimitive((Primitive) geom); |
||
163 | geom = x; |
||
164 | } |
||
165 | break;
|
||
166 | case Geometry.TYPES.POLYGON:
|
||
167 | if( geom.getType()==Geometry.TYPES.MULTIPOLYGON ) {
|
||
168 | MultiPolygon x = (MultiPolygon) geom; |
||
169 | if( x.getPrimitivesNumber()==1 ) { |
||
170 | geom = x.getPrimitiveAt(0);
|
||
171 | } |
||
172 | } |
||
173 | break;
|
||
174 | case Geometry.TYPES.LINE:
|
||
175 | if( geom.getType()==Geometry.TYPES.MULTILINE ) {
|
||
176 | MultiLine x = (MultiLine) geom; |
||
177 | if( x.getPrimitivesNumber()==1 ) { |
||
178 | geom = x.getPrimitiveAt(0);
|
||
179 | } |
||
180 | } |
||
181 | break;
|
||
182 | case Geometry.TYPES.POINT:
|
||
183 | if( geom.getType()==Geometry.TYPES.MULTIPOINT ) {
|
||
184 | MultiPoint x = (MultiPoint) geom; |
||
185 | if( x.getPrimitivesNumber()==1 ) { |
||
186 | geom = x.getPrimitiveAt(0);
|
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | return geom;
|
||
191 | } |
||
192 | |||
193 | 43629 | jjdelcerro | public Disposable setParameters(PreparedStatement st, FeatureReference reference) { |
194 | 43479 | jjdelcerro | try {
|
195 | |||
196 | List<Object> values = new ArrayList<>(); |
||
197 | 44198 | jjdelcerro | for (Parameter parameter : this.parameters()) { |
198 | 43479 | jjdelcerro | if (parameter.is_constant()) {
|
199 | 44198 | jjdelcerro | values.add(parameter.value()); |
200 | 43479 | jjdelcerro | } else {
|
201 | 44198 | jjdelcerro | String name = parameter.name();
|
202 | 43479 | jjdelcerro | values.add(((FeatureReferenceProviderServices)reference).getKeyValue(name)); |
203 | } |
||
204 | } |
||
205 | 43629 | jjdelcerro | return this.setStatementParameters(st, values, this.geometry_support_type()); |
206 | 43479 | jjdelcerro | } catch (Exception ex) { |
207 | String f = "unknow"; |
||
208 | try {
|
||
209 | f = reference.toString(); |
||
210 | } catch (Exception ex2) { |
||
211 | // Do nothing
|
||
212 | } |
||
213 | throw new RuntimeException("Can't set parameters to prepared statement from the feature (" + f + ")", ex); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | 43629 | jjdelcerro | public Disposable setStatementParameters(
|
218 | PreparedStatement st,
|
||
219 | List values,
|
||
220 | GeometrySupportType geometrySupportType) throws SQLException { |
||
221 | |||
222 | if (values == null) { |
||
223 | return new Disposable() { |
||
224 | @Override
|
||
225 | public void dispose() { |
||
226 | } |
||
227 | }; |
||
228 | } |
||
229 | 44198 | jjdelcerro | if( LOGGER.isDebugEnabled() ) {
|
230 | 43732 | jjdelcerro | StringBuilder debug = new StringBuilder(); |
231 | debug.append("st.set(");
|
||
232 | try {
|
||
233 | byte[] bytes; |
||
234 | int columnIndex = 1; |
||
235 | for (Object value : values) { |
||
236 | if (value instanceof Geometry) { |
||
237 | switch(geometrySupportType) {
|
||
238 | case WKT:
|
||
239 | value = ((Geometry) value).convertToWKT(); |
||
240 | debug.append("/*");
|
||
241 | debug.append(columnIndex); |
||
242 | debug.append("*/ ");
|
||
243 | 44198 | jjdelcerro | debug.append(as_string(value)); |
244 | 43732 | jjdelcerro | debug.append(", ");
|
245 | break;
|
||
246 | case NATIVE:
|
||
247 | case WKB:
|
||
248 | bytes = ((Geometry) value).convertToWKB(); |
||
249 | debug.append("/*");
|
||
250 | debug.append(columnIndex); |
||
251 | debug.append("*/ ");
|
||
252 | 44198 | jjdelcerro | debug.append(as_string(bytes)); |
253 | 43732 | jjdelcerro | debug.append(", ");
|
254 | break;
|
||
255 | case EWKB:
|
||
256 | bytes = ((Geometry) value).convertToEWKB(); |
||
257 | debug.append("/*");
|
||
258 | debug.append(columnIndex); |
||
259 | debug.append("*/ ");
|
||
260 | 44198 | jjdelcerro | debug.append(as_string(bytes)); |
261 | 43732 | jjdelcerro | debug.append(", ");
|
262 | break;
|
||
263 | } |
||
264 | } else {
|
||
265 | debug.append("/*");
|
||
266 | debug.append(columnIndex); |
||
267 | debug.append("*/ ");
|
||
268 | if( value instanceof String ) { |
||
269 | 44198 | jjdelcerro | debug.append(as_string(value)); |
270 | 43732 | jjdelcerro | } else if( value instanceof Boolean ) { |
271 | 44198 | jjdelcerro | debug.append( ((Boolean)value)? constant_true:constant_false );
|
272 | 43732 | jjdelcerro | } else {
|
273 | debug.append(value); |
||
274 | } |
||
275 | debug.append(", ");
|
||
276 | } |
||
277 | columnIndex++; |
||
278 | } |
||
279 | debug.append(")");
|
||
280 | 44198 | jjdelcerro | LOGGER.debug(debug.toString()); |
281 | 43732 | jjdelcerro | } catch(Exception ex) { |
282 | } |
||
283 | } |
||
284 | 43629 | jjdelcerro | try {
|
285 | byte[] bytes; |
||
286 | int columnIndex = 1; |
||
287 | for (Object value : values) { |
||
288 | if (value instanceof Geometry) { |
||
289 | switch(geometrySupportType) {
|
||
290 | case WKT:
|
||
291 | value = ((Geometry) value).convertToWKT(); |
||
292 | st.setObject(columnIndex, value); |
||
293 | break;
|
||
294 | case NATIVE:
|
||
295 | case WKB:
|
||
296 | bytes = ((Geometry) value).convertToWKB(); |
||
297 | st.setBytes(columnIndex, bytes); |
||
298 | break;
|
||
299 | case EWKB:
|
||
300 | bytes = ((Geometry) value).convertToEWKB(); |
||
301 | st.setBytes(columnIndex, bytes); |
||
302 | break;
|
||
303 | } |
||
304 | } else {
|
||
305 | st.setObject(columnIndex, value); |
||
306 | } |
||
307 | columnIndex++; |
||
308 | } |
||
309 | return new Disposable() { |
||
310 | @Override
|
||
311 | public void dispose() { |
||
312 | } |
||
313 | }; |
||
314 | } catch(Exception ex) { |
||
315 | throw new SQLException("Can't set values for the prepared statement.", ex); |
||
316 | } |
||
317 | } |
||
318 | 43020 | jjdelcerro | } |