gvsig-sqlite / trunk / org.gvsig.spatialite / org.gvsig.spatialite.provider / src / main / java / org / gvsig / spatialite / dal / SpatiaLiteHelper.java @ 196
History | View | Annotate | Download (14.8 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2016 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.spatialite.dal; |
23 |
|
24 |
import org.gvsig.spatialite.dal.operations.SpatiaLiteOperationsFactory; |
25 |
import java.sql.Connection; |
26 |
import org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType; |
27 |
import org.gvsig.fmap.dal.exception.InitializeException; |
28 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
29 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
30 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
31 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
32 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
33 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
34 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
35 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
36 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCStoreProvider; |
37 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
38 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase; |
39 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
40 |
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverBase; |
41 |
import org.gvsig.fmap.geom.Geometry; |
42 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
43 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
44 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
45 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
46 |
import org.gvsig.fmap.geom.primitive.Primitive; |
47 |
import org.gvsig.fmap.geom.type.GeometryType; |
48 |
import org.slf4j.Logger; |
49 |
import org.slf4j.LoggerFactory; |
50 |
|
51 |
@SuppressWarnings("UseSpecificCatch") |
52 |
public class SpatiaLiteHelper extends JDBCHelperBase { |
53 |
|
54 |
static final Logger LOGGER = LoggerFactory.getLogger(SpatiaLiteHelper.class); |
55 |
|
56 |
public static final String NAME = "SpatiaLite"; |
57 |
// public static final String SPATIALITE_JDBC_DRIVER = "org.sqlite.JDBC";
|
58 |
// public static final String URL_FORMAT = "jdbc:sqlite:%s";
|
59 |
//
|
60 |
// public static String getConnectionURL(SpatiaLiteConnectionParameters params) {
|
61 |
// String fname = params.getFile().getAbsolutePath().replace("\\","/");
|
62 |
// if( StringUtils.isEmpty(FilenameUtils.getExtension(fname)) ) {
|
63 |
// fname = fname + ".sqlite";
|
64 |
// params.setFile(new File(fname));
|
65 |
// }
|
66 |
// String driverClassName = params.getJDBCDriverClassName();
|
67 |
// if( StringUtils.isNotBlank(driverClassName) &&
|
68 |
// !StringUtils.equals(driverClassName, SPATIALITE_JDBC_DRIVER) ) {
|
69 |
// try {
|
70 |
// Class.forName(driverClassName);
|
71 |
// } catch(Throwable th) {
|
72 |
// LOGGER.warn("Can't load SpatiaLite JDBC Driver '"+driverClassName+"'.",th);
|
73 |
// }
|
74 |
// }
|
75 |
// String connectionURLFormat = params.getURLFormat();
|
76 |
// if( StringUtils.isBlank(connectionURLFormat) ) {
|
77 |
// connectionURLFormat = URL_FORMAT;
|
78 |
// }
|
79 |
// String connectionURL = String.format(connectionURLFormat, fname);
|
80 |
// LOGGER.debug("connectionURL: {}", connectionURL);
|
81 |
// return connectionURL;
|
82 |
// }
|
83 |
//
|
84 |
// private static class ConnectionProvider {
|
85 |
//
|
86 |
// private static boolean needRegisterDriver = true;
|
87 |
//
|
88 |
// private BasicDataSource dataSource = null;
|
89 |
//
|
90 |
// private final SpatiaLiteConnectionParameters connectionParameters;
|
91 |
//
|
92 |
// public ConnectionProvider(SpatiaLiteConnectionParameters connectionParameters) {
|
93 |
// this.connectionParameters = connectionParameters;
|
94 |
// }
|
95 |
//
|
96 |
// public Connection getConnection() throws SQLException {
|
97 |
// File f = this.connectionParameters.getFile();
|
98 |
// Connection conn = SpatiaLiteConnections.get(f);
|
99 |
// if( conn != null ) {
|
100 |
// return conn;
|
101 |
// }
|
102 |
// boolean newDs = (this.dataSource == null);
|
103 |
// if (newDs) {
|
104 |
// this.dataSource = this.createDataSource();
|
105 |
// }
|
106 |
// conn = this.dataSource.getConnection();
|
107 |
// loadExtension(conn);
|
108 |
// if (newDs) {
|
109 |
// initSpatialMetadata(conn);
|
110 |
// }
|
111 |
// SpatiaLiteConnections.set(f, conn);
|
112 |
// return conn;
|
113 |
// }
|
114 |
//
|
115 |
// private BasicDataSource createDataSource() throws SQLException {
|
116 |
// if (!this.isRegistered()) {
|
117 |
// this.registerDriver();
|
118 |
// }
|
119 |
// SpatiaLiteConnectionParameters params = connectionParameters;
|
120 |
//
|
121 |
// BasicDataSource ds = new BasicDataSource();
|
122 |
// ds.setDriverClassName(params.getJDBCDriverClassName());
|
123 |
// if( !StringUtils.isEmpty(params.getUser()) ) {
|
124 |
// ds.setUsername(params.getUser());
|
125 |
// }
|
126 |
// if( !StringUtils.isEmpty(params.getPassword()) ) {
|
127 |
// ds.setPassword(params.getPassword());
|
128 |
// }
|
129 |
// ds.setUrl(params.getUrl());
|
130 |
// SQLiteConfig config = new SQLiteConfig();
|
131 |
// config.setSharedCache(true);
|
132 |
// config.enableLoadExtension(true);
|
133 |
// config.setTransactionMode(TransactionMode.IMMEDIATE);
|
134 |
// for (Map.Entry e : config.toProperties().entrySet()) {
|
135 |
// ds.addConnectionProperty((String)e.getKey(), (String)e.getValue());
|
136 |
// }
|
137 |
// ds.setMaxWait(60L * 1000);
|
138 |
// return ds;
|
139 |
// }
|
140 |
//
|
141 |
// private boolean isRegistered() {
|
142 |
// return needRegisterDriver;
|
143 |
// }
|
144 |
//
|
145 |
// public void registerDriver() throws SQLException {
|
146 |
// String className = this.connectionParameters.getJDBCDriverClassName();
|
147 |
// if (className == null) {
|
148 |
// return;
|
149 |
// }
|
150 |
// try {
|
151 |
// Class theClass = Class.forName(className);
|
152 |
// if (theClass == null) {
|
153 |
// throw new JDBCDriverClassNotFoundException(NAME, className);
|
154 |
// }
|
155 |
// } catch (Exception e) {
|
156 |
// throw new SQLException("Can't register JDBC driver '" + className + "'.", e);
|
157 |
// }
|
158 |
// needRegisterDriver = false;
|
159 |
// }
|
160 |
//
|
161 |
// public void loadExtension(Connection conn) {
|
162 |
// Statement st = null;
|
163 |
// try {
|
164 |
// st = conn.createStatement();
|
165 |
// JDBCUtils.execute(st, "SELECT load_extension('mod_spatialite')");
|
166 |
// } catch(Exception ex) {
|
167 |
// LOGGER.warn("Can't load mod_spatialite extension module for SQLite (" +
|
168 |
// " driver class "+StringUtils.defaultString(connectionParameters.getJDBCDriverClassName()) +
|
169 |
// " url format "+StringUtils.defaultString(connectionParameters.getURLFormat()) +
|
170 |
// " file "+connectionParameters.getFile()==null?"null":connectionParameters.getFile().getAbsolutePath() +
|
171 |
// ").", ex);
|
172 |
// }
|
173 |
// finally {
|
174 |
// JDBCUtils.closeQuietly(st);
|
175 |
// }
|
176 |
// }
|
177 |
//
|
178 |
// public void initSpatialMetadata(Connection conn) {
|
179 |
// Statement st = null;
|
180 |
// try {
|
181 |
// st = conn.createStatement();
|
182 |
// //harmless if already existing
|
183 |
// JDBCUtils.execute(st, "SELECT InitSpatialMetaData(1)");
|
184 |
// } catch(Exception ex) {
|
185 |
// LOGGER.warn("Can't initialize spatial metatada in SQLite database (" +
|
186 |
// " driver class "+StringUtils.defaultString(connectionParameters.getJDBCDriverClassName()) +
|
187 |
// " url format "+StringUtils.defaultString(connectionParameters.getURLFormat()) +
|
188 |
// " file "+connectionParameters.getFile()==null?"null":connectionParameters.getFile().getAbsolutePath() +
|
189 |
// ").", ex);
|
190 |
// }
|
191 |
// finally {
|
192 |
// JDBCUtils.closeQuietly(st);
|
193 |
// }
|
194 |
// }
|
195 |
//
|
196 |
// }
|
197 |
//
|
198 |
// private ConnectionProvider connectionProvider = null;
|
199 |
|
200 |
public SpatiaLiteHelper(JDBCConnectionParameters connectionParameters) {
|
201 |
super(connectionParameters);
|
202 |
this.srssolver = new SRSSolverBase(this); |
203 |
} |
204 |
|
205 |
@Override
|
206 |
public synchronized Connection getConnection() throws AccessResourceException { |
207 |
return SpatiaLiteConnections.getConnection(this.getConnectionParameters().getFile()); |
208 |
// try {
|
209 |
// if (this.connectionProvider == null) {
|
210 |
// this.connectionProvider = new ConnectionProvider(this.getConnectionParameters());
|
211 |
// }
|
212 |
// Connection connection = this.connectionProvider.getConnection();
|
213 |
// LOGGER.debug("getConnection: connection = "+connection.hashCode());
|
214 |
// return connection;
|
215 |
// } catch (SQLException ex) {
|
216 |
// throw new AccessResourceException(SpatiaLiteLibrary.NAME, ex);
|
217 |
// }
|
218 |
} |
219 |
|
220 |
@Override
|
221 |
public void closeConnection(Connection connection) { |
222 |
if( connection==null ) { |
223 |
return;
|
224 |
} |
225 |
SpatiaLiteConnections.close(connection); |
226 |
} |
227 |
|
228 |
@Override
|
229 |
public void closeConnectionQuietly(Connection connection) { |
230 |
if( connection==null ) { |
231 |
return;
|
232 |
} |
233 |
try {
|
234 |
SpatiaLiteConnections.close(connection); |
235 |
} catch(Exception ex) { |
236 |
|
237 |
} |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public SpatiaLiteConnectionParameters getConnectionParameters() {
|
242 |
return (SpatiaLiteConnectionParameters) super.getConnectionParameters(); |
243 |
} |
244 |
|
245 |
@Override
|
246 |
public String getConnectionURL() { |
247 |
return SpatiaLiteConnections.getConnectionURL(this.getConnectionParameters().getFile()); |
248 |
// return getConnectionURL(this.getConnectionParameters());
|
249 |
} |
250 |
|
251 |
@Override
|
252 |
protected String getResourceType() { |
253 |
return SpatiaLiteLibrary.NAME;
|
254 |
} |
255 |
|
256 |
@Override
|
257 |
public String getProviderName() { |
258 |
return SpatiaLiteLibrary.NAME;
|
259 |
} |
260 |
|
261 |
@Override
|
262 |
public JDBCSQLBuilderBase createSQLBuilder() {
|
263 |
return new SpatiaLiteSQLBuilder(this); |
264 |
} |
265 |
|
266 |
@Override
|
267 |
public OperationsFactory getOperations() {
|
268 |
if (this.operationsFactory == null) { |
269 |
this.operationsFactory = new SpatiaLiteOperationsFactory(this); |
270 |
} |
271 |
return operationsFactory;
|
272 |
} |
273 |
|
274 |
@Override
|
275 |
public GeometrySupportType getGeometrySupportType() {
|
276 |
return GeometrySupportType.WKB;
|
277 |
} |
278 |
|
279 |
@Override
|
280 |
public boolean hasSpatialFunctions() { |
281 |
return true; |
282 |
} |
283 |
|
284 |
@Override
|
285 |
public boolean allowNestedOperations() { |
286 |
return true; // Como solo hay una conexion, hay que habilitar esto. |
287 |
} |
288 |
|
289 |
@Override
|
290 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) { |
291 |
return true; |
292 |
} |
293 |
|
294 |
@Override
|
295 |
public String getQuoteForIdentifiers() { |
296 |
return "\""; |
297 |
} |
298 |
|
299 |
@Override
|
300 |
public boolean allowAutomaticValues() { |
301 |
return true; |
302 |
} |
303 |
|
304 |
@Override
|
305 |
public boolean supportOffsetInSelect() { |
306 |
return true; |
307 |
} |
308 |
|
309 |
@Override
|
310 |
public String getQuoteForStrings() { |
311 |
return "'"; |
312 |
} |
313 |
|
314 |
@Override
|
315 |
public String getSourceId(JDBCStoreParameters parameters) { |
316 |
return parameters.getDBName() + "." + |
317 |
parameters.getSchema()+ "." +
|
318 |
parameters.getTable(); |
319 |
} |
320 |
|
321 |
@Override
|
322 |
public JDBCNewStoreParameters createNewStoreParameters() {
|
323 |
return new SpatiaLiteNewStoreParameters(); |
324 |
} |
325 |
|
326 |
@Override
|
327 |
public JDBCStoreParameters createOpenStoreParameters() {
|
328 |
return new SpatiaLiteStoreParameters(); |
329 |
} |
330 |
|
331 |
@Override
|
332 |
public JDBCServerExplorerParameters createServerExplorerParameters() {
|
333 |
return new SpatiaLiteExplorerParameters(); |
334 |
} |
335 |
|
336 |
public Geometry forceGeometryType(GeometryType geomtype, Geometry geom) throws CreateGeometryException { |
337 |
if( geom == null ) { |
338 |
return null; |
339 |
} |
340 |
switch( geomtype.getType() ) {
|
341 |
case Geometry.TYPES.MULTIPOLYGON:
|
342 |
if( geom.getType()==Geometry.TYPES.POLYGON ) {
|
343 |
MultiPolygon x = getGeometryManager().createMultiPolygon(geomtype.getSubType()); |
344 |
x.addPrimitive((Primitive) geom); |
345 |
geom = x; |
346 |
} |
347 |
break;
|
348 |
case Geometry.TYPES.MULTILINE:
|
349 |
if( geom.getType()==Geometry.TYPES.LINE ) {
|
350 |
MultiLine x = getGeometryManager().createMultiLine(geomtype.getSubType()); |
351 |
x.addPrimitive((Primitive) geom); |
352 |
geom = x; |
353 |
} |
354 |
break;
|
355 |
case Geometry.TYPES.MULTIPOINT:
|
356 |
if( geom.getType()==Geometry.TYPES.POINT ) {
|
357 |
MultiLine x = getGeometryManager().createMultiLine(geomtype.getSubType()); |
358 |
x.addPrimitive((Primitive) geom); |
359 |
geom = x; |
360 |
} |
361 |
break;
|
362 |
case Geometry.TYPES.POLYGON:
|
363 |
if( geom.getType()==Geometry.TYPES.MULTIPOLYGON ) {
|
364 |
MultiPolygon x = (MultiPolygon) geom; |
365 |
if( x.getPrimitivesNumber()==1 ) { |
366 |
geom = x.getPrimitiveAt(0);
|
367 |
} |
368 |
} |
369 |
break;
|
370 |
case Geometry.TYPES.LINE:
|
371 |
if( geom.getType()==Geometry.TYPES.MULTILINE ) {
|
372 |
MultiLine x = (MultiLine) geom; |
373 |
if( x.getPrimitivesNumber()==1 ) { |
374 |
geom = x.getPrimitiveAt(0);
|
375 |
} |
376 |
} |
377 |
break;
|
378 |
case Geometry.TYPES.POINT:
|
379 |
if( geom.getType()==Geometry.TYPES.MULTIPOINT ) {
|
380 |
MultiPoint x = (MultiPoint) geom; |
381 |
if( x.getPrimitivesNumber()==1 ) { |
382 |
geom = x.getPrimitiveAt(0);
|
383 |
} |
384 |
} |
385 |
} |
386 |
return geom;
|
387 |
} |
388 |
|
389 |
@Override
|
390 |
public JDBCServerExplorer createServerExplorer(JDBCServerExplorerParameters parameters, DataServerExplorerProviderServices providerServices) throws InitializeException { |
391 |
SpatiaLiteExplorer explorer = new SpatiaLiteExplorer(parameters, providerServices, this); |
392 |
return explorer;
|
393 |
} |
394 |
|
395 |
@Override
|
396 |
public JDBCStoreProvider createProvider(JDBCStoreParameters parameters, DataStoreProviderServices providerServices) throws InitializeException { |
397 |
return super.createProvider(parameters, providerServices); |
398 |
} |
399 |
|
400 |
@Override
|
401 |
public boolean isThreadSafe() { |
402 |
return false; |
403 |
} |
404 |
|
405 |
} |