Revision 30231 branches/v2_0_0_prep/extensions/org.gvsig.oracle/src/org/gvsig/fmap/dal/store/oracle/OracleStoreProvider.java

View differences:

OracleStoreProvider.java
1 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
*/
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 22

  
23 23
/*
24 24
 * AUTHORS (In addition to CIT):
......
32 32
import java.util.regex.Matcher;
33 33
import java.util.regex.Pattern;
34 34

  
35
import oracle.sql.STRUCT;
36

  
35 37
import org.gvsig.fmap.dal.DALLocator;
36 38
import org.gvsig.fmap.dal.DataManager;
37 39
import org.gvsig.fmap.dal.DataServerExplorer;
......
56 58
import org.slf4j.Logger;
57 59
import org.slf4j.LoggerFactory;
58 60

  
61
/**
62
 * Oracle Provider
63
 * 
64
 * @author vsanjaime
65
 * 
66
 */
59 67
public class OracleStoreProvider extends JDBCStoreProviderWriter {
60 68

  
61 69
	public final static Logger logger = LoggerFactory
......
66 74
	private static final String DYNCLASS_NAME = "OracleStore";
67 75
	private static DynClass DYNCLASS = null;
68 76

  
69

  
70 77
	protected static void registerDynClass() {
71 78
		DynObjectManager dynman = ToolsLocator.getDynObjectManager();
72 79
		DynClass dynClass;
......
79 86
	}
80 87

  
81 88
	public OracleStoreProvider(OracleStoreParameters params,
82
			DataStoreProviderServices storeServices)
83
			throws InitializeException {
89
			DataStoreProviderServices storeServices) throws InitializeException {
84 90
		super(params, storeServices, ToolsLocator.getDynObjectManager()
85 91
				.createDynObject(DYNCLASS));
86 92
	}
87 93

  
88
	private OracleStoreParameters getOracleParameters() {
94
	private OracleStoreParameters getOracleStoreParameters() {
89 95
		return (OracleStoreParameters) this.getParameters();
90 96
	}
91 97

  
92 98
	protected JDBCHelper createHelper() throws InitializeException {
93
		return new OracleHelper(this, getOracleParameters());
99
		return new OracleHelper(this, getOracleStoreParameters());
94 100
	}
95 101

  
96 102
	protected String fixFilter(String filter) {
......
135 141
		return new OracleSetProvider(this, query, featureType);
136 142
	}
137 143

  
138

  
139 144
	public DataServerExplorer getExplorer() throws ReadException {
140 145
		DataManager manager = DALLocator.getDataManager();
141 146
		OracleServerExplorerParameters exParams;
142
		OracleStoreParameters params = getOracleParameters();
147
		OracleStoreParameters params = getOracleStoreParameters();
143 148
		try {
144 149
			exParams = (OracleServerExplorerParameters) manager
145 150
					.createServerExplorerParameters(OracleServerExplorer.NAME);
......
159 164
		} catch (DataException e) {
160 165
			throw new ReadException(this.getName(), e);
161 166
		} catch (ValidateDataParametersException e) {
162
			// TODO Auto-generated catch block
163 167
			throw new ReadException(this.getName(), e);
164 168
		}
165 169
	}
......
168 172
		return true;
169 173
	}
170 174

  
171

  
172 175
	public boolean hasGeometrySupport() {
173 176
		return true;
174 177
	}
175 178

  
176

  
177

  
178

  
179
	protected OracleHelper getOSHelper() {
179
	protected OracleHelper getOraHelper() {
180 180
		return (OracleHelper) getHelper();
181 181
	}
182
	
183 182

  
184

  
185 183
	public boolean canWriteGeometry(int geometryType, int geometrySubtype)
186 184
			throws DataException {
187 185
		FeatureType type = getFeatureStore().getDefaultFeatureType();
188 186
		FeatureAttributeDescriptor geomAttr = type.getAttributeDescriptor(type
189
								.getDefaultGeometryAttributeName());
187
				.getDefaultGeometryAttributeName());
190 188
		if (geomAttr == null) {
191 189
			return false;
192 190
		}
......
198 196
			return true;
199 197

  
200 198
		case Geometry.TYPES.MULTISURFACE:
201
			return geometryType == Geometry.TYPES.MULTISURFACE
202
					|| geometryType == Geometry.TYPES.SURFACE;
199
			return geometryType == Geometry.TYPES.MULTISURFACE;
203 200

  
204 201
		case Geometry.TYPES.MULTIPOINT:
205
			return geometryType == Geometry.TYPES.MULTIPOINT
206
					|| geometryType == Geometry.TYPES.POINT;
202
			return geometryType == Geometry.TYPES.MULTIPOINT;
207 203

  
208 204
		case Geometry.TYPES.MULTICURVE:
209
			return geometryType == Geometry.TYPES.MULTICURVE
210
					|| geometryType == Geometry.TYPES.CURVE;
205
			return geometryType == Geometry.TYPES.MULTICURVE;
211 206

  
212 207
		case Geometry.TYPES.MULTISOLID:
213
			return geometryType == Geometry.TYPES.MULTISOLID
214
					|| geometryType == Geometry.TYPES.SOLID;
208
			return geometryType == Geometry.TYPES.MULTISOLID;
215 209

  
210
		case Geometry.TYPES.SURFACE:
211
			return geometryType == Geometry.TYPES.SURFACE;
212

  
213
		case Geometry.TYPES.POINT:
214
			return geometryType == Geometry.TYPES.POINT;
215

  
216
		case Geometry.TYPES.CURVE:
217
			return geometryType == Geometry.TYPES.CURVE;
218

  
219
		case Geometry.TYPES.SOLID:
220
			return geometryType == Geometry.TYPES.SOLID;
221

  
216 222
		default:
217 223
			return geometryType == geomAttr.getGeometryType();
218 224
		}
219 225

  
220 226
	}
221 227

  
222

  
223 228
	protected void addToListFeatureValues(FeatureProvider featureProvider,
224 229
			FeatureAttributeDescriptor attrOfList,
225
			FeatureAttributeDescriptor attr,
226
			List values) throws DataException {
230
			FeatureAttributeDescriptor attr, List values) throws DataException {
227 231

  
228
		super.addToListFeatureValues(featureProvider, attrOfList, attr, values);
229 232
		if (attr.getDataType() == DataTypes.GEOMETRY) {
230
			values.add(helper.getProviderSRID(attr.getSRS()));
231
		}
232
	}
233

  
234
	protected void prepareAttributeForInsert(
235
			FeatureAttributeDescriptor attr, List fields, List values) {
236

  
237
		if (attr.getDataType() == DataTypes.GEOMETRY) {
238
			fields.add(helper.escapeFieldName(attr.getName()));
239
			values.add("GeomFromWKB(?,?)");
233
			Geometry geom = (Geometry) featureProvider.get(attr.getIndex());
234
			STRUCT stru = OracleUtils.GeometryToSTRUCT(geom, c, srid, agu_b,
235
					hasSrid);
236
			values.add(stru);
240 237
		} else {
241
			super.prepareAttributeForInsert(attr, fields, values);
238
			super.addToListFeatureValues(featureProvider, attrOfList, attr,
239
					values);
242 240
		}
243

  
244 241
	}
245 242

  
246
	protected void prepareAttributeForUpdate(FeatureAttributeDescriptor attr,
247
			List values) {
248
		if (attr.getDataType() == DataTypes.GEOMETRY) {
249
			values.add(helper.escapeFieldName(attr.getName())
250
					+ " = GeomFromWKB(?,?)");
251
		} else {
252
			super.prepareAttributeForUpdate(attr, values);
253
		}
254
	}
255

  
256 243
	protected String getSqlStatementAddField(FeatureAttributeDescriptor attr,
257 244
			List additionalStatement) throws DataException {
245

  
258 246
		if (attr.getDataType() == DataTypes.GEOMETRY) {
259
			OracleStoreParameters params = getOracleParameters();
247
			OracleStoreParameters params = getOracleStoreParameters();
260 248
			additionalStatement.add(((OracleHelper) helper)
261
					.getSqlGeometyFieldAdd(attr, params.getTable(), params
249
					.getSqlGeometryFieldAdd(attr, params.getTable(), params
262 250
							.getSchema()));
263

  
264 251
		}
252

  
265 253
		return super.getSqlStatementAddField(attr, additionalStatement);
266 254

  
267 255
	}
268
	private Object getSqlGeometyFieldDrop(FeatureAttributeDescriptor attr) {
269
		StringBuilder strb = new StringBuilder();
270
		OracleStoreParameters params = getOracleParameters();
271
		strb.append("Delete from geometry_columns where f_geometry_column = '");
272
		strb.append(attr.getName());
273
		strb.append("' and f_table_nam = '");
274
		strb.append(params.getTable());
275
		strb.append("' and f_table_schema = ");
276
		if (params.getSchema() == null || params.getSchema().length() == 0) {
277
			strb.append("current_schema()");
278
		} else {
279
			strb.append("'");
280
			strb.append(params.getSchema());
281
			strb.append("'");
282
		}
283
		if (params.getCatalog() != null && params.getCatalog().length() > 0) {
284
			strb.append(" and f_table_catalog = '");
285
			strb.append(params.getCatalog());
286
			strb.append("'");
287
		}
288
		return strb.toString();
289
	}
290 256

  
291 257
	protected String getSqlStatementDropField(FeatureAttributeDescriptor attr,
292 258
			List additionalStatement) {
293 259
		String result = super.getSqlStatementDropField(attr,
294 260
				additionalStatement);
295 261
		if (attr.getDataType() == DataTypes.GEOMETRY) {
296
			additionalStatement.add(getSqlGeometyFieldDrop(attr));
262
			additionalStatement.add(getSqlGeometryFieldDrop(attr));
297 263
		}
298 264
		return result;
299 265
	}
300 266

  
301
	protected List getSqlStatementAlterField(
267
	protected List<String> getSqlStatementAlterField(
302 268
			FeatureAttributeDescriptor attrOrg,
303 269
			FeatureAttributeDescriptor attrTrg, List additionalStatement)
304 270
			throws DataException {
305
		//
306
		List actions = super.getSqlStatementAlterField(attrOrg, attrTrg,
307
				additionalStatement);
271

  
272
		List<String> actions = new ArrayList<String>();
308 273
		StringBuilder strb;
309
		OracleStoreParameters params = getOracleParameters();
274
		OracleStoreParameters params = getOracleStoreParameters();
275

  
276
		// diferent column type
310 277
		if (attrOrg.getDataType() != attrTrg.getDataType()) {
278
			strb = new StringBuilder();
279
			strb.append("MODIFY (");
280
			strb.append(helper.escapeFieldName(attrTrg.getName()));
281
			strb.append(" ");
282
			strb.append(helper.getSqlColumnTypeDescription(attrTrg));
283
			strb.append(")");
284

  
311 285
			if (attrOrg.getDataType() == DataTypes.GEOMETRY) {
312
				additionalStatement.add(getSqlGeometyFieldDrop(attrOrg));
286
				additionalStatement.add(getSqlGeometryFieldDrop(attrOrg));
313 287
			}
314 288
			if (attrTrg.getDataType() == DataTypes.GEOMETRY) {
315 289
				additionalStatement.addAll(((OracleHelper) helper)
316
						.getSqlGeometyFieldAdd(attrTrg, params.getTable(),
290
						.getSqlGeometryFieldAdd(attrTrg, params.getTable(),
317 291
								params.getSchema()));
318 292
			}
293

  
294
			actions.add(strb.toString());
319 295
		}
296

  
297
		if (attrOrg.allowNull() != attrTrg.allowNull()) {
298

  
299
			strb = new StringBuilder();
300
			strb.append("MODIFY (");
301
			strb.append(helper.escapeFieldName(attrTrg.getName()));
302
			strb.append(" ");
303
			if (attrTrg.allowNull()) {
304
				strb.append("SET ");
305
			} else {
306
				strb.append("DROP ");
307
			}
308
			strb.append("NOT NULL)");
309
			actions.add(strb.toString());
310
		}
311

  
312
		if (attrOrg.getDefaultValue() != attrTrg.getDefaultValue()) {
313
			if (attrTrg.getDefaultValue() == null) {
314

  
315
				strb = new StringBuilder();
316
				strb.append("MODIFY (");
317
				strb.append(helper.escapeFieldName(attrTrg.getName()));
318
				strb.append(" DROP DEFAULT)");
319
				actions.add(strb.toString());
320
			} else if (!attrTrg.getDefaultValue().equals(
321
					attrOrg.getDefaultValue())) {
322
				// ALTER [ COLUMN ] column DROP DEFAULT
323

  
324
				strb = new StringBuilder();
325
				strb.append("MODIFY (");
326
				strb.append(helper.escapeFieldName(attrTrg.getName()));
327
				strb.append(" SET DEFAULT ");
328
				strb.append(helper.dalValueToJDBC(attrTrg, attrTrg
329
						.getDefaultValue()));
330
				strb.append(")");
331
				actions.add(strb.toString());
332
			}
333
		}
334

  
320 335
		if (attrOrg.getDataType() == attrTrg.getDataType()
321 336
				&& attrTrg.getDataType() == DataTypes.GEOMETRY) {
322 337
			// TODO Checks SRS and GeomType/Subtype
......
324 339

  
325 340
		return actions;
326 341
	}
327
	
328
	public List getSqlGeometyFieldAdd(FeatureAttributeDescriptor attr,
329
			String table, String schema) {
330
	
331 342

  
332
		List sqls = new ArrayList();
333

  
343
	private Object getSqlGeometryFieldDrop(FeatureAttributeDescriptor attr) {
334 344
		StringBuilder strb = new StringBuilder();
335
		strb.append("SELECT AddGeometryColumn('");
336
		if (schema != null && schema.length() > 0) {
337
			strb.append(schema);
338
			strb.append("', '");
339
		}
340
		strb.append(table);
341
		strb.append("', '");
342
		strb.append(attr.getName());
343
		strb.append("', ");
344
		// strb.append("-1");
345
		strb.append(getProviderSRID(attr.getSRS()));
346
		strb.append(", '");
347
		strb.append(getPostgisGeomType(attr.getGeometryType(), attr
348
				.getGeometrySubType()));
349
		strb.append("', ");
350
		strb.append(getPostgisGeomDimensions(attr.getGeometrySubType()));
351
		strb.append(")");
345
		OracleStoreParameters params = getOracleStoreParameters();
352 346

  
353

  
354
		sqls.add(strb.toString());
355

  
356
		/*ALTER TABLE muni10000_peq_test DROP CONSTRAINT enforce_srid_the_geom;*/
357
		/*
358
		strb = new StringBuilder();
359
		strb.append("Alter table ");
360
		if (schema != null && schema.length() > 0) {
361
			strb.append(schema);
362
			strb.append(".");
363
		}
364
		strb.append("f_table_name = '");
365
		strb.append(table);
366
		strb.append("' AND f_geometry_column = '");
347
		// DELETE FROM USER_SDO_GEOM_METADATA WHERE TABLE_NAME like 'XX' And
348
		// COLUMN_NAME like 'GEOMETRY';
349
		strb.append("DELETE FROM ");
350
		strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW);
351
		strb.append(" WHERE ");
352
		strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW_TABLE_NAME);
353
		strb.append(" like '");
354
		strb.append(params.getTable());
355
		strb.append("' AND ");
356
		strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW_COLUMN_NAME);
357
		strb.append(" like '");
367 358
		strb.append(attr.getName());
368
		strb.append("' AND srid = -1");
359
		strb.append("'");
369 360

  
370

  
371
		sqls.add(strb.toString());
372
		*/
373
		return sqls;
361
		return strb.toString();
374 362
	}
375 363

  
376 364
}

Also available in: Unified diff