svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / test / java / org / gvsig / fmap / dal / store / SQLBuilderTest.java @ 44669
History | View | Annotate | Download (19.4 KB)
1 |
package org.gvsig.fmap.dal.store; |
---|---|
2 |
|
3 |
import org.apache.commons.lang3.ArrayUtils; |
4 |
import org.cresques.cts.IProjection; |
5 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
6 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
7 |
import org.gvsig.fmap.crs.CRSFactory; |
8 |
import org.gvsig.fmap.dal.SQLBuilder; |
9 |
import org.gvsig.fmap.dal.SQLBuilder.Privilege; |
10 |
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase; |
11 |
import org.gvsig.fmap.geom.DataTypes; |
12 |
import org.gvsig.fmap.geom.Geometry; |
13 |
import org.gvsig.fmap.geom.GeometryLocator; |
14 |
import org.gvsig.fmap.geom.GeometryManager; |
15 |
import org.gvsig.fmap.geom.primitive.Polygon; |
16 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
17 |
|
18 |
public class SQLBuilderTest extends AbstractLibraryAutoInitTestCase { |
19 |
|
20 |
@Override
|
21 |
protected void doSetUp() throws Exception { |
22 |
|
23 |
} |
24 |
|
25 |
private SQLBuilder createSQLBuilder() {
|
26 |
return new SQLBuilderBase(); |
27 |
} |
28 |
|
29 |
public void testCalulateEnvelope() throws Exception { |
30 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
31 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
32 |
|
33 |
Polygon limit = geometryManager.createPolygon(Geometry.SUBTYPES.GEOM2D);
|
34 |
limit.addVertex(0, 0); |
35 |
limit.addVertex(0, 100); |
36 |
limit.addVertex(100, 100); |
37 |
limit.addVertex(100, 0); |
38 |
limit.addVertex(0, 0); |
39 |
|
40 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
41 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
42 |
|
43 |
sqlbuilder.select().column().value( |
44 |
expbuilder.as_geometry( |
45 |
expbuilder.ST_ExtentAggregate( |
46 |
expbuilder.column("the_geom")
|
47 |
) |
48 |
) |
49 |
).as("envelope");
|
50 |
sqlbuilder.select().from().table().database("master").schema("dbo").name("test1"); |
51 |
sqlbuilder.select().where().set( |
52 |
expbuilder.ST_Intersects( |
53 |
expbuilder.ST_Envelope( |
54 |
expbuilder.column("the_geom")
|
55 |
), |
56 |
expbuilder.geometry(limit, proj) |
57 |
) |
58 |
); |
59 |
sqlbuilder.select().where().and( |
60 |
expbuilder.custom("x = 27")
|
61 |
); |
62 |
|
63 |
// SELECT ST_AsBinary(ST_Extent("the_geom")) AS "envelope" FROM "master"."dbo"."test1" WHERE ST_Intersects((ST_Envelope("the_geom")), (ST_GeomFromText('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0))', (4326)))) AND x = 27
|
64 |
|
65 |
System.out.println("# Test:: testCalulateEnvelope"); |
66 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
67 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
68 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
69 |
assertEquals( |
70 |
"SELECT ST_AsBinary(ST_Extent(\"the_geom\")) AS \"envelope\" FROM \"master\".\"dbo\".\"test1\" WHERE ST_Intersects((ST_Envelope(\"the_geom\")), (ST_GeomFromText('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0))', (4326)))) AND x = 27",
|
71 |
sqlbuilder.toString() |
72 |
); |
73 |
assertEquals( |
74 |
"[the_geom]",
|
75 |
ArrayUtils.toString(expbuilder.variables_names()) |
76 |
); |
77 |
assertEquals( |
78 |
"[]",
|
79 |
ArrayUtils.toString(expbuilder.parameters_names()) |
80 |
); |
81 |
} |
82 |
|
83 |
public void testCount() throws Exception { |
84 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
85 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
86 |
|
87 |
sqlbuilder.select().column().value(sqlbuilder.count().all()); |
88 |
sqlbuilder.select().from().table().database("master").schema("dbo").name("test1"); |
89 |
sqlbuilder.select().from().subquery(null);
|
90 |
sqlbuilder.select().where().set( expbuilder.custom("pp = 200"));
|
91 |
|
92 |
System.out.println("# Test:: testCount"); |
93 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
94 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
95 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
96 |
|
97 |
//# Test:: testCount
|
98 |
//# SQL:: SELECT COUNT(*) FROM "master"."dbo"."test1" WHERE pp = 200
|
99 |
//# Variables:: []
|
100 |
//# Parametros:: []
|
101 |
|
102 |
assertEquals( |
103 |
"SELECT COUNT(*) FROM \"master\".\"dbo\".\"test1\" WHERE pp = 200",
|
104 |
sqlbuilder.toString() |
105 |
); |
106 |
assertEquals( |
107 |
"[]",
|
108 |
ArrayUtils.toString(expbuilder.variables_names()) |
109 |
); |
110 |
assertEquals( |
111 |
"[]",
|
112 |
ArrayUtils.toString(expbuilder.parameters_names()) |
113 |
); |
114 |
} |
115 |
|
116 |
public void testCreateTable() throws Exception { |
117 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
118 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
119 |
|
120 |
sqlbuilder.create_table().table().database("master").schema("dbo").name("test1"); |
121 |
sqlbuilder.create_table().add_column( |
122 |
"name",
|
123 |
DataTypes.STRING, |
124 |
45,
|
125 |
0,
|
126 |
0,
|
127 |
false,
|
128 |
false,
|
129 |
true,
|
130 |
false,
|
131 |
null
|
132 |
); |
133 |
sqlbuilder.create_table().add_column( |
134 |
"id",
|
135 |
DataTypes.INT, |
136 |
0,
|
137 |
0,
|
138 |
0,
|
139 |
true,
|
140 |
false,
|
141 |
false,
|
142 |
true,
|
143 |
0
|
144 |
); |
145 |
sqlbuilder.create_table().add_column( |
146 |
"geom",
|
147 |
DataTypes.GEOMETRY, |
148 |
0,
|
149 |
0,
|
150 |
0,
|
151 |
false,
|
152 |
false,
|
153 |
true,
|
154 |
false,
|
155 |
null
|
156 |
); |
157 |
|
158 |
|
159 |
// CREATE TABLE "master"."dbo"."test1" ("name" VARCHAR(45) DEFAULT NULL NULL, "id" SERIAL DEFAULT '0' NOT NULL PRIMARY KEY, "geom" TEXT DEFAULT NULL NULL )
|
160 |
System.out.println("# Test:: testCreateTable"); |
161 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
162 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
163 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
164 |
assertEquals( |
165 |
"CREATE TABLE \"master\".\"dbo\".\"test1\" (\"name\" VARCHAR(45) DEFAULT NULL NULL, \"id\" SERIAL DEFAULT '0' NOT NULL PRIMARY KEY, \"geom\" TEXT DEFAULT NULL NULL )",
|
166 |
sqlbuilder.toString() |
167 |
); |
168 |
assertEquals( |
169 |
"[]",
|
170 |
ArrayUtils.toString(expbuilder.variables_names()) |
171 |
); |
172 |
assertEquals( |
173 |
"[]",
|
174 |
ArrayUtils.toString(expbuilder.parameters_names()) |
175 |
); |
176 |
} |
177 |
|
178 |
public void testDropTable() throws Exception { |
179 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
180 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
181 |
|
182 |
sqlbuilder.drop_table().table().database("master").schema("dbo").name("test1"); |
183 |
|
184 |
// DROP TABLE "master"."dbo"."test1"; DELETE FROM GEOMETRY_COLUMNS WHERE f_table_schema = 'dbo' AND f_table_name = 'test1'
|
185 |
|
186 |
System.out.println("# Test:: testDropTable"); |
187 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
188 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
189 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
190 |
assertEquals( |
191 |
"DROP TABLE \"master\".\"dbo\".\"test1\"; DELETE FROM GEOMETRY_COLUMNS WHERE f_table_schema = 'dbo' AND f_table_name = 'test1'",
|
192 |
sqlbuilder.toString() |
193 |
); |
194 |
assertEquals( |
195 |
"[]",
|
196 |
ArrayUtils.toString(expbuilder.variables_names()) |
197 |
); |
198 |
assertEquals( |
199 |
"[]",
|
200 |
ArrayUtils.toString(expbuilder.parameters_names()) |
201 |
); |
202 |
} |
203 |
|
204 |
public void testFetchFeatureProviderByReference() throws Exception { |
205 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
206 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
207 |
|
208 |
String value = "yoyo"; |
209 |
sqlbuilder.select().column().name("name");
|
210 |
sqlbuilder.select().column().name("id");
|
211 |
sqlbuilder.select().column().name("geom").as_geometry();
|
212 |
sqlbuilder.select().from().table().database("master").schema("dbo").name("test1"); |
213 |
sqlbuilder.select().where().set( |
214 |
expbuilder.eq( |
215 |
expbuilder.column("name"),
|
216 |
expbuilder.parameter(value).as_constant() |
217 |
) |
218 |
); |
219 |
sqlbuilder.select().limit(1);
|
220 |
|
221 |
// SELECT "name", "id", ST_AsBinary("geom") FROM "master"."dbo"."test1" WHERE ( ("name") = (?) ) LIMIT 1
|
222 |
|
223 |
System.out.println("# Test:: testFetchFeatureProviderByReference"); |
224 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
225 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
226 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
227 |
assertEquals( |
228 |
"SELECT \"name\", \"id\", ST_AsBinary(\"geom\") FROM \"master\".\"dbo\".\"test1\" WHERE ( (\"name\") = (?) ) LIMIT 1",
|
229 |
sqlbuilder.toString() |
230 |
); |
231 |
assertEquals( |
232 |
"[geom, id, name]",
|
233 |
ArrayUtils.toString(expbuilder.variables_names()) |
234 |
); |
235 |
assertEquals( |
236 |
"['yoyo']",
|
237 |
ArrayUtils.toString(expbuilder.parameters_names()) |
238 |
); |
239 |
} |
240 |
|
241 |
public void testFetchFeatureType() throws Exception { |
242 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
243 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
244 |
|
245 |
sqlbuilder.select().column().all(); |
246 |
sqlbuilder.select().from().table().database("master").schema("dbo").name("test1"); |
247 |
sqlbuilder.select().limit(1);
|
248 |
|
249 |
System.out.println("# Test:: testFetchFeatureType"); |
250 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
251 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
252 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
253 |
|
254 |
//# Test:: testFetchFeatureType
|
255 |
//# SQL:: SELECT * FROM [master].[dbo].[test1] LIMMIT 1
|
256 |
//# Variables:: []
|
257 |
//# Parametros:: []
|
258 |
|
259 |
assertEquals( |
260 |
"SELECT * FROM \"master\".\"dbo\".\"test1\" LIMIT 1",
|
261 |
sqlbuilder.toString() |
262 |
); |
263 |
assertEquals( |
264 |
"[]",
|
265 |
ArrayUtils.toString(expbuilder.variables_names()) |
266 |
); |
267 |
assertEquals( |
268 |
"[]",
|
269 |
ArrayUtils.toString(expbuilder.parameters_names()) |
270 |
); |
271 |
} |
272 |
|
273 |
public void testPerformDeletes() throws Exception { |
274 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
275 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
276 |
|
277 |
sqlbuilder.delete().table().database("master").schema("dbo").name("test1"); |
278 |
sqlbuilder.delete().where().and( |
279 |
expbuilder.eq( |
280 |
expbuilder.column("id1"),
|
281 |
expbuilder.parameter("id1").as_variable()
|
282 |
) |
283 |
); |
284 |
sqlbuilder.delete().where().and( |
285 |
expbuilder.eq( |
286 |
expbuilder.column("id2"),
|
287 |
expbuilder.parameter("id2").as_variable()
|
288 |
) |
289 |
); |
290 |
|
291 |
// DELETE FROM "master"."dbo"."test1" WHERE ( ("id1") = (?) ) AND ( ("id2") = (?) )
|
292 |
|
293 |
System.out.println("# Test:: testPerformDeletes"); |
294 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
295 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
296 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
297 |
assertEquals( |
298 |
"DELETE FROM \"master\".\"dbo\".\"test1\" WHERE ( (\"id1\") = (?) ) AND ( (\"id2\") = (?) )",
|
299 |
sqlbuilder.toString() |
300 |
); |
301 |
assertEquals( |
302 |
"[id1, id2]",
|
303 |
ArrayUtils.toString(expbuilder.variables_names()) |
304 |
); |
305 |
assertEquals( |
306 |
"[\"id1\", \"id2\"]",
|
307 |
ArrayUtils.toString(expbuilder.parameters_names()) |
308 |
); |
309 |
} |
310 |
|
311 |
public void testPerformInserts1() throws Exception { |
312 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
313 |
|
314 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
315 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
316 |
|
317 |
sqlbuilder.insert().table().database("master").schema("dbo").name("test1"); |
318 |
sqlbuilder.insert().column().name("id").with_value(expbuilder.parameter("id")); |
319 |
sqlbuilder.insert().column().name("name").with_value(expbuilder.parameter("name")); |
320 |
sqlbuilder.insert().column().name("geom").with_value(expbuilder.parameter("geom").as_variable().srs(proj)); |
321 |
|
322 |
// INSERT INTO "master"."dbo"."test1" ( "id", "name", "geom" ) VALUES ( ?, ?, ST_GeomFromText(?, (4326)) )
|
323 |
|
324 |
System.out.println("# Test:: testPerformInserts1"); |
325 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
326 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
327 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
328 |
assertEquals( |
329 |
"INSERT INTO \"master\".\"dbo\".\"test1\" ( \"id\", \"name\", \"geom\" ) VALUES ( ?, ?, ST_GeomFromText(?, (4326)) )",
|
330 |
sqlbuilder.toString() |
331 |
); |
332 |
assertEquals( |
333 |
"[geom, id, name]",
|
334 |
ArrayUtils.toString(expbuilder.variables_names()) |
335 |
); |
336 |
assertEquals( |
337 |
"[\"id\", \"name\", \"geom\"]",
|
338 |
ArrayUtils.toString(expbuilder.parameters_names()) |
339 |
); |
340 |
} |
341 |
|
342 |
public void testPerformInserts2() throws Exception { |
343 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
344 |
|
345 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
346 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
347 |
|
348 |
sqlbuilder.insert().table().database("master").schema("dbo").name("test1"); |
349 |
sqlbuilder.insert().column().name("id").with_value(expbuilder.parameter("id")); |
350 |
sqlbuilder.insert().column().name("name").with_value(expbuilder.parameter("name")); |
351 |
sqlbuilder.insert().column().name("geom").with_value(expbuilder.parameter("geom").as_variable().srs(expbuilder.parameter().value(proj))); |
352 |
|
353 |
System.out.println("# Test:: testPerformInserts2"); |
354 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
355 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
356 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
357 |
assertEquals( |
358 |
"INSERT INTO \"master\".\"dbo\".\"test1\" ( \"id\", \"name\", \"geom\" ) VALUES ( ?, ?, ST_GeomFromText(?, (?)) )",
|
359 |
sqlbuilder.toString() |
360 |
); |
361 |
assertEquals( |
362 |
"[geom, id, name]",
|
363 |
ArrayUtils.toString(expbuilder.variables_names()) |
364 |
); |
365 |
assertEquals( |
366 |
"[\"id\", \"name\", \"geom\", 4326]",
|
367 |
ArrayUtils.toString(expbuilder.parameters_names()) |
368 |
); |
369 |
} |
370 |
|
371 |
public void testPerformUpdates() throws Exception { |
372 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
373 |
|
374 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
375 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
376 |
|
377 |
sqlbuilder.update().table().database("master").schema("dbo").name("test1"); |
378 |
sqlbuilder.update().where().and( |
379 |
expbuilder.eq( |
380 |
expbuilder.column("id"),
|
381 |
expbuilder.parameter("id").as_variable()
|
382 |
) |
383 |
); |
384 |
sqlbuilder.update().column().name("name").with_value(expbuilder.parameter("name")); |
385 |
sqlbuilder.update().column().name("geom").with_value(
|
386 |
expbuilder.parameter("geom").as_variable()
|
387 |
.srs(expbuilder.parameter().value(proj)) |
388 |
); |
389 |
|
390 |
// UPDATE "master"."dbo"."test1" SET "name" = ?, "geom" = ST_GeomFromText(?, (?)) WHERE ( ("id") = (?) )
|
391 |
|
392 |
System.out.println("# Test:: testPerformUpdates"); |
393 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
394 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
395 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
396 |
assertEquals( |
397 |
"UPDATE \"master\".\"dbo\".\"test1\" SET \"name\" = ?, \"geom\" = ST_GeomFromText(?, (?)) WHERE ( (\"id\") = (?) )",
|
398 |
sqlbuilder.toString() |
399 |
); |
400 |
assertEquals( |
401 |
"[geom, id, name]",
|
402 |
ArrayUtils.toString(expbuilder.variables_names()) |
403 |
); |
404 |
assertEquals( |
405 |
"[\"name\", \"geom\", 4326, \"id\"]",
|
406 |
ArrayUtils.toString(expbuilder.parameters_names()) |
407 |
); |
408 |
} |
409 |
|
410 |
public void testGrant1() throws Exception { |
411 |
|
412 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
413 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
414 |
|
415 |
sqlbuilder.grant().table().database("master").schema("dbo").name("test1"); |
416 |
sqlbuilder.grant().role("prueba").select().insert().update();
|
417 |
sqlbuilder.grant().role("gis").all();
|
418 |
|
419 |
|
420 |
System.out.println("# Test:: testGrant1"); |
421 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
422 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
423 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
424 |
assertEquals( |
425 |
"",
|
426 |
sqlbuilder.toString() |
427 |
); |
428 |
assertEquals( |
429 |
"[]",
|
430 |
ArrayUtils.toString(expbuilder.variables_names()) |
431 |
); |
432 |
assertEquals( |
433 |
"[]",
|
434 |
ArrayUtils.toString(expbuilder.parameters_names()) |
435 |
); |
436 |
} |
437 |
|
438 |
public void testGrant2() throws Exception { |
439 |
|
440 |
SQLBuilder sqlbuilder = createSQLBuilder(); |
441 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
442 |
|
443 |
sqlbuilder.grant().table().database("master").schema("dbo").name("test1"); |
444 |
sqlbuilder.grant().role("prueba").privilege(Privilege.SELECT)
|
445 |
.privilege(Privilege.INSERT) |
446 |
.privilege(Privilege.UPDATE); |
447 |
sqlbuilder.grant().role("gis").privilege(Privilege.ALL);
|
448 |
|
449 |
|
450 |
System.out.println("# Test:: testGrant2"); |
451 |
System.out.println("# SQL:: " + sqlbuilder.toString()); |
452 |
System.out.println("# Variables:: " + ArrayUtils.toString(expbuilder.variables_names())); |
453 |
System.out.println("# Parametros:: " + ArrayUtils.toString(expbuilder.parameters_names())); |
454 |
assertEquals( |
455 |
"",
|
456 |
sqlbuilder.toString() |
457 |
); |
458 |
assertEquals( |
459 |
"[]",
|
460 |
ArrayUtils.toString(expbuilder.variables_names()) |
461 |
); |
462 |
assertEquals( |
463 |
"[]",
|
464 |
ArrayUtils.toString(expbuilder.parameters_names()) |
465 |
); |
466 |
} |
467 |
|
468 |
|
469 |
} |