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 / jdbc / JDBCServerExplorer.java @ 41638
History | View | Annotate | Download (20.4 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
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 3
|
||
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 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc; |
25 | |||
26 | import java.sql.Connection; |
||
27 | import java.sql.DatabaseMetaData; |
||
28 | import java.sql.ResultSet; |
||
29 | import java.sql.SQLException; |
||
30 | import java.sql.Statement; |
||
31 | import java.util.ArrayList; |
||
32 | import java.util.Iterator; |
||
33 | import java.util.List; |
||
34 | 41638 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
35 | 40435 | jjdelcerro | |
36 | import org.slf4j.Logger; |
||
37 | import org.slf4j.LoggerFactory; |
||
38 | |||
39 | import org.gvsig.fmap.dal.DALLocator; |
||
40 | import org.gvsig.fmap.dal.DataManager; |
||
41 | import org.gvsig.fmap.dal.DataStore; |
||
42 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
43 | import org.gvsig.fmap.dal.NewDataStoreParameters; |
||
44 | import org.gvsig.fmap.dal.exception.CloseException; |
||
45 | import org.gvsig.fmap.dal.exception.DataException; |
||
46 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
47 | import org.gvsig.fmap.dal.exception.OpenException; |
||
48 | import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
||
49 | import org.gvsig.fmap.dal.exception.ReadException; |
||
50 | import org.gvsig.fmap.dal.exception.RemoveException; |
||
51 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
52 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
53 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
54 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
55 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
56 | import org.gvsig.fmap.dal.serverexplorer.db.spi.AbstractDBServerExplorer; |
||
57 | import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
||
58 | import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
||
59 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
||
60 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
61 | import org.gvsig.tools.exception.BaseException; |
||
62 | |||
63 | |||
64 | /**
|
||
65 | * @author jmvivo
|
||
66 | *
|
||
67 | */
|
||
68 | public class JDBCServerExplorer extends AbstractDBServerExplorer |
||
69 | implements JDBCHelperUser {
|
||
70 | |||
71 | private static final Logger LOG = LoggerFactory |
||
72 | .getLogger(JDBCServerExplorer.class); |
||
73 | |||
74 | private static final String METADATA_COLUMN_TABLE_CATALOG = "TABLE_CAT"; |
||
75 | private static final String METADATA_COLUMN_TABLE_SCHEMA = "TABLE_SCHEM"; |
||
76 | private static final String METADATA_COLUMN_TABLE_NAME = "TABLE_NAME"; |
||
77 | |||
78 | public static final String NAME = "JDBCServerExplorer"; |
||
79 | protected JDBCHelper helper;
|
||
80 | |||
81 | private Boolean canAdd; |
||
82 | |||
83 | public JDBCServerExplorer(JDBCServerExplorerParameters parameters,
|
||
84 | DataServerExplorerProviderServices services) |
||
85 | throws InitializeException {
|
||
86 | super(parameters, services);
|
||
87 | this.helper = createHelper();
|
||
88 | } |
||
89 | |||
90 | protected JDBCServerExplorerParameters getJDBCParameters() {
|
||
91 | return (JDBCServerExplorerParameters) getParameters();
|
||
92 | } |
||
93 | |||
94 | protected JDBCHelper createHelper() throws InitializeException { |
||
95 | return new JDBCHelper(this, getJDBCParameters()); |
||
96 | } |
||
97 | |||
98 | protected JDBCHelper getHelper() {
|
||
99 | return helper;
|
||
100 | } |
||
101 | |||
102 | |||
103 | public List list() throws DataException { |
||
104 | return this.list(MODE_ALL); |
||
105 | } |
||
106 | |||
107 | public List list(boolean showInformationDBTables) throws DataException { |
||
108 | return this.list(MODE_ALL, showInformationDBTables); |
||
109 | } |
||
110 | |||
111 | public List list(int mode) throws DataException { |
||
112 | JDBCServerExplorerParameters parameters = getJDBCParameters(); |
||
113 | if (parameters.getShowInformationDBTables() != null) { |
||
114 | return this.list(mode, parameters.getShowInformationDBTables() |
||
115 | .booleanValue()); |
||
116 | } |
||
117 | Boolean show = (Boolean) parameters |
||
118 | .getDynClass() |
||
119 | .getDynField( |
||
120 | JDBCServerExplorerParameters.SHOWINFORMATIONDBTABLES_PARAMTER_NAME) |
||
121 | .getDefaultValue(); |
||
122 | if (show == null){ |
||
123 | show = Boolean.FALSE;
|
||
124 | } |
||
125 | |||
126 | return this.list(mode, show.booleanValue()); |
||
127 | } |
||
128 | |||
129 | |||
130 | protected DataManagerProviderServices getManager() {
|
||
131 | return (DataManagerProviderServices) DALLocator.getDataManager();
|
||
132 | } |
||
133 | |||
134 | public boolean hasGeometrySupport() { |
||
135 | return false; |
||
136 | } |
||
137 | |||
138 | public boolean closeResourceRequested(ResourceProvider resource) { |
||
139 | try {
|
||
140 | this.helper.close();
|
||
141 | } catch (CloseException e) {
|
||
142 | LOG.error("Exception in close Request", e);
|
||
143 | } |
||
144 | return !this.helper.isOpen(); |
||
145 | } |
||
146 | |||
147 | public void resourceChanged(ResourceProvider resource) { |
||
148 | // Nothing to do
|
||
149 | } |
||
150 | |||
151 | public void remove(DataStoreParameters dsp) throws RemoveException { |
||
152 | final JDBCStoreParameters dsParams =(JDBCStoreParameters) dsp;
|
||
153 | |||
154 | TransactionalAction action = new TransactionalAction() {
|
||
155 | public boolean continueTransactionAllowed() { |
||
156 | return false; |
||
157 | } |
||
158 | public Object action(Connection conn) throws DataException { |
||
159 | Statement st;
|
||
160 | try{
|
||
161 | st = conn.createStatement(); |
||
162 | } catch (SQLException e) { |
||
163 | throw new JDBCSQLException(e); |
||
164 | } |
||
165 | |||
166 | String sqlDrop = "Drop table " |
||
167 | + dsParams.tableID(); |
||
168 | |||
169 | try{
|
||
170 | try{
|
||
171 | 41485 | jjdelcerro | JDBCHelper.execute(st, sqlDrop); |
172 | 40435 | jjdelcerro | } catch (SQLException e) { |
173 | throw new JDBCExecuteSQLException(sqlDrop, e); |
||
174 | } |
||
175 | |||
176 | } finally{
|
||
177 | try{ st.close(); } catch (SQLException e) {}; |
||
178 | } |
||
179 | return null; |
||
180 | } |
||
181 | }; |
||
182 | try {
|
||
183 | this.helper.doConnectionAction(action);
|
||
184 | } catch (Exception e) { |
||
185 | throw new RemoveException(this.getProviderName(), e); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | 41486 | jjdelcerro | public DataStoreParameters getOpenParameters() throws DataException { |
190 | 40435 | jjdelcerro | JDBCServerExplorerParameters parameters = getJDBCParameters(); |
191 | 41486 | jjdelcerro | JDBCStoreParameters params = new JDBCStoreParameters();
|
192 | params.setHost(parameters.getHost()); |
||
193 | params.setPort(parameters.getPort()); |
||
194 | params.setDBName(parameters.getDBName()); |
||
195 | params.setUser(parameters.getUser()); |
||
196 | params.setPassword(parameters.getPassword()); |
||
197 | params.setCatalog(parameters.getCatalog()); |
||
198 | params.setSchema(parameters.getSchema()); |
||
199 | params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
||
200 | params.setUrl(parameters.getUrl()); |
||
201 | return params;
|
||
202 | } |
||
203 | |||
204 | public NewDataStoreParameters getAddParameters() throws DataException { |
||
205 | JDBCServerExplorerParameters parameters = getJDBCParameters(); |
||
206 | 40435 | jjdelcerro | JDBCNewStoreParameters params = new JDBCNewStoreParameters();
|
207 | params.setHost(parameters.getHost()); |
||
208 | params.setPort(parameters.getPort()); |
||
209 | params.setDBName(parameters.getDBName()); |
||
210 | params.setUser(parameters.getUser()); |
||
211 | params.setPassword(parameters.getPassword()); |
||
212 | params.setCatalog(parameters.getCatalog()); |
||
213 | params.setSchema(parameters.getSchema()); |
||
214 | params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
||
215 | params.setUrl(parameters.getUrl()); |
||
216 | |||
217 | params.setDefaultFeatureType(this.getServerExplorerProviderServices()
|
||
218 | .createNewFeatureType()); |
||
219 | |||
220 | return params;
|
||
221 | } |
||
222 | |||
223 | public void closeDone() throws DataException { |
||
224 | // Nothing to do
|
||
225 | } |
||
226 | |||
227 | public void opendDone() throws DataException { |
||
228 | // Nothin to do
|
||
229 | |||
230 | } |
||
231 | |||
232 | public DataStore open(DataStoreParameters dsp) throws DataException { |
||
233 | checkIsMine(dsp); |
||
234 | DataManager dataMan = DALLocator.getDataManager(); |
||
235 | DataStore store; |
||
236 | try {
|
||
237 | store = dataMan.openStore(dsp.getDataStoreName(), dsp); |
||
238 | } catch (ValidateDataParametersException e) {
|
||
239 | throw new InitializeException(e); |
||
240 | } |
||
241 | |||
242 | return store;
|
||
243 | } |
||
244 | |||
245 | protected void checkIsMine(DataStoreParameters dsp) { |
||
246 | 41509 | jjdelcerro | if (!(dsp instanceof JDBCConnectionParameters)) { |
247 | 40435 | jjdelcerro | // FIXME Exception ???
|
248 | throw new IllegalArgumentException( |
||
249 | "not instance of FilesystemStoreParameters");
|
||
250 | } |
||
251 | |||
252 | // try {
|
||
253 | // dsp.validate();
|
||
254 | // } catch (ValidateDataParametersException e) {
|
||
255 | // throw new IllegalArgumentException("check parameters", e);
|
||
256 | // }
|
||
257 | |||
258 | JDBCServerExplorerParameters parameters = getJDBCParameters(); |
||
259 | |||
260 | 41509 | jjdelcerro | JDBCConnectionParameters pgp = (JDBCConnectionParameters) dsp; |
261 | 40435 | jjdelcerro | if (!compare(pgp.getHost(), parameters.getHost())) {
|
262 | throw new IllegalArgumentException("wrong explorer: Host (mine: " |
||
263 | + parameters.getHost() + " other:" + pgp.getHost() + ")"); |
||
264 | } |
||
265 | if (!compare(pgp.getPort(), parameters.getPort())) {
|
||
266 | throw new IllegalArgumentException("wrong explorer: Port (mine: " |
||
267 | + parameters.getPort() + " other:" + pgp.getPort() + ")"); |
||
268 | } |
||
269 | if (!compare(pgp.getDBName(), parameters.getDBName())) {
|
||
270 | throw new IllegalArgumentException("wrong explorer: DBName (mine: " |
||
271 | + parameters.getDBName() + " other:" + pgp.getDBName()
|
||
272 | + ")");
|
||
273 | } |
||
274 | if (parameters.getCatalog() != null && !parameters.getCatalog().trim().equals("")) { |
||
275 | // implicit catalog
|
||
276 | if (!compare(pgp.getCatalog(), parameters.getCatalog())) {
|
||
277 | throw new IllegalArgumentException( |
||
278 | "wrong explorer: Catalog (mine: "
|
||
279 | + parameters.getCatalog() + " other:"
|
||
280 | + pgp.getCatalog() + ")");
|
||
281 | } |
||
282 | } |
||
283 | if (parameters.getSchema() != null && !parameters.getSchema().trim().equals("")) { |
||
284 | // implicit schema
|
||
285 | if (!compare(pgp.getSchema(), parameters.getSchema())) {
|
||
286 | throw new IllegalArgumentException( |
||
287 | "wrong explorer: Schema (mine: "
|
||
288 | + parameters.getSchema() + " other:"
|
||
289 | + pgp.getSchema() + ")");
|
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | |||
294 | protected boolean compare(Object str1, Object str2) { |
||
295 | if (str1 == str2){
|
||
296 | return true; |
||
297 | } |
||
298 | if (str1 == null){ |
||
299 | return false; |
||
300 | } |
||
301 | return str1.equals(str2);
|
||
302 | } |
||
303 | |||
304 | protected JDBCStoreParameters createStoreParams()
|
||
305 | throws InitializeException, ProviderNotRegisteredException {
|
||
306 | DataManagerProviderServices manager = this.getManager();
|
||
307 | JDBCServerExplorerParameters parameters = getJDBCParameters(); |
||
308 | JDBCStoreParameters orgParams = (JDBCStoreParameters) manager |
||
309 | .createStoreParameters(getStoreName()); |
||
310 | orgParams.setHost(parameters.getHost()); |
||
311 | orgParams.setPort(parameters.getPort()); |
||
312 | orgParams.setDBName(parameters.getDBName()); |
||
313 | orgParams.setUser(parameters.getUser()); |
||
314 | orgParams.setPassword(parameters.getPassword()); |
||
315 | orgParams.setCatalog(parameters.getCatalog()); |
||
316 | orgParams.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
||
317 | orgParams.setSchema(parameters.getSchema()); |
||
318 | orgParams.setUrl(parameters.getUrl()); |
||
319 | return orgParams;
|
||
320 | } |
||
321 | |||
322 | public List list(final int mode, final boolean showInformationDBTables) |
||
323 | throws DataException {
|
||
324 | |||
325 | final JDBCStoreParameters orgParams = createStoreParams();
|
||
326 | |||
327 | ConnectionAction action = new ConnectionAction() {
|
||
328 | |||
329 | public Object action(Connection conn) throws DataException { |
||
330 | |||
331 | String[] tableTypes = null; |
||
332 | if (!showInformationDBTables) {
|
||
333 | tableTypes = new String[] { "TABLE", "VIEW" }; |
||
334 | } |
||
335 | |||
336 | ResultSet result = null; |
||
337 | try {
|
||
338 | DatabaseMetaData metadata = conn.getMetaData();
|
||
339 | result = metadata.getTables(null, null, null, |
||
340 | tableTypes); |
||
341 | List<JDBCStoreParameters> paramList = new ArrayList<JDBCStoreParameters>(); |
||
342 | while (result.next()) {
|
||
343 | JDBCStoreParameters params = (JDBCStoreParameters) orgParams |
||
344 | .getCopy(); |
||
345 | params.setCatalog(result |
||
346 | .getString(METADATA_COLUMN_TABLE_CATALOG)); |
||
347 | params.setSchema(result |
||
348 | .getString(METADATA_COLUMN_TABLE_SCHEMA)); |
||
349 | params.setTable(result |
||
350 | .getString(METADATA_COLUMN_TABLE_NAME)); |
||
351 | paramList.add(params); |
||
352 | } |
||
353 | |||
354 | return paramList;
|
||
355 | } catch (SQLException e) { |
||
356 | throw new JDBCSQLException(e); |
||
357 | } finally {
|
||
358 | if (result != null) { |
||
359 | try {
|
||
360 | result.close(); |
||
361 | } catch (Exception e) { |
||
362 | LOG.error("Error closing DatabaseMetadata "
|
||
363 | + "getTables() Resultset", e);
|
||
364 | } |
||
365 | } |
||
366 | } |
||
367 | } |
||
368 | |||
369 | }; |
||
370 | |||
371 | try {
|
||
372 | return (List) helper.doConnectionAction(action); |
||
373 | } catch (Exception e) { |
||
374 | throw new ReadException(this.getProviderName(), e); |
||
375 | } |
||
376 | } |
||
377 | |||
378 | |||
379 | public void open() throws OpenException { |
||
380 | helper.open(); |
||
381 | } |
||
382 | |||
383 | public void close() throws CloseException { |
||
384 | helper.close(); |
||
385 | } |
||
386 | |||
387 | @Override
|
||
388 | protected void doDispose() throws BaseException { |
||
389 | helper.dispose(); |
||
390 | helper = null;
|
||
391 | } |
||
392 | |||
393 | public String getProviderName() { |
||
394 | return NAME;
|
||
395 | } |
||
396 | |||
397 | 41486 | jjdelcerro | public String getStoreName() { |
398 | 40435 | jjdelcerro | return JDBCStoreProvider.NAME;
|
399 | } |
||
400 | |||
401 | public boolean canAdd() { |
||
402 | if (this.canAdd == null){ |
||
403 | ConnectionAction action = new ConnectionAction(){
|
||
404 | |||
405 | public Object action(Connection conn) throws DataException { |
||
406 | try {
|
||
407 | DatabaseMetaData metadata = conn.getMetaData();
|
||
408 | if (metadata.isReadOnly()) {
|
||
409 | return Boolean.FALSE; |
||
410 | } |
||
411 | return Boolean.TRUE; |
||
412 | } catch (SQLException e) { |
||
413 | throw new JDBCSQLException(e); |
||
414 | } |
||
415 | } |
||
416 | |||
417 | }; |
||
418 | |||
419 | try {
|
||
420 | this.canAdd = (Boolean) helper.doConnectionAction(action); |
||
421 | } catch (Exception e) { |
||
422 | // FIXME Exception
|
||
423 | throw new RuntimeException(e); |
||
424 | } |
||
425 | } |
||
426 | return this.canAdd.booleanValue(); |
||
427 | } |
||
428 | |||
429 | public FeatureType getFeatureType(DataStoreParameters dsp)
|
||
430 | throws DataException {
|
||
431 | checkIsMine(dsp); |
||
432 | |||
433 | // TODO: checks geometry columns and driver geometry supports
|
||
434 | EditableFeatureType edType = this.getServerExplorerProviderServices()
|
||
435 | .createNewFeatureType(); |
||
436 | helper.loadFeatureType(edType, (JDBCStoreParameters) dsp); |
||
437 | |||
438 | return edType;
|
||
439 | |||
440 | } |
||
441 | 41638 | jjdelcerro | |
442 | protected List createGrantStatements(String tableName, String privilege, String theRoles) { |
||
443 | List statements = new ArrayList(); |
||
444 | String[] roles = StringUtils.split(theRoles,","); |
||
445 | for( int i=0; i<roles.length; i++) { |
||
446 | String statement = "GRANT "+ privilege + "ON TABLE " + tableName + " TO " + roles[i]; |
||
447 | statements.add(statement); |
||
448 | } |
||
449 | return statements;
|
||
450 | } |
||
451 | |||
452 | protected List getPermissionsSQLs(JDBCNewStoreParameters ndsp) { |
||
453 | List statements = null; |
||
454 | String roles;
|
||
455 | |||
456 | roles = ndsp.getSelectRole(); |
||
457 | if( roles!=null ) { |
||
458 | if( statements == null ) { |
||
459 | statements = new ArrayList(); |
||
460 | } |
||
461 | statements.addAll(this.createGrantStatements(ndsp.tableID(), "SELECT", roles)); |
||
462 | } |
||
463 | roles = ndsp.getInsertRole(); |
||
464 | if( roles!=null ) { |
||
465 | if( statements == null ) { |
||
466 | statements = new ArrayList(); |
||
467 | } |
||
468 | statements.addAll(this.createGrantStatements(ndsp.tableID(), "INSERT", roles)); |
||
469 | } |
||
470 | roles = ndsp.getUpdateRole(); |
||
471 | if( roles!=null ) { |
||
472 | if( statements == null ) { |
||
473 | statements = new ArrayList(); |
||
474 | } |
||
475 | statements.addAll(this.createGrantStatements(ndsp.tableID(), "UPDATE", roles)); |
||
476 | } |
||
477 | roles = ndsp.getDeleteRole(); |
||
478 | if( roles!=null ) { |
||
479 | if( statements == null ) { |
||
480 | statements = new ArrayList(); |
||
481 | } |
||
482 | statements.addAll(this.createGrantStatements(ndsp.tableID(), "DELETE", roles)); |
||
483 | } |
||
484 | roles = ndsp.getTruncateRole(); |
||
485 | if( roles!=null ) { |
||
486 | if( statements == null ) { |
||
487 | statements = new ArrayList(); |
||
488 | } |
||
489 | statements.addAll(this.createGrantStatements(ndsp.tableID(), "TRUNCATE", roles)); |
||
490 | } |
||
491 | roles = ndsp.getReferenceRole(); |
||
492 | if( roles!=null ) { |
||
493 | if( statements == null ) { |
||
494 | statements = new ArrayList(); |
||
495 | } |
||
496 | statements.addAll(this.createGrantStatements(ndsp.tableID(), "REFERENCE", roles)); |
||
497 | } |
||
498 | roles = ndsp.getTriggerRole(); |
||
499 | if( roles!=null ) { |
||
500 | if( statements == null ) { |
||
501 | statements = new ArrayList(); |
||
502 | } |
||
503 | statements.addAll(this.createGrantStatements(ndsp.tableID(), "TRIGGER", roles)); |
||
504 | } |
||
505 | roles = ndsp.getAllRole(); |
||
506 | if( roles!=null ) { |
||
507 | if( statements == null ) { |
||
508 | statements = new ArrayList(); |
||
509 | } |
||
510 | statements.addAll(this.createGrantStatements(ndsp.tableID(), "ALL", roles)); |
||
511 | } |
||
512 | |||
513 | return statements;
|
||
514 | } |
||
515 | 40435 | jjdelcerro | |
516 | public boolean add(String providerName, NewDataStoreParameters ndsp, boolean overwrite) |
||
517 | throws DataException {
|
||
518 | |||
519 | /**
|
||
520 | * CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
|
||
521 | * { column_name data_type [ DEFAULT default_expr ] [ column_constraint
|
||
522 | * [ ... ] ] | table_constraint | LIKE parent_table [ { INCLUDING |
|
||
523 | * EXCLUDING } DEFAULTS ] } [, ... ] ) [ INHERITS ( parent_table [, ...
|
||
524 | * ] ) ] [ WITH OIDS | WITHOUT OIDS ] [ ON COMMIT { PRESERVE ROWS |
|
||
525 | * DELETE ROWS | DROP } ]
|
||
526 | *
|
||
527 | * where column_constraint is:
|
||
528 | *
|
||
529 | * [ CONSTRAINT constraint_name ] { NOT NULL | NULL | UNIQUE | PRIMARY
|
||
530 | * KEY | CHECK (expression) | REFERENCES reftable [ ( refcolumn ) ] [
|
||
531 | * MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON
|
||
532 | * UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY
|
||
533 | * DEFERRED | INITIALLY IMMEDIATE ]
|
||
534 | *
|
||
535 | * and table_constraint is:
|
||
536 | *
|
||
537 | * [ CONSTRAINT constraint_name ] { UNIQUE ( column_name [, ... ] ) |
|
||
538 | * PRIMARY KEY ( column_name [, ... ] ) | CHECK ( expression ) | FOREIGN
|
||
539 | * KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ...
|
||
540 | * ] ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE
|
||
541 | * action ] [ ON UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [
|
||
542 | * INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
|
||
543 | */
|
||
544 | |||
545 | 41509 | jjdelcerro | if (!(ndsp instanceof JDBCNewStoreParameters)) { |
546 | 40435 | jjdelcerro | // FIXME exception
|
547 | throw new IllegalArgumentException(); |
||
548 | } |
||
549 | checkIsMine(ndsp); |
||
550 | |||
551 | 41509 | jjdelcerro | JDBCNewStoreParameters jdbcnsp = (JDBCNewStoreParameters) ndsp; |
552 | 40435 | jjdelcerro | |
553 | StringBuilder sql = new StringBuilder(); |
||
554 | |||
555 | 41509 | jjdelcerro | if (!jdbcnsp.isValid()) {
|
556 | 40435 | jjdelcerro | // TODO Exception
|
557 | throw new InitializeException(this.getProviderName(), new Exception( |
||
558 | "Parameters not valid"));
|
||
559 | } |
||
560 | try {
|
||
561 | 41509 | jjdelcerro | jdbcnsp.validate(); |
562 | 40435 | jjdelcerro | } catch (ValidateDataParametersException e1) {
|
563 | throw new InitializeException(this.getProviderName(), e1); |
||
564 | } |
||
565 | |||
566 | 41509 | jjdelcerro | FeatureType fType = jdbcnsp.getDefaultFeatureType(); |
567 | 40435 | jjdelcerro | |
568 | 41509 | jjdelcerro | sql.append("Create table " + jdbcnsp.tableID()
|
569 | 40435 | jjdelcerro | + "(");
|
570 | Iterator attrs = fType.iterator();
|
||
571 | String sqlAttr;
|
||
572 | List sqlAttrs = new ArrayList(); |
||
573 | |||
574 | while (attrs.hasNext()) {
|
||
575 | sqlAttr = helper |
||
576 | .getSqlFieldDescription((FeatureAttributeDescriptor) attrs |
||
577 | .next()); |
||
578 | if (sqlAttr != null) { |
||
579 | sqlAttrs.add(sqlAttr); |
||
580 | } |
||
581 | } |
||
582 | |||
583 | helper.stringJoin(sqlAttrs, ", ", sql);
|
||
584 | |||
585 | sql.append(")");
|
||
586 | |||
587 | final String sqlCreate = sql.toString(); |
||
588 | 41638 | jjdelcerro | final List sqlAdditional = helper.getAdditionalSqlToCreate(ndsp, fType); |
589 | 40435 | jjdelcerro | |
590 | 41638 | jjdelcerro | List permissions = getPermissionsSQLs((JDBCNewStoreParameters) ndsp);
|
591 | if( permissions != null ) { |
||
592 | sqlAdditional.addAll(permissions); |
||
593 | } |
||
594 | |||
595 | if( ((JDBCNewStoreParameters) ndsp).getPostCreatingStatement()!=null ) { |
||
596 | sqlAdditional.add(((JDBCNewStoreParameters) ndsp).getPostCreatingStatement()); |
||
597 | } |
||
598 | |||
599 | 40435 | jjdelcerro | TransactionalAction action = new TransactionalAction() {
|
600 | |||
601 | public boolean continueTransactionAllowed() { |
||
602 | // TODO Auto-generated method stub
|
||
603 | return false; |
||
604 | } |
||
605 | |||
606 | public Object action(Connection conn) throws DataException { |
||
607 | Statement st = null; |
||
608 | |||
609 | try {
|
||
610 | st = conn.createStatement(); |
||
611 | } catch (SQLException e1) { |
||
612 | throw new JDBCSQLException(e1); |
||
613 | } |
||
614 | String sql = null; |
||
615 | |||
616 | try {
|
||
617 | sql = sqlCreate; |
||
618 | 41485 | jjdelcerro | JDBCHelper.execute(st, sql); |
619 | 40435 | jjdelcerro | if (sqlAdditional != null) { |
620 | Iterator iter = sqlAdditional.iterator();
|
||
621 | while (iter.hasNext()) {
|
||
622 | sql = (String) iter.next();
|
||
623 | 41485 | jjdelcerro | JDBCHelper.execute(st, sql); |
624 | 40435 | jjdelcerro | } |
625 | } |
||
626 | |||
627 | } catch (SQLException e) { |
||
628 | throw new JDBCExecuteSQLException(sql, e); |
||
629 | } finally {
|
||
630 | try {
|
||
631 | st.close(); |
||
632 | } catch (SQLException e) { |
||
633 | LOG.error("Exception clossing statement", e);
|
||
634 | } |
||
635 | ; |
||
636 | } |
||
637 | |||
638 | return Boolean.TRUE; |
||
639 | } |
||
640 | |||
641 | }; |
||
642 | |||
643 | Boolean result = Boolean.FALSE; |
||
644 | |||
645 | try {
|
||
646 | result = (Boolean) helper.doConnectionAction(action);
|
||
647 | 41437 | jjdelcerro | } catch (DataException e) {
|
648 | throw e;
|
||
649 | 40435 | jjdelcerro | } catch (Exception e) { |
650 | 41437 | jjdelcerro | // FIXME Exception
|
651 | throw new RuntimeException(e); |
||
652 | 40435 | jjdelcerro | } |
653 | |||
654 | return result.booleanValue();
|
||
655 | } |
||
656 | |||
657 | public List getDataStoreProviderNames() { |
||
658 | List x = new ArrayList(1); |
||
659 | x.add(JDBCStoreProvider.NAME); |
||
660 | return x;
|
||
661 | } |
||
662 | 41638 | jjdelcerro | |
663 | public void updateTableStatistics(String tableName) { |
||
664 | |||
665 | } |
||
666 | 40435 | jjdelcerro | |
667 | } |