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 @ 42811
History | View | Annotate | Download (21 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 | 41638 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
34 | 40435 | jjdelcerro | |
35 | import org.slf4j.Logger; |
||
36 | import org.slf4j.LoggerFactory; |
||
37 | |||
38 | import org.gvsig.fmap.dal.DALLocator; |
||
39 | import org.gvsig.fmap.dal.DataManager; |
||
40 | import org.gvsig.fmap.dal.DataStore; |
||
41 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
42 | import org.gvsig.fmap.dal.NewDataStoreParameters; |
||
43 | import org.gvsig.fmap.dal.exception.CloseException; |
||
44 | import org.gvsig.fmap.dal.exception.DataException; |
||
45 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
46 | import org.gvsig.fmap.dal.exception.OpenException; |
||
47 | import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
||
48 | import org.gvsig.fmap.dal.exception.ReadException; |
||
49 | import org.gvsig.fmap.dal.exception.RemoveException; |
||
50 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
51 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
52 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
53 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
54 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
55 | import org.gvsig.fmap.dal.serverexplorer.db.spi.AbstractDBServerExplorer; |
||
56 | import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
||
57 | import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
||
58 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
||
59 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
60 | import org.gvsig.tools.exception.BaseException; |
||
61 | |||
62 | /**
|
||
63 | * @author jmvivo
|
||
64 | *
|
||
65 | */
|
||
66 | public class JDBCServerExplorer extends AbstractDBServerExplorer |
||
67 | 42775 | jjdelcerro | implements JDBCHelperUser {
|
68 | 40435 | jjdelcerro | |
69 | 42775 | jjdelcerro | private static final Logger LOG = LoggerFactory |
70 | .getLogger(JDBCServerExplorer.class); |
||
71 | 40435 | jjdelcerro | |
72 | 42775 | jjdelcerro | private static final String METADATA_COLUMN_TABLE_CATALOG = "TABLE_CAT"; |
73 | private static final String METADATA_COLUMN_TABLE_SCHEMA = "TABLE_SCHEM"; |
||
74 | private static final String METADATA_COLUMN_TABLE_NAME = "TABLE_NAME"; |
||
75 | 40435 | jjdelcerro | |
76 | 42775 | jjdelcerro | public static final String NAME = "JDBCServerExplorer"; |
77 | protected JDBCHelper helper;
|
||
78 | 40435 | jjdelcerro | |
79 | 42775 | jjdelcerro | private Boolean canAdd; |
80 | 40435 | jjdelcerro | |
81 | 42775 | jjdelcerro | public JDBCServerExplorer(JDBCServerExplorerParameters parameters,
|
82 | DataServerExplorerProviderServices services) |
||
83 | throws InitializeException {
|
||
84 | super(parameters, services);
|
||
85 | this.helper = createHelper();
|
||
86 | } |
||
87 | 40435 | jjdelcerro | |
88 | 42775 | jjdelcerro | protected JDBCServerExplorerParameters getJDBCParameters() {
|
89 | return (JDBCServerExplorerParameters) getParameters();
|
||
90 | } |
||
91 | 40435 | jjdelcerro | |
92 | 42775 | jjdelcerro | protected JDBCHelper createHelper() throws InitializeException { |
93 | return new JDBCHelper(this, getJDBCParameters()); |
||
94 | } |
||
95 | 40435 | jjdelcerro | |
96 | 42775 | jjdelcerro | protected JDBCHelper getHelper() {
|
97 | return helper;
|
||
98 | } |
||
99 | 40435 | jjdelcerro | |
100 | 42775 | jjdelcerro | public List list() throws DataException { |
101 | return this.list(MODE_ALL); |
||
102 | } |
||
103 | 40435 | jjdelcerro | |
104 | 42775 | jjdelcerro | public List list(boolean showInformationDBTables) throws DataException { |
105 | return this.list(MODE_ALL, showInformationDBTables); |
||
106 | } |
||
107 | 40435 | jjdelcerro | |
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 | 42775 | jjdelcerro | public void remove(DataStoreParameters dsp) throws RemoveException { |
148 | final JDBCStoreParameters dsParams = (JDBCStoreParameters) dsp;
|
||
149 | 40435 | jjdelcerro | |
150 | 42775 | jjdelcerro | TransactionalAction action = new TransactionalAction() {
|
151 | public boolean continueTransactionAllowed() { |
||
152 | return false; |
||
153 | } |
||
154 | 40435 | jjdelcerro | |
155 | 42775 | jjdelcerro | public Object action(Connection conn) throws DataException { |
156 | Statement st;
|
||
157 | try {
|
||
158 | st = conn.createStatement(); |
||
159 | } catch (SQLException e) { |
||
160 | throw new JDBCSQLException(e); |
||
161 | } |
||
162 | 40435 | jjdelcerro | |
163 | 42775 | jjdelcerro | String sqlDrop = "Drop table " |
164 | + dsParams.tableID(); |
||
165 | 40435 | jjdelcerro | |
166 | 42775 | jjdelcerro | try {
|
167 | try {
|
||
168 | JDBCHelper.execute(st, sqlDrop); |
||
169 | } catch (SQLException e) { |
||
170 | throw new JDBCExecuteSQLException(sqlDrop, e); |
||
171 | } |
||
172 | 40435 | jjdelcerro | |
173 | 42775 | jjdelcerro | } finally {
|
174 | try {
|
||
175 | st.close(); |
||
176 | } catch (SQLException e) { |
||
177 | }; |
||
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 | 41486 | jjdelcerro | } |
187 | 42775 | jjdelcerro | } |
188 | 41486 | jjdelcerro | |
189 | 42775 | jjdelcerro | public DataStoreParameters getOpenParameters() throws DataException { |
190 | JDBCServerExplorerParameters parameters = getJDBCParameters(); |
||
191 | 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 | 40435 | jjdelcerro | |
204 | 42775 | jjdelcerro | public NewDataStoreParameters getAddParameters() throws DataException { |
205 | JDBCServerExplorerParameters parameters = getJDBCParameters(); |
||
206 | 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 | 40435 | jjdelcerro | |
217 | 42775 | jjdelcerro | params.setDefaultFeatureType(this.getServerExplorerProviderServices()
|
218 | .createNewFeatureType()); |
||
219 | 40435 | jjdelcerro | |
220 | 42775 | jjdelcerro | return params;
|
221 | } |
||
222 | 40435 | jjdelcerro | |
223 | 42775 | jjdelcerro | public void closeDone() throws DataException { |
224 | // Nothing to do
|
||
225 | } |
||
226 | 40435 | jjdelcerro | |
227 | 42775 | jjdelcerro | public void opendDone() throws DataException { |
228 | // Nothin to do
|
||
229 | 40435 | jjdelcerro | |
230 | 42775 | jjdelcerro | } |
231 | 40435 | jjdelcerro | |
232 | 42775 | jjdelcerro | 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 | 40435 | jjdelcerro | |
242 | 42775 | jjdelcerro | return store;
|
243 | } |
||
244 | 40435 | jjdelcerro | |
245 | 42775 | jjdelcerro | protected void checkIsMine(DataStoreParameters dsp) { |
246 | if (!(dsp instanceof JDBCConnectionParameters)) { |
||
247 | // FIXME Exception ???
|
||
248 | throw new IllegalArgumentException( |
||
249 | "not instance of FilesystemStoreParameters");
|
||
250 | } |
||
251 | |||
252 | 40435 | jjdelcerro | // try {
|
253 | 42775 | jjdelcerro | // dsp.validate();
|
254 | // } catch (ValidateDataParametersException e) {
|
||
255 | // throw new IllegalArgumentException("check parameters", e);
|
||
256 | // }
|
||
257 | JDBCServerExplorerParameters parameters = getJDBCParameters(); |
||
258 | 40435 | jjdelcerro | |
259 | 42775 | jjdelcerro | JDBCConnectionParameters pgp = (JDBCConnectionParameters) dsp; |
260 | if (!compare(pgp.getHost(), parameters.getHost())) {
|
||
261 | throw new IllegalArgumentException("wrong explorer: Host (mine: " |
||
262 | + parameters.getHost() + " other:" + pgp.getHost() + ")"); |
||
263 | } |
||
264 | if (!compare(pgp.getPort(), parameters.getPort())) {
|
||
265 | throw new IllegalArgumentException("wrong explorer: Port (mine: " |
||
266 | + parameters.getPort() + " other:" + pgp.getPort() + ")"); |
||
267 | } |
||
268 | if (!compare(pgp.getDBName(), parameters.getDBName())) {
|
||
269 | throw new IllegalArgumentException("wrong explorer: DBName (mine: " |
||
270 | + parameters.getDBName() + " other:" + pgp.getDBName()
|
||
271 | + ")");
|
||
272 | } |
||
273 | if (parameters.getCatalog() != null && !parameters.getCatalog().trim().equals("")) { |
||
274 | // implicit catalog
|
||
275 | if (!compare(pgp.getCatalog(), parameters.getCatalog())) {
|
||
276 | throw new IllegalArgumentException( |
||
277 | "wrong explorer: Catalog (mine: "
|
||
278 | + parameters.getCatalog() + " other:"
|
||
279 | + pgp.getCatalog() + ")");
|
||
280 | } |
||
281 | } |
||
282 | if (parameters.getSchema() != null && !parameters.getSchema().trim().equals("")) { |
||
283 | // implicit schema
|
||
284 | if (!compare(pgp.getSchema(), parameters.getSchema())) {
|
||
285 | throw new IllegalArgumentException( |
||
286 | "wrong explorer: Schema (mine: "
|
||
287 | + parameters.getSchema() + " other:"
|
||
288 | + pgp.getSchema() + ")");
|
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | 40435 | jjdelcerro | |
293 | 42775 | jjdelcerro | protected boolean compare(Object str1, Object str2) { |
294 | if (str1 == str2) {
|
||
295 | return true; |
||
296 | } |
||
297 | if (str1 == null) { |
||
298 | return false; |
||
299 | } |
||
300 | return str1.equals(str2);
|
||
301 | } |
||
302 | 40435 | jjdelcerro | |
303 | 42775 | jjdelcerro | protected JDBCStoreParameters createStoreParams()
|
304 | throws InitializeException, ProviderNotRegisteredException {
|
||
305 | DataManagerProviderServices manager = this.getManager();
|
||
306 | JDBCServerExplorerParameters parameters = getJDBCParameters(); |
||
307 | JDBCStoreParameters orgParams = (JDBCStoreParameters) manager |
||
308 | .createStoreParameters(getStoreName()); |
||
309 | orgParams.setHost(parameters.getHost()); |
||
310 | orgParams.setPort(parameters.getPort()); |
||
311 | orgParams.setDBName(parameters.getDBName()); |
||
312 | orgParams.setUser(parameters.getUser()); |
||
313 | orgParams.setPassword(parameters.getPassword()); |
||
314 | 40435 | jjdelcerro | orgParams.setCatalog(parameters.getCatalog()); |
315 | orgParams.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
||
316 | orgParams.setSchema(parameters.getSchema()); |
||
317 | orgParams.setUrl(parameters.getUrl()); |
||
318 | 42775 | jjdelcerro | return orgParams;
|
319 | } |
||
320 | 40435 | jjdelcerro | |
321 | 42775 | jjdelcerro | public List list(final int mode, final boolean showInformationDBTables) |
322 | throws DataException {
|
||
323 | 40435 | jjdelcerro | |
324 | 42775 | jjdelcerro | final JDBCStoreParameters orgParams = createStoreParams();
|
325 | 40435 | jjdelcerro | |
326 | 42775 | jjdelcerro | ConnectionAction action = new ConnectionAction() {
|
327 | 40435 | jjdelcerro | |
328 | 42775 | jjdelcerro | public Object action(Connection conn) throws DataException { |
329 | 40435 | jjdelcerro | |
330 | 42775 | jjdelcerro | String[] tableTypes = null; |
331 | if (!showInformationDBTables) {
|
||
332 | tableTypes = new String[]{"TABLE", "VIEW"}; |
||
333 | } |
||
334 | 40435 | jjdelcerro | |
335 | 42775 | jjdelcerro | ResultSet result = null; |
336 | try {
|
||
337 | DatabaseMetaData metadata = conn.getMetaData();
|
||
338 | result = metadata.getTables(null, null, null, |
||
339 | tableTypes); |
||
340 | List<JDBCStoreParameters> paramList = new ArrayList<JDBCStoreParameters>(); |
||
341 | while (result.next()) {
|
||
342 | JDBCStoreParameters params = (JDBCStoreParameters) orgParams |
||
343 | .getCopy(); |
||
344 | params.setCatalog(result |
||
345 | .getString(METADATA_COLUMN_TABLE_CATALOG)); |
||
346 | params.setSchema(result |
||
347 | .getString(METADATA_COLUMN_TABLE_SCHEMA)); |
||
348 | params.setTable(result |
||
349 | .getString(METADATA_COLUMN_TABLE_NAME)); |
||
350 | paramList.add(params); |
||
351 | } |
||
352 | 40435 | jjdelcerro | |
353 | 42775 | jjdelcerro | return paramList;
|
354 | } catch (SQLException e) { |
||
355 | throw new JDBCSQLException(e); |
||
356 | } finally {
|
||
357 | if (result != null) { |
||
358 | try {
|
||
359 | result.close(); |
||
360 | } catch (Exception e) { |
||
361 | LOG.error("Error closing DatabaseMetadata "
|
||
362 | + "getTables() Resultset", e);
|
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | } |
||
367 | 40435 | jjdelcerro | |
368 | 42775 | jjdelcerro | }; |
369 | 40435 | jjdelcerro | |
370 | 42775 | jjdelcerro | try {
|
371 | return (List) helper.doConnectionAction(action); |
||
372 | 42811 | jjdelcerro | } catch(JDBCSQLException e) {
|
373 | throw e;
|
||
374 | 42775 | jjdelcerro | } catch (Exception e) { |
375 | 42811 | jjdelcerro | throw new ReadException(this.getProviderName(),e); |
376 | 42775 | jjdelcerro | } |
377 | } |
||
378 | 40435 | jjdelcerro | |
379 | 42775 | jjdelcerro | public void open() throws OpenException { |
380 | helper.open(); |
||
381 | } |
||
382 | 40435 | jjdelcerro | |
383 | 42775 | jjdelcerro | public void close() throws CloseException { |
384 | helper.close(); |
||
385 | } |
||
386 | 40435 | jjdelcerro | |
387 | 42775 | jjdelcerro | @Override
|
388 | protected void doDispose() throws BaseException { |
||
389 | helper.dispose(); |
||
390 | helper = null;
|
||
391 | } |
||
392 | 40435 | jjdelcerro | |
393 | 42775 | jjdelcerro | public String getProviderName() { |
394 | return NAME;
|
||
395 | } |
||
396 | 40435 | jjdelcerro | |
397 | 42775 | jjdelcerro | public String getStoreName() { |
398 | return JDBCStoreProvider.NAME;
|
||
399 | } |
||
400 | 40435 | jjdelcerro | |
401 | 42775 | jjdelcerro | public boolean canAdd() { |
402 | if (this.canAdd == null) { |
||
403 | ConnectionAction action = new ConnectionAction() {
|
||
404 | 40435 | jjdelcerro | |
405 | 42775 | jjdelcerro | 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 | 40435 | jjdelcerro | |
417 | 42775 | jjdelcerro | }; |
418 | 40435 | jjdelcerro | |
419 | 42775 | jjdelcerro | 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 | 40435 | jjdelcerro | |
429 | 42775 | jjdelcerro | public FeatureType getFeatureType(DataStoreParameters dsp)
|
430 | throws DataException {
|
||
431 | checkIsMine(dsp); |
||
432 | 40435 | jjdelcerro | |
433 | 42775 | jjdelcerro | // TODO: checks geometry columns and driver geometry supports
|
434 | EditableFeatureType edType = this.getServerExplorerProviderServices()
|
||
435 | .createNewFeatureType(); |
||
436 | helper.loadFeatureType(edType, (JDBCStoreParameters) dsp); |
||
437 | 40435 | jjdelcerro | |
438 | 42775 | jjdelcerro | return edType;
|
439 | 40435 | jjdelcerro | |
440 | 42775 | jjdelcerro | } |
441 | 40435 | jjdelcerro | |
442 | 42775 | jjdelcerro | public boolean add(String providerName, NewDataStoreParameters ndsp, boolean overwrite) |
443 | throws DataException {
|
||
444 | 40435 | jjdelcerro | |
445 | 42775 | jjdelcerro | /**
|
446 | * CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
|
||
447 | * { column_name data_type [ DEFAULT default_expr ] [ column_constraint
|
||
448 | * [ ... ] ] | table_constraint | LIKE parent_table [ { INCLUDING |
|
||
449 | * EXCLUDING } DEFAULTS ] } [, ... ] ) [ INHERITS ( parent_table [, ...
|
||
450 | * ] ) ] [ WITH OIDS | WITHOUT OIDS ] [ ON COMMIT { PRESERVE ROWS |
|
||
451 | * DELETE ROWS | DROP } ]
|
||
452 | *
|
||
453 | * where column_constraint is:
|
||
454 | *
|
||
455 | * [ CONSTRAINT constraint_name ] { NOT NULL | NULL | UNIQUE | PRIMARY
|
||
456 | * KEY | CHECK (expression) | REFERENCES reftable [ ( refcolumn ) ] [
|
||
457 | * MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON
|
||
458 | * UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY
|
||
459 | * DEFERRED | INITIALLY IMMEDIATE ]
|
||
460 | *
|
||
461 | * and table_constraint is:
|
||
462 | *
|
||
463 | * [ CONSTRAINT constraint_name ] { UNIQUE ( column_name [, ... ] ) |
|
||
464 | * PRIMARY KEY ( column_name [, ... ] ) | CHECK ( expression ) | FOREIGN
|
||
465 | * KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ...
|
||
466 | * ] ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE
|
||
467 | * action ] [ ON UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [
|
||
468 | * INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
|
||
469 | */
|
||
470 | if (!(ndsp instanceof JDBCNewStoreParameters)) { |
||
471 | // FIXME exception
|
||
472 | throw new IllegalArgumentException(); |
||
473 | } |
||
474 | checkIsMine(ndsp); |
||
475 | 40435 | jjdelcerro | |
476 | 42775 | jjdelcerro | JDBCNewStoreParameters jdbcnsp = (JDBCNewStoreParameters) ndsp; |
477 | 40435 | jjdelcerro | |
478 | 42775 | jjdelcerro | StringBuilder sql = new StringBuilder(); |
479 | 40435 | jjdelcerro | |
480 | 42775 | jjdelcerro | if (!jdbcnsp.isValid()) {
|
481 | // TODO Exception
|
||
482 | throw new InitializeException(this.getProviderName(), new Exception( |
||
483 | "Parameters not valid"));
|
||
484 | } |
||
485 | try {
|
||
486 | jdbcnsp.validate(); |
||
487 | } catch (ValidateDataParametersException e1) {
|
||
488 | throw new InitializeException(this.getProviderName(), e1); |
||
489 | } |
||
490 | 40435 | jjdelcerro | |
491 | 42775 | jjdelcerro | FeatureType fType = jdbcnsp.getDefaultFeatureType(); |
492 | 40435 | jjdelcerro | |
493 | 42775 | jjdelcerro | sql.append("Create table " + jdbcnsp.tableID()
|
494 | + "(");
|
||
495 | Iterator attrs = fType.iterator();
|
||
496 | String sqlAttr;
|
||
497 | List sqlAttrs = new ArrayList(); |
||
498 | 40435 | jjdelcerro | |
499 | 42775 | jjdelcerro | while (attrs.hasNext()) {
|
500 | sqlAttr = helper |
||
501 | .getSqlFieldDescription((FeatureAttributeDescriptor) attrs |
||
502 | .next()); |
||
503 | if (sqlAttr != null) { |
||
504 | sqlAttrs.add(sqlAttr); |
||
505 | } |
||
506 | } |
||
507 | 40435 | jjdelcerro | |
508 | 42775 | jjdelcerro | helper.stringJoin(sqlAttrs, ", ", sql);
|
509 | 40435 | jjdelcerro | |
510 | 42775 | jjdelcerro | sql.append(")");
|
511 | 40435 | jjdelcerro | |
512 | 42775 | jjdelcerro | final String sqlCreate = sql.toString(); |
513 | final List sqlAdditional = helper.getAdditionalSqlToCreate(ndsp, fType); |
||
514 | 40435 | jjdelcerro | |
515 | 42775 | jjdelcerro | List permissions = this.getHelper().createGrantStatements((JDBCNewStoreParameters) ndsp); |
516 | if (permissions != null) { |
||
517 | sqlAdditional.addAll(permissions); |
||
518 | } |
||
519 | 40435 | jjdelcerro | |
520 | 42775 | jjdelcerro | if (((JDBCNewStoreParameters) ndsp).getPostCreatingStatement() != null) { |
521 | sqlAdditional.add(((JDBCNewStoreParameters) ndsp).getPostCreatingStatement()); |
||
522 | } |
||
523 | |||
524 | TransactionalAction action = new TransactionalAction() {
|
||
525 | |||
526 | public boolean continueTransactionAllowed() { |
||
527 | // TODO Auto-generated method stub
|
||
528 | return false; |
||
529 | } |
||
530 | |||
531 | public Object action(Connection conn) throws DataException { |
||
532 | Statement st = null; |
||
533 | |||
534 | try {
|
||
535 | st = conn.createStatement(); |
||
536 | } catch (SQLException e1) { |
||
537 | throw new JDBCSQLException(e1); |
||
538 | 41638 | jjdelcerro | } |
539 | 42775 | jjdelcerro | String sql = null; |
540 | 40435 | jjdelcerro | |
541 | 42775 | jjdelcerro | try {
|
542 | sql = sqlCreate; |
||
543 | JDBCHelper.execute(st, sql); |
||
544 | if (sqlAdditional != null) { |
||
545 | Iterator iter = sqlAdditional.iterator();
|
||
546 | while (iter.hasNext()) {
|
||
547 | sql = (String) iter.next();
|
||
548 | JDBCHelper.execute(st, sql); |
||
549 | } |
||
550 | } |
||
551 | 40435 | jjdelcerro | |
552 | 42775 | jjdelcerro | } catch (SQLException e) { |
553 | throw new JDBCExecuteSQLException(sql, e); |
||
554 | } finally {
|
||
555 | try {
|
||
556 | st.close(); |
||
557 | } catch (SQLException e) { |
||
558 | LOG.error("Exception clossing statement", e);
|
||
559 | } |
||
560 | ; |
||
561 | } |
||
562 | 40435 | jjdelcerro | |
563 | 42775 | jjdelcerro | return Boolean.TRUE; |
564 | } |
||
565 | 40435 | jjdelcerro | |
566 | 42775 | jjdelcerro | }; |
567 | 40435 | jjdelcerro | |
568 | 42775 | jjdelcerro | Boolean result = Boolean.FALSE; |
569 | 40435 | jjdelcerro | |
570 | 42775 | jjdelcerro | try {
|
571 | result = (Boolean) helper.doConnectionAction(action);
|
||
572 | } catch (DataException e) {
|
||
573 | throw e;
|
||
574 | } catch (Exception e) { |
||
575 | // FIXME Exception
|
||
576 | throw new RuntimeException(e); |
||
577 | } |
||
578 | 40435 | jjdelcerro | |
579 | 42775 | jjdelcerro | return result.booleanValue();
|
580 | } |
||
581 | 40435 | jjdelcerro | |
582 | 42775 | jjdelcerro | public List getDataStoreProviderNames() { |
583 | List x = new ArrayList(1); |
||
584 | x.add(JDBCStoreProvider.NAME); |
||
585 | return x;
|
||
586 | } |
||
587 | 40435 | jjdelcerro | |
588 | 42775 | jjdelcerro | public void updateTableStatistics(String tableName) throws JDBCExecuteSQLException { |
589 | 40435 | jjdelcerro | |
590 | 42775 | jjdelcerro | } |
591 | 40435 | jjdelcerro | |
592 | 42775 | jjdelcerro | @Override
|
593 | public DataStoreParameters get(String name) throws DataException { |
||
594 | JDBCStoreParameters params = this.createStoreParams();
|
||
595 | params.setTable(name); |
||
596 | return params;
|
||
597 | } |
||
598 | 40435 | jjdelcerro | } |