Revision 128
tags/org.gvsig.spatialite-1.0.19/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
|
|
3 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
4 |
|
|
5 |
<modelVersion>4.0.0</modelVersion> |
|
6 |
<artifactId>org.gvsig.spatialite</artifactId> |
|
7 |
<version>1.0.19</version> |
|
8 |
<packaging>pom</packaging> |
|
9 |
<name>${project.artifactId}</name> |
|
10 |
<description>SpatiaLite support for DAL and gvSIG</description> |
|
11 |
<parent> |
|
12 |
<groupId>org.gvsig</groupId> |
|
13 |
<artifactId>org.gvsig.desktop</artifactId> |
|
14 |
<version>2.0.215</version> |
|
15 |
</parent> |
|
16 |
|
|
17 |
<url>https://devel.gvsig.org/redmine/projects/gvsig-sqlite</url> |
|
18 |
<scm> |
|
19 |
<connection>scm:svn:https://devel.gvsig.org/svn/gvsig-sqlite/tags/org.gvsig.spatialite-1.0.19</connection> |
|
20 |
<developerConnection>scm:svn:https://devel.gvsig.org/svn/gvsig-sqlite/tags/org.gvsig.spatialite-1.0.19</developerConnection> |
|
21 |
<url>https://devel.gvsig.org/redmine/projects/gvsig-sqlite/repository/show/tags/org.gvsig.spatialite-1.0.19</url> |
|
22 |
</scm> |
|
23 |
|
|
24 |
<repositories> |
|
25 |
<repository> |
|
26 |
<id>gvsig-public-http-repository</id> |
|
27 |
<name>gvSIG maven public HTTP repository</name> |
|
28 |
<url>http://devel.gvsig.org/m2repo/j2se</url> |
|
29 |
<releases> |
|
30 |
<enabled>true</enabled> |
|
31 |
<updatePolicy>daily</updatePolicy> |
|
32 |
<checksumPolicy>warn</checksumPolicy> |
|
33 |
</releases> |
|
34 |
<snapshots> |
|
35 |
<enabled>true</enabled> |
|
36 |
<updatePolicy>daily</updatePolicy> |
|
37 |
<checksumPolicy>warn</checksumPolicy> |
|
38 |
</snapshots> |
|
39 |
</repository> |
|
40 |
</repositories> |
|
41 |
|
|
42 |
|
|
43 |
<build> |
|
44 |
<plugins> |
|
45 |
<plugin> |
|
46 |
<groupId>org.apache.maven.plugins</groupId> |
|
47 |
<artifactId>maven-release-plugin</artifactId> |
|
48 |
<configuration> |
|
49 |
<tagBase>https://devel.gvsig.org/svn/gvsig-sqlite/tags/</tagBase> |
|
50 |
</configuration> |
|
51 |
</plugin> |
|
52 |
</plugins> |
|
53 |
</build> |
|
54 |
|
|
55 |
|
|
56 |
<dependencyManagement> |
|
57 |
<dependencies> |
|
58 |
<dependency> |
|
59 |
<groupId>org.gvsig</groupId> |
|
60 |
<artifactId>org.gvsig.spatialite.provider</artifactId> |
|
61 |
<version>1.0.19</version> |
|
62 |
</dependency> |
|
63 |
<dependency> |
|
64 |
<groupId>org.gvsig</groupId> |
|
65 |
<artifactId>org.gvsig.spatialite.app.mainplugin.common</artifactId> |
|
66 |
<version>1.0.19</version> |
|
67 |
<type>zip</type> |
|
68 |
</dependency> |
|
69 |
<dependency> |
|
70 |
<groupId>org.xerial</groupId> |
|
71 |
<artifactId>sqlite-jdbc</artifactId> |
|
72 |
<version>3.21.0</version> |
|
73 |
</dependency> |
|
74 |
|
|
75 |
</dependencies> |
|
76 |
</dependencyManagement> |
|
77 |
|
|
78 |
<modules> |
|
79 |
<module>org.gvsig.spatialite.app</module> |
|
80 |
<module>org.gvsig.spatialite.provider</module> |
|
81 |
</modules> |
|
82 |
|
|
83 |
|
|
84 |
</project> |
tags/org.gvsig.spatialite-1.0.19/org.gvsig.spatialite.provider/src/test/java/org/gvsig/spatialite/dal/SpatiaLiteSQLBuilderTest.java | ||
---|---|---|
1 |
package org.gvsig.spatialite.dal; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.ArrayUtils; |
|
6 |
import org.cresques.cts.IProjection; |
|
7 |
import org.gvsig.fmap.crs.CRSFactory; |
|
8 |
import org.gvsig.fmap.dal.DALLocator; |
|
9 |
import org.gvsig.fmap.dal.ExpressionBuilder; |
|
10 |
import org.gvsig.fmap.dal.ExpressionBuilder.Parameter; |
|
11 |
import org.gvsig.fmap.dal.ExpressionBuilder.Variable; |
|
12 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
13 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
|
14 |
import org.gvsig.fmap.dal.store.db.DBHelper; |
|
15 |
import org.gvsig.fmap.geom.DataTypes; |
|
16 |
import org.gvsig.fmap.geom.Geometry; |
|
17 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
18 |
import org.gvsig.fmap.geom.GeometryManager; |
|
19 |
import org.gvsig.fmap.geom.primitive.Polygon; |
|
20 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
|
21 |
|
|
22 |
public class SpatiaLiteSQLBuilderTest extends AbstractLibraryAutoInitTestCase { |
|
23 |
|
|
24 |
private SpatiaLiteHelper helper; |
|
25 |
|
|
26 |
@Override |
|
27 |
protected void doSetUp() throws Exception { |
|
28 |
DataManagerProviderServices dataman = |
|
29 |
(DataManagerProviderServices) DALLocator.getDataManager(); |
|
30 |
|
|
31 |
DBHelper.registerParametersDefinition( |
|
32 |
SpatiaLiteLibrary.NAME + "StoreParameters", |
|
33 |
SpatiaLiteStoreParameters.class, |
|
34 |
dataman.getResourceAsStream(this, SpatiaLiteLibrary.NAME + "Parameters.xml") |
|
35 |
); |
|
36 |
|
|
37 |
SpatiaLiteStoreParameters params = new SpatiaLiteStoreParameters(); |
|
38 |
params.setDBName("test.sqlite"); |
|
39 |
params.setTable("test1"); |
|
40 |
helper = new SpatiaLiteHelper(params); |
|
41 |
} |
|
42 |
|
|
43 |
List<String> getVariableNames(ExpressionBuilder builder) { |
|
44 |
List<String> vars = new ArrayList<>(); |
|
45 |
for (Variable var : builder.getVariables()) { |
|
46 |
vars.add(var.getName()); |
|
47 |
} |
|
48 |
return vars; |
|
49 |
} |
|
50 |
|
|
51 |
List<String> getParameterNames(ExpressionBuilder builder) { |
|
52 |
List<String> params = new ArrayList<>(); |
|
53 |
for (Parameter param : builder.getParameters()) { |
|
54 |
String s; |
|
55 |
switch(param.getType()) { |
|
56 |
case Constant: |
|
57 |
Object value = param.getValue(); |
|
58 |
if( value==null ) { |
|
59 |
s = "null"; |
|
60 |
} else if( value instanceof String ) { |
|
61 |
s = "'" + (String)value + "'"; |
|
62 |
} else { |
|
63 |
s = value.toString(); |
|
64 |
} |
|
65 |
break; |
|
66 |
case Geometry: |
|
67 |
case Variable: |
|
68 |
default: |
|
69 |
s = "\"" + param.getName() + "\""; |
|
70 |
} |
|
71 |
params.add(s); |
|
72 |
} |
|
73 |
return params; |
|
74 |
} |
|
75 |
|
|
76 |
public SQLBuilder createSQLBuilder() { |
|
77 |
return new SpatiaLiteSQLBuilder(helper); |
|
78 |
} |
|
79 |
|
|
80 |
public void testCalulateEnvelope() throws Exception { |
|
81 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
|
82 |
IProjection proj = CRSFactory.getCRS("EPSG:4326"); |
|
83 |
|
|
84 |
Polygon limit = geometryManager.createPolygon(Geometry.SUBTYPES.GEOM2D); |
|
85 |
limit.addVertex(0, 0); |
|
86 |
limit.addVertex(0, 100); |
|
87 |
limit.addVertex(100, 100); |
|
88 |
limit.addVertex(100, 0); |
|
89 |
limit.addVertex(0, 0); |
|
90 |
|
|
91 |
SQLBuilder builder = createSQLBuilder(); |
|
92 |
|
|
93 |
builder.select().column().value( |
|
94 |
builder.getAsGeometry( |
|
95 |
builder.ST_ExtentAggregate( |
|
96 |
builder.column("the_geom") |
|
97 |
) |
|
98 |
) |
|
99 |
).as("envelope"); |
|
100 |
builder.select().from().table().database("master").schema("dbo").name("test1"); |
|
101 |
builder.select().where().set( |
|
102 |
builder.ST_Intersects( |
|
103 |
builder.ST_Envelope( |
|
104 |
builder.column("the_geom") |
|
105 |
), |
|
106 |
builder.geometry(limit, proj) |
|
107 |
) |
|
108 |
); |
|
109 |
builder.select().where().and( |
|
110 |
builder.custom("x = 27").add( builder.variable("x") ) |
|
111 |
); |
|
112 |
|
|
113 |
System.out.println("# Test:: testCalulateEnvelope"); |
|
114 |
System.out.println("# SQL:: " + builder.toString()); |
|
115 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
116 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
117 |
|
|
118 |
//# Test:: testCalulateEnvelope |
|
119 |
//# SQL:: SELECT ST_AsBinary(Extent("the_geom")) AS "envelope" FROM "test1" WHERE ST_Intersects((ST_Envelope("the_geom")), (ST_GeomFromWKB((x'000000000300000001000000050000000000000000000000000000000000000000000000004059000000000000405900000000000040590000000000004059000000000000000000000000000000000000000000000000000000000000'), (4326)))) AND x = 27 |
|
120 |
//# Variables:: [the_geom, x] |
|
121 |
//# Parametros:: [] |
|
122 |
assertEquals( |
|
123 |
"SELECT ST_AsBinary(Extent(\"the_geom\")) AS \"envelope\" FROM \"test1\" WHERE ST_Intersects((ST_Envelope(\"the_geom\")), (ST_GeomFromWKB((x'000000000300000001000000050000000000000000000000000000000000000000000000004059000000000000405900000000000040590000000000004059000000000000000000000000000000000000000000000000000000000000'), (4326)))) AND x = 27", |
|
124 |
builder.toString() |
|
125 |
); |
|
126 |
assertEquals( |
|
127 |
"[the_geom, x]", |
|
128 |
ArrayUtils.toString(getVariableNames(builder)) |
|
129 |
); |
|
130 |
assertEquals( |
|
131 |
"[]", |
|
132 |
ArrayUtils.toString(getParameterNames(builder)) |
|
133 |
); |
|
134 |
} |
|
135 |
|
|
136 |
public void testCount() throws Exception { |
|
137 |
SQLBuilder builder = createSQLBuilder(); |
|
138 |
|
|
139 |
builder.select().column().value(builder.count().all()); |
|
140 |
builder.select().from().table().database("master").schema("dbo").name("test1"); |
|
141 |
builder.select().from().subquery(null); |
|
142 |
builder.select().where().set( builder.custom("pp = 200").add(builder.variable("pp"))); |
|
143 |
|
|
144 |
System.out.println("# Test:: testCount"); |
|
145 |
System.out.println("# SQL:: " + builder.toString()); |
|
146 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
147 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
148 |
|
|
149 |
//# Test:: testCount |
|
150 |
//# SQL:: SELECT COUNT(*) FROM "test1" WHERE pp = 200 |
|
151 |
//# Variables:: [pp] |
|
152 |
//# Parametros:: [] |
|
153 |
|
|
154 |
assertEquals( |
|
155 |
"SELECT COUNT(*) FROM \"test1\" WHERE pp = 200", |
|
156 |
builder.toString() |
|
157 |
); |
|
158 |
assertEquals( |
|
159 |
"[pp]", |
|
160 |
ArrayUtils.toString(getVariableNames(builder)) |
|
161 |
); |
|
162 |
assertEquals( |
|
163 |
"[]", |
|
164 |
ArrayUtils.toString(getParameterNames(builder)) |
|
165 |
); |
|
166 |
} |
|
167 |
|
|
168 |
public void testUpdateStatistics() throws Exception { |
|
169 |
SQLBuilder builder = createSQLBuilder(); |
|
170 |
|
|
171 |
builder.update_table_statistics().table().database("master").schema("dbo").name("test1"); |
|
172 |
|
|
173 |
System.out.println("# Test:: testUpdateStatistics"); |
|
174 |
System.out.println("# SQL:: " + builder.toString()); |
|
175 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
176 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
177 |
|
|
178 |
//# Test:: testUpdateStatistics |
|
179 |
//# SQL:: SELECT UpdateLayerStatistics("test1") |
|
180 |
//# Variables:: [] |
|
181 |
//# Parametros:: [] |
|
182 |
|
|
183 |
assertEquals( |
|
184 |
"SELECT UpdateLayerStatistics(\"test1\")", |
|
185 |
builder.toString() |
|
186 |
); |
|
187 |
assertEquals( |
|
188 |
"[]", |
|
189 |
ArrayUtils.toString(getVariableNames(builder)) |
|
190 |
); |
|
191 |
assertEquals( |
|
192 |
"[]", |
|
193 |
ArrayUtils.toString(getParameterNames(builder)) |
|
194 |
); |
|
195 |
} |
|
196 |
|
|
197 |
public void testCreateTable() throws Exception { |
|
198 |
SQLBuilder builder = createSQLBuilder(); |
|
199 |
|
|
200 |
builder.create_table().table().database("master").schema("dbo").name("test1"); |
|
201 |
builder.create_table().add_column( |
|
202 |
"name", |
|
203 |
DataTypes.STRING, |
|
204 |
45, |
|
205 |
0, |
|
206 |
false, |
|
207 |
false, |
|
208 |
true, |
|
209 |
false, |
|
210 |
null |
|
211 |
); |
|
212 |
builder.create_table().add_column( |
|
213 |
"id", |
|
214 |
DataTypes.INT, |
|
215 |
0, |
|
216 |
0, |
|
217 |
true, |
|
218 |
false, |
|
219 |
false, |
|
220 |
true, |
|
221 |
0 |
|
222 |
); |
|
223 |
builder.create_table().add_column( |
|
224 |
"geom", |
|
225 |
DataTypes.GEOMETRY, |
|
226 |
0, |
|
227 |
0, |
|
228 |
false, |
|
229 |
false, |
|
230 |
true, |
|
231 |
false, |
|
232 |
null |
|
233 |
); |
|
234 |
|
|
235 |
System.out.println("# Test:: testCreateTable"); |
|
236 |
System.out.println("# SQL:: " + builder.toString()); |
|
237 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
238 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
239 |
|
|
240 |
//# Test:: testCreateTable |
|
241 |
//# SQL:: CREATE TABLE "test1" ("name" TEXT DEFAULT NULL NULL, "id" INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT '0' NOT NULL ); SELECT AddGeometryColumn("test1" , "geom" , -1 , 'GEOMETRY' , 'XYZ', 1) |
|
242 |
//# Variables:: [] |
|
243 |
//# Parametros:: [] |
|
244 |
|
|
245 |
assertEquals( |
|
246 |
"CREATE TABLE \"test1\" (\"name\" TEXT DEFAULT NULL NULL, \"id\" INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT '0' NOT NULL ); SELECT AddGeometryColumn(\"test1\" , \"geom\" , -1 , 'GEOMETRY' , 'XYZ', 1)", |
|
247 |
builder.toString() |
|
248 |
); |
|
249 |
assertEquals( |
|
250 |
"[]", |
|
251 |
ArrayUtils.toString(getVariableNames(builder)) |
|
252 |
); |
|
253 |
assertEquals( |
|
254 |
"[]", |
|
255 |
ArrayUtils.toString(getParameterNames(builder)) |
|
256 |
); |
|
257 |
} |
|
258 |
|
|
259 |
public void testDropTable() throws Exception { |
|
260 |
SQLBuilder builder = createSQLBuilder(); |
|
261 |
|
|
262 |
builder.drop_table().table().database("master").schema("dbo").name("test1"); |
|
263 |
|
|
264 |
System.out.println("# Test:: testDropTable"); |
|
265 |
System.out.println("# SQL:: " + builder.toString()); |
|
266 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
267 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
268 |
|
|
269 |
//# Test:: testDropTable |
|
270 |
//# SQL:: DROP TABLE [master].[dbo].[test1] |
|
271 |
//# Variables:: [] |
|
272 |
//# Parametros:: [] |
|
273 |
|
|
274 |
assertEquals( |
|
275 |
"DROP TABLE [master].[dbo].[test1]", |
|
276 |
builder.toString() |
|
277 |
); |
|
278 |
assertEquals( |
|
279 |
"[]", |
|
280 |
ArrayUtils.toString(getVariableNames(builder)) |
|
281 |
); |
|
282 |
assertEquals( |
|
283 |
"[]", |
|
284 |
ArrayUtils.toString(getParameterNames(builder)) |
|
285 |
); |
|
286 |
} |
|
287 |
|
|
288 |
public void testFetchFeatureProviderByReference() throws Exception { |
|
289 |
SQLBuilder builder = createSQLBuilder(); |
|
290 |
|
|
291 |
String value = "yoyo"; |
|
292 |
builder.select().column().name("name"); |
|
293 |
builder.select().column().name("id"); |
|
294 |
builder.select().column().name("geom").as_geometry(); |
|
295 |
builder.select().from().table().database("master").schema("dbo").name("test1"); |
|
296 |
builder.select().where().set( |
|
297 |
builder.eq( |
|
298 |
builder.column("name"), |
|
299 |
builder.parameter(value).as_constant() |
|
300 |
) |
|
301 |
); |
|
302 |
builder.select().limit(1); |
|
303 |
|
|
304 |
System.out.println("# Test:: testFetchFeatureProviderByReference"); |
|
305 |
System.out.println("# SQL:: " + builder.toString()); |
|
306 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
307 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
308 |
|
|
309 |
//# Test:: testFetchFeatureProviderByReference |
|
310 |
//# SQL:: SELECT TOP 1 [name], [id], ([geom]).STAsBinary() FROM [master].[dbo].[test1] WHERE ( ([name]) = (?) ) |
|
311 |
//# Variables:: [geom, id, name] |
|
312 |
//# Parametros:: ['yoyo'] |
|
313 |
|
|
314 |
assertEquals( |
|
315 |
"SELECT TOP 1 [name], [id], ([geom]).STAsBinary() FROM [master].[dbo].[test1] WHERE ( ([name]) = (?) )", |
|
316 |
builder.toString() |
|
317 |
); |
|
318 |
assertEquals( |
|
319 |
"[geom, id, name]", |
|
320 |
ArrayUtils.toString(getVariableNames(builder)) |
|
321 |
); |
|
322 |
assertEquals( |
|
323 |
"['yoyo']", |
|
324 |
ArrayUtils.toString(getParameterNames(builder)) |
|
325 |
); |
|
326 |
} |
|
327 |
|
|
328 |
public void testIsNull() throws Exception { |
|
329 |
SQLBuilder builder = createSQLBuilder(); |
|
330 |
|
|
331 |
builder.select().column().name("name"); |
|
332 |
builder.select().column().name("id"); |
|
333 |
builder.select().column().name("geom").as_geometry(); |
|
334 |
builder.select().from().table().database("master").schema("dbo").name("test1"); |
|
335 |
builder.select().where().set( |
|
336 |
builder.and( |
|
337 |
builder.isNull( |
|
338 |
builder.column("name") |
|
339 |
), |
|
340 |
builder.notIsNull( |
|
341 |
builder.column("id") |
|
342 |
) |
|
343 |
) |
|
344 |
); |
|
345 |
builder.select().limit(2); |
|
346 |
|
|
347 |
System.out.println("# Test:: testIsNull"); |
|
348 |
System.out.println("# SQL:: " + builder.toString()); |
|
349 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
350 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
351 |
|
|
352 |
//# Test:: testIsNull |
|
353 |
//# SQL:: SELECT TOP 2 [name], [id], ([geom]).STAsBinary() FROM [master].[dbo].[test1] WHERE ( ([name]) IS NULL ) AND ( ([id]) NOT IS NULL ) |
|
354 |
//# Variables:: [geom, id, name] |
|
355 |
//# Parametros:: [] |
|
356 |
|
|
357 |
assertEquals( |
|
358 |
"SELECT TOP 2 [name], [id], ([geom]).STAsBinary() FROM [master].[dbo].[test1] WHERE ( ([name]) IS NULL ) AND ( ([id]) NOT IS NULL )", |
|
359 |
builder.toString() |
|
360 |
); |
|
361 |
assertEquals( |
|
362 |
"[geom, id, name]", |
|
363 |
ArrayUtils.toString(getVariableNames(builder)) |
|
364 |
); |
|
365 |
assertEquals( |
|
366 |
"[]", |
|
367 |
ArrayUtils.toString(getParameterNames(builder)) |
|
368 |
); |
|
369 |
} |
|
370 |
|
|
371 |
public void testFetchFeatureType() throws Exception { |
|
372 |
SQLBuilder builder = createSQLBuilder(); |
|
373 |
|
|
374 |
builder.select().column().all(); |
|
375 |
builder.select().from().table().database("master").schema("dbo").name("test1"); |
|
376 |
builder.select().limit(1); |
|
377 |
|
|
378 |
System.out.println("# Test:: testFetchFeatureType"); |
|
379 |
System.out.println("# SQL:: " + builder.toString()); |
|
380 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
381 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
382 |
|
|
383 |
//# Test:: testFetchFeatureType |
|
384 |
//# SQL:: SELECT TOP 1 * FROM [master].[dbo].[test1] |
|
385 |
//# Variables:: [] |
|
386 |
//# Parametros:: [] |
|
387 |
|
|
388 |
assertEquals( |
|
389 |
"SELECT TOP 1 * FROM [master].[dbo].[test1]", |
|
390 |
builder.toString() |
|
391 |
); |
|
392 |
assertEquals( |
|
393 |
"[]", |
|
394 |
ArrayUtils.toString(getVariableNames(builder)) |
|
395 |
); |
|
396 |
assertEquals( |
|
397 |
"[]", |
|
398 |
ArrayUtils.toString(getParameterNames(builder)) |
|
399 |
); |
|
400 |
} |
|
401 |
|
|
402 |
public void testPerformDeletes() throws Exception { |
|
403 |
SQLBuilder builder = createSQLBuilder(); |
|
404 |
|
|
405 |
builder.delete().table().database("master").schema("dbo").name("test1"); |
|
406 |
builder.delete().where().and( |
|
407 |
builder.eq( |
|
408 |
builder.column("id1"), |
|
409 |
builder.parameter("id1").as_variable() |
|
410 |
) |
|
411 |
); |
|
412 |
builder.delete().where().and( |
|
413 |
builder.eq( |
|
414 |
builder.column("id2"), |
|
415 |
builder.parameter("id2").as_variable() |
|
416 |
) |
|
417 |
); |
|
418 |
|
|
419 |
System.out.println("# Test:: testPerformDeletes"); |
|
420 |
System.out.println("# SQL:: " + builder.toString()); |
|
421 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
422 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
423 |
|
|
424 |
//# Test:: testPerformDeletes |
|
425 |
//# SQL:: DELETE FROM [master].[dbo].[test1] WHERE ( ([id1]) = (?) ) AND ( ([id2]) = (?) ) |
|
426 |
//# Variables:: [id1, id2] |
|
427 |
//# Parametros:: ["id1", "id2"] |
|
428 |
|
|
429 |
assertEquals( |
|
430 |
"DELETE FROM [master].[dbo].[test1] WHERE ( ([id1]) = (?) ) AND ( ([id2]) = (?) )", |
|
431 |
builder.toString() |
|
432 |
); |
|
433 |
assertEquals( |
|
434 |
"[id1, id2]", |
|
435 |
ArrayUtils.toString(getVariableNames(builder)) |
|
436 |
); |
|
437 |
assertEquals( |
|
438 |
"[\"id1\", \"id2\"]", |
|
439 |
ArrayUtils.toString(getParameterNames(builder)) |
|
440 |
); |
|
441 |
} |
|
442 |
|
|
443 |
public void testPerformInserts1() throws Exception { |
|
444 |
IProjection proj = CRSFactory.getCRS("EPSG:4326"); |
|
445 |
|
|
446 |
SQLBuilder builder = createSQLBuilder(); |
|
447 |
|
|
448 |
builder.insert().table().database("master").schema("dbo").name("test1"); |
|
449 |
builder.insert().column().name("id").with_value(builder.parameter("id")); |
|
450 |
builder.insert().column().name("name").with_value(builder.parameter("name")); |
|
451 |
builder.insert().column().name("geom").with_value(builder.parameter("geom").as_geometry_variable().srs(builder.parameter().srs(proj))); |
|
452 |
|
|
453 |
System.out.println("# Test:: testPerformInserts1"); |
|
454 |
System.out.println("# SQL:: " + builder.toString()); |
|
455 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
456 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
457 |
|
|
458 |
//# Test:: testPerformInserts1 |
|
459 |
//# SQL:: INSERT INTO [master].[dbo].[test1] ( [id], [name], [geom] ) VALUES ( ?, ?, geometry::STGeomFromWKB(?, 4326) ) |
|
460 |
//# Variables:: [geom, id, name] |
|
461 |
//# Parametros:: ["id", "name", "geom"] |
|
462 |
|
|
463 |
assertEquals( |
|
464 |
"INSERT INTO [master].[dbo].[test1] ( [id], [name], [geom] ) VALUES ( ?, ?, geometry::STGeomFromWKB(?, 4326) )", |
|
465 |
builder.toString() |
|
466 |
); |
|
467 |
assertEquals( |
|
468 |
"[geom, id, name]", |
|
469 |
ArrayUtils.toString(getVariableNames(builder)) |
|
470 |
); |
|
471 |
assertEquals( |
|
472 |
"[\"id\", \"name\", \"geom\"]", |
|
473 |
ArrayUtils.toString(getParameterNames(builder)) |
|
474 |
); |
|
475 |
} |
|
476 |
|
|
477 |
public void testPerformInserts2() throws Exception { |
|
478 |
IProjection proj = CRSFactory.getCRS("EPSG:4326"); |
|
479 |
|
|
480 |
SQLBuilder builder = createSQLBuilder(); |
|
481 |
|
|
482 |
builder.insert().table().database("master").schema("dbo").name("test1"); |
|
483 |
builder.insert().column().name("id").with_value(builder.parameter("id")); |
|
484 |
builder.insert().column().name("name").with_value(builder.parameter("name")); |
|
485 |
builder.insert().column().name("geom").with_value(builder.parameter("geom").as_geometry_variable().srs(builder.parameter().srs(proj))); |
|
486 |
|
|
487 |
System.out.println("# Test:: testPerformInserts2"); |
|
488 |
System.out.println("# SQL:: " + builder.toString()); |
|
489 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
490 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
491 |
|
|
492 |
//# Test:: testPerformInserts2 |
|
493 |
//# SQL:: INSERT INTO [master].[dbo].[test1] ( [id], [name], [geom] ) VALUES ( ?, ?, geometry::STGeomFromWKB(?, ?) ) |
|
494 |
//# Variables:: [geom, id, name] |
|
495 |
//# Parametros:: ["id", "name", "geom", 4326] |
|
496 |
|
|
497 |
assertEquals( |
|
498 |
"INSERT INTO [master].[dbo].[test1] ( [id], [name], [geom] ) VALUES ( ?, ?, geometry::STGeomFromWKB(?, ?) )", |
|
499 |
builder.toString() |
|
500 |
); |
|
501 |
assertEquals( |
|
502 |
"[geom, id, name]", |
|
503 |
ArrayUtils.toString(getVariableNames(builder)) |
|
504 |
); |
|
505 |
assertEquals( |
|
506 |
"[\"id\", \"name\", \"geom\", 4326]", |
|
507 |
ArrayUtils.toString(getParameterNames(builder)) |
|
508 |
); |
|
509 |
} |
|
510 |
|
|
511 |
public void testPerformUpdates() throws Exception { |
|
512 |
IProjection proj = CRSFactory.getCRS("EPSG:4326"); |
|
513 |
|
|
514 |
SQLBuilder builder = createSQLBuilder(); |
|
515 |
|
|
516 |
builder.update().table().database("master").schema("dbo").name("test1"); |
|
517 |
builder.update().where().and( |
|
518 |
builder.eq( |
|
519 |
builder.column("id"), |
|
520 |
builder.parameter("id").as_variable() |
|
521 |
) |
|
522 |
); |
|
523 |
builder.update().column().name("name").with_value(builder.parameter("name")); |
|
524 |
builder.update().column().name("geom").with_value( |
|
525 |
builder.parameter("geom").as_geometry_variable().srs(builder.parameter().srs(proj)) |
|
526 |
); |
|
527 |
|
|
528 |
System.out.println("# Test:: testPerformUpdates"); |
|
529 |
System.out.println("# SQL:: " + builder.toString()); |
|
530 |
System.out.println("# Variables:: " + ArrayUtils.toString(getVariableNames(builder))); |
|
531 |
System.out.println("# Parametros:: " + ArrayUtils.toString(getParameterNames(builder))); |
|
532 |
|
|
533 |
//# Test:: testPerformUpdates |
|
534 |
//# SQL:: UPDATE [master].[dbo].[test1] SET [name] = ?, [geom] = geometry::STGeomFromWKB(?, ?) WHERE ( ([id]) = (?) ) |
|
535 |
//# Variables:: [geom, id, name] |
|
536 |
//# Parametros:: ["name", "geom", 4326, "id"] |
|
537 |
|
|
538 |
assertEquals( |
|
539 |
"UPDATE [master].[dbo].[test1] SET [name] = ?, [geom] = geometry::STGeomFromWKB(?, ?) WHERE ( ([id]) = (?) )", |
|
540 |
builder.toString() |
|
541 |
); |
|
542 |
assertEquals( |
|
543 |
"[geom, id, name]", |
|
544 |
ArrayUtils.toString(getVariableNames(builder)) |
|
545 |
); |
|
546 |
assertEquals( |
|
547 |
"[\"name\", \"geom\", 4326, \"id\"]", |
|
548 |
ArrayUtils.toString(getParameterNames(builder)) |
|
549 |
); |
|
550 |
} |
|
551 |
|
|
552 |
} |
tags/org.gvsig.spatialite-1.0.19/org.gvsig.spatialite.provider/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.spatialite.dal.SpatiaLiteLibrary |
tags/org.gvsig.spatialite-1.0.19/org.gvsig.spatialite.provider/src/main/resources/org/gvsig/spatialite/dal/SpatiaLiteParameters.xml | ||
---|---|---|
1 |
<?xml version="1.0"?> |
|
2 |
<definitions> |
|
3 |
<version>1.0.0</version> |
|
4 |
<classes> |
|
5 |
<class name="SpatiaLiteResourceParameters"> |
|
6 |
<extends> |
|
7 |
<class>JDBC2ResourceParameters</class> |
|
8 |
</extends> |
|
9 |
<fields> |
|
10 |
<field name="JDBCDriverClass" type="string" mandatory="true" |
|
11 |
defaultValue="org.sqlite.JDBC" group="Advanced"> |
|
12 |
<description>JDBC driver class for SpatiaLite</description> |
|
13 |
</field> |
|
14 |
<field name="url_format" type="string" mandatory="true" |
|
15 |
defaultValue="jdbc:sqlite:%s" group="Advanced"> |
|
16 |
<description></description> |
|
17 |
</field> |
|
18 |
<field name="port" type="integer" mandatory="false" |
|
19 |
defaultValue="0" group="Connection" hidden="true"> |
|
20 |
<description></description> |
|
21 |
</field> |
|
22 |
<field name="host" type="string" mandatory="false" |
|
23 |
defaultValue="" group="Connection" hidden="true"> |
|
24 |
<description></description> |
|
25 |
</field> |
|
26 |
<field name="dbname" type="string" mandatory="false" hidden="true" defaultValue="" group="Basic"> |
|
27 |
<description></description> |
|
28 |
</field> |
|
29 |
<field name="schema" type="string" mandatory="false" hidden="true" defaultValue="" group="Basic"> |
|
30 |
<description></description> |
|
31 |
</field> |
|
32 |
<field name="catalog" type="string" mandatory="false" hidden="true" defaultValue="" group="Basic"> |
|
33 |
<description></description> |
|
34 |
</field> |
|
35 |
<field name="url" type="string" mandatory="false" hidden="true" defaultValue="" group="Basic"> |
|
36 |
<description></description> |
|
37 |
</field> |
|
38 |
<field name="dbuser" type="string" mandatory="false" defaultValue="" group="Basic"> |
|
39 |
<description></description> |
|
40 |
</field> |
|
41 |
<field name="password" type="string" mandatory="false" defaultValue="" group="Basic"> |
|
42 |
<description></description> |
|
43 |
</field> |
|
44 |
<field name="database_file" label="Database file" type="file" mandatory="true" defaultValue="" group="Basic"> |
|
45 |
<description></description> |
|
46 |
</field> |
|
47 |
<field name="enable_shared_cache" label="Enable shared cache" type="boolean" mandatory="false" defaultValue="true" group="Advanced"> |
|
48 |
<description></description> |
|
49 |
</field> |
|
50 |
<field name="enable_load_extension" label="Enable load extension" type="boolean" mandatory="false" defaultValue="true" group="Advanced"> |
|
51 |
<description></description> |
|
52 |
</field> |
|
53 |
<field name="enable_spatialite" label="Enable spatialite" type="boolean" mandatory="false" defaultValue="true" group="Advanced"> |
|
54 |
<description></description> |
|
55 |
</field> |
|
56 |
</fields> |
|
57 |
</class> |
|
58 |
|
|
59 |
<class name="SpatiaLiteStoreParameters"> |
|
60 |
<extends> |
|
61 |
<class>JDBC2StoreParameters</class> |
|
62 |
<class>SpatiaLiteResourceParameters</class> |
|
63 |
</extends> |
|
64 |
<fields/> |
|
65 |
</class> |
|
66 |
|
|
67 |
<class name="SpatiaLiteNewStoreParameters"> |
|
68 |
<extends> |
|
69 |
<class>JDBC2NewStoreParameters</class> |
|
70 |
<class>SpatiaLiteResourceParameters</class> |
|
71 |
</extends> |
|
72 |
<fields/> |
|
73 |
</class> |
|
74 |
|
|
75 |
|
|
76 |
<class name="SpatiaLiteServerExplorerParameters"> |
|
77 |
<extends> |
|
78 |
<class>SpatiaLiteResourceParameters</class> |
|
79 |
<class>JDBC2ServerExplorerParameters</class> |
|
80 |
</extends> |
|
81 |
<fields/> |
|
82 |
</class> |
|
83 |
|
|
84 |
|
|
85 |
</classes> |
|
86 |
</definitions> |
tags/org.gvsig.spatialite-1.0.19/org.gvsig.spatialite.provider/src/main/resources/org/gvsig/spatialite/dal/SpatiaLiteConfig.xml | ||
---|---|---|
1 |
<?xml version="1.0"?> |
|
2 |
<definitions> |
|
3 |
<version>1.0.0</version> |
|
4 |
<classes> |
|
5 |
<class name="JDBC2"> |
|
6 |
<extends> |
|
7 |
</extends> |
|
8 |
<fields> |
|
9 |
<field name="function_ST_AsWKB" type="string" group="functions" |
|
10 |
defaultValue="ST_AsWKB({0})" > |
|
11 |
<description></description> |
|
12 |
</field> |
|
13 |
<field name="function_ST_envelope" type="string" group="functions" |
|
14 |
defaultValue="ST_envelope({0})" > |
|
15 |
<description></description> |
|
16 |
</field> |
|
17 |
<field name="function_ST_Extent" type="string" group="functions" |
|
18 |
defaultValue="ST_Extent({0})" > |
|
19 |
<description></description> |
|
20 |
</field> |
|
21 |
<field name="function_ST_intersects" type="string" group="functions" |
|
22 |
defaultValue="ST_intersects({0}, {1})" > |
|
23 |
<description></description> |
|
24 |
</field> |
|
25 |
<field name="function_ST_GeomFromText" type="string" group="functions" |
|
26 |
defaultValue="ST_GeomFromText({0}, {1})" > |
|
27 |
<description></description> |
|
28 |
</field> |
|
29 |
<field name="function_ST_GeomFromWKB" type="string" group="functions" |
|
30 |
defaultValue="ST_GeomFromWKB({0}, {1})" > |
|
31 |
<description></description> |
|
32 |
</field> |
|
33 |
<field name="function_ST_GeomFromEWKB" type="string" group="functions" |
|
34 |
defaultValue="ST_GeomFromEWKB({0}, {1})" > |
|
35 |
<description></description> |
|
36 |
</field> |
|
37 |
<field name="function_current_schema" type="string" group="functions" |
|
38 |
defaultValue="current_schema()" > |
|
39 |
<description></description> |
|
40 |
</field> |
|
41 |
<field name="function_lcase" type="string" group="functions" |
|
42 |
defaultValue="LCASE({0})" > |
|
43 |
<description></description> |
|
44 |
</field> |
|
45 |
<field name="function_count" type="string" group="functions" |
|
46 |
defaultValue="count({0})" > |
|
47 |
<description></description> |
|
48 |
</field> |
|
49 |
<field name="function_crs" type="string" group="functions" |
|
50 |
defaultValue="{0}" > |
|
51 |
<description>Use {0} for the abbreviature, {1} for the autority name and {2} for the autority code</description> |
|
52 |
</field> |
|
53 |
|
|
54 |
<!-- operators --> |
|
55 |
<field name="operator_isnull" type="string" group="operators" |
|
56 |
defaultValue="{0} is null" > |
|
57 |
<description></description> |
|
58 |
</field> |
|
59 |
<field name="operator_eq" type="string" group="operators" |
|
60 |
defaultValue="{0} = {1}" > |
|
61 |
<description></description> |
|
62 |
</field> |
|
63 |
<field name="operator_neq" type="string" group="operators" |
|
64 |
defaultValue="{0} <> {1}" > |
|
65 |
<description></description> |
|
66 |
</field> |
|
67 |
<field name="operator_lt" type="string" group="operators" |
|
68 |
defaultValue="{0} < {1}" > |
|
69 |
<description></description> |
|
70 |
</field> |
|
71 |
<field name="operator_le" type="string" group="operators" |
|
72 |
defaultValue="{0} <= {1}" > |
|
73 |
<description></description> |
|
74 |
</field> |
|
75 |
<field name="operator_gt" type="string" group="operators" |
|
76 |
defaultValue="{0} > {1}" > |
|
77 |
<description></description> |
|
78 |
</field> |
|
79 |
<field name="operator_ge" type="string" group="operators" |
|
80 |
defaultValue="{0} >= {1}" > |
|
81 |
<description></description> |
|
82 |
</field> |
|
83 |
<field name="operator_and" type="string" group="operators" |
|
84 |
defaultValue="{0} AND {1}" > |
|
85 |
<description></description> |
|
86 |
</field> |
|
87 |
<field name="operator_or" type="string" group="operators" |
|
88 |
defaultValue="{0} OR {1}" > |
|
89 |
<description></description> |
|
90 |
</field> |
|
91 |
<field name="operator_like" type="string" group="operators" |
|
92 |
defaultValue="{0} LIKE {1}" > |
|
93 |
<description></description> |
|
94 |
</field> |
|
95 |
<field name="operator_ilike" type="string" group="operators" |
|
96 |
defaultValue="{0} ILIKE {1}" > |
|
97 |
<description></description> |
|
98 |
</field> |
|
99 |
|
|
100 |
<!-- properties --> |
|
101 |
<field name="AllowAutomaticValues" type="boolean" group="properties" |
|
102 |
defaultValue="true" > |
|
103 |
<description></description> |
|
104 |
</field> |
|
105 |
<field name="QuoteForStrings" type="string" group="properties" |
|
106 |
defaultValue="'" > |
|
107 |
<description></description> |
|
108 |
</field> |
|
109 |
<field name="QuoteForIdentifiers" type="string" group="properties" |
|
110 |
defaultValue="" > |
|
111 |
<description></description> |
|
112 |
</field> |
|
113 |
<field name="GeometrySupportType" type="string" group="properties" |
|
114 |
defaultValue="WKB" > |
|
115 |
<description></description> |
|
116 |
<availableValues> |
|
117 |
<value label="WKT">WKT</value> |
|
118 |
<value label="WKB">WKB</value> |
|
119 |
<value label="EWKB">EWKB</value> |
|
120 |
</availableValues> |
|
121 |
</field> |
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
</fields> |
|
126 |
</class> |
|
127 |
|
|
128 |
|
|
129 |
<class name="SpatiaLite"> |
|
130 |
<extends> |
|
131 |
<class>JDBC2</class> |
|
132 |
</extends> |
|
133 |
<fields> |
|
134 |
<field name="function_ST_AsWKB" type="string" group="functions" |
|
135 |
defaultValue="({0}).STAsWKB" > |
|
136 |
<description></description> |
|
137 |
</field> |
|
138 |
<field name="function_ST_envelope" type="string" group="functions" |
|
139 |
defaultValue="({0}).STEnvelope" > |
|
140 |
<description></description> |
|
141 |
</field> |
|
142 |
<field name="function_ST_Extent" type="string" group="functions" |
|
143 |
defaultValue="({0}).STEnvelope" > |
|
144 |
<description></description> |
|
145 |
</field> |
|
146 |
<field name="function_ST_intersects" type="string" group="functions" |
|
147 |
defaultValue="({0}).STIntersects({1})" > |
|
148 |
<description></description> |
|
149 |
</field> |
|
150 |
<field name="function_ST_GeomFromText" type="string" group="functions" |
|
151 |
defaultValue="geometry::STGeomFromText({0}, {1})" > |
|
152 |
<description></description> |
|
153 |
</field> |
|
154 |
<field name="function_ST_GeomFromWKB" type="string" group="functions" |
|
155 |
defaultValue="geometry::STGeomFromWKB({0}, {1})" > |
|
156 |
<description></description> |
|
157 |
</field> |
|
158 |
<field name="function_ST_GeomFromEWKB" type="string" group="functions" |
|
159 |
defaultValue="geometry::STGeomFromWKB({0}, {1})" > |
|
160 |
<description></description> |
|
161 |
</field> |
|
162 |
<field name="function_current_schema" type="string" group="functions" |
|
163 |
defaultValue="current_schema()" > |
|
164 |
<description></description> |
|
165 |
</field> |
|
166 |
<field name="function_crs" type="string" group="functions" |
|
167 |
defaultValue="{2}" > |
|
168 |
<description>Use {0} for the abbreviature, {1} for the autority name and {2} for the autority code</description> |
|
169 |
</field> |
|
170 |
<field name="function_lcase" type="string" group="functions" |
|
171 |
defaultValue="LOWER({0})" > |
|
172 |
<description></description> |
|
173 |
</field> |
|
174 |
|
|
175 |
<!-- operators --> |
|
176 |
<field name="operator_ilike" type="string" group="operators" |
|
177 |
defaultValue="{0} ILIKE {1}" > |
|
178 |
<description></description> |
|
179 |
</field> |
|
180 |
|
|
181 |
|
|
182 |
</classes> |
|
183 |
</definitions> |
tags/org.gvsig.spatialite-1.0.19/org.gvsig.spatialite.provider/src/main/resources/org/gvsig/spatialite/dal/SpatiaLiteMetadata.xml | ||
---|---|---|
1 |
<?xml version="1.0"?> |
|
2 |
<definitions> |
|
3 |
<version>1.0.0</version> |
|
4 |
<classes> |
|
5 |
<class name="SpatiaLite" namespace="Metadata"> |
|
6 |
<extends> |
|
7 |
<class name="JDBC2" namespace="Metadata"/> |
|
8 |
</extends> |
|
9 |
<description>Metadata for SpatiaLite</description> |
|
10 |
<fields> |
|
11 |
</fields> |
|
12 |
</class> |
|
13 |
|
|
14 |
</classes> |
|
15 |
</definitions> |
tags/org.gvsig.spatialite-1.0.19/org.gvsig.spatialite.provider/src/main/java/org/gvsig/spatialite/dal/SpatiaLiteHelper.java | ||
---|---|---|
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 java.io.File; |
|
25 |
import org.gvsig.spatialite.dal.operations.SpatiaLiteOperationsFactory; |
|
26 |
import java.sql.Connection; |
|
27 |
import java.sql.SQLException; |
|
28 |
import java.sql.Statement; |
|
29 |
import java.util.Map; |
|
30 |
import org.apache.commons.dbcp.BasicDataSource; |
|
31 |
import org.apache.commons.io.FilenameUtils; |
|
32 |
import org.apache.commons.lang3.StringUtils; |
|
33 |
import org.cresques.cts.IProjection; |
|
34 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
35 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
|
36 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
37 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
|
38 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
|
39 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
40 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
|
41 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
|
42 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
|
43 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase; |
|
44 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
|
45 |
import org.gvsig.fmap.geom.Geometry; |
|
46 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
|
47 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
|
48 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
|
49 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
|
50 |
import org.gvsig.fmap.geom.primitive.Primitive; |
|
51 |
import org.gvsig.fmap.geom.type.GeometryType; |
|
52 |
import org.slf4j.Logger; |
|
53 |
import org.slf4j.LoggerFactory; |
|
54 |
import org.sqlite.SQLiteConfig; |
|
55 |
import org.sqlite.SQLiteConfig.TransactionMode; |
|
56 |
|
|
57 |
public class SpatiaLiteHelper extends JDBCHelperBase { |
|
58 |
|
|
59 |
static final Logger logger = LoggerFactory.getLogger(SpatiaLiteHelper.class); |
|
60 |
|
|
61 |
public static final String NAME = "SpatiaLite"; |
|
62 |
public static final String SpatiaLiteJDBCDriver = "org.sqlite.JDBC"; |
|
63 |
public static final String URL_FORMAT = "jdbc:sqlite:%s"; |
|
64 |
|
|
65 |
public static String getConnectionURL(SpatiaLiteConnectionParameters params) { |
|
66 |
String fname = params.getFile().getAbsolutePath().replace("\\","/"); |
|
67 |
if( StringUtils.isEmpty(FilenameUtils.getExtension(fname)) ) { |
|
68 |
fname = fname + ".sqlite"; |
|
69 |
params.setFile(new File(fname)); |
|
70 |
} |
|
71 |
String driverClassName = params.getJDBCDriverClassName(); |
|
72 |
if( StringUtils.isNotBlank(driverClassName) && |
|
73 |
!StringUtils.equals(driverClassName, SpatiaLiteJDBCDriver) ) { |
|
74 |
try { |
|
75 |
Class.forName(driverClassName); |
|
76 |
} catch(Throwable th) { |
|
77 |
logger.warn("Can't load SpatiaLite JDBC Driver '"+driverClassName+"'.",th); |
|
78 |
} |
|
79 |
} |
|
80 |
String connectionURLFormat = params.getURLFormat(); |
|
81 |
if( StringUtils.isBlank(connectionURLFormat) ) { |
|
82 |
connectionURLFormat = URL_FORMAT; |
|
83 |
} |
|
84 |
String connectionURL = String.format(connectionURLFormat, fname); |
|
85 |
logger.debug("connectionURL: {}", connectionURL); |
|
86 |
return connectionURL; |
|
87 |
} |
|
88 |
|
|
89 |
private static class ConnectionProvider { |
|
90 |
|
|
91 |
private static boolean needRegisterDriver = true; |
|
92 |
|
|
93 |
private BasicDataSource dataSource = null; |
|
94 |
|
|
95 |
private final SpatiaLiteConnectionParameters connectionParameters; |
|
96 |
|
|
97 |
public ConnectionProvider(SpatiaLiteConnectionParameters connectionParameters) { |
|
98 |
this.connectionParameters = connectionParameters; |
|
99 |
} |
|
100 |
|
|
101 |
public Connection getConnection() throws SQLException { |
|
102 |
if (this.dataSource == null) { |
|
103 |
this.dataSource = this.createDataSource(); |
|
104 |
} |
|
105 |
Connection conn = this.dataSource.getConnection(); |
|
106 |
loadExtension(conn); |
|
107 |
return conn; |
|
108 |
} |
|
109 |
|
|
110 |
private BasicDataSource createDataSource() throws SQLException { |
|
111 |
if (!this.isRegistered()) { |
|
112 |
this.registerDriver(); |
|
113 |
} |
|
114 |
SpatiaLiteConnectionParameters params = connectionParameters; |
|
115 |
|
|
116 |
BasicDataSource ds = new BasicDataSource(); |
|
117 |
ds.setDriverClassName(params.getJDBCDriverClassName()); |
|
118 |
if( !StringUtils.isEmpty(params.getUser()) ) { |
|
119 |
ds.setUsername(params.getUser()); |
|
120 |
} |
|
121 |
if( !StringUtils.isEmpty(params.getPassword()) ) { |
|
122 |
ds.setPassword(params.getPassword()); |
|
123 |
} |
|
124 |
ds.setUrl(params.getUrl()); |
|
125 |
SQLiteConfig config = new SQLiteConfig(); |
|
126 |
config.setSharedCache(true); |
|
127 |
config.enableLoadExtension(true); |
|
128 |
config.setTransactionMode(TransactionMode.IMMEDIATE); |
|
129 |
for (Map.Entry e : config.toProperties().entrySet()) { |
|
130 |
ds.addConnectionProperty((String)e.getKey(), (String)e.getValue()); |
|
131 |
} |
|
132 |
ds.setMaxWait(60L * 1000); |
|
133 |
return ds; |
|
134 |
} |
|
135 |
|
|
136 |
private boolean isRegistered() { |
|
137 |
return needRegisterDriver; |
|
138 |
} |
|
139 |
|
|
140 |
public void registerDriver() throws SQLException { |
|
141 |
String className = this.connectionParameters.getJDBCDriverClassName(); |
|
142 |
if (className == null) { |
|
143 |
return; |
|
144 |
} |
|
145 |
try { |
|
146 |
Class theClass = Class.forName(className); |
|
147 |
if (theClass == null) { |
|
148 |
throw new JDBCDriverClassNotFoundException(NAME, className); |
|
149 |
} |
|
150 |
} catch (Exception e) { |
|
151 |
throw new SQLException("Can't register JDBC driver '" + className + "'.", e); |
|
152 |
} |
|
153 |
needRegisterDriver = false; |
|
154 |
} |
|
155 |
|
|
156 |
public void loadExtension(Connection conn) { |
|
157 |
Statement st = null; |
|
158 |
try { |
|
159 |
st = conn.createStatement(); |
|
160 |
st.execute("SELECT load_extension('mod_spatialite')"); |
|
161 |
} catch(Exception ex) { |
|
162 |
logger.warn("Can't load mod_spatialite extension module for SQLite (" + |
|
163 |
" driver class "+StringUtils.defaultString(connectionParameters.getJDBCDriverClassName()) + |
|
164 |
" url format "+StringUtils.defaultString(connectionParameters.getURLFormat()) + |
|
165 |
" file "+connectionParameters.getFile()==null?"null":connectionParameters.getFile().getAbsolutePath() + |
|
166 |
").", ex); |
|
167 |
} |
|
168 |
finally { |
|
169 |
JDBCUtils.closeQuietly(st); |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
173 |
|
|
174 |
private ConnectionProvider connectionProvider = null; |
|
175 |
|
|
176 |
public SpatiaLiteHelper(JDBCConnectionParameters connectionParameters) { |
|
177 |
super(connectionParameters); |
|
178 |
} |
|
179 |
|
|
180 |
@Override |
|
181 |
public synchronized Connection getConnection() throws AccessResourceException { |
|
182 |
try { |
|
183 |
if (this.connectionProvider == null) { |
|
184 |
this.connectionProvider = new ConnectionProvider(this.getConnectionParameters()); |
|
185 |
} |
|
186 |
Connection connection = this.connectionProvider.getConnection(); |
|
187 |
logger.debug("getConnection: connection = "+connection.hashCode()); |
|
188 |
return connection; |
|
189 |
} catch (SQLException ex) { |
|
190 |
throw new AccessResourceException(SpatiaLiteLibrary.NAME, ex); |
|
191 |
} |
|
192 |
} |
|
193 |
|
|
194 |
@Override |
|
195 |
public SpatiaLiteConnectionParameters getConnectionParameters() { |
|
196 |
return (SpatiaLiteConnectionParameters) super.getConnectionParameters(); |
|
197 |
} |
|
198 |
|
|
199 |
@Override |
|
200 |
public String getConnectionURL() { |
|
201 |
return getConnectionURL(this.getConnectionParameters()); |
|
202 |
} |
|
203 |
|
|
204 |
@Override |
|
205 |
protected String getResourceType() { |
|
206 |
return SpatiaLiteLibrary.NAME; |
|
207 |
} |
|
208 |
|
|
209 |
@Override |
|
210 |
public String getProviderName() { |
|
211 |
return SpatiaLiteLibrary.NAME; |
|
212 |
} |
|
213 |
|
|
214 |
@Override |
|
215 |
public JDBCSQLBuilderBase createSQLBuilder() { |
|
216 |
return new SpatiaLiteSQLBuilder(this); |
|
217 |
} |
|
218 |
|
|
219 |
@Override |
|
220 |
public OperationsFactory getOperations() { |
|
221 |
if (this.operationsFactory == null) { |
|
222 |
this.operationsFactory = new SpatiaLiteOperationsFactory(this); |
|
223 |
} |
|
224 |
return operationsFactory; |
|
225 |
} |
|
226 |
|
|
227 |
@Override |
|
228 |
public SQLBuilder.GeometrySupportType getGeometrySupportType() { |
|
229 |
return SQLBuilder.GeometrySupportType.WKB; |
|
230 |
} |
|
231 |
|
|
232 |
@Override |
|
233 |
public boolean hasSpatialFunctions() { |
|
234 |
return true; |
|
235 |
} |
|
236 |
|
|
237 |
@Override |
|
238 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) { |
|
239 |
return true; |
|
240 |
} |
|
241 |
|
|
242 |
@Override |
|
243 |
public String getQuoteForIdentifiers() { |
|
244 |
return "\""; |
|
245 |
} |
|
246 |
|
|
247 |
@Override |
|
248 |
public boolean allowAutomaticValues() { |
|
249 |
return true; |
|
250 |
} |
|
251 |
|
|
252 |
@Override |
|
253 |
public boolean supportOffsetInSelect() { |
|
254 |
return true; |
|
255 |
} |
|
256 |
|
|
257 |
@Override |
|
258 |
public String getQuoteForStrings() { |
|
259 |
return "'"; |
|
260 |
} |
|
261 |
|
|
262 |
@Override |
|
263 |
public String getSourceId(JDBCStoreParameters parameters) { |
|
264 |
return parameters.getDBName() + "." + |
|
265 |
parameters.getSchema()+ "." + |
|
266 |
parameters.getTable(); |
|
267 |
} |
|
268 |
|
|
269 |
@Override |
|
270 |
public JDBCNewStoreParameters createNewStoreParameters() { |
|
271 |
return new SpatiaLiteNewStoreParameters(); |
|
272 |
} |
|
273 |
|
|
274 |
@Override |
|
275 |
public JDBCStoreParameters createOpenStoreParameters() { |
|
276 |
return new SpatiaLiteStoreParameters(); |
|
277 |
} |
|
278 |
|
|
279 |
@Override |
|
280 |
public JDBCServerExplorerParameters createServerExplorerParameters() { |
|
281 |
return new SpatiaLiteExplorerParameters(); |
|
282 |
} |
|
283 |
|
|
284 |
public Geometry forceGeometryType(GeometryType geomtype, Geometry geom) throws CreateGeometryException { |
|
285 |
if( geom == null ) { |
|
286 |
return null; |
|
287 |
} |
|
288 |
switch( geomtype.getType() ) { |
|
289 |
case Geometry.TYPES.MULTIPOLYGON: |
|
290 |
if( geom.getType()==Geometry.TYPES.POLYGON ) { |
|
291 |
MultiPolygon x = getGeometryManager().createMultiPolygon(geomtype.getSubType()); |
|
292 |
x.addPrimitive((Primitive) geom); |
|
293 |
geom = x; |
|
294 |
} |
|
295 |
break; |
|
296 |
case Geometry.TYPES.MULTILINE: |
|
297 |
if( geom.getType()==Geometry.TYPES.LINE ) { |
|
298 |
MultiLine x = getGeometryManager().createMultiLine(geomtype.getSubType()); |
|
299 |
x.addPrimitive((Primitive) geom); |
|
300 |
geom = x; |
|
301 |
} |
|
302 |
break; |
|
303 |
case Geometry.TYPES.MULTIPOINT: |
|
304 |
if( geom.getType()==Geometry.TYPES.POINT ) { |
|
305 |
MultiLine x = getGeometryManager().createMultiLine(geomtype.getSubType()); |
|
306 |
x.addPrimitive((Primitive) geom); |
|
307 |
geom = x; |
|
308 |
} |
|
309 |
break; |
|
310 |
case Geometry.TYPES.POLYGON: |
|
311 |
if( geom.getType()==Geometry.TYPES.MULTIPOLYGON ) { |
|
312 |
MultiPolygon x = (MultiPolygon) geom; |
|
313 |
if( x.getPrimitivesNumber()==1 ) { |
|
314 |
geom = x.getPrimitiveAt(0); |
|
315 |
} |
|
316 |
} |
|
317 |
break; |
|
318 |
case Geometry.TYPES.LINE: |
|
319 |
if( geom.getType()==Geometry.TYPES.MULTILINE ) { |
|
320 |
MultiLine x = (MultiLine) geom; |
|
321 |
if( x.getPrimitivesNumber()==1 ) { |
|
322 |
geom = x.getPrimitiveAt(0); |
|
323 |
} |
|
324 |
} |
|
325 |
break; |
|
326 |
case Geometry.TYPES.POINT: |
|
327 |
if( geom.getType()==Geometry.TYPES.MULTIPOINT ) { |
|
328 |
MultiPoint x = (MultiPoint) geom; |
|
329 |
if( x.getPrimitivesNumber()==1 ) { |
|
330 |
geom = x.getPrimitiveAt(0); |
|
331 |
} |
|
332 |
} |
|
333 |
} |
|
334 |
return geom; |
|
335 |
} |
|
336 |
|
|
337 |
} |
tags/org.gvsig.spatialite-1.0.19/org.gvsig.spatialite.provider/src/main/java/org/gvsig/spatialite/dal/SpatiaLiteStoreProviderFactory.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.spatialite.dal; |
|
3 |
|
|
4 |
import org.gvsig.fmap.dal.DataParameters; |
|
5 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
6 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
|
7 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
8 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
9 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
|
10 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCStoreProvider; |
|
11 |
import org.gvsig.fmap.dal.store.jdbc2.impl.JDBCStoreProviderFactory; |
|
12 |
|
|
13 |
|
|
14 |
public class SpatiaLiteStoreProviderFactory extends JDBCStoreProviderFactory { |
|
15 |
|
|
16 |
private static final String NAME = SpatiaLiteLibrary.NAME; |
|
17 |
|
|
18 |
public SpatiaLiteStoreProviderFactory() { |
|
19 |
super( |
|
20 |
NAME, |
|
21 |
"SpatiaLite store" |
|
22 |
); |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public JDBCStoreProvider createProvider( |
|
27 |
DataParameters parameters, |
|
28 |
DataStoreProviderServices providerServices |
|
29 |
) throws InitializeException { |
|
30 |
JDBCHelper helper = new SpatiaLiteHelper((JDBCConnectionParameters) parameters); |
|
31 |
JDBCStoreProvider provider = helper.createProvider( |
|
32 |
(JDBCStoreParameters) parameters, |
|
33 |
providerServices |
|
34 |
); |
|
35 |
return provider; |
|
36 |
} |
|
37 |
|
|
38 |
@Override |
|
39 |
public JDBCStoreParameters createParameters() { |
|
40 |
JDBCStoreParameters params = new SpatiaLiteStoreParameters(); |
|
41 |
return params; |
|
42 |
} |
|
43 |
|
|
44 |
} |
tags/org.gvsig.spatialite-1.0.19/org.gvsig.spatialite.provider/src/main/java/org/gvsig/spatialite/dal/SpatiaLiteStoreParameters.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 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 2 |
|
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 java.io.File; |
|
25 |
import java.util.Properties; |
|
26 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
27 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
28 |
|
|
29 |
public class SpatiaLiteStoreParameters extends JDBCStoreParameters implements SpatiaLiteConnectionParameters { |
|
30 |
|
|
31 |
private final SpatiaLiteConnectionParametersHelper helper; |
|
32 |
|
|
33 |
public SpatiaLiteStoreParameters() { |
|
34 |
super( |
|
35 |
SpatiaLiteLibrary.NAME + "StoreParameters", |
|
36 |
SpatiaLiteLibrary.NAME |
|
37 |
); |
|
38 |
this.helper = new SpatiaLiteConnectionParametersHelper(this); |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
public String getUrl() { |
|
43 |
return this.helper.getUrl(); |
|
44 |
} |
|
45 |
|
|
46 |
@Override |
|
47 |
public void validate() throws ValidateDataParametersException { |
|
48 |
this.helper.validate(); |
|
49 |
super.validate(); |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
public boolean getEnableSpatiaLite() { |
|
54 |
return this.helper.getEnableSpatiaLite(); |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public boolean getEnableLoadExtension() { |
|
59 |
return this.helper.getEnableLoadExtension(); |
|
60 |
} |
|
61 |
|
|
62 |
@Override |
|
63 |
public boolean getEnableSharedCache() { |
|
64 |
return this.helper.getEnableSharedCache(); |
|
65 |
} |
|
66 |
|
|
67 |
@Override |
|
68 |
public void setEnableSpatiaLite(boolean v) { |
|
69 |
this.helper.setEnableSpatiaLite(v); |
|
70 |
} |
|
71 |
|
|
72 |
@Override |
|
73 |
public void setEnableLoadExtension(boolean v) { |
|
74 |
this.helper.setEnableLoadExtension(v); |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
public void setEnableSharedCache(boolean v) { |
|
79 |
this.helper.setEnableSharedCache(v); |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
|
83 |
public Properties getProperties() { |
|
84 |
return this.helper.getProperties(); |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
|
88 |
public File getFile() { |
|
89 |
return this.helper.getFile(); |
|
90 |
} |
|
91 |
|
|
92 |
@Override |
|
93 |
public void setFile(File database) { |
|
94 |
this.helper.setFile(database); |
|
95 |
} |
|
96 |
|
|
97 |
@Override |
|
98 |
public String getURLFormat() { |
|
99 |
return this.helper.getURLFormat(); |
|
100 |
} |
|
101 |
|
|
102 |
} |
tags/org.gvsig.spatialite-1.0.19/org.gvsig.spatialite.provider/src/main/java/org/gvsig/spatialite/dal/SpatiaLiteConnectionParameters.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.spatialite.dal; |
|
3 |
|
|
4 |
import java.util.Properties; |
|
5 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
|
6 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
7 |
|
|
8 |
public interface SpatiaLiteConnectionParameters extends JDBCConnectionParameters, FilesystemStoreParameters { |
|
9 |
|
|
10 |
public boolean getEnableSpatiaLite(); |
|
11 |
public boolean getEnableLoadExtension(); |
|
12 |
public boolean getEnableSharedCache(); |
|
13 |
|
|
14 |
public void setEnableSpatiaLite(boolean v); |
|
15 |
public void setEnableLoadExtension(boolean v); |
|
16 |
public void setEnableSharedCache(boolean v); |
|
17 |
|
|
18 |
public Properties getProperties(); |
|
19 |
|
|
20 |
public String getURLFormat(); |
|
21 |
} |
tags/org.gvsig.spatialite-1.0.19/org.gvsig.spatialite.provider/src/main/java/org/gvsig/spatialite/dal/SpatiaLiteConnectionParametersHelper.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.spatialite.dal; |
|
3 |
|
|
4 |
import java.io.File; |
|
5 |
import java.util.Properties; |
|
6 |
import org.apache.commons.io.FilenameUtils; |
|
7 |
import org.apache.commons.lang3.BooleanUtils; |
|
8 |
import org.apache.commons.lang3.StringUtils; |
|
9 |
import org.gvsig.fmap.dal.DataParameters; |
|
10 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
11 |
import org.gvsig.fmap.dal.resource.db.DBParameters; |
|
12 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
13 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
14 |
|
|
15 |
|
|
16 |
public class SpatiaLiteConnectionParametersHelper { |
|
17 |
|
|
18 |
private final JDBCConnectionParameters parameters; |
|
19 |
|
|
20 |
private static final String ENABLE_SHARED_CACHE = "enable_shared_cache"; |
Also available in: Unified diff