Revision 32684 branches/v2_0_0_prep/libraries/org.gvsig.fmap.dal.db.h2spatial/src/main/java/org/gvsig/fmap/dal/db/h2spatial/impl/H2SpatialServerExplorer.java

View differences:

H2SpatialServerExplorer.java
33 33
import org.slf4j.Logger;
34 34
import org.slf4j.LoggerFactory;
35 35

  
36
/**
37
 * 
38
 * @author Vicente Caballero Navarro
39
 */
36 40
public class H2SpatialServerExplorer extends JDBCServerExplorer {
37 41
	
38 42
	final static private Logger logger = LoggerFactory
......
62 66
		"net.sourceforge.hatbox.jts.Proc.spatialize"
63 67
	};
64 68

  
69
	private static boolean initialized=false;
70

  
65 71
	public H2SpatialServerExplorer(JDBCServerExplorerParameters parameters,
66 72
			DataServerExplorerProviderServices services)
67 73
			throws InitializeException {
......
73 79
	}
74 80

  
75 81

  
82
	/* (non-Javadoc)
83
	 * @see org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer#createHelper()
84
	 */
76 85
	protected JDBCHelper createHelper() throws InitializeException {
77 86
		return new H2SpatialHelper(this, getH2Spatialparameters());
78 87
	}
79 88

  
80 89

  
90
	/* (non-Javadoc)
91
	 * @see org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer#getStoreName()
92
	 */
81 93
	protected String getStoreName() {
82 94
		return H2SpatialStoreProvider.NAME;
83 95
	}
84 96

  
97
	/* (non-Javadoc)
98
	 * @see org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer#getName()
99
	 */
85 100
	public String getName() {
86 101
		return NAME;
87 102
	}
88 103

  
104
	/* (non-Javadoc)
105
	 * @see org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer#createStoreParams()
106
	 */
89 107
	protected JDBCStoreParameters createStoreParams()
90 108
			throws InitializeException, ProviderNotRegisteredException {
91 109
		H2SpatialStoreParameters orgParams = (H2SpatialStoreParameters) super
......
96 114
		return orgParams;
97 115
	}
98 116

  
99

  
100
	// ****************************
101
	
117
	/* (non-Javadoc)
118
	 * @see org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer#canAdd()
119
	 */
102 120
	public boolean canAdd() {
103 121
		return true;
104 122
	}
105 123

  
124
	/* (non-Javadoc)
125
	 * @see org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer#checkIsMine(org.gvsig.fmap.dal.DataStoreParameters)
126
	 */
106 127
	protected void checkIsMine(DataStoreParameters dsp) {
107 128
		if (!(dsp instanceof H2SpatialStoreParameters)) {
108 129
			// FIXME Excpetion ???
......
120 141
		}
121 142
	}
122 143
	
144
	/* (non-Javadoc)
145
	 * @see org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer#remove(org.gvsig.fmap.dal.DataStoreParameters)
146
	 */
123 147
	public void remove(DataStoreParameters dsp) throws RemoveException {
124 148
		final H2SpatialStoreParameters slParams =(H2SpatialStoreParameters) dsp;
125 149

  
......
129 153
			}
130 154
			public Object action(Connection conn) throws DataException {
131 155

  
132

  
156
				
133 157
				Statement st;
134 158
				try{
159
					Proc.deSpatialize(conn, slParams.getSchema().toUpperCase(), slParams.getTable().toUpperCase());
135 160
					st = conn.createStatement();
136 161
				} catch (SQLException e) {
137 162
					throw new JDBCSQLException(e);
138 163
				}
139 164

  
140
				String sqlDrop = "Drop table "
141
					+ slParams.tableID();
142
				
143

  
144
				StringBuilder strb = new StringBuilder();
145
				strb.append("Delete from geometry_columns where f_table_name = ");
146
				strb.append('\'');
147
				strb.append(slParams.getTable());
148
				strb.append('\'');
149
				
150
				StringBuilder strb2 = new StringBuilder();
151
				strb2.append("Delete from geom_cols_ref_sys where f_table_name = ");
152
				strb2.append('\'');
153
				strb2.append(slParams.getTable());
154
				strb2.append('\'');
155
				
156 165
				StringBuilder strb3 = new StringBuilder();
157
				strb3.append("DROP TABLE IF EXISTS");
158
				strb3.append('\'');
159
				strb3.append("idx_");
160
				strb3.append(slParams.getTable());
161
				strb3.append("_the_geom");
162
				strb3.append('\'');
166
				strb3.append("DROP TABLE IF EXISTS ");
167
				strb3.append(slParams.getTable().toUpperCase());
163 168
				
164 169
				StringBuilder strb4 = new StringBuilder();
165
				strb4.append("DROP TABLE IF EXISTS");
166
				strb4.append('\'');
167
				strb4.append("idx_");
168
				strb4.append(slParams.getTable());
169
				strb4.append("_the_geom_node");
170
				strb4.append('\'');
170
				strb4.append("DROP TABLE IF EXISTS ");
171
				strb4.append(slParams.getTable().toUpperCase());
172
				strb4.append("_HATBOX");
173
			
174
				String sqlDeleteTable = strb3.toString();
175
				String sqlDeleteTable_HATBOX = strb4.toString();
171 176
				
172
				StringBuilder strb5 = new StringBuilder();
173
				strb5.append("DROP TABLE IF EXISTS");
174
				strb5.append('\'');
175
				strb5.append("idx_");
176
				strb5.append(slParams.getTable());
177
				strb5.append("_the_geom_parent");
178
				strb5.append('\'');
179
				
180
				StringBuilder strb6 = new StringBuilder();
181
				strb6.append("DROP TABLE IF EXISTS");
182
				strb6.append('\'');
183
				strb6.append("idx_");
184
				strb6.append(slParams.getTable());
185
				strb6.append("_the_geom_rowid");
186
				strb6.append('\'');
187
				
188
				StringBuilder strb7 = new StringBuilder();
189
				strb6.append("DROP TABLE IF EXISTS");
190
				strb6.append('\'');
191
				strb6.append("cache_");
192
				strb6.append(slParams.getTable());
193
				strb6.append("_the_geom");
194
				strb6.append('\'');
195

  
196
				String sqlDeleteFromGeometry_column = strb.toString();
197
				String sqlDeleteFromGeom_cols_ref = strb2.toString();
198
				String sqlDeleteIdx = strb3.toString();
199
				String sqlDeleteIdx_node = strb4.toString();
200
				String sqlDeleteIdx_parent = strb5.toString();
201
				String sqlDeleteIdx_rowid = strb6.toString();
202
				String sqlDeleteCache = strb7.toString();
203
				
204 177
				try{
205 178
					try{
206
						st.execute(sqlDrop);
179
						st.execute(sqlDeleteTable);
207 180
					} catch (SQLException e) {
208
						throw new JDBCExecuteSQLException(sqlDrop, e);
181
						throw new JDBCExecuteSQLException(sqlDeleteTable, e);
209 182
					}
210 183

  
211 184
					try {
212
						st.execute(sqlDeleteFromGeometry_column);
185
						st.execute(sqlDeleteTable_HATBOX);
213 186
					} catch (SQLException e) {
214
						throw new JDBCExecuteSQLException(
215
								sqlDeleteFromGeometry_column, e);
187
						throw new JDBCExecuteSQLException(sqlDeleteTable_HATBOX, e);
216 188
					}
217
					
218
					try {
219
						st.execute(sqlDeleteFromGeom_cols_ref);
220
					} catch (SQLException e) {
221
						throw new JDBCExecuteSQLException(
222
								sqlDeleteFromGeom_cols_ref, e);
223
					}
224
					
225
					try {
226
						st.execute(sqlDeleteIdx);
227
					} catch (SQLException e) {
228
						throw new JDBCExecuteSQLException(
229
								sqlDeleteIdx, e);
230
					}
231
					
232
					try {
233
						st.execute(sqlDeleteIdx_node);
234
					} catch (SQLException e) {
235
						throw new JDBCExecuteSQLException(
236
								sqlDeleteIdx_node, e);
237
					}
238
					
239
					try {
240
						st.execute(sqlDeleteIdx_parent);
241
					} catch (SQLException e) {
242
						throw new JDBCExecuteSQLException(
243
								sqlDeleteIdx_parent, e);
244
					}
245
					
246
					try {
247
						st.execute(sqlDeleteIdx_rowid);
248
					} catch (SQLException e) {
249
						throw new JDBCExecuteSQLException(
250
								sqlDeleteIdx_rowid, e);
251
					}
252
					
253
					try {
254
						st.execute(sqlDeleteCache);
255
					} catch (SQLException e) {
256
						throw new JDBCExecuteSQLException(
257
								sqlDeleteCache, e);
258
					}
259

  
260 189
				} finally{
261 190
					try{ st.close(); } catch (SQLException e) {};
262 191
				}
......
270 199
		}
271 200
	}
272 201
	
202
	/* (non-Javadoc)
203
	 * @see org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer#getAddParameters()
204
	 */
273 205
	public NewDataStoreParameters getAddParameters() throws DataException {
274 206
		H2SpatialServerExplorerParameters parameters = getH2Spatialparameters();
275 207
		H2SpatialNewStoreParameters params = new H2SpatialNewStoreParameters();
......
292 224
		return params;
293 225
	}
294 226

  
295

  
296

  
297
	// ***********************
298
	// ***********************
299
	
227
	/* (non-Javadoc)
228
	 * @see org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer#getSQLForList(int, boolean)
229
	 */
300 230
	protected List getSQLForList(int mode, boolean showInformationDBTables) {
301 231
		List list = new ArrayList(1);
302 232
		String sql="SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA<>'INFORMATION_SCHEMA' AND TABLE_NAME NOT LIKE '%_HATBOX' AND EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '%_HATBOX')";
......
304 234
		return list;
305 235

  
306 236
	}
237
	/* (non-Javadoc)
238
	 * @see org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer#add(org.gvsig.fmap.dal.NewDataStoreParameters, boolean)
239
	 */
307 240
	public boolean add(NewDataStoreParameters ndsp, boolean overwrite)
308 241
	throws DataException {
309 242

  
310 243
		//crea el fichero de la bbdd y la hace espacial
311
		this.initializedb();
244
		if (!initialized){
245
			this.initializedb();
246
			initialized=true;
247
		}
312 248
		
313
		/**
314
		 * CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
315
		 * { column_name data_type [ DEFAULT default_expr ] [ column_constraint
316
		 * [ ... ] ] | table_constraint | LIKE parent_table [ { INCLUDING |
317
		 * EXCLUDING } DEFAULTS ] } [, ... ] ) [ INHERITS ( parent_table [, ...
318
		 * ] ) ] [ WITH OIDS | WITHOUT OIDS ] [ ON COMMIT { PRESERVE ROWS |
319
		 * DELETE ROWS | DROP } ]
320
		 *
321
		 * where column_constraint is:
322
		 *
323
		 * [ CONSTRAINT constraint_name ] { NOT NULL | NULL | UNIQUE | PRIMARY
324
		 * KEY | CHECK (expression) | REFERENCES reftable [ ( refcolumn ) ] [
325
		 * MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON
326
		 * UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY
327
		 * DEFERRED | INITIALLY IMMEDIATE ]
328
		 *
329
		 * and table_constraint is:
330
		 *
331
		 * [ CONSTRAINT constraint_name ] { UNIQUE ( column_name [, ... ] ) |
332
		 * PRIMARY KEY ( column_name [, ... ] ) | CHECK ( expression ) | FOREIGN
333
		 * KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ...
334
		 * ] ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE
335
		 * action ] [ ON UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [
336
		 * INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
337
		 */
338

  
339 249
		if (!(ndsp instanceof NewFeatureStoreParameters)) {
340
			// FIXME exception
341 250
			throw new IllegalArgumentException();
342 251
		}
343 252
		checkIsMine(ndsp);
......
347 256
		StringBuilder sql = new StringBuilder();
348 257

  
349 258
		if (!nfdsp.isValid()) {
350
			// TODO Exception
351 259
			throw new InitializeException(this.getName(), new Exception(
352 260
			"Parameters not valid"));
353 261
		}
......
399 307
		TransactionalAction action = new TransactionalAction() {
400 308

  
401 309
			public boolean continueTransactionAllowed() {
402
				// TODO Auto-generated method stub
403 310
				return false;
404 311
			}
405 312

  
......
434 341
						logger.error("Exception clossing statement", e);
435 342
					}
436 343
				}
437

  
438 344
				return Boolean.TRUE;
439 345
			}
440 346

  
......
447 353
			Proc.spatialize(helper.getConnection(), schema, table, geomCol, geomType, epsg, "false", "49");
448 354
			Proc.buildIndex(helper.getConnection(), schema, table, 100, null);
449 355
		} catch (Exception e) {
450
			// FIXME Exception
451 356
			throw new RuntimeException(e);
452 357
		}
453 358

  
......
458 363
		try {
459 364
    		Class.forName("org.h2.Driver");
460 365
			conn = getHelper().getConnection();//DriverManager.getConnection("jdbc:h2:"+System.getProperty("user.home")+File.separator+"gvSIG"+File.separator+"cache"+File.separator+"cache", null, null);
366
		
367
			StringBuilder strb = new StringBuilder();
368
			strb.append("DROP ALL OBJECTS");
369
		
370
			String sqlDeleteTable = strb.toString();
371
			
372
			Statement st;
373
			try{
374
				st = conn.createStatement();
375
				st.execute(sqlDeleteTable);
376
			} catch (SQLException e) {
377
				throw new JDBCExecuteSQLException(sqlDeleteTable, e);
378
			}
379
		
461 380
		} catch (Exception e) {
462 381
			e.printStackTrace();
463 382
		}
464
		//si esta no esta la bd espacializada se espacializa
383
		//si no est? la bd espacializada se espacializa
465 384
		if(!determineDbStatus(conn)){
466 385
			spatializedb(conn);
467 386
		}
......
503 422
		}
504 423
		return spatialized;
505 424
	}
425
	
506 426
}

Also available in: Unified diff