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 / expressionevaluator / TestGrammarCompiler.java @ 47735
History | View | Annotate | Download (34.8 KB)
1 | 44738 | jjdelcerro | package org.gvsig.expressionevaluator; |
---|---|---|---|
2 | 44139 | jjdelcerro | |
3 | 45080 | jjdelcerro | import java.util.Objects; |
4 | 47141 | fdiaz | import javax.json.JsonObject; |
5 | 44139 | jjdelcerro | import junit.framework.TestCase; |
6 | 46678 | fdiaz | import static org.gvsig.fmap.dal.SQLBuilder.PROP_SQLBUILDER; |
7 | import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase; |
||
8 | 47141 | fdiaz | import org.gvsig.json.Json; |
9 | 44139 | jjdelcerro | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
10 | 47735 | jjdelcerro | import org.slf4j.Logger; |
11 | import org.slf4j.LoggerFactory; |
||
12 | 44139 | jjdelcerro | |
13 | /**
|
||
14 | *
|
||
15 | * @author jjdelcerro
|
||
16 | */
|
||
17 | public class TestGrammarCompiler extends TestCase { |
||
18 | |||
19 | 47735 | jjdelcerro | private static Logger LOGGER = LoggerFactory.getLogger(TestGrammarCompiler.class); |
20 | |||
21 | 44139 | jjdelcerro | public TestGrammarCompiler(String testName) { |
22 | super(testName);
|
||
23 | } |
||
24 | |||
25 | @Override
|
||
26 | protected void setUp() throws Exception { |
||
27 | super.setUp();
|
||
28 | new DefaultLibrariesInitializer().fullInitialize();
|
||
29 | } |
||
30 | |||
31 | @Override
|
||
32 | protected void tearDown() throws Exception { |
||
33 | super.tearDown();
|
||
34 | } |
||
35 | |||
36 | // TODO add test methods here. The name must begin with 'test'. For example:
|
||
37 | // public void testHello() {}
|
||
38 | |||
39 | protected LexicalAnalyzer createLexicalAnalyzer() {
|
||
40 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
41 | LexicalAnalyzer lexer = manager.createLexicalAnalyzer(); |
||
42 | return lexer;
|
||
43 | } |
||
44 | |||
45 | protected org.gvsig.expressionevaluator.Compiler createCompiler() {
|
||
46 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
47 | Compiler compiler = manager.createCompiler();
|
||
48 | compiler.setLexicalAnalyzer(createLexicalAnalyzer()); |
||
49 | return compiler;
|
||
50 | } |
||
51 | |||
52 | protected SymbolTable createSymbolTable() {
|
||
53 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
54 | MutableSymbolTable symbolTable = manager.createSymbolTable(); |
||
55 | symbolTable.setVar("precio", 200); |
||
56 | symbolTable.setVar("1990", 0.168873933773767); |
||
57 | return symbolTable;
|
||
58 | } |
||
59 | |||
60 | protected Interpreter createInterpreter(SymbolTable symbolTable) {
|
||
61 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
62 | Interpreter interpreter = manager.createInterpreter(); |
||
63 | interpreter.setSymbolTable(symbolTable); |
||
64 | return interpreter;
|
||
65 | } |
||
66 | 44738 | jjdelcerro | |
67 | private void link(Code code) { |
||
68 | code.link(createSymbolTable()); |
||
69 | 44139 | jjdelcerro | } |
70 | |||
71 | 45080 | jjdelcerro | private void dump(String testname, Object expected, Object actual) { |
72 | System.out.println("### ---------------------"); |
||
73 | System.out.println("### "+testname); |
||
74 | 47117 | jjdelcerro | System.out.println("### expected: ["+Objects.toString(expected)+"]"); |
75 | System.out.println("### actual : ["+Objects.toString(actual)+"]"); |
||
76 | 45080 | jjdelcerro | } |
77 | |||
78 | private void checkEquals(String testname, Object expected, Object actual) { |
||
79 | dump(testname,expected,actual); |
||
80 | assertEquals(expected, actual); |
||
81 | } |
||
82 | |||
83 | 47117 | jjdelcerro | private void checkEquals(Object expected, Object actual) { |
84 | try {
|
||
85 | throw new RuntimeException(); |
||
86 | } catch(Throwable t) { |
||
87 | String testname = t.getStackTrace()[1].getMethodName(); |
||
88 | dump(testname,expected,actual); |
||
89 | assertEquals(expected, actual); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | private String replaceExists(String s) { |
||
94 | String s2 = s.replaceAll("'EXISTS[a-zA-Z0-9]*'", "'EXISTS0123456789abcdef'"); |
||
95 | return s2;
|
||
96 | } |
||
97 | |||
98 | private String replaceExists(Code code) { |
||
99 | return replaceExists(code.toString());
|
||
100 | } |
||
101 | |||
102 | 44738 | jjdelcerro | public void testSelect() { |
103 | StringBuilder source = new StringBuilder(); |
||
104 | 47117 | jjdelcerro | source.append("SELECT * FROM countries");
|
105 | |||
106 | Compiler compiler = createCompiler();
|
||
107 | |||
108 | Code code = compiler.compileExpression(source.toString()); |
||
109 | checkEquals("testSelect", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), NULL)", code.toString()); |
||
110 | link(code); |
||
111 | checkEquals("testSelect", "( SELECT * FROM \"countries\" )", code.toString()); |
||
112 | } |
||
113 | |||
114 | public void testSelect1() { |
||
115 | StringBuilder source = new StringBuilder(); |
||
116 | 44738 | jjdelcerro | source.append("SELECT * FROM countries;");
|
117 | 44139 | jjdelcerro | |
118 | Compiler compiler = createCompiler();
|
||
119 | 47117 | jjdelcerro | compiler.addCompatibility("SELECT","OPTIONAL_SEMICOLON_AT_END"); |
120 | 44139 | jjdelcerro | |
121 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
122 | 47117 | jjdelcerro | checkEquals("testSelect1", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), NULL)", code.toString()); |
123 | 44738 | jjdelcerro | link(code); |
124 | 47117 | jjdelcerro | checkEquals("testSelect1", "( SELECT * FROM \"countries\" )", code.toString()); |
125 | 44139 | jjdelcerro | } |
126 | 45080 | jjdelcerro | |
127 | 47141 | fdiaz | public void testSelectInExpression1() { |
128 | StringBuilder source = new StringBuilder(); |
||
129 | source.append("{ \"phrase\" : \"SELECT * FROM countries;\" }");
|
||
130 | |||
131 | JsonObject exp_json = Json.createObject(source.toString()); |
||
132 | Expression exp = ExpressionUtils.createExpressionFromJson(exp_json);
|
||
133 | |||
134 | checkEquals("( SELECT * FROM \"countries\" )", exp.getPhrase());
|
||
135 | |||
136 | } |
||
137 | |||
138 | public void testSelectInExpression2() { |
||
139 | StringBuilder source = new StringBuilder(); |
||
140 | source.append("\n" +
|
||
141 | "{\n" +
|
||
142 | " \"__classname__\":\"org.gvsig.expressionevaluator.impl.DefaultExpression\",\n" +
|
||
143 | " \"phrase\":\"SELECT * FROM countries;\",\n" +
|
||
144 | " \"version\":\"2.6.0-1\"\n" +
|
||
145 | "}");
|
||
146 | |||
147 | JsonObject exp_json = Json.createObject(source.toString()); |
||
148 | Expression exp = ExpressionUtils.createExpressionFromJson(exp_json);
|
||
149 | |||
150 | // Esta comprobaci?n es para asegurarnos de que en la versi?n "2.6.0-1" no elimina el ";" aunque la sentencia sea incorrecta
|
||
151 | checkEquals("SELECT * FROM countries;", exp.getPhrase());
|
||
152 | } |
||
153 | |||
154 | public void testSelectInExpression3() { |
||
155 | StringBuilder source = new StringBuilder(); |
||
156 | source.append("\n" +
|
||
157 | "{\n" +
|
||
158 | " \"__classname__\":\"org.gvsig.expressionevaluator.impl.DefaultExpression\",\n" +
|
||
159 | " \"phrase\":\"SELECT * FROM countries\",\n" +
|
||
160 | " \"version\":\"2.6.0-1\"\n" +
|
||
161 | "}");
|
||
162 | |||
163 | JsonObject exp_json = Json.createObject(source.toString()); |
||
164 | Expression exp = ExpressionUtils.createExpressionFromJson(exp_json);
|
||
165 | |||
166 | // Esta comprobaci?n es para asegurarnos de que en la versi?n "2.6.0-1" no la toca
|
||
167 | checkEquals("SELECT * FROM countries", exp.getPhrase());
|
||
168 | } |
||
169 | |||
170 | 46678 | fdiaz | public void testSelectToValue() { |
171 | StringBuilder source = new StringBuilder(); |
||
172 | 47117 | jjdelcerro | source.append("SELECT * FROM countries");
|
173 | 46678 | fdiaz | |
174 | Compiler compiler = createCompiler();
|
||
175 | |||
176 | Code code = compiler.compileExpression(source.toString()); |
||
177 | link(code); |
||
178 | |||
179 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
180 | builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase());
|
||
181 | checkEquals("testSelectToValue", "( SELECT * FROM \"countries\" )", code.toValue(builder).toString()); |
||
182 | } |
||
183 | |||
184 | public void testSelectCount() { |
||
185 | StringBuilder source = new StringBuilder(); |
||
186 | 47117 | jjdelcerro | source.append("SELECT COUNT(*) FROM countries");
|
187 | 46678 | fdiaz | |
188 | Compiler compiler = createCompiler();
|
||
189 | |||
190 | Code code = compiler.compileExpression(source.toString()); |
||
191 | link(code); |
||
192 | 47117 | jjdelcerro | checkEquals("testSelectCount", "( SELECT COUNT(*) FROM \"countries\" )", code.toString()); |
193 | 46678 | fdiaz | } |
194 | |||
195 | 46954 | fdiaz | public void testSelectAggregate() { |
196 | StringBuilder source = new StringBuilder(); |
||
197 | 47117 | jjdelcerro | source.append("SELECT SUM(\"TOTAL_MUERTOS\") FROM \"ARENA2_ACCIDENTES\"");
|
198 | 46954 | fdiaz | |
199 | Compiler compiler = createCompiler();
|
||
200 | |||
201 | Code code = compiler.compileExpression(source.toString()); |
||
202 | link(code); |
||
203 | 47117 | jjdelcerro | checkEquals("testSelectAggregate", "( SELECT SUM(\"TOTAL_MUERTOS\") FROM \"ARENA2_ACCIDENTES\" )", code.toString()); |
204 | 46954 | fdiaz | } |
205 | |||
206 | 46678 | fdiaz | public void testSelectCountToValue() { |
207 | StringBuilder source = new StringBuilder(); |
||
208 | 47117 | jjdelcerro | source.append("SELECT COUNT(*) FROM countries");
|
209 | 46678 | fdiaz | |
210 | Compiler compiler = createCompiler();
|
||
211 | |||
212 | Code code = compiler.compileExpression(source.toString()); |
||
213 | link(code); |
||
214 | |||
215 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
216 | builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase());
|
||
217 | 47117 | jjdelcerro | checkEquals("testSelectCountToValue", "( SELECT COUNT(*) FROM \"countries\" )", code.toValue(builder).toString()); |
218 | 46678 | fdiaz | } |
219 | |||
220 | 44738 | jjdelcerro | public void testSelect2() { |
221 | StringBuilder source = new StringBuilder(); |
||
222 | source.append("BEGIN ");
|
||
223 | source.append(" SET X = 0; ");
|
||
224 | 47117 | jjdelcerro | source.append(" FOR row in SELECT * FROM countries "); // Con ; |
225 | 46960 | jjdelcerro | source.append(" LOOP ");
|
226 | 44738 | jjdelcerro | source.append(" IF row.LASTCENSUS > 0 THEN ");
|
227 | source.append(" SET X = X + row.LASTCENSUS ");
|
||
228 | source.append(" END IF ");
|
||
229 | 46960 | jjdelcerro | source.append(" END LOOP ");
|
230 | 44738 | jjdelcerro | source.append("END");
|
231 | 44384 | jjdelcerro | |
232 | Compiler compiler = createCompiler();
|
||
233 | |||
234 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
235 | link(code); |
||
236 | 47735 | jjdelcerro | checkEquals("testSelect2", "BEGIN \"X\" := 0; FOR \"row\" IN ( SELECT * FROM \"countries\" ) LOOP IF (\"row\".\"LASTCENSUS\" > 0) THEN \"X\" := (\"X\" + \"row\".\"LASTCENSUS\"); END IF END LOOP; END ", code.toString()); |
237 | 44384 | jjdelcerro | } |
238 | |||
239 | 44750 | jjdelcerro | public void testSelect3() { |
240 | StringBuilder source = new StringBuilder(); |
||
241 | source.append("BEGIN ");
|
||
242 | source.append(" SET X = 0; ");
|
||
243 | 47117 | jjdelcerro | source.append(" FOR row in (SELECT * FROM countries) "); // Con parentesis |
244 | 46960 | jjdelcerro | source.append(" LOOP ");
|
245 | 44750 | jjdelcerro | source.append(" IF row.LASTCENSUS > 0 THEN ");
|
246 | source.append(" SET X = X + row.LASTCENSUS ");
|
||
247 | source.append(" END IF ");
|
||
248 | 46960 | jjdelcerro | source.append(" END LOOP ");
|
249 | 44750 | jjdelcerro | source.append("END");
|
250 | |||
251 | Compiler compiler = createCompiler();
|
||
252 | |||
253 | Code code = compiler.compileExpression(source.toString()); |
||
254 | link(code); |
||
255 | 47735 | jjdelcerro | checkEquals("testSelect3", "BEGIN \"X\" := 0; FOR \"row\" IN ( SELECT * FROM \"countries\" ) LOOP IF (\"row\".\"LASTCENSUS\" > 0) THEN \"X\" := (\"X\" + \"row\".\"LASTCENSUS\"); END IF END LOOP; END ", code.toString()); |
256 | 44750 | jjdelcerro | } |
257 | |||
258 | 46960 | jjdelcerro | public void testSelect4() { |
259 | StringBuilder source = new StringBuilder(); |
||
260 | source.append("BEGIN ");
|
||
261 | source.append(" SET X = 0; ");
|
||
262 | source.append(" FOR row in SELECT * FROM countries ");
|
||
263 | source.append(" LOOP ");
|
||
264 | source.append(" IF row.LASTCENSUS > 0 THEN ");
|
||
265 | source.append(" SET X = X + row.LASTCENSUS ");
|
||
266 | source.append(" END IF ");
|
||
267 | source.append(" END LOOP ");
|
||
268 | source.append("END");
|
||
269 | |||
270 | Compiler compiler = createCompiler();
|
||
271 | |||
272 | Code code = compiler.compileExpression(source.toString()); |
||
273 | link(code); |
||
274 | 47735 | jjdelcerro | checkEquals("testSelect4", "BEGIN \"X\" := 0; FOR \"row\" IN ( SELECT * FROM \"countries\" ) LOOP IF (\"row\".\"LASTCENSUS\" > 0) THEN \"X\" := (\"X\" + \"row\".\"LASTCENSUS\"); END IF END LOOP; END ", code.toString()); |
275 | 46960 | jjdelcerro | } |
276 | |||
277 | 46994 | fdiaz | public void testSelect5() { |
278 | StringBuilder source = new StringBuilder(); |
||
279 | source.append("BEGIN ");
|
||
280 | source.append(" SELECT * INTO ROW FROM countries ;");
|
||
281 | source.append(" ROW; ");
|
||
282 | source.append("END");
|
||
283 | |||
284 | Compiler compiler = createCompiler();
|
||
285 | |||
286 | Code code = compiler.compileExpression(source.toString()); |
||
287 | link(code); |
||
288 | 47117 | jjdelcerro | checkEquals("testSelect5", "BEGIN ( SELECT * INTO ROW FROM \"countries\" ); \"ROW\"; END ", code.toString()); |
289 | 46994 | fdiaz | } |
290 | |||
291 | 47137 | jjdelcerro | public void testSelect5b() { |
292 | StringBuilder source = new StringBuilder(); |
||
293 | source.append("BEGIN ");
|
||
294 | source.append(" X := 10; ");
|
||
295 | source.append(" SELECT * FROM countries ORDER BY (LASTCENSUS + X);");
|
||
296 | source.append("END");
|
||
297 | |||
298 | Compiler compiler = createCompiler();
|
||
299 | |||
300 | Code code = compiler.compileExpression(source.toString()); |
||
301 | link(code); |
||
302 | checkEquals("testSelect5", "BEGIN \"X\" := 10; ( SELECT * FROM \"countries\" ORDER BY (\"LASTCENSUS\" + \"X\") ASC NULLS LAST ); END ", code.toString()); |
||
303 | } |
||
304 | |||
305 | 46994 | fdiaz | public void testSelect6() { |
306 | StringBuilder source = new StringBuilder(); |
||
307 | source.append("BEGIN ");
|
||
308 | source.append(" ROWS := (SELECT * FROM countries );");
|
||
309 | source.append(" ROWS; ");
|
||
310 | source.append("END");
|
||
311 | |||
312 | Compiler compiler = createCompiler();
|
||
313 | |||
314 | Code code = compiler.compileExpression(source.toString()); |
||
315 | link(code); |
||
316 | 47117 | jjdelcerro | checkEquals("testSelect6", "BEGIN \"ROWS\" := ( SELECT * FROM \"countries\" ); \"ROWS\"; END ", code.toString()); |
317 | 46994 | fdiaz | } |
318 | |||
319 | 44738 | jjdelcerro | public void testSelectWhere() { |
320 | StringBuilder source = new StringBuilder(); |
||
321 | source.append("SELECT * FROM countries ");
|
||
322 | 47117 | jjdelcerro | source.append(" WHERE LASTCENSUS > 0");
|
323 | 44139 | jjdelcerro | |
324 | Compiler compiler = createCompiler();
|
||
325 | |||
326 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
327 | link(code); |
||
328 | 47117 | jjdelcerro | checkEquals("testSelectWhere", "( SELECT * FROM \"countries\" WHERE (\"LASTCENSUS\" > 0) )", code.toString()); |
329 | 44139 | jjdelcerro | } |
330 | |||
331 | 44738 | jjdelcerro | public void testSelectCountWhere() { |
332 | StringBuilder source = new StringBuilder(); |
||
333 | source.append("SELECT COUNT(*) FROM countries ");
|
||
334 | 47117 | jjdelcerro | source.append(" WHERE LASTCENSUS > 0");
|
335 | 44384 | jjdelcerro | |
336 | Compiler compiler = createCompiler();
|
||
337 | |||
338 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
339 | link(code); |
||
340 | 47117 | jjdelcerro | checkEquals("testSelectCountWhere", "( SELECT COUNT(*) FROM \"countries\" WHERE (\"LASTCENSUS\" > 0) )", code.toString()); |
341 | 44384 | jjdelcerro | } |
342 | |||
343 | 46088 | jjdelcerro | public void testSelectCountWhere2() { |
344 | StringBuilder source = new StringBuilder(); |
||
345 | source.append("BEGIN");
|
||
346 | source.append(" tableName := 'countries';");
|
||
347 | 47735 | jjdelcerro | source.append(" SELECT COUNT(*) FROM :(tableName) ");
|
348 | 46088 | jjdelcerro | source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
349 | source.append("END");
|
||
350 | |||
351 | Compiler compiler = createCompiler();
|
||
352 | |||
353 | Code code = compiler.compileExpression(source.toString()); |
||
354 | link(code); |
||
355 | 47735 | jjdelcerro | checkEquals("testSelectCountWhere2", "BEGIN \"tableName\" := 'countries'; ( SELECT COUNT(*) FROM :(\"tableName\") WHERE (\"countries\".\"LASTCENSUS\" > 0) ); END ", code.toString()); |
356 | 46088 | jjdelcerro | } |
357 | |||
358 | 44738 | jjdelcerro | public void testSelectWhere2() { |
359 | StringBuilder source = new StringBuilder(); |
||
360 | source.append("SELECT * FROM countries ");
|
||
361 | 47117 | jjdelcerro | source.append(" WHERE countries.LASTCENSUS > 0");
|
362 | 44139 | jjdelcerro | |
363 | Compiler compiler = createCompiler();
|
||
364 | |||
365 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
366 | link(code); |
||
367 | 47117 | jjdelcerro | checkEquals("testSelectWhere2", "( SELECT * FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) )", code.toString()); |
368 | 44139 | jjdelcerro | } |
369 | |||
370 | 46088 | jjdelcerro | public void testSelectWhere3() { |
371 | StringBuilder source = new StringBuilder(); |
||
372 | source.append("BEGIN");
|
||
373 | source.append(" tableName := 'countries';");
|
||
374 | source.append(" SELECT * FROM :tableName ");
|
||
375 | source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
||
376 | source.append("END");
|
||
377 | |||
378 | Compiler compiler = createCompiler();
|
||
379 | |||
380 | Code code = compiler.compileExpression(source.toString()); |
||
381 | link(code); |
||
382 | 47735 | jjdelcerro | checkEquals("testSelectWhere3", "BEGIN \"tableName\" := 'countries'; ( SELECT * FROM :(\"tableName\") WHERE (\"countries\".\"LASTCENSUS\" > 0) ); END ", code.toString()); |
383 | 46088 | jjdelcerro | } |
384 | |||
385 | 46678 | fdiaz | public void testSelectWhereToValue3() { |
386 | StringBuilder source = new StringBuilder(); |
||
387 | source.append("BEGIN");
|
||
388 | source.append(" SELECT * FROM countries ");
|
||
389 | source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
||
390 | source.append("END");
|
||
391 | |||
392 | Compiler compiler = createCompiler();
|
||
393 | |||
394 | Code code = compiler.compileExpression(source.toString()); |
||
395 | link(code); |
||
396 | 47117 | jjdelcerro | checkEquals("testSelectWhereToValue3", "BEGIN ( SELECT * FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) ); END ", code.toString()); |
397 | 46678 | fdiaz | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
398 | builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase());
|
||
399 | 47117 | jjdelcerro | checkEquals("testSelectWhereToValue3", "BLOCK(( SELECT * FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) ))", code.toValue(builder).toString()); |
400 | 46678 | fdiaz | } |
401 | |||
402 | public void testSelectCountWhereToValue3() { |
||
403 | StringBuilder source = new StringBuilder(); |
||
404 | source.append("BEGIN");
|
||
405 | source.append(" SELECT COUNT(*) FROM countries ");
|
||
406 | source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
||
407 | source.append("END");
|
||
408 | |||
409 | Compiler compiler = createCompiler();
|
||
410 | |||
411 | Code code = compiler.compileExpression(source.toString()); |
||
412 | link(code); |
||
413 | 47117 | jjdelcerro | checkEquals("testSelectCountWhereToValue3", "BEGIN ( SELECT COUNT(*) FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) ); END ", code.toString()); |
414 | 46678 | fdiaz | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
415 | builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase());
|
||
416 | 47117 | jjdelcerro | checkEquals("testSelectCountWhereToValue3", "BLOCK(( SELECT COUNT(*) FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) ))", code.toValue(builder).toString()); |
417 | 46678 | fdiaz | } |
418 | |||
419 | 44738 | jjdelcerro | public void testSelectOrder() { |
420 | StringBuilder source = new StringBuilder(); |
||
421 | source.append("SELECT * FROM countries ");
|
||
422 | 47117 | jjdelcerro | source.append(" ORDER BY CONTINENT ASC, LASTCENSUS DESC");
|
423 | 44139 | jjdelcerro | |
424 | Compiler compiler = createCompiler();
|
||
425 | |||
426 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
427 | link(code); |
||
428 | 47117 | jjdelcerro | checkEquals("testSelectOrder", "( SELECT * FROM \"countries\" ORDER BY \"CONTINENT\" ASC NULLS LAST, \"LASTCENSUS\" DESC NULLS LAST )", code.toString()); |
429 | 44139 | jjdelcerro | } |
430 | |||
431 | 44738 | jjdelcerro | public void testSelectWhereOrder() { |
432 | StringBuilder source = new StringBuilder(); |
||
433 | source.append("SELECT * FROM countries ");
|
||
434 | source.append(" WHERE LASTCENSUS > 0 ");
|
||
435 | 47117 | jjdelcerro | source.append(" ORDER BY ID");
|
436 | 44139 | jjdelcerro | |
437 | Compiler compiler = createCompiler();
|
||
438 | |||
439 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
440 | link(code); |
||
441 | 47117 | jjdelcerro | checkEquals("testSelectWhereOrder", "( SELECT * FROM \"countries\" WHERE (\"LASTCENSUS\" > 0) ORDER BY \"ID\" ASC NULLS LAST )", code.toString()); |
442 | 44139 | jjdelcerro | } |
443 | |||
444 | 47735 | jjdelcerro | public void testSelectWhereOrderNullsLast() { |
445 | StringBuilder source = new StringBuilder(); |
||
446 | source.append("SELECT * FROM countries ");
|
||
447 | source.append(" WHERE LASTCENSUS > 0 ");
|
||
448 | source.append(" ORDER BY ID NULLS LAST");
|
||
449 | |||
450 | Compiler compiler = createCompiler();
|
||
451 | |||
452 | Code code = compiler.compileExpression(source.toString()); |
||
453 | link(code); |
||
454 | checkEquals("testSelectWhereOrder", "( SELECT * FROM \"countries\" WHERE (\"LASTCENSUS\" > 0) ORDER BY \"ID\" ASC NULLS LAST )", code.toString()); |
||
455 | } |
||
456 | |||
457 | public void testSelectWhereOrderNullsFirst() { |
||
458 | StringBuilder source = new StringBuilder(); |
||
459 | source.append("SELECT * FROM countries ");
|
||
460 | source.append(" WHERE LASTCENSUS > 0 ");
|
||
461 | source.append(" ORDER BY ID NULLS FIRST");
|
||
462 | |||
463 | Compiler compiler = createCompiler();
|
||
464 | |||
465 | Code code = compiler.compileExpression(source.toString()); |
||
466 | link(code); |
||
467 | checkEquals("testSelectWhereOrder", "( SELECT * FROM \"countries\" WHERE (\"LASTCENSUS\" > 0) ORDER BY \"ID\" ASC NULLS LAST )", code.toString()); |
||
468 | } |
||
469 | |||
470 | 44738 | jjdelcerro | public void testSelectWhereOrderLimit() { |
471 | StringBuilder source = new StringBuilder(); |
||
472 | source.append("SELECT * FROM countries ");
|
||
473 | source.append(" WHERE LASTCENSUS > 0 ");
|
||
474 | source.append(" ORDER BY LASTCENSUS DESC");
|
||
475 | 47117 | jjdelcerro | source.append(" LIMIT 3");
|
476 | 44139 | jjdelcerro | |
477 | Compiler compiler = createCompiler();
|
||
478 | |||
479 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
480 | link(code); |
||
481 | 47117 | jjdelcerro | checkEquals("testSelectWhereOrderLimit", "( SELECT * FROM \"countries\" WHERE (\"LASTCENSUS\" > 0) ORDER BY \"LASTCENSUS\" DESC NULLS LAST LIMIT 3 )", code.toString()); |
482 | 44139 | jjdelcerro | } |
483 | |||
484 | 47735 | jjdelcerro | public void testSelectWhereOrderNullsLastLimit() { |
485 | StringBuilder source = new StringBuilder(); |
||
486 | source.append("SELECT * FROM countries ");
|
||
487 | source.append(" WHERE LASTCENSUS > 0 ");
|
||
488 | source.append(" ORDER BY LASTCENSUS DESC NULLS LAST");
|
||
489 | source.append(" LIMIT 3");
|
||
490 | |||
491 | Compiler compiler = createCompiler();
|
||
492 | |||
493 | Code code = compiler.compileExpression(source.toString()); |
||
494 | link(code); |
||
495 | checkEquals("testSelectWhereOrderLimit", "( SELECT * FROM \"countries\" WHERE (\"LASTCENSUS\" > 0) ORDER BY \"LASTCENSUS\" DESC NULLS LAST LIMIT 3 )", code.toString()); |
||
496 | } |
||
497 | |||
498 | public void testSelectWhereOrderNullsFirstLimit() { |
||
499 | StringBuilder source = new StringBuilder(); |
||
500 | source.append("SELECT * FROM countries ");
|
||
501 | source.append(" WHERE LASTCENSUS > 0 ");
|
||
502 | source.append(" ORDER BY LASTCENSUS DESC NULLS FIRST");
|
||
503 | source.append(" LIMIT 3");
|
||
504 | |||
505 | Compiler compiler = createCompiler();
|
||
506 | |||
507 | Code code = compiler.compileExpression(source.toString()); |
||
508 | link(code); |
||
509 | checkEquals("testSelectWhereOrderLimit", "( SELECT * FROM \"countries\" WHERE (\"LASTCENSUS\" > 0) ORDER BY \"LASTCENSUS\" DESC NULLS LAST LIMIT 3 )", code.toString()); |
||
510 | } |
||
511 | |||
512 | 47168 | fdiaz | public void testSelectWhereOrderLimit2() { |
513 | try {
|
||
514 | StringBuilder source = new StringBuilder(); |
||
515 | source.append("((((GETATTR(\"ARENA2_ACCIDENTES\", 'TITULARIDAD_VIA') = 2) AND (GETATTR(\"ARENA2_ACCIDENTES\", 'FECHA_ACCIDENTE') >= DATE('2020-01-01'))) AND (GETATTR(\"ARENA2_ACCIDENTES\", 'FECHA_ACCIDENTE') <= DATE('2020-12-31'))) AND (EXISTS(SELECT(\"LID_ACCIDENTE\", \"ARENA2_VEHICULOS\", ((GETATTR(\"ARENA2_VEHICULOS\", 'ID_ACCIDENTE') = GETATTR(\"ARENA2_ACCIDENTES\", 'LID_ACCIDENTE')) AND (GETATTR(\"ARENA2_VEHICULOS\", 'TIPO_VEHICULO') = 4)), TUPLE(), TUPLE(), 1), 'EXISTSaa58b804d9094968ba04664f0c4c8553') OR EXISTS(SELECT(\"LID_ACCIDENTE\", \"ARENA2_VEHICULOS\", ((GETATTR(\"ARENA2_VEHICULOS\", 'ID_ACCIDENTE') = GETATTR(\"ARENA2_ACCIDENTES\", 'LID_ACCIDENTE')) AND (GETATTR(\"ARENA2_VEHICULOS\", 'TIPO_VEHICULO') = 30)), TUPLE(), TUPLE(), 1), 'EXISTSb67e753993c54d1da722ff25a00c2a02')))");
|
||
516 | |||
517 | Compiler compiler = createCompiler();
|
||
518 | |||
519 | Code code = compiler.compileExpression(source.toString()); |
||
520 | link(code); |
||
521 | checkEquals("((((\"ARENA2_ACCIDENTES\".\"TITULARIDAD_VIA\" = 2) AND (\"ARENA2_ACCIDENTES\".\"FECHA_ACCIDENTE\" >= DATE('2020-01-01'))) AND (\"ARENA2_ACCIDENTES\".\"FECHA_ACCIDENTE\" <= DATE('2020-12-31'))) AND (EXISTS((SELECT \"LID_ACCIDENTE\" FROM \"ARENA2_VEHICULOS\" WHERE ((\"ARENA2_VEHICULOS\".\"ID_ACCIDENTE\" = \"ARENA2_ACCIDENTES\".\"LID_ACCIDENTE\") AND (\"ARENA2_VEHICULOS\".\"TIPO_VEHICULO\" = 4)) LIMIT 1), 'EXISTSaa58b804d9094968ba04664f0c4c8553') OR EXISTS((SELECT \"LID_ACCIDENTE\" FROM \"ARENA2_VEHICULOS\" WHERE ((\"ARENA2_VEHICULOS\".\"ID_ACCIDENTE\" = \"ARENA2_ACCIDENTES\".\"LID_ACCIDENTE\") AND (\"ARENA2_VEHICULOS\".\"TIPO_VEHICULO\" = 30)) LIMIT 1), 'EXISTSb67e753993c54d1da722ff25a00c2a02')))", code.toString());
|
||
522 | } catch (Exception ex) { |
||
523 | ex.printStackTrace(); |
||
524 | throw ex;
|
||
525 | } |
||
526 | } |
||
527 | |||
528 | 44738 | jjdelcerro | public void testSelectLimit() { |
529 | StringBuilder source = new StringBuilder(); |
||
530 | source.append("SELECT * FROM countries ");
|
||
531 | 47117 | jjdelcerro | source.append(" LIMIT 3");
|
532 | 44139 | jjdelcerro | |
533 | Compiler compiler = createCompiler();
|
||
534 | |||
535 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
536 | link(code); |
||
537 | 47117 | jjdelcerro | checkEquals("testSelectLimit", "( SELECT * FROM \"countries\" LIMIT 3 )", code.toString()); |
538 | 44139 | jjdelcerro | } |
539 | |||
540 | 44738 | jjdelcerro | public void testExists() { |
541 | StringBuilder source = new StringBuilder(); |
||
542 | source.append("EXISTS(NULL)");
|
||
543 | 44139 | jjdelcerro | |
544 | Compiler compiler = createCompiler();
|
||
545 | |||
546 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
547 | link(code); |
||
548 | 47117 | jjdelcerro | checkEquals("testExists", "EXISTS(NULL, 'EXISTS0123456789abcdef')", replaceExists(code)); |
549 | 44139 | jjdelcerro | } |
550 | |||
551 | 44738 | jjdelcerro | public void testExistsSelect1() { |
552 | StringBuilder source = new StringBuilder(); |
||
553 | source.append("EXISTS(");
|
||
554 | 44769 | jjdelcerro | source.append(" SELECT \"ISO_A2\" FROM countries");
|
555 | 47117 | jjdelcerro | source.append(" WHERE countries.LASTCENSUS > 0 ");
|
556 | 44738 | jjdelcerro | source.append(")");
|
557 | 44139 | jjdelcerro | |
558 | Compiler compiler = createCompiler();
|
||
559 | |||
560 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
561 | 47117 | jjdelcerro | checkEquals("testExistsSelect1-1", "EXISTS(SELECT(\"ISO_A2\", \"countries\", (GETATTR(\"countries\", 'LASTCENSUS') > 0), TUPLE(), TUPLE(), NULL), 'EXISTS0123456789abcdef')", replaceExists(code)); |
562 | 44738 | jjdelcerro | link(code); |
563 | 47117 | jjdelcerro | checkEquals("testExistsSelect1-2", "EXISTS(( SELECT \"countries\".\"ISO_A2\" FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) ), 'EXISTS0123456789abcdef')", replaceExists(code)); |
564 | 44139 | jjdelcerro | } |
565 | |||
566 | 44750 | jjdelcerro | public void testExistsSelect2() { |
567 | StringBuilder source = new StringBuilder(); |
||
568 | source.append("EXISTS(");
|
||
569 | 44769 | jjdelcerro | source.append(" SELECT \"ISO_A2\" FROM countries");
|
570 | 44750 | jjdelcerro | source.append(" WHERE countries.LASTCENSUS > 0 ");
|
571 | source.append(")");
|
||
572 | |||
573 | Compiler compiler = createCompiler();
|
||
574 | |||
575 | Code code = compiler.compileExpression(source.toString()); |
||
576 | link(code); |
||
577 | 47117 | jjdelcerro | checkEquals("testExistsSelect2", "EXISTS(( SELECT \"countries\".\"ISO_A2\" FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) ), 'EXISTS0123456789abcdef')", replaceExists(code)); |
578 | 44750 | jjdelcerro | } |
579 | |||
580 | 47117 | jjdelcerro | public void testExistsSelect2b() { |
581 | StringBuilder source = new StringBuilder(); |
||
582 | source.append("EXISTS(");
|
||
583 | source.append(" SELECT \"ISO_A2\", \"ISO_A3\" FROM countries");
|
||
584 | source.append(" WHERE countries.LASTCENSUS > 0; ");
|
||
585 | source.append(")");
|
||
586 | |||
587 | Compiler compiler = createCompiler();
|
||
588 | compiler.addCompatibility("SELECT","OPTIONAL_SEMICOLON_AT_END"); |
||
589 | |||
590 | Code code = compiler.compileExpression(source.toString()); |
||
591 | link(code); |
||
592 | String source2 = replaceExists(code);
|
||
593 | checkEquals("testExistsSelect2b-1", "EXISTS(( SELECT \"countries\".\"ISO_A2\", \"countries\".\"ISO_A3\" FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) ), 'EXISTS0123456789abcdef')", source2); |
||
594 | |||
595 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
596 | builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase());
|
||
597 | checkEquals("testExistsSelect2b-2", "EXISTS(( SELECT \"countries\".\"ISO_A2\", \"countries\".\"ISO_A3\" FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) ), 'EXISTS0123456789abcdef')", replaceExists(code.toValue(builder).toString())); |
||
598 | |||
599 | Code code2 = compiler.compileExpression(source2); |
||
600 | link(code2); |
||
601 | checkEquals("testExistsSelect2b-3", "EXISTS(( SELECT \"countries\".\"ISO_A2\", \"countries\".\"ISO_A3\" FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) ), 'EXISTS0123456789abcdef')", code2.toString()); |
||
602 | } |
||
603 | |||
604 | public void testExistsSelect2c() { |
||
605 | StringBuilder source = new StringBuilder(); |
||
606 | source.append("EXISTS(");
|
||
607 | source.append(" SELECT \"ISO_A2\" FROM countries");
|
||
608 | source.append(" WHERE countries.LASTCENSUS > 0; ");
|
||
609 | source.append(")");
|
||
610 | |||
611 | Compiler compiler = createCompiler();
|
||
612 | compiler.addCompatibility("SELECT","OPTIONAL_SEMICOLON_AT_END"); |
||
613 | |||
614 | Code code = compiler.compileExpression(source.toString()); |
||
615 | link(code); |
||
616 | String source2 = replaceExists(code);
|
||
617 | checkEquals("testExistsSelect2c-1", "EXISTS(( SELECT \"countries\".\"ISO_A2\" FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) ), 'EXISTS0123456789abcdef')", source2); |
||
618 | Code code2 = compiler.compileExpression(source2); |
||
619 | link(code2); |
||
620 | checkEquals("testExistsSelect2c-2", "EXISTS(( SELECT \"countries\".\"ISO_A2\" FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) ), 'EXISTS0123456789abcdef')", code2.toString()); |
||
621 | } |
||
622 | |||
623 | 44738 | jjdelcerro | public void testExistsSelectLimit1() { |
624 | StringBuilder source = new StringBuilder(); |
||
625 | source.append("EXISTS(");
|
||
626 | 44769 | jjdelcerro | source.append(" SELECT \"ISO_A2\" FROM countries ");
|
627 | 44738 | jjdelcerro | source.append(" WHERE countries.LASTCENSUS > 0 ");
|
628 | 47117 | jjdelcerro | source.append(" LIMIT 1");
|
629 | 44738 | jjdelcerro | source.append(")");
|
630 | 44139 | jjdelcerro | |
631 | Compiler compiler = createCompiler();
|
||
632 | |||
633 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
634 | link(code); |
||
635 | 47117 | jjdelcerro | checkEquals("testExistsSelectLimit1", "EXISTS(( SELECT \"countries\".\"ISO_A2\" FROM \"countries\" WHERE (\"countries\".\"LASTCENSUS\" > 0) LIMIT 1 ), 'EXISTS0123456789abcdef')", replaceExists(code)); |
636 | 44139 | jjdelcerro | } |
637 | |||
638 | 44738 | jjdelcerro | public void testExistsSelectLimit2() { |
639 | // Sobre la tabla de continentes, para obtener la lista de continentes
|
||
640 | // que tienen paises sin censo.
|
||
641 | // Filtramos continentes por un campo del pais.
|
||
642 | StringBuilder source = new StringBuilder(); |
||
643 | source.append("EXISTS(");
|
||
644 | 44769 | jjdelcerro | source.append(" SELECT \"ISO_A2\" FROM countries");
|
645 | 44738 | jjdelcerro | source.append(" WHERE ");
|
646 | source.append(" continents.NAME = countries.CONTINENT AND ");
|
||
647 | source.append(" countries.LASTCENSUS < 0 ");
|
||
648 | 47117 | jjdelcerro | source.append(" LIMIT 1 ");
|
649 | 44738 | jjdelcerro | source.append(")");
|
650 | 44139 | jjdelcerro | |
651 | Compiler compiler = createCompiler();
|
||
652 | |||
653 | 44738 | jjdelcerro | Code code = compiler.compileExpression(source.toString()); |
654 | link(code); |
||
655 | 47117 | jjdelcerro | checkEquals("testExistsSelectLimit2", "EXISTS(( SELECT \"countries\".\"ISO_A2\" FROM \"countries\" WHERE ((\"continents\".\"NAME\" = \"countries\".\"CONTINENT\") AND (\"countries\".\"LASTCENSUS\" < 0)) LIMIT 1 ), 'EXISTS0123456789abcdef')", replaceExists(code)); |
656 | 44139 | jjdelcerro | } |
657 | |||
658 | 44748 | jjdelcerro | public void testExistsSelectLimit3() { |
659 | // Sobre la tabla de continentes, para obtener la lista de continentes
|
||
660 | // que tienen paises sin censo.
|
||
661 | // Filtramos continentes por un campo del pais.
|
||
662 | StringBuilder source = new StringBuilder(); |
||
663 | source.append("EXISTS(");
|
||
664 | 44769 | jjdelcerro | source.append(" SELECT \"ISO_A2\" FROM countries");
|
665 | 44748 | jjdelcerro | source.append(" WHERE ");
|
666 | source.append(" continents.NAME = countries.CONTINENT AND ");
|
||
667 | source.append(" countries.LASTCENSUS < 0 ");
|
||
668 | 44750 | jjdelcerro | source.append(" LIMIT 1 ");
|
669 | 44748 | jjdelcerro | source.append(" ,");
|
670 | source.append(" 'patata'");
|
||
671 | source.append(")");
|
||
672 | |||
673 | Compiler compiler = createCompiler();
|
||
674 | |||
675 | Code code = compiler.compileExpression(source.toString()); |
||
676 | link(code); |
||
677 | 47117 | jjdelcerro | checkEquals("testExistsSelectLimit3", "EXISTS(( SELECT \"countries\".\"ISO_A2\" FROM \"countries\" WHERE ((\"continents\".\"NAME\" = \"countries\".\"CONTINENT\") AND (\"countries\".\"LASTCENSUS\" < 0)) LIMIT 1 ), 'patata')", code.toString()); |
678 | 44748 | jjdelcerro | } |
679 | |||
680 | 47117 | jjdelcerro | public void testSelectFromSelection() { |
681 | StringBuilder source = new StringBuilder(); |
||
682 | source.append("SELECT * FROM SELECTION OF countries");
|
||
683 | |||
684 | Compiler compiler = createCompiler();
|
||
685 | |||
686 | Code code = compiler.compileExpression(source.toString()); |
||
687 | link(code); |
||
688 | checkEquals("testSelectFromSelection", "( SELECT * FROM SELECTION OF \"countries\" )", code.toString()); |
||
689 | } |
||
690 | |||
691 | public void testSelectFromSelection4() { |
||
692 | StringBuilder source = new StringBuilder(); |
||
693 | source.append("BEGIN ");
|
||
694 | source.append(" SET X = 0; ");
|
||
695 | source.append(" FOR row in SELECT * FROM SELECTION OF countries ");
|
||
696 | source.append(" LOOP ");
|
||
697 | source.append(" IF row.LASTCENSUS > 0 THEN ");
|
||
698 | source.append(" SET X = X + row.LASTCENSUS ");
|
||
699 | source.append(" END IF ");
|
||
700 | source.append(" END LOOP ");
|
||
701 | source.append("END");
|
||
702 | |||
703 | Compiler compiler = createCompiler();
|
||
704 | |||
705 | Code code = compiler.compileExpression(source.toString()); |
||
706 | checkEquals("testSelectFromSelection4-1", "BLOCK(LET('X', 0), FOREACH('row', SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), NULL, 1), IF((GETATTR(\"row\", 'LASTCENSUS') > 0), LET('X', (\"X\" + GETATTR(\"row\", 'LASTCENSUS'))))))", code.toString()); |
||
707 | link(code); |
||
708 | String source2 = code.toString();
|
||
709 | 47735 | jjdelcerro | checkEquals("testSelectFromSelection4-2", "BEGIN \"X\" := 0; FOR \"row\" IN ( SELECT * FROM SELECTION OF \"countries\" ) LOOP IF (\"row\".\"LASTCENSUS\" > 0) THEN \"X\" := (\"X\" + \"row\".\"LASTCENSUS\"); END IF END LOOP; END ", source2); |
710 | 47117 | jjdelcerro | Code code2 = compiler.compileExpression(source2); |
711 | checkEquals("testSelectFromSelection4-3", "BLOCK(LET('X', 0), FOREACH('row', SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), NULL, 1), IF((GETATTR(\"row\", 'LASTCENSUS') > 0), LET('X', (\"X\" + GETATTR(\"row\", 'LASTCENSUS'))))))", code2.toString()); |
||
712 | link(code2); |
||
713 | 47735 | jjdelcerro | checkEquals("testSelectFromSelection4-4", "BEGIN \"X\" := 0; FOR \"row\" IN ( SELECT * FROM SELECTION OF \"countries\" ) LOOP IF (\"row\".\"LASTCENSUS\" > 0) THEN \"X\" := (\"X\" + \"row\".\"LASTCENSUS\"); END IF END LOOP; END ", code2.toString()); |
714 | 47117 | jjdelcerro | |
715 | } |
||
716 | |||
717 | 47735 | jjdelcerro | public void testUseCase1() { |
718 | try {
|
||
719 | String source =
|
||
720 | "BEGIN\n" +
|
||
721 | " IF urbanroads IS NULL THEN\n" +
|
||
722 | " calle := NULL;\n" +
|
||
723 | " urbanroadtype := NULL;\n" +
|
||
724 | " \n" +
|
||
725 | " IF thegeometry IS NULL THEN\n" +
|
||
726 | " RETURN TRUE;\n" +
|
||
727 | " END IF;\n" +
|
||
728 | " SELECT * INTO calle FROM \"ISIROAD_URBANROADS\" \n" +
|
||
729 | " ORDER BY ST_DISTANCE(\"ISIROAD_URBANROADS\".\"Geometria\", thegeometry) \n" +
|
||
730 | " LIMIT 1;\n" +
|
||
731 | " IF calle IS NULL THEN\n" +
|
||
732 | " RETURN TRUE;\n" +
|
||
733 | " END IF;\n" +
|
||
734 | " urbanroads := calle.Id;\n" +
|
||
735 | " \n" +
|
||
736 | " SELECT * INTO tipodecalle FROM \"ISIROAD_URBANROADTYPES\" \n" +
|
||
737 | " WHERE \"ISIROAD_URBANROADTYPES\".\"DESCRIPTION\" = :(calle.Descripcion)\n" +
|
||
738 | " LIMIT 1; \n" +
|
||
739 | " urbanroadtype := tipodecalle.CODE;\n" +
|
||
740 | " END IF;\n" +
|
||
741 | " TRUE;\n" +
|
||
742 | "EXCEPT\n" +
|
||
743 | " print('Error en el calculo de calle de horizontal');\n" +
|
||
744 | " TRUE;\n" +
|
||
745 | "END";
|
||
746 | |||
747 | Formatter formatter = new Code.EmptyFormatter(true, 4); |
||
748 | |||
749 | Compiler compiler = createCompiler();
|
||
750 | Code code = compiler.compileExpression(source); |
||
751 | code.link(); |
||
752 | checkEquals( |
||
753 | "BEGIN \n" +
|
||
754 | " IF (\"urbanroads\" IS NULL) THEN \n" +
|
||
755 | " \"calle\" := NULL; \n" +
|
||
756 | " \"urbanroadtype\" := NULL; \n" +
|
||
757 | " IF (\"thegeometry\" IS NULL) THEN \n" +
|
||
758 | " RETURN TRUE; \n" +
|
||
759 | " END IF; \n" +
|
||
760 | " ( SELECT * INTO calle FROM \"ISIROAD_URBANROADS\" ORDER BY ST_Distance(\"ISIROAD_URBANROADS\".\"Geometria\", \"thegeometry\") ASC NULLS LAST LIMIT 1 ); \n" +
|
||
761 | " IF (\"calle\" IS NULL) THEN \n" +
|
||
762 | " RETURN TRUE; \n" +
|
||
763 | " END IF; \n" +
|
||
764 | " \"urbanroads\" := \"calle\".\"Id\"; \n" +
|
||
765 | " ( SELECT * INTO tipodecalle FROM \"ISIROAD_URBANROADTYPES\" WHERE (\"ISIROAD_URBANROADTYPES\".\"DESCRIPTION\" = $HOSTEXPRESSION(\"calle\".\"Descripcion\")) LIMIT 1 ); \n" +
|
||
766 | " \"urbanroadtype\" := \"tipodecalle\".\"CODE\"; \n" +
|
||
767 | " END IF; \n" +
|
||
768 | " TRUE; \n" +
|
||
769 | "EXCEPT \n" +
|
||
770 | " print('Error en el calculo de calle de horizontal'); \n" +
|
||
771 | " TRUE; \n" +
|
||
772 | "END \n",
|
||
773 | code.toString(formatter)); |
||
774 | } catch(Exception ex) { |
||
775 | LOGGER.warn("", ex);
|
||
776 | throw ex;
|
||
777 | } |
||
778 | } |
||
779 | |||
780 | |||
781 | |||
782 | 44139 | jjdelcerro | } |