svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.h2spatial / org.gvsig.h2spatial.h2gis132 / org.gvsig.h2spatial.h2gis132.provider / src / main / java / org / gvsig / fmap / dal / store / h2 / H2SpatialHelper.java @ 47716
History | View | Annotate | Download (21.2 KB)
1 | 45472 | jjdelcerro | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2020 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 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 | */
|
||
22 | package org.gvsig.fmap.dal.store.h2; |
||
23 | |||
24 | import java.io.File; |
||
25 | import java.sql.Connection; |
||
26 | 45742 | fdiaz | import java.sql.DriverManager; |
27 | 45472 | jjdelcerro | import java.sql.SQLException; |
28 | import org.apache.commons.dbcp.BasicDataSource; |
||
29 | import org.apache.commons.lang3.StringUtils; |
||
30 | import org.gvsig.expressionevaluator.GeometryExpressionBuilderHelper.GeometrySupportType; |
||
31 | 45488 | fdiaz | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_CONFIGURATION_NAME; |
32 | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_CONFIGURATION_VALUE; |
||
33 | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_RESOURCES_NAME; |
||
34 | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_RESOURCES_RESOURCE; |
||
35 | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_CONFIGURATION_NAME; |
||
36 | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES_NAME; |
||
37 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.exception.InitializeException; |
38 | import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
||
39 | import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
||
40 | 46315 | jjdelcerro | import org.gvsig.fmap.dal.spi.DataTransactionServices; |
41 | 45472 | jjdelcerro | import static org.gvsig.fmap.dal.store.h2.H2SpatialHelper.LOGGER; |
42 | 46047 | jjdelcerro | import org.gvsig.fmap.dal.store.h2.functions.Json_value; |
43 | import org.gvsig.fmap.dal.store.h2.functions.Reverse; |
||
44 | import org.gvsig.fmap.dal.store.h2.functions.Reverseinstr; |
||
45 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.store.h2.operations.H2SpatialOperationsFactory; |
46 | import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
||
47 | import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
||
48 | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
||
49 | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
||
50 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
||
51 | 46315 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
52 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
53 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
54 | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
||
55 | 46361 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.spi.AbstractConnectionProvider; |
56 | 45488 | fdiaz | import org.gvsig.fmap.dal.store.jdbc2.spi.ConnectionProvider; |
57 | 46315 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCConnectionBase; |
58 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase; |
59 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
60 | import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverDumb; |
||
61 | 45488 | fdiaz | import org.h2gis.functions.factory.H2GISFunctions; |
62 | 45472 | jjdelcerro | import org.slf4j.Logger; |
63 | import org.slf4j.LoggerFactory; |
||
64 | |||
65 | |||
66 | @SuppressWarnings("UseSpecificCatch") |
||
67 | public class H2SpatialHelper extends JDBCHelperBase { |
||
68 | |||
69 | 47456 | jjdelcerro | @SuppressWarnings("FieldNameHidesFieldInSuperclass") |
70 | protected static final Logger LOGGER = LoggerFactory.getLogger(H2SpatialHelper.class); |
||
71 | 45472 | jjdelcerro | |
72 | public static final String H2SPATIAL_JDBC_DRIVER = "org.h2.Driver"; |
||
73 | |||
74 | 47716 | jjdelcerro | public static class H2SpatialConnectionProvider extends AbstractConnectionProvider implements ConnectionProvider { |
75 | |||
76 | 45472 | jjdelcerro | private static boolean needRegisterDriver = true; |
77 | |||
78 | private BasicDataSource dataSource = null; |
||
79 | |||
80 | 45694 | fdiaz | private H2SpatialConnectionParameters connectionParameters;
|
81 | 45472 | jjdelcerro | |
82 | 47716 | jjdelcerro | public H2SpatialConnectionProvider(H2SpatialConnectionParameters connectionParameters) {
|
83 | 45472 | jjdelcerro | this.connectionParameters = connectionParameters;
|
84 | } |
||
85 | |||
86 | 47716 | jjdelcerro | public H2SpatialConnectionParameters getConnectionParameters() {
|
87 | return this.connectionParameters; |
||
88 | } |
||
89 | |||
90 | 45472 | jjdelcerro | @Override
|
91 | public String getStatus() { |
||
92 | 45742 | fdiaz | if(dataSource == null) { |
93 | return "Not using pool."; |
||
94 | } |
||
95 | 45472 | jjdelcerro | StringBuilder builder = new StringBuilder(); |
96 | builder.append("Pool: ");
|
||
97 | builder.append(JDBCUtils.getHexId(dataSource)); |
||
98 | builder.append(" Actives: ");
|
||
99 | builder.append(dataSource.getNumActive()); |
||
100 | builder.append("/");
|
||
101 | builder.append(dataSource.getMaxActive()); |
||
102 | builder.append(" idle: ");
|
||
103 | builder.append(dataSource.getNumIdle()); |
||
104 | builder.append("/");
|
||
105 | builder.append(dataSource.getMinIdle()); |
||
106 | builder.append(":");
|
||
107 | builder.append(dataSource.getMaxIdle()); |
||
108 | return builder.toString();
|
||
109 | } |
||
110 | |||
111 | @SuppressWarnings("ConvertToTryWithResources") |
||
112 | public void shutdown() { |
||
113 | LOGGER.info("Shutdown H2 connection.");
|
||
114 | try {
|
||
115 | Connection conn = this.getConnection(); |
||
116 | 46200 | jjdelcerro | conn.createStatement().execute("CHECKPOINT SYNC");
|
117 | 46230 | jjdelcerro | conn.createStatement().execute("ANALYZE");
|
118 | conn.createStatement().execute("SHUTDOWN COMPACT");
|
||
119 | 45472 | jjdelcerro | conn.close(); |
120 | 46230 | jjdelcerro | LOGGER.info("Shutdown completed.");
|
121 | 45472 | jjdelcerro | } catch (Throwable th) { |
122 | LOGGER.warn("Problems shutdown the database.", th);
|
||
123 | } |
||
124 | 45742 | fdiaz | closeDataSource(); |
125 | 45472 | jjdelcerro | } |
126 | |||
127 | @Override
|
||
128 | public String toString() { |
||
129 | StringBuilder builder = new StringBuilder(); |
||
130 | builder.append(" url=").append(connectionParameters.getUrl());
|
||
131 | builder.append(" driver name=").append(connectionParameters.getJDBCDriverClassName());
|
||
132 | builder.append(" user=").append(connectionParameters.getUser());
|
||
133 | return builder.toString();
|
||
134 | } |
||
135 | |||
136 | @Override
|
||
137 | public synchronized Connection getConnection() throws SQLException { |
||
138 | 45901 | jjdelcerro | File f = H2SpatialUtils.getLocalFile(connectionParameters);
|
139 | 46252 | jjdelcerro | boolean newdb = f!=null && !f.exists(); |
140 | 45472 | jjdelcerro | |
141 | Connection conn;
|
||
142 | 45742 | fdiaz | |
143 | 45472 | jjdelcerro | try {
|
144 | 45742 | fdiaz | conn = DriverManager.getConnection(
|
145 | connectionParameters.getUrl(), |
||
146 | connectionParameters.getUser(), |
||
147 | connectionParameters.getPassword() |
||
148 | ); |
||
149 | |||
150 | 45472 | jjdelcerro | } catch(Throwable th) { |
151 | throw th;
|
||
152 | } |
||
153 | 45742 | fdiaz | |
154 | 45901 | jjdelcerro | H2SpatialUtils.server_start( |
155 | this.connectionParameters.getServerPortAsString(),
|
||
156 | this.connectionParameters.getServerAllowOthers()
|
||
157 | ); |
||
158 | 45742 | fdiaz | |
159 | // if (this.dataSource == null) {
|
||
160 | // this.dataSource = this.createDataSource();
|
||
161 | // }
|
||
162 | //
|
||
163 | // try {
|
||
164 | // conn = this.dataSource.getConnection();
|
||
165 | // } catch(Throwable th) {
|
||
166 | // LOGGER.warn("Can't create connection to '"+this.dataSource.getUrl()+"'. "+this.getStatus());
|
||
167 | // LOGGER.warn("Can't create connection to '"+this.dataSource.getUrl()+"'.",th);
|
||
168 | // throw th;
|
||
169 | // }
|
||
170 | |||
171 | |||
172 | 45472 | jjdelcerro | try {
|
173 | conn.createStatement().execute("SELECT TOP 1 SRID FROM SPATIAL_REF_SYS");
|
||
174 | } catch(SQLException ex) { |
||
175 | H2GISFunctions.load(conn); |
||
176 | } |
||
177 | 46047 | jjdelcerro | |
178 | String[] sqls = new String[] { |
||
179 | "CREATE SCHEMA IF NOT EXISTS PUBLIC;SET SCHEMA PUBLIC",
|
||
180 | 46050 | omartinez | Json_value.getSQL(), |
181 | Reverse.getSQL(), |
||
182 | Reverseinstr.getSQL() |
||
183 | 46047 | jjdelcerro | }; |
184 | for (String sql : sqls) { |
||
185 | try {
|
||
186 | conn.createStatement().execute(sql); |
||
187 | } catch(SQLException ex) { |
||
188 | LOGGER.debug("Can't configure gvsig tables.",ex);
|
||
189 | LOGGER.warn("Can't configure gvsig tables. "+sql);
|
||
190 | // Ignore this error.
|
||
191 | } |
||
192 | 45472 | jjdelcerro | } |
193 | 45488 | fdiaz | |
194 | 45472 | jjdelcerro | if( newdb ) {
|
195 | 46047 | jjdelcerro | String[] sqls2 = new String[] { |
196 | 45488 | fdiaz | "CREATE CACHED TABLE PUBLIC.\""+TABLE_RESOURCES_NAME+"\"(\""+FIELD_RESOURCES_NAME+"\" VARCHAR(150) NOT NULL, \""+FIELD_RESOURCES_RESOURCE+"\" BLOB DEFAULT NULL)", |
197 | "ALTER TABLE PUBLIC.\""+TABLE_RESOURCES_NAME+"\" ADD CONSTRAINT PUBLIC.CONSTRAINT_E PRIMARY KEY(\""+FIELD_RESOURCES_NAME+"\")", |
||
198 | 45767 | fdiaz | "CREATE CACHED TABLE PUBLIC.\""+TABLE_CONFIGURATION_NAME+"\"(\""+FIELD_CONFIGURATION_NAME+"\" VARCHAR(200) NOT NULL, \""+FIELD_CONFIGURATION_VALUE+"\" CLOB DEFAULT NULL)", |
199 | 46047 | jjdelcerro | "ALTER TABLE PUBLIC.\""+TABLE_CONFIGURATION_NAME+"\" ADD CONSTRAINT PUBLIC.CONSTRAINT_2 PRIMARY KEY(\""+FIELD_CONFIGURATION_NAME+"\")", |
200 | 45472 | jjdelcerro | }; |
201 | 46047 | jjdelcerro | for (String sql : sqls2) { |
202 | 45472 | jjdelcerro | try {
|
203 | conn.createStatement().execute(sql); |
||
204 | } catch(SQLException ex) { |
||
205 | LOGGER.debug("Can't configure gvsig tables.",ex);
|
||
206 | LOGGER.warn("Can't configure gvsig tables. "+sql);
|
||
207 | // Ignore this error.
|
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | return conn;
|
||
212 | } |
||
213 | |||
214 | private BasicDataSource createDataSource() throws SQLException { |
||
215 | if (!this.isRegistered()) { |
||
216 | this.registerDriver();
|
||
217 | } |
||
218 | H2SpatialConnectionParameters params = connectionParameters; |
||
219 | |||
220 | BasicDataSource ds = new BasicDataSource();
|
||
221 | ds.setDriverClassName(params.getJDBCDriverClassName()); |
||
222 | if( !StringUtils.isEmpty(params.getUser()) ) {
|
||
223 | ds.setUsername(params.getUser()); |
||
224 | } |
||
225 | if( !StringUtils.isEmpty(params.getPassword()) ) {
|
||
226 | ds.setPassword(params.getPassword()); |
||
227 | } |
||
228 | ds.setUrl(params.getUrl()); |
||
229 | |||
230 | ds.setMaxWait(60L * 1000); |
||
231 | |||
232 | //
|
||
233 | // Ajustamos el pool para que las conexiones se cierren a los
|
||
234 | // 10 segundos, asi tratamos de que al salir de gvSIG no queden
|
||
235 | // conexiones abiertas con la BBDD y pueda quedar corrupta esta.
|
||
236 | // Hay que tener en cuenta que es una BBDD embebida, y mientras
|
||
237 | // hayan conexiones abiertas pueden quedar cosas por bajar a disco.
|
||
238 | //
|
||
239 | int sidle = this.connectionParameters.getMaxSecondsIdle(); |
||
240 | if( sidle < 0 ) { |
||
241 | ds.setTimeBetweenEvictionRunsMillis(-1);
|
||
242 | ds.setMinEvictableIdleTimeMillis(30*1000); |
||
243 | } else {
|
||
244 | // Revisamos las conexiones inactivas cada 10 segundos
|
||
245 | ds.setTimeBetweenEvictionRunsMillis(sidle*1000);
|
||
246 | // Eliminadmos las conexiones que lleven inactivas mas de 10 segundos.
|
||
247 | ds.setMinEvictableIdleTimeMillis(sidle*1000);
|
||
248 | } |
||
249 | |||
250 | // Ajustamos el numero minimo de conexiones a 0 para permitir
|
||
251 | // que se lleguen a cerrar todas las conexiones del pool.
|
||
252 | ds.setMinIdle(0);
|
||
253 | // dejaremos el MaxIdle a 20, no parece importante. .
|
||
254 | ds.setMaxIdle(20);
|
||
255 | |||
256 | return ds;
|
||
257 | } |
||
258 | |||
259 | private boolean isRegistered() { |
||
260 | return needRegisterDriver;
|
||
261 | } |
||
262 | |||
263 | @Override
|
||
264 | public void registerDriver() throws SQLException { |
||
265 | String className = this.connectionParameters.getJDBCDriverClassName(); |
||
266 | if (className == null) { |
||
267 | return;
|
||
268 | } |
||
269 | try {
|
||
270 | Class theClass = Class.forName(className); |
||
271 | if (theClass == null) { |
||
272 | throw new JDBCDriverClassNotFoundException(H2SpatialLibrary.NAME, className); |
||
273 | } |
||
274 | } catch (Exception e) { |
||
275 | throw new SQLException("Can't register JDBC driver '" + className + "'.", e); |
||
276 | } |
||
277 | needRegisterDriver = false;
|
||
278 | } |
||
279 | |||
280 | 45694 | fdiaz | @Override
|
281 | public void dispose() { |
||
282 | 45742 | fdiaz | closeDataSource(); |
283 | this.connectionParameters = null; |
||
284 | } |
||
285 | |||
286 | private void closeDataSource() { |
||
287 | try {
|
||
288 | if( dataSource!=null ) { |
||
289 | LOGGER.info("Clossing connection pool.");
|
||
290 | LOGGER.info(this.getStatus());
|
||
291 | dataSource.close(); |
||
292 | dataSource = null;
|
||
293 | LOGGER.info("Connection pool closed.");
|
||
294 | LOGGER.info(this.getStatus());
|
||
295 | 45694 | fdiaz | } |
296 | 45742 | fdiaz | } catch (Throwable th) { |
297 | LOGGER.warn("Problems closing connections pool.", th);
|
||
298 | 45694 | fdiaz | } |
299 | 45742 | fdiaz | |
300 | 45694 | fdiaz | } |
301 | |||
302 | 46361 | jjdelcerro | @Override
|
303 | public boolean isDisposed() { |
||
304 | return this.connectionParameters == null; |
||
305 | } |
||
306 | 45472 | jjdelcerro | } |
307 | |||
308 | private ConnectionProvider connectionProvider = null; |
||
309 | |||
310 | /**
|
||
311 | * Constructor for use only for testing purposes.
|
||
312 | *
|
||
313 | * @param connectionParameters
|
||
314 | * @param connectionProvider
|
||
315 | */
|
||
316 | public H2SpatialHelper(JDBCConnectionParameters connectionParameters, ConnectionProvider connectionProvider) {
|
||
317 | super(connectionParameters);
|
||
318 | this.srssolver = new SRSSolverDumb(this); |
||
319 | this.connectionProvider = connectionProvider;
|
||
320 | } |
||
321 | |||
322 | public H2SpatialHelper(JDBCConnectionParameters connectionParameters) {
|
||
323 | super(connectionParameters);
|
||
324 | 46354 | jjdelcerro | // this.srssolver = new SRSSolverBase(this);
|
325 | this.srssolver = new SRSSolverDumb(this); //Tratando de reducir accesos a la BBDD ? Ponerlo en los parametros ? |
||
326 | 45472 | jjdelcerro | } |
327 | |||
328 | public void shutdown() { |
||
329 | try {
|
||
330 | 46230 | jjdelcerro | try {
|
331 | this.getConnection();
|
||
332 | } catch(Throwable th) { |
||
333 | LOGGER.debug("Can't get connection-provider",th);
|
||
334 | } |
||
335 | 45472 | jjdelcerro | if( this.connectionProvider!=null ) { |
336 | 47716 | jjdelcerro | ((H2SpatialConnectionProvider) this.connectionProvider).shutdown();
|
337 | 45472 | jjdelcerro | this.connectionProvider = null; |
338 | } |
||
339 | 45901 | jjdelcerro | H2SpatialUtils.server_stop(); |
340 | 45472 | jjdelcerro | } catch (Throwable ex) { |
341 | LOGGER.warn("Problems shutdown H2", ex);
|
||
342 | } |
||
343 | } |
||
344 | |||
345 | private void logConnectionStatus(String msg, Connection conn) { |
||
346 | ConnectionProvider cp = this.getConnectionProvider();
|
||
347 | StringBuilder builder = new StringBuilder(); |
||
348 | builder.append(msg); |
||
349 | if( conn == null ) { |
||
350 | builder.append(": connection null");
|
||
351 | } else {
|
||
352 | Boolean closed = null; |
||
353 | try {
|
||
354 | closed = conn.isClosed(); |
||
355 | } catch(Throwable th) { |
||
356 | } |
||
357 | builder.append(": connection ");
|
||
358 | builder.append(JDBCUtils.getConnId(conn)); |
||
359 | if( closed ) {
|
||
360 | builder.append(" (c)");
|
||
361 | } |
||
362 | builder.append(" ");
|
||
363 | } |
||
364 | builder.append(cp.getStatus()); |
||
365 | LOGGER.info(builder.toString()); |
||
366 | } |
||
367 | |||
368 | 47456 | jjdelcerro | @Override
|
369 | 46361 | jjdelcerro | public ConnectionProvider getConnectionProvider() {
|
370 | 45472 | jjdelcerro | if (this.connectionProvider == null) { |
371 | H2SpatialConnectionParameters connectionParameters = this.getConnectionParameters();
|
||
372 | if( connectionParameters==null ) { |
||
373 | return null; // Testing mode? |
||
374 | } |
||
375 | 47716 | jjdelcerro | this.connectionProvider = new H2SpatialConnectionProvider(connectionParameters); |
376 | 45472 | jjdelcerro | } |
377 | return this.connectionProvider; |
||
378 | } |
||
379 | |||
380 | @Override
|
||
381 | 46315 | jjdelcerro | public synchronized JDBCConnection getConnection() throws AccessResourceException { |
382 | 45472 | jjdelcerro | try {
|
383 | 46315 | jjdelcerro | H2SpatialConnectionParameters connectionParameters = this.getConnectionParameters();
|
384 | 47108 | jjdelcerro | JDBCConnection conn = (JDBCConnection) DataTransactionServices.getConnection( |
385 | 47611 | fdiaz | this.getTransaction(),
|
386 | 47108 | jjdelcerro | this.getConnectionProviderKey(connectionParameters)
|
387 | ); |
||
388 | 46315 | jjdelcerro | if( conn != null ) { |
389 | return conn;
|
||
390 | } |
||
391 | 45472 | jjdelcerro | if (this.connectionProvider == null) { |
392 | if( connectionParameters==null ) { |
||
393 | return null; // Testing mode? |
||
394 | } |
||
395 | 47716 | jjdelcerro | this.connectionProvider = new H2SpatialConnectionProvider(connectionParameters); |
396 | H2SpatialUtils.addGlobalConnection((H2SpatialConnectionProvider) this.connectionProvider);
|
||
397 | 45472 | jjdelcerro | } |
398 | 46315 | jjdelcerro | JDBCConnection connection = new JDBCConnectionBase(
|
399 | 47611 | fdiaz | this.getTransaction(),
|
400 | 46315 | jjdelcerro | this.connectionProvider.getConnection(),
|
401 | 47108 | jjdelcerro | this.getConnectionProviderKey(connectionParameters)
|
402 | 46315 | jjdelcerro | ); |
403 | 45472 | jjdelcerro | if( LOGGER.isDebugEnabled() ) {
|
404 | 46315 | jjdelcerro | LOGGER.debug("["+JDBCUtils.getConnId(connection.get())+"] getConnection "+connectionProvider.getStatus()+" "+ connectionProvider.toString()); |
405 | 45472 | jjdelcerro | } |
406 | return connection;
|
||
407 | } catch (SQLException ex) { |
||
408 | throw new AccessResourceException(H2SpatialLibrary.NAME, ex); |
||
409 | } |
||
410 | } |
||
411 | |||
412 | @Override
|
||
413 | public H2SpatialConnectionParameters getConnectionParameters() {
|
||
414 | return (H2SpatialConnectionParameters) super.getConnectionParameters(); |
||
415 | } |
||
416 | |||
417 | @Override
|
||
418 | public String getConnectionURL() { |
||
419 | 45901 | jjdelcerro | return H2SpatialUtils.getConnectionURL(this.getConnectionParameters()); |
420 | 45472 | jjdelcerro | } |
421 | |||
422 | @Override
|
||
423 | protected String getResourceType() { |
||
424 | return H2SpatialLibrary.NAME;
|
||
425 | } |
||
426 | |||
427 | @Override
|
||
428 | public String getProviderName() { |
||
429 | return H2SpatialLibrary.NAME;
|
||
430 | } |
||
431 | |||
432 | @Override
|
||
433 | public JDBCSQLBuilderBase createSQLBuilder() {
|
||
434 | return new H2SpatialSQLBuilder(this); |
||
435 | } |
||
436 | |||
437 | @Override
|
||
438 | public OperationsFactory getOperations() {
|
||
439 | if (this.operationsFactory == null) { |
||
440 | this.operationsFactory = new H2SpatialOperationsFactory(this); |
||
441 | } |
||
442 | return operationsFactory;
|
||
443 | } |
||
444 | |||
445 | @Override
|
||
446 | public GeometrySupportType getGeometrySupportType() {
|
||
447 | return GeometrySupportType.WKB;
|
||
448 | } |
||
449 | |||
450 | @Override
|
||
451 | public boolean hasSpatialFunctions() { |
||
452 | return true; |
||
453 | } |
||
454 | |||
455 | @Override
|
||
456 | public boolean canWriteGeometry(int geometryType, int geometrySubtype) { |
||
457 | return true; |
||
458 | } |
||
459 | |||
460 | @Override
|
||
461 | public String getQuoteForIdentifiers() { |
||
462 | return "\""; |
||
463 | } |
||
464 | |||
465 | @Override
|
||
466 | public boolean allowAutomaticValues() { |
||
467 | return true; |
||
468 | } |
||
469 | |||
470 | @Override
|
||
471 | public boolean supportOffsetInSelect() { |
||
472 | return true; |
||
473 | } |
||
474 | |||
475 | @Override
|
||
476 | public String getQuoteForStrings() { |
||
477 | return "'"; |
||
478 | } |
||
479 | |||
480 | @Override
|
||
481 | public String getSourceId(JDBCStoreParameters parameters) { |
||
482 | 46942 | fdiaz | H2SpatialStoreParameters params = (H2SpatialStoreParameters) parameters; |
483 | 45499 | jjdelcerro | StringBuilder builder = new StringBuilder(); |
484 | 46942 | fdiaz | builder.append(params.getTable()); |
485 | 45499 | jjdelcerro | builder.append("(");
|
486 | 46942 | fdiaz | boolean needComma = false; |
487 | if( StringUtils.isNotBlank(params.getHost()) ) {
|
||
488 | builder.append("host=");
|
||
489 | builder.append(params.getHost()); |
||
490 | needComma = true;
|
||
491 | 45499 | jjdelcerro | } |
492 | 46942 | fdiaz | if( params.getPort()>0 ) { |
493 | if (needComma ) {
|
||
494 | builder.append(", ");
|
||
495 | } |
||
496 | builder.append("port=");
|
||
497 | builder.append(params.getPort()); |
||
498 | needComma = true;
|
||
499 | 45499 | jjdelcerro | } |
500 | 46942 | fdiaz | if( StringUtils.isNotBlank(params.getDBName()) ) {
|
501 | if (needComma ) {
|
||
502 | builder.append(", ");
|
||
503 | } |
||
504 | builder.append("db=");
|
||
505 | builder.append(params.getDBName()); |
||
506 | needComma = true;
|
||
507 | 45499 | jjdelcerro | } |
508 | 46942 | fdiaz | if( StringUtils.isNotBlank(params.getSchema()) ) {
|
509 | if (needComma ) {
|
||
510 | builder.append(", ");
|
||
511 | } |
||
512 | builder.append("schema=");
|
||
513 | builder.append(params.getSchema()); |
||
514 | needComma = true;
|
||
515 | } |
||
516 | File f = params.getFile();
|
||
517 | if( f != null) { |
||
518 | if (needComma ) {
|
||
519 | builder.append(", ");
|
||
520 | } |
||
521 | builder.append("file=");
|
||
522 | builder.append(params.getFile().getAbsolutePath()); |
||
523 | } |
||
524 | 45499 | jjdelcerro | builder.append(")");
|
525 | return builder.toString();
|
||
526 | 46942 | fdiaz | |
527 | 45472 | jjdelcerro | } |
528 | 46942 | fdiaz | |
529 | |||
530 | |||
531 | 45472 | jjdelcerro | |
532 | @Override
|
||
533 | public JDBCNewStoreParameters createNewStoreParameters() {
|
||
534 | return new H2SpatialNewStoreParameters(); |
||
535 | } |
||
536 | |||
537 | @Override
|
||
538 | public JDBCStoreParameters createOpenStoreParameters() {
|
||
539 | return new H2SpatialStoreParameters(); |
||
540 | } |
||
541 | |||
542 | @Override
|
||
543 | public JDBCServerExplorerParameters createServerExplorerParameters() {
|
||
544 | return new H2SpatialExplorerParameters(); |
||
545 | } |
||
546 | |||
547 | @Override
|
||
548 | public JDBCServerExplorer createServerExplorer(
|
||
549 | JDBCServerExplorerParameters parameters, |
||
550 | DataServerExplorerProviderServices providerServices |
||
551 | ) throws InitializeException {
|
||
552 | |||
553 | JDBCServerExplorer explorer = new H2SpatialExplorer(
|
||
554 | parameters, |
||
555 | providerServices, |
||
556 | this
|
||
557 | ); |
||
558 | this.initialize(explorer, parameters, null); |
||
559 | return explorer;
|
||
560 | } |
||
561 | |||
562 | 47456 | jjdelcerro | @Override
|
563 | 45742 | fdiaz | public String getConnectionProviderStatus(){ |
564 | return this.getConnectionProvider().getStatus(); |
||
565 | } |
||
566 | 46338 | fdiaz | |
567 | @Override
|
||
568 | public JDBCStoreParameters createOpenStoreParameters(JDBCServerExplorerParameters parameters) {
|
||
569 | JDBCStoreParameters p = super.createOpenStoreParameters(parameters);
|
||
570 | if(StringUtils.isBlank(p.getCatalog())){
|
||
571 | p.setCatalog(StringUtils.upperCase(p.getDBName())); |
||
572 | } |
||
573 | return p;
|
||
574 | } |
||
575 | 45472 | jjdelcerro | } |