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