Revision 37896 branches/v2_0_0_prep/extensions/org.gvsig.oracle/src/org/gvsig/fmap/dal/store/oracle/OracleStoreProvider.java
OracleStoreProvider.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2009 Prodevelop S.L. main development |
|
26 |
*/ |
|
27 |
|
|
28 |
package org.gvsig.fmap.dal.store.oracle; |
|
29 |
|
|
30 |
import java.security.InvalidParameterException; |
|
31 |
import java.sql.Connection; |
|
32 |
import java.sql.PreparedStatement; |
|
33 |
import java.sql.ResultSet; |
|
34 |
import java.sql.SQLException; |
|
35 |
import java.sql.Statement; |
|
36 |
import java.sql.Timestamp; |
|
37 |
import java.util.ArrayList; |
|
38 |
import java.util.List; |
|
39 |
|
|
40 |
import oracle.sql.STRUCT; |
|
41 |
import oracle.sql.TIMESTAMP; |
|
42 |
|
|
43 |
import org.gvsig.fmap.dal.DALLocator; |
|
44 |
import org.gvsig.fmap.dal.DataManager; |
|
45 |
import org.gvsig.fmap.dal.DataServerExplorer; |
|
46 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
47 |
import org.gvsig.fmap.dal.DataTypes; |
|
48 |
import org.gvsig.fmap.dal.exception.DataException; |
|
49 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
50 |
import org.gvsig.fmap.dal.exception.OpenException; |
|
51 |
import org.gvsig.fmap.dal.exception.ReadException; |
|
52 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
53 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
54 |
import org.gvsig.fmap.dal.feature.FeatureIndexes; |
|
55 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
56 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
57 |
import org.gvsig.fmap.dal.feature.exception.CreateFeatureException; |
|
58 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureIndexes; |
|
59 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore; |
|
60 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
|
61 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
|
62 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
63 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
|
64 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
|
65 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
|
66 |
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException; |
|
67 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
|
68 |
import org.gvsig.fmap.dal.store.db.DBHelper; |
|
69 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
|
70 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
71 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProviderWriter; |
|
72 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCException; |
|
73 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecutePreparedSQLException; |
|
74 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
|
75 |
import org.gvsig.fmap.dal.store.oracle.index.OracleFeatureIndex; |
|
76 |
import org.gvsig.fmap.dal.store.oracle.index.OracleRemoteFeatureIndexProvider; |
|
77 |
import org.gvsig.fmap.geom.Geometry; |
|
78 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
79 |
import org.gvsig.fmap.geom.operation.towkb.ToWKB; |
|
80 |
import org.gvsig.tools.ToolsLocator; |
|
81 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
82 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
|
83 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDateTime; |
|
84 |
import org.gvsig.tools.dynobject.DynObject; |
|
85 |
import org.gvsig.tools.exception.BaseException; |
|
86 |
import org.gvsig.utils.DateTime; |
|
87 |
|
|
88 |
import org.postgresql.jdbc2.TimestampUtils; |
|
89 |
import org.slf4j.Logger; |
|
90 |
import org.slf4j.LoggerFactory; |
|
91 |
|
|
92 |
/** |
|
93 |
* Oracle Provider |
|
94 |
* |
|
95 |
* @author vsanjaime |
|
96 |
* |
|
97 |
*/ |
|
98 |
public class OracleStoreProvider extends JDBCStoreProviderWriter { |
|
99 |
|
|
100 |
public final static Logger logger = LoggerFactory |
|
101 |
.getLogger(OracleStoreProvider.class); |
|
102 |
|
|
103 |
public static String NAME = "Oracle"; |
|
104 |
public static String DESCRIPTION = "Oracle source"; |
|
105 |
public static final String METADATA_DEFINITION_NAME = NAME; |
|
106 |
|
|
107 |
private boolean oracleIndexSet = false; |
|
108 |
|
|
109 |
/* |
|
110 |
protected static void registerDynClass() { |
|
111 |
DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
|
112 |
DynClass dynClass; |
|
113 |
if (DYNCLASS == null) { |
|
114 |
dynClass = dynman.add(DYNCLASS_NAME, DESCRIPTION); |
|
115 |
|
|
116 |
// dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME)); |
|
117 |
DYNCLASS = dynClass; |
|
118 |
} |
|
119 |
} |
|
120 |
*/ |
|
121 |
|
|
122 |
public OracleStoreProvider(OracleStoreParameters params, |
|
123 |
DataStoreProviderServices storeServices) throws InitializeException { |
|
124 |
super(params, storeServices, |
|
125 |
DBHelper.newMetadataContainer(METADATA_DEFINITION_NAME)); |
|
126 |
|
|
127 |
String defaultGeometryAttributeName; |
|
128 |
try { |
|
129 |
defaultGeometryAttributeName = this.getFeatureStore() |
|
130 |
.getDefaultFeatureType() |
|
131 |
.getDefaultGeometryAttributeName(); |
|
132 |
} catch (DataException e) { |
|
133 |
throw new InitializeException(e); |
|
134 |
} |
|
135 |
|
|
136 |
String ora_srid = |
|
137 |
getOraHelper().getOraTableSrid(params, defaultGeometryAttributeName); |
|
138 |
this.setDynValue(OracleStoreParameters.ORA_TABLE_SRID_KEY, ora_srid); |
|
139 |
|
|
140 |
|
|
141 |
} |
|
142 |
|
|
143 |
/** |
|
144 |
* Default Constructor. |
|
145 |
* |
|
146 |
* @param params |
|
147 |
* @param storeServices |
|
148 |
* @param metadata |
|
149 |
*/ |
|
150 |
protected OracleStoreProvider(DataStoreParameters params, |
|
151 |
DataStoreProviderServices storeServices, DynObject metadata) throws InitializeException { |
|
152 |
super((JDBCStoreParameters) params, storeServices, metadata); |
|
153 |
} |
|
154 |
|
|
155 |
/** |
|
156 |
* Constructor when cannot create metada in constrution time. <br> |
|
157 |
* <br> |
|
158 |
* <strong>Note: </strong> Don't use it if not is necesary. Set metada |
|
159 |
* <strong>as soon as posible</strong> by |
|
160 |
* {@link AbstractFeatureStoreProvider#setMetadata(DynObject)} |
|
161 |
* |
|
162 |
* @param params |
|
163 |
* @param storeServices |
|
164 |
*/ |
|
165 |
protected OracleStoreProvider(DataStoreParameters params, |
|
166 |
DataStoreProviderServices storeServices) throws InitializeException { |
|
167 |
this((JDBCStoreParameters) params, storeServices, null); |
|
168 |
} |
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
private OracleStoreParameters getOracleStoreParameters() { |
|
174 |
return (OracleStoreParameters) this.getParameters(); |
|
175 |
} |
|
176 |
|
|
177 |
protected JDBCHelper createHelper() throws InitializeException { |
|
178 |
return new OracleHelper(this, getOracleStoreParameters()); |
|
179 |
} |
|
180 |
|
|
181 |
public void open() throws OpenException { |
|
182 |
super.open(); |
|
183 |
|
|
184 |
// =============== add remote oracle geometry index to prevent default index |
|
185 |
if (!oracleIndexSet) { |
|
186 |
|
|
187 |
DefaultFeatureStore dfs = null; |
|
188 |
if (!(getFeatureStore() instanceof DefaultFeatureStore)) { |
|
189 |
logger.warn("Feature store not DefaultFeatureStore: " + dfs.getClass().getName()); |
|
190 |
return; |
|
191 |
} |
|
192 |
dfs = (DefaultFeatureStore) getFeatureStore(); |
|
193 |
|
|
194 |
|
|
195 |
FeatureIndexes findexes = dfs.getIndexes(); |
|
196 |
if (findexes instanceof DefaultFeatureIndexes) { |
|
197 |
DefaultFeatureIndexes dfi = (DefaultFeatureIndexes) findexes; |
|
198 |
|
|
199 |
FeatureType ft = null; |
|
200 |
try { |
|
201 |
ft = dfs.getDefaultFeatureType(); |
|
202 |
} catch (DataException e) { |
|
203 |
throw new OpenException(getFeatureStore().getName(), e); |
|
204 |
} |
|
205 |
dfi.addIndex( |
|
206 |
new OracleFeatureIndex( |
|
207 |
(FeatureStoreProviderServices) getFeatureStore(), |
|
208 |
ft, |
|
209 |
new OracleRemoteFeatureIndexProvider(dfs), |
|
210 |
ft.getDefaultGeometryAttributeName(), |
|
211 |
OracleRemoteFeatureIndexProvider.NAME)); |
|
212 |
} |
|
213 |
oracleIndexSet = true; |
|
214 |
} |
|
215 |
// =============== |
|
216 |
} |
|
217 |
|
|
218 |
|
|
219 |
protected String fixFilter(String filter) { |
|
220 |
// filter was created by a class in oracle plugin, should not need fix |
|
221 |
return filter; |
|
222 |
} |
|
223 |
|
|
224 |
public String getName() { |
|
225 |
return NAME; |
|
226 |
} |
|
227 |
|
|
228 |
public int getOIDType() { |
|
229 |
return DataTypes.LONG; |
|
230 |
} |
|
231 |
|
|
232 |
private static long ora_oid = 100000000; |
|
233 |
|
|
234 |
public Object createNewOID() { |
|
235 |
return new Long(ora_oid++); |
|
236 |
} |
|
237 |
|
|
238 |
public FeatureSetProvider createSet(FeatureQuery query, |
|
239 |
FeatureType featureType) throws DataException { |
|
240 |
|
|
241 |
return new OracleSetProvider(this, query, featureType); |
|
242 |
} |
|
243 |
|
|
244 |
public DataServerExplorer getExplorer() throws ReadException { |
|
245 |
DataManager manager = DALLocator.getDataManager(); |
|
246 |
OracleServerExplorerParameters exParams; |
|
247 |
OracleStoreParameters params = getOracleStoreParameters(); |
|
248 |
try { |
|
249 |
exParams = (OracleServerExplorerParameters) manager |
|
250 |
.createServerExplorerParameters(OracleServerExplorer.NAME); |
|
251 |
exParams.setUrl(params.getUrl()); |
|
252 |
exParams.setHost(params.getHost()); |
|
253 |
exParams.setPort(params.getPort()); |
|
254 |
exParams.setDBName(params.getDBName()); |
|
255 |
exParams.setUser(params.getUser()); |
|
256 |
exParams.setPassword(params.getPassword()); |
|
257 |
exParams.setCatalog(params.getCatalog()); |
|
258 |
exParams.setSchema(params.getSchema()); |
|
259 |
exParams.setJDBCDriverClassName(params.getJDBCDriverClassName()); |
|
260 |
exParams.setUseSSL(params.getUseSSL()); |
|
261 |
exParams.setOraDriverType(params.getOraDriverType()); |
|
262 |
|
|
263 |
return manager.openServerExplorer(OracleServerExplorer.NAME, exParams); |
|
264 |
} catch (DataException e) { |
|
265 |
throw new ReadException(this.getName(), e); |
|
266 |
} catch (ValidateDataParametersException e) { |
|
267 |
throw new ReadException(this.getName(), e); |
|
268 |
} |
|
269 |
} |
|
270 |
|
|
271 |
public boolean allowAutomaticValues() { |
|
272 |
return true; |
|
273 |
} |
|
274 |
|
|
275 |
public boolean hasGeometrySupport() { |
|
276 |
return true; |
|
277 |
} |
|
278 |
|
|
279 |
protected OracleHelper getOraHelper() { |
|
280 |
return (OracleHelper) getHelper(); |
|
281 |
} |
|
282 |
|
|
283 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) |
|
284 |
throws DataException { |
|
285 |
FeatureType type = getFeatureStore().getDefaultFeatureType(); |
|
286 |
FeatureAttributeDescriptor geomAttr = type.getAttributeDescriptor(type |
|
287 |
.getDefaultGeometryAttributeName()); |
|
288 |
if (geomAttr == null) { |
|
289 |
return false; |
|
290 |
} |
|
291 |
if (geometrySubtype != geomAttr.getGeometrySubType()) { |
|
292 |
return false; |
|
293 |
} |
|
294 |
switch (geomAttr.getGeometryType()) { |
|
295 |
case Geometry.TYPES.GEOMETRY: |
|
296 |
return true; |
|
297 |
|
|
298 |
case Geometry.TYPES.MULTISURFACE: |
|
299 |
return geometryType == Geometry.TYPES.MULTISURFACE; |
|
300 |
|
|
301 |
case Geometry.TYPES.MULTIPOINT: |
|
302 |
return geometryType == Geometry.TYPES.MULTIPOINT; |
|
303 |
|
|
304 |
case Geometry.TYPES.MULTICURVE: |
|
305 |
return geometryType == Geometry.TYPES.MULTICURVE; |
|
306 |
|
|
307 |
case Geometry.TYPES.MULTISOLID: |
|
308 |
return geometryType == Geometry.TYPES.MULTISOLID; |
|
309 |
|
|
310 |
case Geometry.TYPES.SURFACE: |
|
311 |
return geometryType == Geometry.TYPES.SURFACE; |
|
312 |
|
|
313 |
case Geometry.TYPES.POINT: |
|
314 |
return geometryType == Geometry.TYPES.POINT; |
|
315 |
|
|
316 |
case Geometry.TYPES.CURVE: |
|
317 |
return geometryType == Geometry.TYPES.CURVE; |
|
318 |
|
|
319 |
case Geometry.TYPES.SOLID: |
|
320 |
return geometryType == Geometry.TYPES.SOLID; |
|
321 |
|
|
322 |
default: |
|
323 |
return geometryType == geomAttr.getGeometryType(); |
|
324 |
} |
|
325 |
|
|
326 |
} |
|
327 |
|
|
328 |
protected void addToListFeatureValues(FeatureProvider featureProvider, |
|
329 |
FeatureAttributeDescriptor attrOfList, |
|
330 |
FeatureAttributeDescriptor attr, List values) throws DataException { |
|
331 |
|
|
332 |
|
|
333 |
|
|
334 |
// geometry |
|
335 |
if (attr.getDataType().getType() == DataTypes.GEOMETRY) { |
|
336 |
Geometry geom = (Geometry) featureProvider.get(attr.getIndex()); |
|
337 |
|
|
338 |
STRUCT stru = OracleUtils.buildSTRUCT(geom, 0, helper.getConnection(), null, false, false, false); |
|
339 |
values.add(stru); |
|
340 |
} else { |
|
341 |
super.addToListFeatureValues(featureProvider, attrOfList, attr, values); |
|
342 |
} |
|
343 |
} |
|
344 |
|
|
345 |
protected String getSqlStatementAddField(FeatureAttributeDescriptor attr, |
|
346 |
List additionalStatement) throws DataException { |
|
347 |
|
|
348 |
if (attr.getDataType().getType() == DataTypes.GEOMETRY) { |
|
349 |
OracleStoreParameters params = getOracleStoreParameters(); |
|
350 |
additionalStatement.add(((OracleHelper) helper) |
|
351 |
.getSqlGeometryFieldAdd(attr, params.getTable(), params |
|
352 |
.getSchema())); |
|
353 |
} |
|
354 |
|
|
355 |
return super.getSqlStatementAddField(attr, additionalStatement); |
|
356 |
|
|
357 |
} |
|
358 |
|
|
359 |
protected String getSqlStatementDropField(FeatureAttributeDescriptor attr, |
|
360 |
List additionalStatement) { |
|
361 |
String result = super.getSqlStatementDropField(attr, |
|
362 |
additionalStatement); |
|
363 |
if (attr.getDataType().getType() == DataTypes.GEOMETRY) { |
|
364 |
additionalStatement.add(getSqlGeometryFieldDrop(attr)); |
|
365 |
} |
|
366 |
return result; |
|
367 |
} |
|
368 |
|
|
369 |
protected List<String> getSqlStatementAlterField( |
|
370 |
FeatureAttributeDescriptor attrOrg, |
|
371 |
FeatureAttributeDescriptor attrTrg, List additionalStatement) |
|
372 |
throws DataException { |
|
373 |
|
|
374 |
List<String> actions = new ArrayList<String>(); |
|
375 |
StringBuilder strb; |
|
376 |
OracleStoreParameters params = getOracleStoreParameters(); |
|
377 |
|
|
378 |
// diferent column type |
|
379 |
if (attrOrg.getDataType() != attrTrg.getDataType()) { |
|
380 |
strb = new StringBuilder(); |
|
381 |
strb.append("MODIFY ("); |
|
382 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
|
383 |
strb.append(" "); |
|
384 |
strb.append(helper.getSqlColumnTypeDescription(attrTrg)); |
|
385 |
strb.append(")"); |
|
386 |
|
|
387 |
if (attrOrg.getDataType().getType() == DataTypes.GEOMETRY) { |
|
388 |
additionalStatement.add(getSqlGeometryFieldDrop(attrOrg)); |
|
389 |
} |
|
390 |
if (attrTrg.getDataType().getType() == DataTypes.GEOMETRY) { |
|
391 |
additionalStatement.addAll(((OracleHelper) helper) |
|
392 |
.getSqlGeometryFieldAdd(attrTrg, params.getTable(), |
|
393 |
params.getSchema())); |
|
394 |
} |
|
395 |
|
|
396 |
actions.add(strb.toString()); |
|
397 |
} |
|
398 |
|
|
399 |
if (attrOrg.allowNull() != attrTrg.allowNull()) { |
|
400 |
|
|
401 |
strb = new StringBuilder(); |
|
402 |
strb.append("MODIFY ("); |
|
403 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
|
404 |
strb.append(" "); |
|
405 |
if (attrTrg.allowNull()) { |
|
406 |
strb.append("SET "); |
|
407 |
} else { |
|
408 |
strb.append("DROP "); |
|
409 |
} |
|
410 |
strb.append("NOT NULL)"); |
|
411 |
actions.add(strb.toString()); |
|
412 |
} |
|
413 |
|
|
414 |
if (attrOrg.getDefaultValue() != attrTrg.getDefaultValue()) { |
|
415 |
if (attrTrg.getDefaultValue() == null) { |
|
416 |
|
|
417 |
strb = new StringBuilder(); |
|
418 |
strb.append("MODIFY ("); |
|
419 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
|
420 |
strb.append(" DROP DEFAULT)"); |
|
421 |
actions.add(strb.toString()); |
|
422 |
} else if (!attrTrg.getDefaultValue().equals( |
|
423 |
attrOrg.getDefaultValue())) { |
|
424 |
// ALTER [ COLUMN ] column DROP DEFAULT |
|
425 |
|
|
426 |
strb = new StringBuilder(); |
|
427 |
strb.append("MODIFY ("); |
|
428 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
|
429 |
strb.append(" SET DEFAULT "); |
|
430 |
strb.append(helper.dalValueToJDBC(attrTrg, attrTrg |
|
431 |
.getDefaultValue())); |
|
432 |
strb.append(")"); |
|
433 |
actions.add(strb.toString()); |
|
434 |
} |
|
435 |
} |
|
436 |
|
|
437 |
if (attrOrg.getDataType() == attrTrg.getDataType() |
|
438 |
&& attrTrg.getDataType().getType() == DataTypes.GEOMETRY) { |
|
439 |
// TODO Checks SRS and GeomType/Subtype |
|
440 |
} |
|
441 |
|
|
442 |
return actions; |
|
443 |
} |
|
444 |
|
|
445 |
private Object getSqlGeometryFieldDrop(FeatureAttributeDescriptor attr) { |
|
446 |
StringBuilder strb = new StringBuilder(); |
|
447 |
OracleStoreParameters params = getOracleStoreParameters(); |
|
448 |
|
|
449 |
strb.append("DELETE FROM "); |
|
450 |
strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW); |
|
451 |
strb.append(" WHERE "); |
|
452 |
strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW_TABLE_NAME); |
|
453 |
strb.append(" = '"); |
|
454 |
strb.append(params.getTable()); |
|
455 |
strb.append("' AND "); |
|
456 |
strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW_COLUMN_NAME); |
|
457 |
strb.append(" = '"); |
|
458 |
strb.append(attr.getName()); |
|
459 |
strb.append("'"); |
|
460 |
|
|
461 |
return strb.toString(); |
|
462 |
} |
|
463 |
|
|
464 |
|
|
465 |
protected void loadFeatureProviderValue(FeatureProvider data, ResultSet rs, |
|
466 |
FeatureAttributeDescriptor attr) throws DataException { |
|
467 |
if (attr.getDataType().getType() == DataTypes.GEOMETRY) { |
|
468 |
|
|
469 |
try { |
|
470 |
Object geo_str_obj = rs.getObject(attr.getIndex() + 1); |
|
471 |
if (geo_str_obj == null) { |
|
472 |
data.set(attr.getIndex(), OracleUtils.createNullGeometry(SUBTYPES.GEOM2D)); |
|
473 |
} else { |
|
474 |
STRUCT geo_str = (STRUCT) geo_str_obj; |
|
475 |
|
|
476 |
// IProjection proj = attr.getSRS(); |
|
477 |
// OracleUtils. |
|
478 |
|
|
479 |
Geometry geom = OracleUtils.getGeometry( |
|
480 |
geo_str, |
|
481 |
false, |
|
482 |
false, |
|
483 |
"", |
|
484 |
helper.getConnection()); |
|
485 |
data.set(attr.getIndex(), geom); |
|
486 |
} |
|
487 |
} catch (SQLException e) { |
|
488 |
throw new JDBCSQLException(e); |
|
489 |
} catch (BaseException e) { |
|
490 |
throw new ReadException(getName(), e); |
|
491 |
} |
|
492 |
|
|
493 |
} else { |
|
494 |
|
|
495 |
try { |
|
496 |
Object obj_data = rs.getObject(attr.getName()); |
|
497 |
|
|
498 |
if (obj_data != null) { |
|
499 |
obj_data = coerce(attr, obj_data); |
|
500 |
} |
|
501 |
|
|
502 |
data.set(attr.getName(), obj_data); |
|
503 |
|
|
504 |
if (attr.isPrimaryKey()) { |
|
505 |
|
|
506 |
try { |
|
507 |
obj_data = getDataTypesManager().coerce(DataTypes.LONG, obj_data); |
|
508 |
data.setOID(obj_data); |
|
509 |
} catch (Exception ex) { |
|
510 |
logger.warn("Unable to convert OID to Long. Auto-increm will be used."); |
|
511 |
} |
|
512 |
|
|
513 |
} |
|
514 |
} catch (Exception e) { |
|
515 |
throw new CreateFeatureException(e, NAME); |
|
516 |
} |
|
517 |
} |
|
518 |
} |
|
519 |
|
|
520 |
|
|
521 |
|
|
522 |
/** |
|
523 |
* Load data form a resulset.<br> |
|
524 |
* |
|
525 |
* <strong>Note:</strong><br> |
|
526 |
* this method have to perform <code>resouceBegin</code> at the begining and |
|
527 |
* <code>resourceEnd</code> at the end of execution. |
|
528 |
* |
|
529 |
* |
|
530 |
* @param data |
|
531 |
* @param resulsetID |
|
532 |
* |
|
533 |
* @return |
|
534 |
* @throws DataException |
|
535 |
*/ |
|
536 |
public void loadFeatureProvider(final FeatureProvider data, final int resultsetID) |
|
537 |
throws DataException { |
|
538 |
getResource().execute(new ResourceAction() { |
|
539 |
public Object run() throws Exception { |
|
540 |
ResultSet rs = getResultSet(resultsetID); |
|
541 |
|
|
542 |
FeatureAttributeDescriptor attr; |
|
543 |
FeatureAttributeDescriptor[] atts = data.getType().getAttributeDescriptors(); |
|
544 |
int len = atts.length; |
|
545 |
for (int i=0; i<len; i++) { |
|
546 |
attr = atts[i]; |
|
547 |
loadFeatureProviderValue(data, rs, attr); |
|
548 |
} |
|
549 |
|
|
550 |
// this should not happen, loadFeatureProviderValue checks PK |
|
551 |
if (data.getOID() == null) { |
|
552 |
data.setOID(createNewOID()); |
|
553 |
} |
|
554 |
return null; |
|
555 |
} |
|
556 |
}); |
|
557 |
} |
|
558 |
|
|
559 |
|
|
560 |
|
|
561 |
protected ResultSet createNewResultSet(final String sql, |
|
562 |
final Object[] values, final int fetchSize) |
|
563 |
throws DataException { |
|
564 |
this.open(); |
|
565 |
return (ResultSet) getResource().execute(new ResourceAction() { |
|
566 |
public Object run() throws Exception { |
|
567 |
Connection conn = null; |
|
568 |
PreparedStatement st = null; |
|
569 |
ResultSet rs = null; |
|
570 |
try { |
|
571 |
|
|
572 |
conn = helper.getConnection(); |
|
573 |
conn.setAutoCommit(false); |
|
574 |
st = conn.prepareStatement(sql); |
|
575 |
|
|
576 |
if (values != null) { |
|
577 |
Object value; |
|
578 |
for (int i = 0; i < values.length; i++) { |
|
579 |
value = values[i]; |
|
580 |
if (value instanceof Geometry) { |
|
581 |
byte[] bytes; |
|
582 |
try { |
|
583 |
bytes = |
|
584 |
(byte[]) ((Geometry) value).invokeOperation( |
|
585 |
ToWKB.CODE, null); |
|
586 |
} catch (BaseException e) { |
|
587 |
throw new InvalidParameterException(); |
|
588 |
} |
|
589 |
st.setBytes(i + 1, bytes); |
|
590 |
} |
|
591 |
st.setObject(i + 1, value); |
|
592 |
} |
|
593 |
} |
|
594 |
|
|
595 |
if (fetchSize > 0) { |
|
596 |
st.setFetchSize(fetchSize); |
|
597 |
} |
|
598 |
rs = st.executeQuery(); |
|
599 |
if (fetchSize > 0) { |
|
600 |
rs.setFetchSize(fetchSize); |
|
601 |
} |
|
602 |
return rs; |
|
603 |
} catch (SQLException e) { |
|
604 |
try { |
|
605 |
rs.close(); |
|
606 |
} catch (Exception e1) { |
|
607 |
} |
|
608 |
try { |
|
609 |
st.close(); |
|
610 |
} catch (Exception e1) { |
|
611 |
} |
|
612 |
/* |
|
613 |
try { |
|
614 |
conn.close(); |
|
615 |
} catch (Exception e1) { |
|
616 |
} |
|
617 |
*/ |
|
618 |
throw new JDBCExecutePreparedSQLException(sql,values,e); |
|
619 |
} |
|
620 |
} |
|
621 |
}); |
|
622 |
} |
|
623 |
|
|
624 |
|
|
625 |
|
|
626 |
|
|
627 |
|
|
628 |
protected void closeResulset(final ResultSet rs) throws JDBCException, |
|
629 |
ResourceExecuteException { |
|
630 |
getResource().execute(new ResourceAction() { |
|
631 |
public Object run() throws Exception { |
|
632 |
Statement st = rs.getStatement(); |
|
633 |
Connection con = st.getConnection(); |
|
634 |
try { |
|
635 |
rs.close(); |
|
636 |
} finally { |
|
637 |
// TODO revisar esto |
|
638 |
try{ st.close(); } catch (Exception ex){ }; |
|
639 |
// try{ con.close(); } catch (Exception ex){ }; |
|
640 |
} |
|
641 |
return null; |
|
642 |
} |
|
643 |
}); |
|
644 |
} |
|
645 |
|
|
646 |
|
|
647 |
|
|
648 |
public String compoundSelect(FeatureType type, String filter, String order, |
|
649 |
long limit, long offset) throws DataException { |
|
650 |
StringBuilder sql = new StringBuilder(); |
|
651 |
JDBCStoreParameters params = getJDBCParameters(); |
|
652 |
if (directSQLMode) { |
|
653 |
sql.append(params.getSQL()); |
|
654 |
sql.append(' '); |
|
655 |
} else { |
|
656 |
FeatureAttributeDescriptor[] fields = type |
|
657 |
.getAttributeDescriptors(); |
|
658 |
|
|
659 |
// Select |
|
660 |
sql.append("Select "); |
|
661 |
for (int i = 0; i < fields.length - 1; i++) { |
|
662 |
sql.append(helper.getSqlFieldName(fields[i])); |
|
663 |
sql.append(", "); |
|
664 |
} |
|
665 |
sql.append(helper.getSqlFieldName(fields[fields.length - 1])); |
|
666 |
sql.append(' '); |
|
667 |
|
|
668 |
FeatureAttributeDescriptor[] pkFields = getStoreServices() |
|
669 |
.getProviderFeatureType(type.getId()).getPrimaryKey(); |
|
670 |
|
|
671 |
if (pkFields != null && pkFields.length > 0) { |
|
672 |
// checks for pk fields are in select |
|
673 |
boolean toAdd; |
|
674 |
for (int i = 0; i < pkFields.length; i++) { |
|
675 |
toAdd = true; |
|
676 |
for (int j = 0; j < fields.length; j++) { |
|
677 |
if (pkFields[i].getName().equals(fields[j].getName())) { |
|
678 |
toAdd = false; |
|
679 |
break; |
|
680 |
} |
|
681 |
if (toAdd) { |
|
682 |
sql.append(", "); |
|
683 |
sql.append(helper.getSqlFieldName(pkFields[i])); |
|
684 |
} |
|
685 |
} |
|
686 |
} |
|
687 |
sql.append(' '); |
|
688 |
} |
|
689 |
|
|
690 |
// table |
|
691 |
sql.append("from "); |
|
692 |
sql.append(params.tableID()); |
|
693 |
sql.append(' '); |
|
694 |
|
|
695 |
// Where |
|
696 |
appendWhere(sql, filter); |
|
697 |
|
|
698 |
// Order |
|
699 |
/* |
|
700 |
if ((params.getBaseOrder() != null && params.getBaseOrder() |
|
701 |
.length() != 0) |
|
702 |
|| (order != null && order.length() != 0)) { |
|
703 |
sql.append("order by "); |
|
704 |
|
|
705 |
if (order != null && order.length() != 0) { |
|
706 |
// order |
|
707 |
sql.append(order); |
|
708 |
} else { |
|
709 |
// initial order |
|
710 |
sql.append(params.getBaseOrder()); |
|
711 |
} |
|
712 |
sql.append(' '); |
|
713 |
} |
|
714 |
*/ |
|
715 |
} |
|
716 |
// limit offset |
|
717 |
/* |
|
718 |
if (limit > 0 || offset > 0) { |
|
719 |
sql.append(helper.compoundLimitAndOffset(limit,offset)); |
|
720 |
}^ |
|
721 |
*/ |
|
722 |
return sql.toString(); |
|
723 |
} |
|
724 |
|
|
725 |
private Object coerce(FeatureAttributeDescriptor att, Object v) throws CoercionException { |
|
726 |
|
|
727 |
if (!att.getObjectClass().isInstance(v)) { |
|
728 |
|
|
729 |
if (v instanceof TIMESTAMP && att.getDataType().getCoercion() instanceof CoerceToDateTime) { |
|
730 |
|
|
731 |
TIMESTAMP ora_ts = (TIMESTAMP) v; |
|
732 |
try { |
|
733 |
return ora_ts.timestampValue(); |
|
734 |
} catch (Exception ex) { |
|
735 |
throw new CoercionException(ex); |
|
736 |
} |
|
737 |
|
|
738 |
} else { |
|
739 |
return getDataTypesManager().coerce(att.getType(), v); |
|
740 |
} |
|
741 |
|
|
742 |
} else { |
|
743 |
return v; |
|
744 |
} |
|
745 |
} |
|
746 |
|
|
747 |
private static DataTypesManager dtManager = null; |
|
748 |
|
|
749 |
|
|
750 |
protected DataTypesManager getDataTypesManager() { |
|
751 |
if( dtManager==null ) { |
|
752 |
dtManager = ToolsLocator.getDataTypesManager(); |
|
753 |
} |
|
754 |
return dtManager; |
|
755 |
} |
|
756 |
|
|
757 |
|
|
758 |
|
|
759 |
|
|
760 |
@Override |
|
761 |
protected FeatureProvider internalGetFeatureProviderByReference( |
|
762 |
FeatureReferenceProviderServices reference, |
|
763 |
FeatureType featureType) |
|
764 |
throws DataException { |
|
765 |
StringBuilder filter = new StringBuilder(); |
|
766 |
FeatureAttributeDescriptor[] pk = |
|
767 |
getFeatureStore().getFeatureType(featureType.getId()) |
|
768 |
.getPrimaryKey(); |
|
769 |
|
|
770 |
List<Object> values = new ArrayList<Object>(); |
|
771 |
|
|
772 |
int i; |
|
773 |
Object id_obj = null; |
|
774 |
|
|
775 |
// assumes PK with one component (Long) !! |
|
776 |
id_obj = OracleUtils.getId(reference, featureType); |
|
777 |
|
|
778 |
for (i = 0; i < pk.length - 1; i++) { |
|
779 |
|
|
780 |
|
|
781 |
|
|
782 |
values.add( |
|
783 |
helper.dalValueToJDBC(pk[i], |
|
784 |
id_obj)); |
|
785 |
filter.append(helper.getSqlFieldName(pk[i])); |
|
786 |
filter.append(" = ? AND "); |
|
787 |
} |
|
788 |
|
|
789 |
values.add(helper.dalValueToJDBC(pk[i], |
|
790 |
id_obj)); |
|
791 |
filter.append(helper.getSqlFieldName(pk[i])); |
|
792 |
filter.append(" = ? "); |
|
793 |
|
|
794 |
String sql = compoundSelect(featureType, filter.toString(), null, 1, 0); |
|
795 |
|
|
796 |
FeatureProvider data; |
|
797 |
int rsId = createResultSet(sql, values.toArray(), 1); |
|
798 |
try { |
|
799 |
if (!resulsetNext(rsId)) { |
|
800 |
throw new RuntimeException("Reference Not found"); |
|
801 |
} |
|
802 |
data = createFeatureProvider(featureType); |
|
803 |
loadFeatureProvider(data, rsId); |
|
804 |
} finally { |
|
805 |
closeResulset(rsId); |
|
806 |
} |
|
807 |
|
|
808 |
return data; |
|
809 |
} |
|
810 |
|
|
811 |
|
|
812 |
|
|
813 |
|
|
814 |
} |
|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2009 Prodevelop S.L. main development |
|
26 |
*/ |
|
27 |
|
|
28 |
package org.gvsig.fmap.dal.store.oracle; |
|
29 |
|
|
30 |
import java.security.InvalidParameterException; |
|
31 |
import java.sql.Connection; |
|
32 |
import java.sql.PreparedStatement; |
|
33 |
import java.sql.ResultSet; |
|
34 |
import java.sql.SQLException; |
|
35 |
import java.sql.Statement; |
|
36 |
import java.util.ArrayList; |
|
37 |
import java.util.HashMap; |
|
38 |
import java.util.Iterator; |
|
39 |
import java.util.List; |
|
40 |
import java.util.Map; |
|
41 |
|
|
42 |
import oracle.sql.STRUCT; |
|
43 |
import oracle.sql.TIMESTAMP; |
|
44 |
|
|
45 |
import org.slf4j.Logger; |
|
46 |
import org.slf4j.LoggerFactory; |
|
47 |
|
|
48 |
import org.gvsig.fmap.dal.DALLocator; |
|
49 |
import org.gvsig.fmap.dal.DataManager; |
|
50 |
import org.gvsig.fmap.dal.DataServerExplorer; |
|
51 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
52 |
import org.gvsig.fmap.dal.DataTypes; |
|
53 |
import org.gvsig.fmap.dal.exception.DataException; |
|
54 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
55 |
import org.gvsig.fmap.dal.exception.OpenException; |
|
56 |
import org.gvsig.fmap.dal.exception.ReadException; |
|
57 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
58 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
59 |
import org.gvsig.fmap.dal.feature.FeatureIndexes; |
|
60 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
61 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
62 |
import org.gvsig.fmap.dal.feature.exception.CreateFeatureException; |
|
63 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureIndexes; |
|
64 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore; |
|
65 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
|
66 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
|
67 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
68 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
|
69 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
|
70 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
|
71 |
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException; |
|
72 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
|
73 |
import org.gvsig.fmap.dal.store.db.DBHelper; |
|
74 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
|
75 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
76 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProviderWriter; |
|
77 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCException; |
|
78 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecutePreparedSQLException; |
|
79 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
|
80 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
|
81 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCUpdateWithoutChangesException; |
|
82 |
import org.gvsig.fmap.dal.store.oracle.index.OracleFeatureIndex; |
|
83 |
import org.gvsig.fmap.dal.store.oracle.index.OracleRemoteFeatureIndexProvider; |
|
84 |
import org.gvsig.fmap.geom.Geometry; |
|
85 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
86 |
import org.gvsig.fmap.geom.operation.towkb.ToWKB; |
|
87 |
import org.gvsig.tools.ToolsLocator; |
|
88 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
89 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
|
90 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDateTime; |
|
91 |
import org.gvsig.tools.dynobject.DynObject; |
|
92 |
import org.gvsig.tools.exception.BaseException; |
|
93 |
|
|
94 |
/** |
|
95 |
* Oracle Provider |
|
96 |
* |
|
97 |
* @author vsanjaime |
|
98 |
* |
|
99 |
*/ |
|
100 |
public class OracleStoreProvider extends JDBCStoreProviderWriter { |
|
101 |
|
|
102 |
public final static Logger logger = LoggerFactory |
|
103 |
.getLogger(OracleStoreProvider.class); |
|
104 |
|
|
105 |
public static String NAME = "Oracle"; |
|
106 |
public static String DESCRIPTION = "Oracle source"; |
|
107 |
public static final String METADATA_DEFINITION_NAME = NAME; |
|
108 |
|
|
109 |
private boolean oracleIndexSet = false; |
|
110 |
|
|
111 |
private Map selectedFields = null; |
|
112 |
|
|
113 |
/* |
|
114 |
protected static void registerDynClass() { |
|
115 |
DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
|
116 |
DynClass dynClass; |
|
117 |
if (DYNCLASS == null) { |
|
118 |
dynClass = dynman.add(DYNCLASS_NAME, DESCRIPTION); |
|
119 |
|
|
120 |
// dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME)); |
|
121 |
DYNCLASS = dynClass; |
|
122 |
} |
|
123 |
} |
|
124 |
*/ |
|
125 |
|
|
126 |
public OracleStoreProvider(OracleStoreParameters params, |
|
127 |
DataStoreProviderServices storeServices) throws InitializeException { |
|
128 |
super(params, storeServices, |
|
129 |
DBHelper.newMetadataContainer(METADATA_DEFINITION_NAME)); |
|
130 |
|
|
131 |
String defaultGeometryAttributeName; |
|
132 |
try { |
|
133 |
defaultGeometryAttributeName = this.getFeatureStore() |
|
134 |
.getDefaultFeatureType() |
|
135 |
.getDefaultGeometryAttributeName(); |
|
136 |
} catch (DataException e) { |
|
137 |
throw new InitializeException(e); |
|
138 |
} |
|
139 |
|
|
140 |
String ora_srid = |
|
141 |
getOraHelper().getOraTableSrid(params, defaultGeometryAttributeName); |
|
142 |
this.setDynValue(OracleStoreParameters.ORA_TABLE_SRID_KEY, ora_srid); |
|
143 |
String sFields = (String)getParameters().getDynValue("fields"); |
|
144 |
if (sFields != null){ |
|
145 |
selectedFields = new HashMap(); |
|
146 |
String[] fields = sFields.split(","); |
|
147 |
for (int i=0 ; i<fields.length ; i++){ |
|
148 |
selectedFields.put(fields[i], Boolean.TRUE); |
|
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
|
|
153 |
/** |
|
154 |
* Default Constructor. |
|
155 |
* |
|
156 |
* @param params |
|
157 |
* @param storeServices |
|
158 |
* @param metadata |
|
159 |
*/ |
|
160 |
protected OracleStoreProvider(DataStoreParameters params, |
|
161 |
DataStoreProviderServices storeServices, DynObject metadata) throws InitializeException { |
|
162 |
super((JDBCStoreParameters) params, storeServices, metadata); |
|
163 |
} |
|
164 |
|
|
165 |
/** |
|
166 |
* Constructor when cannot create metada in constrution time. <br> |
|
167 |
* <br> |
|
168 |
* <strong>Note: </strong> Don't use it if not is necesary. Set metada |
|
169 |
* <strong>as soon as posible</strong> by |
|
170 |
* {@link AbstractFeatureStoreProvider#setMetadata(DynObject)} |
|
171 |
* |
|
172 |
* @param params |
|
173 |
* @param storeServices |
|
174 |
*/ |
|
175 |
protected OracleStoreProvider(DataStoreParameters params, |
|
176 |
DataStoreProviderServices storeServices) throws InitializeException { |
|
177 |
this((JDBCStoreParameters) params, storeServices, null); |
|
178 |
} |
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
private OracleStoreParameters getOracleStoreParameters() { |
|
184 |
return (OracleStoreParameters) this.getParameters(); |
|
185 |
} |
|
186 |
|
|
187 |
protected JDBCHelper createHelper() throws InitializeException { |
|
188 |
return new OracleHelper(this, getOracleStoreParameters()); |
|
189 |
} |
|
190 |
|
|
191 |
public void open() throws OpenException { |
|
192 |
super.open(); |
|
193 |
|
|
194 |
// =============== add remote oracle geometry index to prevent default index |
|
195 |
if (!oracleIndexSet) { |
|
196 |
|
|
197 |
DefaultFeatureStore dfs = null; |
|
198 |
if (!(getFeatureStore() instanceof DefaultFeatureStore)) { |
|
199 |
logger.warn("Feature store not DefaultFeatureStore: " + dfs.getClass().getName()); |
|
200 |
return; |
|
201 |
} |
|
202 |
dfs = (DefaultFeatureStore) getFeatureStore(); |
|
203 |
|
|
204 |
FeatureType ft = null; |
|
205 |
try { |
|
206 |
ft = dfs.getDefaultFeatureType(); |
|
207 |
} catch (DataException e) { |
|
208 |
throw new OpenException(getFeatureStore().getName(), e); |
|
209 |
} |
|
210 |
|
|
211 |
//It there is not a geometry the index is not created |
|
212 |
if (ft.getDefaultGeometryAttributeName() != null){ |
|
213 |
FeatureIndexes findexes = dfs.getIndexes(); |
|
214 |
if (findexes instanceof DefaultFeatureIndexes) { |
|
215 |
DefaultFeatureIndexes dfi = (DefaultFeatureIndexes) findexes; |
|
216 |
dfi.addIndex( |
|
217 |
new OracleFeatureIndex( |
|
218 |
(FeatureStoreProviderServices) getFeatureStore(), |
|
219 |
ft, |
|
220 |
new OracleRemoteFeatureIndexProvider(dfs), |
|
221 |
ft.getDefaultGeometryAttributeName(), |
|
222 |
OracleRemoteFeatureIndexProvider.NAME)); |
|
223 |
} |
|
224 |
} |
|
225 |
oracleIndexSet = true; |
|
226 |
} |
|
227 |
// =============== |
|
228 |
} |
|
229 |
|
|
230 |
|
|
231 |
protected String fixFilter(String filter) { |
|
232 |
// filter was created by a class in oracle plugin, should not need fix |
|
233 |
return filter; |
|
234 |
} |
|
235 |
|
|
236 |
public String getName() { |
|
237 |
return NAME; |
|
238 |
} |
|
239 |
|
|
240 |
public int getOIDType() { |
|
241 |
return DataTypes.LONG; |
|
242 |
} |
|
243 |
|
|
244 |
private static long ora_oid = 100000000; |
|
245 |
|
|
246 |
public Object createNewOID() { |
|
247 |
return new Long(ora_oid++); |
|
248 |
} |
|
249 |
|
|
250 |
public FeatureSetProvider createSet(FeatureQuery query, |
|
251 |
FeatureType featureType) throws DataException { |
|
252 |
|
|
253 |
return new OracleSetProvider(this, query, featureType); |
|
254 |
} |
|
255 |
|
|
256 |
public DataServerExplorer getExplorer() throws ReadException { |
|
257 |
DataManager manager = DALLocator.getDataManager(); |
|
258 |
OracleServerExplorerParameters exParams; |
|
259 |
OracleStoreParameters params = getOracleStoreParameters(); |
|
260 |
try { |
|
261 |
exParams = (OracleServerExplorerParameters) manager |
|
262 |
.createServerExplorerParameters(OracleServerExplorer.NAME); |
|
263 |
exParams.setUrl(params.getUrl()); |
|
264 |
exParams.setHost(params.getHost()); |
|
265 |
exParams.setPort(params.getPort()); |
|
266 |
exParams.setDBName(params.getDBName()); |
|
267 |
exParams.setUser(params.getUser()); |
|
268 |
exParams.setPassword(params.getPassword()); |
|
269 |
exParams.setCatalog(params.getCatalog()); |
|
270 |
exParams.setSchema(params.getSchema()); |
|
271 |
exParams.setJDBCDriverClassName(params.getJDBCDriverClassName()); |
|
272 |
exParams.setUseSSL(params.getUseSSL()); |
|
273 |
exParams.setOraDriverType(params.getOraDriverType()); |
|
274 |
|
|
275 |
return manager.openServerExplorer(OracleServerExplorer.NAME, exParams); |
|
276 |
} catch (DataException e) { |
|
277 |
throw new ReadException(this.getName(), e); |
|
278 |
} catch (ValidateDataParametersException e) { |
|
279 |
throw new ReadException(this.getName(), e); |
|
280 |
} |
|
281 |
} |
|
282 |
|
|
283 |
public boolean allowAutomaticValues() { |
|
284 |
return true; |
|
285 |
} |
|
286 |
|
|
287 |
public boolean hasGeometrySupport() { |
|
288 |
return true; |
|
289 |
} |
|
290 |
|
|
291 |
protected OracleHelper getOraHelper() { |
|
292 |
return (OracleHelper) getHelper(); |
|
293 |
} |
|
294 |
|
|
295 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) |
|
296 |
throws DataException { |
|
297 |
FeatureType type = getFeatureStore().getDefaultFeatureType(); |
|
298 |
FeatureAttributeDescriptor geomAttr = type.getAttributeDescriptor(type |
|
299 |
.getDefaultGeometryAttributeName()); |
|
300 |
if (geomAttr == null) { |
|
301 |
return false; |
|
302 |
} |
|
303 |
if (geometrySubtype != geomAttr.getGeometrySubType()) { |
|
304 |
return false; |
|
305 |
} |
|
306 |
switch (geomAttr.getGeometryType()) { |
|
307 |
case Geometry.TYPES.GEOMETRY: |
|
308 |
return true; |
|
309 |
|
|
310 |
case Geometry.TYPES.MULTISURFACE: |
|
311 |
return geometryType == Geometry.TYPES.MULTISURFACE; |
|
312 |
|
|
313 |
case Geometry.TYPES.MULTIPOINT: |
|
314 |
return geometryType == Geometry.TYPES.MULTIPOINT; |
|
315 |
|
|
316 |
case Geometry.TYPES.MULTICURVE: |
|
317 |
return geometryType == Geometry.TYPES.MULTICURVE; |
|
318 |
|
|
319 |
case Geometry.TYPES.MULTISOLID: |
|
320 |
return geometryType == Geometry.TYPES.MULTISOLID; |
|
321 |
|
|
322 |
case Geometry.TYPES.SURFACE: |
|
323 |
return geometryType == Geometry.TYPES.SURFACE; |
|
324 |
|
|
325 |
case Geometry.TYPES.POINT: |
|
326 |
return geometryType == Geometry.TYPES.POINT; |
|
327 |
|
|
328 |
case Geometry.TYPES.CURVE: |
|
329 |
return geometryType == Geometry.TYPES.CURVE; |
|
330 |
|
|
331 |
case Geometry.TYPES.SOLID: |
|
332 |
return geometryType == Geometry.TYPES.SOLID; |
|
333 |
|
|
334 |
default: |
|
335 |
return geometryType == geomAttr.getGeometryType(); |
|
336 |
} |
|
337 |
|
|
338 |
} |
|
339 |
|
|
340 |
protected void addToListFeatureValues(FeatureProvider featureProvider, |
|
341 |
FeatureAttributeDescriptor attrOfList, |
|
342 |
FeatureAttributeDescriptor attr, List values) throws DataException { |
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
// geometry |
|
347 |
if (attr.getDataType().getType() == DataTypes.GEOMETRY) { |
|
348 |
Geometry geom = (Geometry) featureProvider.get(attr.getIndex()); |
|
349 |
|
|
350 |
String ora_srid_str = null; |
|
351 |
boolean is_geo = false; |
|
352 |
boolean has_srid = false; |
|
353 |
|
|
354 |
Object ora_srid = getDynValue("ora_table_srid"); |
|
355 |
if (ora_srid != null) { |
|
356 |
if (ora_srid instanceof String) { |
|
357 |
ora_srid_str = (String) ora_srid; |
|
358 |
} else { |
|
359 |
if (ora_srid instanceof Integer) { |
|
360 |
ora_srid_str = "" + ((Integer) ora_srid).intValue(); |
|
361 |
} |
|
362 |
} |
|
363 |
} |
|
364 |
|
|
365 |
has_srid = ora_srid_str != null && ora_srid_str.compareToIgnoreCase("0") != 0; |
|
366 |
is_geo = OracleUtils.getIsGCS( |
|
367 |
ora_srid_str, has_srid); |
|
368 |
|
|
369 |
// OracleUtils.is |
|
370 |
|
|
371 |
STRUCT stru = OracleUtils.buildSTRUCT( |
|
372 |
geom, 0, helper.getConnection(), |
|
373 |
ora_srid_str, has_srid, false, is_geo); |
|
374 |
values.add(stru); |
|
375 |
} else { |
|
376 |
super.addToListFeatureValues(featureProvider, attrOfList, attr, values); |
|
377 |
} |
|
378 |
} |
|
379 |
|
|
380 |
protected String getSqlStatementAddField(FeatureAttributeDescriptor attr, |
|
381 |
List additionalStatement) throws DataException { |
|
382 |
|
|
383 |
if (attr.getDataType().getType() == DataTypes.GEOMETRY) { |
|
384 |
OracleStoreParameters params = getOracleStoreParameters(); |
|
385 |
additionalStatement.add(((OracleHelper) helper) |
|
386 |
.getSqlGeometryFieldAdd(attr, params.getTable(), params |
|
387 |
.getSchema())); |
|
388 |
} |
|
389 |
|
|
390 |
return super.getSqlStatementAddField(attr, additionalStatement); |
|
391 |
|
|
392 |
} |
|
393 |
|
|
394 |
protected String getSqlStatementDropField(FeatureAttributeDescriptor attr, |
|
395 |
List additionalStatement) { |
|
396 |
String result = super.getSqlStatementDropField(attr, |
|
397 |
additionalStatement); |
|
398 |
if (attr.getDataType().getType() == DataTypes.GEOMETRY) { |
|
399 |
additionalStatement.add(getSqlGeometryFieldDrop(attr)); |
|
400 |
} |
|
401 |
return result; |
|
402 |
} |
|
403 |
|
|
404 |
protected List<String> getSqlStatementAlterField( |
|
405 |
FeatureAttributeDescriptor attrOrg, |
|
406 |
FeatureAttributeDescriptor attrTrg, List additionalStatement) |
|
407 |
throws DataException { |
|
408 |
|
|
409 |
List<String> actions = new ArrayList<String>(); |
|
410 |
StringBuilder strb; |
|
411 |
OracleStoreParameters params = getOracleStoreParameters(); |
|
412 |
|
|
413 |
// diferent column type |
|
414 |
if (attrOrg.getDataType() != attrTrg.getDataType()) { |
|
415 |
strb = new StringBuilder(); |
|
416 |
strb.append("MODIFY ("); |
|
417 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
|
418 |
strb.append(" "); |
|
419 |
strb.append(helper.getSqlColumnTypeDescription(attrTrg)); |
|
420 |
strb.append(")"); |
|
421 |
|
|
422 |
if (attrOrg.getDataType().getType() == DataTypes.GEOMETRY) { |
|
423 |
additionalStatement.add(getSqlGeometryFieldDrop(attrOrg)); |
|
424 |
} |
|
425 |
if (attrTrg.getDataType().getType() == DataTypes.GEOMETRY) { |
|
426 |
additionalStatement.addAll(((OracleHelper) helper) |
|
427 |
.getSqlGeometryFieldAdd(attrTrg, params.getTable(), |
|
428 |
params.getSchema())); |
|
429 |
} |
|
430 |
|
|
431 |
actions.add(strb.toString()); |
|
432 |
} |
|
433 |
|
|
434 |
if (attrOrg.allowNull() != attrTrg.allowNull()) { |
|
435 |
|
|
436 |
strb = new StringBuilder(); |
|
437 |
strb.append("MODIFY ("); |
|
438 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
|
439 |
strb.append(" "); |
|
440 |
if (attrTrg.allowNull()) { |
|
441 |
strb.append("SET "); |
|
442 |
} else { |
|
443 |
strb.append("DROP "); |
|
444 |
} |
|
445 |
strb.append("NOT NULL)"); |
|
446 |
actions.add(strb.toString()); |
|
447 |
} |
|
448 |
|
|
449 |
if (attrOrg.getDefaultValue() != attrTrg.getDefaultValue()) { |
|
450 |
if (attrTrg.getDefaultValue() == null) { |
|
451 |
|
|
452 |
strb = new StringBuilder(); |
|
453 |
strb.append("MODIFY ("); |
|
454 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
|
455 |
strb.append(" DROP DEFAULT)"); |
|
456 |
actions.add(strb.toString()); |
|
457 |
} else if (!attrTrg.getDefaultValue().equals( |
|
458 |
attrOrg.getDefaultValue())) { |
|
459 |
// ALTER [ COLUMN ] column DROP DEFAULT |
|
460 |
|
|
461 |
strb = new StringBuilder(); |
|
462 |
strb.append("MODIFY ("); |
|
463 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
|
464 |
strb.append(" SET DEFAULT "); |
|
465 |
strb.append(helper.dalValueToJDBC(attrTrg, attrTrg |
|
466 |
.getDefaultValue())); |
|
467 |
strb.append(")"); |
|
468 |
actions.add(strb.toString()); |
|
469 |
} |
|
470 |
} |
|
471 |
|
|
472 |
if (attrOrg.getDataType() == attrTrg.getDataType() |
|
473 |
&& attrTrg.getDataType().getType() == DataTypes.GEOMETRY) { |
|
474 |
// TODO Checks SRS and GeomType/Subtype |
|
475 |
} |
|
476 |
|
|
477 |
return actions; |
|
478 |
} |
|
479 |
|
|
480 |
private Object getSqlGeometryFieldDrop(FeatureAttributeDescriptor attr) { |
|
481 |
StringBuilder strb = new StringBuilder(); |
|
482 |
OracleStoreParameters params = getOracleStoreParameters(); |
|
483 |
|
|
484 |
strb.append("DELETE FROM "); |
|
485 |
strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW); |
|
486 |
strb.append(" WHERE "); |
|
487 |
strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW_TABLE_NAME); |
|
488 |
strb.append(" = '"); |
|
489 |
strb.append(params.getTable()); |
|
490 |
strb.append("' AND "); |
|
491 |
strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW_COLUMN_NAME); |
|
492 |
strb.append(" = '"); |
|
493 |
strb.append(attr.getName()); |
|
494 |
strb.append("'"); |
|
495 |
|
|
496 |
return strb.toString(); |
|
497 |
} |
|
498 |
|
|
499 |
|
|
500 |
protected void loadFeatureProviderValue(FeatureProvider data, ResultSet rs, |
|
501 |
FeatureAttributeDescriptor attr) throws DataException { |
|
502 |
if (attr.getDataType().getType() == DataTypes.GEOMETRY) { |
|
503 |
|
|
504 |
try { |
|
505 |
Object geo_str_obj = rs.getObject(attr.getIndex() + 1); |
|
506 |
if (geo_str_obj == null) { |
|
507 |
data.set(attr.getIndex(), OracleUtils.createNullGeometry(SUBTYPES.GEOM2D)); |
|
508 |
} else { |
|
509 |
STRUCT geo_str = (STRUCT) geo_str_obj; |
|
510 |
|
|
511 |
// IProjection proj = attr.getSRS(); |
|
512 |
// OracleUtils. |
|
513 |
|
|
514 |
Geometry geom = OracleUtils.getGeometry( |
|
515 |
geo_str, |
|
516 |
false, |
|
517 |
false, |
|
518 |
"", |
|
519 |
helper.getConnection()); |
|
520 |
data.set(attr.getIndex(), geom); |
|
521 |
} |
|
522 |
} catch (SQLException e) { |
|
523 |
throw new JDBCSQLException(e); |
|
524 |
} catch (BaseException e) { |
|
525 |
throw new ReadException(getName(), e); |
|
526 |
} |
|
527 |
|
|
528 |
} else { |
|
529 |
|
|
530 |
try { |
|
531 |
Object obj_data = rs.getObject(attr.getName()); |
|
532 |
|
|
533 |
if (obj_data != null) { |
|
534 |
obj_data = coerce(attr, obj_data); |
|
535 |
} |
|
536 |
|
|
537 |
data.set(attr.getName(), obj_data); |
|
538 |
|
|
539 |
if (attr.isPrimaryKey()) { |
|
540 |
|
|
541 |
try { |
|
542 |
obj_data = getDataTypesManager().coerce(DataTypes.LONG, obj_data); |
|
543 |
data.setOID(obj_data); |
|
544 |
} catch (Exception ex) { |
|
545 |
logger.warn("Unable to convert OID to Long. Auto-increm will be used."); |
|
546 |
} |
|
547 |
|
|
548 |
} |
|
549 |
} catch (Exception e) { |
|
550 |
throw new CreateFeatureException(e, NAME); |
|
551 |
} |
|
552 |
} |
|
553 |
} |
|
554 |
|
|
555 |
|
|
556 |
|
|
557 |
/** |
|
558 |
* Load data form a resulset.<br> |
|
559 |
* |
|
560 |
* <strong>Note:</strong><br> |
|
561 |
* this method have to perform <code>resouceBegin</code> at the begining and |
|
562 |
* <code>resourceEnd</code> at the end of execution. |
|
563 |
* |
|
564 |
* |
|
565 |
* @param data |
|
566 |
* @param resulsetID |
|
567 |
* |
|
568 |
* @return |
|
569 |
* @throws DataException |
|
570 |
*/ |
|
571 |
public void loadFeatureProvider(final FeatureProvider data, final int resultsetID) |
|
572 |
throws DataException { |
|
573 |
getResource().execute(new ResourceAction() { |
|
574 |
public Object run() throws Exception { |
|
575 |
ResultSet rs = getResultSet(resultsetID); |
|
576 |
|
|
577 |
FeatureAttributeDescriptor attr; |
|
578 |
FeatureAttributeDescriptor[] atts = data.getType().getAttributeDescriptors(); |
|
579 |
int len = atts.length; |
|
580 |
if (selectedFields == null){ |
|
581 |
for (int i=0; i<len; i++) { |
|
582 |
attr = atts[i]; |
|
583 |
loadFeatureProviderValue(data, rs, attr); |
|
584 |
} |
|
585 |
}else{ |
|
586 |
for (int i=0; i<len; i++) { |
|
587 |
attr = atts[i]; |
|
588 |
if (selectedFields.containsKey(attr.getName())){ |
|
589 |
loadFeatureProviderValue(data, rs, attr); |
|
590 |
} |
|
591 |
} |
|
592 |
} |
|
593 |
|
|
594 |
// this should not happen, loadFeatureProviderValue checks PK |
|
595 |
if (data.getOID() == null) { |
|
596 |
data.setOID(createNewOID()); |
|
597 |
} |
|
598 |
return null; |
|
599 |
} |
|
600 |
}); |
|
601 |
} |
|
602 |
|
|
603 |
|
|
604 |
|
|
605 |
protected ResultSet createNewResultSet(final String sql, |
|
606 |
final Object[] values, final int fetchSize) |
|
607 |
throws DataException { |
|
608 |
this.open(); |
|
609 |
return (ResultSet) getResource().execute(new ResourceAction() { |
|
610 |
public Object run() throws Exception { |
|
611 |
Connection conn = null; |
|
612 |
PreparedStatement st = null; |
|
613 |
ResultSet rs = null; |
|
614 |
try { |
|
615 |
|
|
616 |
conn = helper.getConnection(); |
|
617 |
conn.setAutoCommit(false); |
|
618 |
st = conn.prepareStatement(sql); |
|
619 |
|
|
620 |
if (values != null) { |
|
621 |
Object value; |
|
622 |
for (int i = 0; i < values.length; i++) { |
|
623 |
value = values[i]; |
|
624 |
if (value instanceof Geometry) { |
|
625 |
byte[] bytes; |
|
626 |
try { |
|
627 |
bytes = |
|
628 |
(byte[]) ((Geometry) value).invokeOperation( |
|
629 |
ToWKB.CODE, null); |
|
630 |
} catch (BaseException e) { |
|
631 |
throw new InvalidParameterException(); |
|
632 |
} |
|
633 |
st.setBytes(i + 1, bytes); |
|
634 |
} |
|
635 |
st.setObject(i + 1, value); |
|
636 |
} |
|
637 |
} |
|
638 |
|
|
639 |
if (fetchSize > 0) { |
|
640 |
st.setFetchSize(fetchSize); |
|
641 |
} |
|
642 |
rs = st.executeQuery(); |
|
643 |
if (fetchSize > 0) { |
|
644 |
rs.setFetchSize(fetchSize); |
|
645 |
} |
|
646 |
return rs; |
|
647 |
} catch (SQLException e) { |
|
648 |
try { |
|
649 |
rs.close(); |
|
650 |
} catch (Exception e1) { |
|
651 |
} |
|
652 |
try { |
|
653 |
st.close(); |
|
654 |
} catch (Exception e1) { |
|
655 |
} |
|
656 |
/* |
|
657 |
try { |
|
658 |
conn.close(); |
|
659 |
} catch (Exception e1) { |
|
660 |
} |
|
661 |
*/ |
|
662 |
throw new JDBCExecutePreparedSQLException(sql,values,e); |
|
663 |
} |
|
664 |
} |
|
665 |
}); |
|
666 |
} |
|
667 |
|
|
668 |
|
|
669 |
|
|
670 |
|
|
671 |
|
|
672 |
protected void closeResulset(final ResultSet rs) throws JDBCException, |
|
673 |
ResourceExecuteException { |
|
674 |
getResource().execute(new ResourceAction() { |
|
675 |
public Object run() throws Exception { |
|
676 |
Statement st = rs.getStatement(); |
|
677 |
Connection con = st.getConnection(); |
|
678 |
try { |
|
679 |
rs.close(); |
|
680 |
} finally { |
|
681 |
// TODO revisar esto |
|
682 |
try{ st.close(); } catch (Exception ex){ }; |
Also available in: Unified diff