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 / JDBCHelperBase.java @ 44058
History | View | Annotate | Download (13.1 KB)
1 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2.spi; |
---|---|---|---|
2 | |||
3 | import org.gvsig.fmap.dal.store.jdbc2.impl.ResulSetControlerBase; |
||
4 | import org.gvsig.fmap.dal.store.jdbc2.spi.operations.OperationsFactoryBase; |
||
5 | import java.sql.Connection; |
||
6 | import java.sql.ResultSet; |
||
7 | import org.apache.commons.lang3.StringUtils; |
||
8 | 44042 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType; |
9 | import static org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType.EWKB; |
||
10 | import static org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType.NATIVE; |
||
11 | import static org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType.WKB; |
||
12 | import static org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType.WKT; |
||
13 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.DataTypes; |
14 | import org.gvsig.fmap.dal.exception.DataException; |
||
15 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
16 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
17 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
18 | |||
19 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
20 | import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
||
21 | import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
||
22 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
23 | import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
||
24 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
25 | import org.gvsig.fmap.dal.store.db.DBHelper; |
||
26 | import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
||
27 | import org.gvsig.fmap.dal.store.jdbc.JDBCLibrary; |
||
28 | import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
||
29 | import org.gvsig.fmap.dal.store.jdbc.JDBCResource; |
||
30 | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
||
31 | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
||
32 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCCantFetchValueException; |
||
33 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
34 | import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
||
35 | import org.gvsig.fmap.dal.store.jdbc2.JDBCStoreProvider; |
||
36 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
37 | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
||
38 | import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler; |
||
39 | import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler.ResultSetEntry; |
||
40 | import org.gvsig.fmap.geom.Geometry; |
||
41 | import org.gvsig.fmap.geom.GeometryLocator; |
||
42 | import org.gvsig.fmap.geom.GeometryManager; |
||
43 | import org.gvsig.tools.dispose.impl.AbstractDisposable; |
||
44 | import org.gvsig.tools.exception.BaseException; |
||
45 | 43377 | jjdelcerro | import org.gvsig.tools.exception.NotYetImplemented; |
46 | 43020 | jjdelcerro | import org.slf4j.Logger; |
47 | import org.slf4j.LoggerFactory; |
||
48 | |||
49 | public class JDBCHelperBase extends AbstractDisposable implements ResourceConsumer, JDBCHelper { |
||
50 | |||
51 | private static final boolean ALLOW_AUTOMATIC_VALUES = true; |
||
52 | private static final String QUOTE_FOR_USE_IN_IDENTIFIERS = "\""; |
||
53 | private static final String QUOTE_FOR_USE_IN_STRINGS = "'"; |
||
54 | |||
55 | private static final Logger logger = LoggerFactory.getLogger(JDBCHelperBase.class); |
||
56 | |||
57 | private ResulSetControler resulSetControler = null; |
||
58 | |||
59 | // Quien ha creado este helper.
|
||
60 | // Se le reenviaran las notificaciones del recurso asociado al helper.
|
||
61 | private ResourceConsumer helperClient = null; |
||
62 | |||
63 | private GeometryManager geometryManager = null; |
||
64 | |||
65 | 43035 | jjdelcerro | private JDBCConnectionParameters connectionParameters;
|
66 | 43020 | jjdelcerro | |
67 | private JDBCStoreProvider store;
|
||
68 | |||
69 | protected OperationsFactory operationsFactory = null; |
||
70 | |||
71 | 43606 | jjdelcerro | protected SRSSolver srssolver;
|
72 | 43355 | jjdelcerro | |
73 | 43020 | jjdelcerro | public JDBCHelperBase(JDBCConnectionParameters connectionParameters) {
|
74 | this.connectionParameters = connectionParameters;
|
||
75 | 43355 | jjdelcerro | |
76 | // If a particular treatment is required to convert SRS to the
|
||
77 | // BBDD format, overwrite JDBCSRSsBase and use it instead of JDBCSRSsDumb.
|
||
78 | 43606 | jjdelcerro | this.srssolver = new SRSSolverDumb(this); |
79 | 43020 | jjdelcerro | } |
80 | |||
81 | protected void initialize( |
||
82 | ResourceConsumer helperClient, |
||
83 | JDBCConnectionParameters connectionParameters, |
||
84 | JDBCStoreProvider store |
||
85 | ) { |
||
86 | 43093 | jjdelcerro | this.store = store;
|
87 | 43020 | jjdelcerro | this.helperClient = helperClient;
|
88 | this.connectionParameters = connectionParameters;
|
||
89 | initializeResource(connectionParameters); |
||
90 | } |
||
91 | |||
92 | protected String getResourceType() { |
||
93 | return JDBCResource.NAME;
|
||
94 | } |
||
95 | |||
96 | @Override
|
||
97 | public String getProviderName() { |
||
98 | return JDBCLibrary.NAME;
|
||
99 | } |
||
100 | |||
101 | @Override
|
||
102 | public GeometrySupportType getGeometrySupportType() {
|
||
103 | // El proveedor generico de JDBC guadara las geoemtrias como WKT
|
||
104 | return GeometrySupportType.WKT;
|
||
105 | } |
||
106 | |||
107 | @Override
|
||
108 | public boolean hasSpatialFunctions() { |
||
109 | // Por defecto el proveedor generico de JDBC asume que la BBDD no
|
||
110 | // tiene soporte para funciones espaciales.
|
||
111 | return false; |
||
112 | } |
||
113 | |||
114 | @Override
|
||
115 | public boolean canWriteGeometry(int geometryType, int geometrySubtype) { |
||
116 | // Como va a guardar las geometrias en WKT, puede escribirlas todas.
|
||
117 | return true; |
||
118 | } |
||
119 | |||
120 | @Override
|
||
121 | public JDBCSQLBuilderBase createSQLBuilder() {
|
||
122 | 43687 | jjdelcerro | return new JDBCSQLBuilderBase(this); |
123 | 43020 | jjdelcerro | } |
124 | |||
125 | @Override
|
||
126 | public String getQuoteForIdentifiers() { |
||
127 | return QUOTE_FOR_USE_IN_IDENTIFIERS;
|
||
128 | } |
||
129 | |||
130 | @Override
|
||
131 | public boolean allowAutomaticValues() { |
||
132 | return ALLOW_AUTOMATIC_VALUES;
|
||
133 | } |
||
134 | |||
135 | @Override
|
||
136 | public boolean supportOffsetInSelect() { |
||
137 | // Asumimos que la BBDD soporta OFFSET
|
||
138 | return true; |
||
139 | } |
||
140 | |||
141 | @Override
|
||
142 | public String getQuoteForStrings() { |
||
143 | return QUOTE_FOR_USE_IN_STRINGS;
|
||
144 | } |
||
145 | |||
146 | @Override
|
||
147 | public OperationsFactory getOperations() {
|
||
148 | if (this.operationsFactory== null) { |
||
149 | this.operationsFactory = new OperationsFactoryBase(this); |
||
150 | } |
||
151 | return operationsFactory;
|
||
152 | } |
||
153 | |||
154 | protected void initializeResource(JDBCConnectionParameters params) { |
||
155 | // Object[] resourceParams = new Object[]{
|
||
156 | // params.getUrl(),
|
||
157 | // params.getHost(),
|
||
158 | // params.getPort(),
|
||
159 | // params.getDBName(),
|
||
160 | // params.getUser(),
|
||
161 | // params.getPassword(),
|
||
162 | // params.getJDBCDriverClassName()
|
||
163 | // };
|
||
164 | //
|
||
165 | // try {
|
||
166 | // ResourceManagerProviderServices manager
|
||
167 | // = (ResourceManagerProviderServices) DALLocator.getResourceManager();
|
||
168 | // JDBCResource resource = (JDBCResource) manager.createAddResource(
|
||
169 | // this.getResourceType(),
|
||
170 | // resourceParams
|
||
171 | // );
|
||
172 | // this.resource = resource;
|
||
173 | // this.resource.addConsumer(this);
|
||
174 | // } catch (InitializeException ex) {
|
||
175 | // logger.debug("Can't initialize resource (" + ArrayUtils.toString(resourceParams) + ").", ex);
|
||
176 | // throw new RuntimeException("Can't initialize resource (" + ArrayUtils.toString(resourceParams) + ").", ex);
|
||
177 | // }
|
||
178 | |||
179 | } |
||
180 | |||
181 | @Override
|
||
182 | public String getSourceId() { |
||
183 | return this.store.getSourceId(); |
||
184 | } |
||
185 | |||
186 | @Override
|
||
187 | public JDBCResource getResource() {
|
||
188 | return null; |
||
189 | // return this.resource;
|
||
190 | } |
||
191 | |||
192 | @Override
|
||
193 | public Connection getConnection() throws AccessResourceException { |
||
194 | 43377 | jjdelcerro | throw new NotYetImplemented(); |
195 | 43020 | jjdelcerro | } |
196 | |||
197 | @Override
|
||
198 | 43377 | jjdelcerro | public Connection getConnectionWritable() throws AccessResourceException { |
199 | return this.getConnection(); |
||
200 | } |
||
201 | |||
202 | @Override
|
||
203 | 43035 | jjdelcerro | public String getConnectionURL() { |
204 | return null; |
||
205 | } |
||
206 | |||
207 | 43358 | jjdelcerro | @Override
|
208 | 43035 | jjdelcerro | public JDBCConnectionParameters getConnectionParameters() {
|
209 | return connectionParameters;
|
||
210 | } |
||
211 | |||
212 | @Override
|
||
213 | 43020 | jjdelcerro | public void closeConnection(Connection connection) { |
214 | 43377 | jjdelcerro | if( connection != null ) { |
215 | logger.debug("Clossing connection "+connection.hashCode());
|
||
216 | try {
|
||
217 | connection.close(); |
||
218 | } catch(Exception ex) { |
||
219 | logger.warn("Can't close connection.", ex);
|
||
220 | } |
||
221 | 43020 | jjdelcerro | } |
222 | 43377 | jjdelcerro | } |
223 | 43020 | jjdelcerro | |
224 | @Override
|
||
225 | protected void doDispose() throws BaseException { |
||
226 | JDBCUtils.closeQuietly(this);
|
||
227 | } |
||
228 | |||
229 | @Override
|
||
230 | public void close() throws Exception { |
||
231 | // this.resource.removeConsumer(this);
|
||
232 | JDBCUtils.closeQuietly(this.resulSetControler);
|
||
233 | // this.resource = null;
|
||
234 | this.resulSetControler = null; |
||
235 | } |
||
236 | |||
237 | @Override
|
||
238 | public boolean closeResourceRequested(ResourceProvider resource) { |
||
239 | return this.helperClient.closeResourceRequested(resource); |
||
240 | } |
||
241 | |||
242 | @Override
|
||
243 | public void resourceChanged(ResourceProvider resource) { |
||
244 | this.helperClient.resourceChanged(resource);
|
||
245 | } |
||
246 | |||
247 | @Override
|
||
248 | public GeometryManager getGeometryManager() {
|
||
249 | if (this.geometryManager == null) { |
||
250 | this.geometryManager = GeometryLocator.getGeometryManager();
|
||
251 | } |
||
252 | return this.geometryManager; |
||
253 | } |
||
254 | |||
255 | @Override
|
||
256 | public ResulSetControler getResulSetControler() {
|
||
257 | if (this.resulSetControler == null) { |
||
258 | this.resulSetControler = new ResulSetControlerBase(this); |
||
259 | } |
||
260 | return this.resulSetControler; |
||
261 | } |
||
262 | |||
263 | @Override
|
||
264 | public void fetchFeature(FeatureProvider feature, ResultSetEntry rs) throws DataException { |
||
265 | 43358 | jjdelcerro | fetchFeature(feature, rs.get(), rs.getColumns()); |
266 | 43020 | jjdelcerro | } |
267 | |||
268 | @Override
|
||
269 | 43358 | jjdelcerro | public void fetchFeature(FeatureProvider feature, ResultSet rs, FeatureAttributeDescriptor[] columns) throws DataException { |
270 | 43420 | jjdelcerro | Object value;
|
271 | FeatureAttributeDescriptor column; |
||
272 | 43020 | jjdelcerro | try {
|
273 | 43358 | jjdelcerro | for (int index = 0; index < columns.length; index++) { |
274 | 43420 | jjdelcerro | column = columns[index]; |
275 | 43358 | jjdelcerro | switch (column.getType()) {
|
276 | 43020 | jjdelcerro | case DataTypes.GEOMETRY:
|
277 | value = this.getGeometryFromColumn(rs, index + 1); |
||
278 | break;
|
||
279 | default:
|
||
280 | value = rs.getObject(index + 1);
|
||
281 | } |
||
282 | 43358 | jjdelcerro | feature.set(column.getIndex(), value); |
283 | 43020 | jjdelcerro | } |
284 | } catch (Exception ex) { |
||
285 | throw new JDBCCantFetchValueException(ex); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | @Override
|
||
290 | public Geometry getGeometryFromColumn(ResultSetEntry rs, int index) throws DataException { |
||
291 | return getGeometryFromColumn(rs.get(), index);
|
||
292 | } |
||
293 | |||
294 | @Override
|
||
295 | public Geometry getGeometryFromColumn(ResultSet rs, int index) throws DataException { |
||
296 | try {
|
||
297 | Object value;
|
||
298 | switch (this.getGeometrySupportType()) { |
||
299 | 43358 | jjdelcerro | case NATIVE:
|
300 | 43020 | jjdelcerro | case WKB:
|
301 | value = rs.getBytes(index); |
||
302 | if (value == null) { |
||
303 | return null; |
||
304 | } |
||
305 | return this.getGeometryManager().createFrom((byte[]) value); |
||
306 | |||
307 | case EWKB:
|
||
308 | value = rs.getBytes(index); |
||
309 | if (value == null) { |
||
310 | return null; |
||
311 | } |
||
312 | return this.getGeometryManager().createFrom((byte[]) value); |
||
313 | case WKT:
|
||
314 | default:
|
||
315 | value = rs.getString(index); |
||
316 | if (value == null) { |
||
317 | return null; |
||
318 | } |
||
319 | return this.getGeometryManager().createFrom((String) value); |
||
320 | |||
321 | } |
||
322 | } catch (Exception ex) { |
||
323 | throw new JDBCCantFetchValueException(ex); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | @Override
|
||
328 | public FeatureProvider createFeature(FeatureType featureType) throws DataException { |
||
329 | return this.store.getStoreServices().createDefaultFeatureProvider(featureType); |
||
330 | } |
||
331 | |||
332 | @Override
|
||
333 | public boolean useSubquery() { |
||
334 | if( this.store == null ) { |
||
335 | return false; |
||
336 | } |
||
337 | return !StringUtils.isEmpty(this.store.getParameters().getSQL()); |
||
338 | } |
||
339 | 43355 | jjdelcerro | |
340 | @Override
|
||
341 | 43606 | jjdelcerro | public SRSSolver getSRSSolver() {
|
342 | return this.srssolver; |
||
343 | } |
||
344 | |||
345 | @Override
|
||
346 | 43020 | jjdelcerro | public JDBCStoreProvider createProvider(
|
347 | JDBCStoreParameters parameters, |
||
348 | DataStoreProviderServices providerServices |
||
349 | ) throws InitializeException {
|
||
350 | |||
351 | JDBCStoreProviderBase store = new JDBCStoreProviderBase(
|
||
352 | parameters, |
||
353 | providerServices, |
||
354 | DBHelper.newMetadataContainer(JDBCLibrary.NAME), |
||
355 | this
|
||
356 | ); |
||
357 | this.initialize(store, parameters, store);
|
||
358 | return store;
|
||
359 | } |
||
360 | |||
361 | @Override
|
||
362 | public JDBCServerExplorer createServerExplorer(
|
||
363 | JDBCServerExplorerParameters parameters, |
||
364 | DataServerExplorerProviderServices providerServices |
||
365 | ) throws InitializeException {
|
||
366 | |||
367 | JDBCServerExplorer explorer = new JDBCServerExplorerBase(
|
||
368 | parameters, |
||
369 | providerServices, |
||
370 | this
|
||
371 | ); |
||
372 | this.initialize(explorer, parameters, null); |
||
373 | return explorer;
|
||
374 | } |
||
375 | |||
376 | @Override
|
||
377 | public JDBCNewStoreParameters createNewStoreParameters() {
|
||
378 | return new JDBCNewStoreParameters(); |
||
379 | } |
||
380 | |||
381 | @Override
|
||
382 | public JDBCStoreParameters createOpenStoreParameters() {
|
||
383 | return new JDBCStoreParameters(); |
||
384 | } |
||
385 | |||
386 | 43358 | jjdelcerro | @Override
|
387 | 43020 | jjdelcerro | public JDBCServerExplorerParameters createServerExplorerParameters() {
|
388 | return new JDBCServerExplorerParameters(); |
||
389 | } |
||
390 | |||
391 | @Override
|
||
392 | public String getSourceId(JDBCStoreParameters parameters) { |
||
393 | return parameters.getHost() + ":" + |
||
394 | parameters.getDBName() + ":" +
|
||
395 | parameters.getSchema()+ ":" +
|
||
396 | parameters.tableID(); |
||
397 | } |
||
398 | |||
399 | } |