svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.h2spatial / org.gvsig.h2spatial.h2gis132 / org.gvsig.h2spatial.h2gis132.provider / src / test / java / org / gvsig / fmap / dal / store / h2 / operations / usecases / arena2 / TestArena2GroupByForeignValue.java @ 46110
History | View | Annotate | Download (5.66 KB)
1 | 46110 | omartinez | package org.gvsig.fmap.dal.store.h2.operations.usecases.arena2; |
---|---|---|---|
2 | 45472 | jjdelcerro | |
3 | import java.util.List; |
||
4 | import junit.framework.TestCase; |
||
5 | 46050 | omartinez | import org.gvsig.expressionevaluator.ExpressionUtils; |
6 | import org.gvsig.fmap.dal.DataTypes; |
||
7 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
8 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
9 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQuery; |
10 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
11 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
12 | 46050 | omartinez | import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureAttributeEmulatorExpression; |
13 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.store.h2.TestUtils; |
14 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
15 | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
||
16 | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
||
17 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
18 | 46104 | omartinez | import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CountOperation; |
19 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation; |
20 | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
||
21 | import org.slf4j.Logger; |
||
22 | import org.slf4j.LoggerFactory; |
||
23 | |||
24 | 46010 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
25 | 46104 | omartinez | public class TestArena2GroupByForeignValue extends TestCase { |
26 | 45472 | jjdelcerro | |
27 | 46104 | omartinez | private static final Logger LOGGER = LoggerFactory.getLogger(TestArena2GroupByForeignValue.class); |
28 | 45472 | jjdelcerro | |
29 | 46104 | omartinez | public TestArena2GroupByForeignValue(String testName) { |
30 | 45472 | jjdelcerro | super(testName);
|
31 | } |
||
32 | |||
33 | @Override
|
||
34 | protected void setUp() throws Exception { |
||
35 | super.setUp();
|
||
36 | new DefaultLibrariesInitializer().fullInitialize();
|
||
37 | } |
||
38 | |||
39 | @Override
|
||
40 | protected void tearDown() throws Exception { |
||
41 | super.tearDown();
|
||
42 | } |
||
43 | 46050 | omartinez | |
44 | 46104 | omartinez | protected List<String> getExpectedSQLs(String name) throws Exception { |
45 | return TestUtils.getSQLs(name);
|
||
46 | 46050 | omartinez | } |
47 | |||
48 | 46104 | omartinez | public void testGroupByForeignValue() throws Exception { |
49 | 46010 | jjdelcerro | try {
|
50 | JDBCHelper helper = TestUtils.createJDBCHelper(); |
||
51 | JDBCSQLBuilderBase sqlbuilder = helper.createSQLBuilder(); |
||
52 | OperationsFactory operations = helper.getOperations(); |
||
53 | 45472 | jjdelcerro | |
54 | 46104 | omartinez | List<String> expectedSQLs = getExpectedSQLs("usecases/arena2/testGroupByForeignValue.sql"); |
55 | 45472 | jjdelcerro | |
56 | 46104 | omartinez | FeatureStore arena2_conductores = TestUtils.openStoreTest("usecases/arena2/ARENA2_CONDUCTORES.csv");
|
57 | FeatureStore arena2_accidentes = TestUtils.openStoreTest("usecases/arena2/ARENA2_ACCIDENTES.csv");
|
||
58 | FeatureStore arena2_vehiculos = TestUtils.openStoreTest("usecases/arena2/ARENA2_VEHICULOS.csv");
|
||
59 | 45472 | jjdelcerro | |
60 | 46010 | jjdelcerro | TableReference table = operations.createTableReference( |
61 | "dbtest",
|
||
62 | 46104 | omartinez | "public", //sqlbuilder.default_schema(), |
63 | "ARENA2_CONDUCTORES",
|
||
64 | 46010 | jjdelcerro | null
|
65 | ); |
||
66 | 46104 | omartinez | FeatureType featureType = arena2_conductores.getDefaultFeatureType(); |
67 | 46050 | omartinez | EditableFeatureType eFeatureType = featureType.getEditable(); |
68 | 46104 | omartinez | FeatureQuery query = arena2_conductores.createFeatureQuery(); |
69 | query.setFilter("( ( ( (FOREING_VALUE('ID_ACCIDENTE.TITULARIDAD_VIA') = 2) )) AND \n" +
|
||
70 | "( (( (FOREING_VALUE('LID_VEHICULO.TIPO_VEHICULO') = 5) ) OR ( (FOREING_VALUE('LID_VEHICULO.TIPO_VEHICULO') = 6) ) OR ( (FOREING_VALUE('LID_VEHICULO.TIPO_VEHICULO') = 7) )))) AND \n" +
|
||
71 | "( (( (FOREING_VALUE('ID_ACCIDENTE.FECHA_ACCIDENTE') >= DATE '2019-01-01') ) AND ( (FOREING_VALUE('ID_ACCIDENTE.FECHA_ACCIDENTE') <= DATE '2019-12-31') )))");
|
||
72 | addExtraColumn(eFeatureType, query, "ano", DataTypes.INTEGER, "EXTRACT(YEAR FROM FOREING_VALUE('ID_ACCIDENTE.FECHA_ACCIDENTE'))"); |
||
73 | addExtraColumn(eFeatureType, query, "provincia", DataTypes.STRING, "FOREING_VALUE('ID_ACCIDENTE.COD_PROVINCIA')"); |
||
74 | addExtraColumn(eFeatureType, query, "lesividad", DataTypes.STRING, "CASE\n" + |
||
75 | "WHEN ASISTENCIA_SANITARIA=1 THEN 'Muerto'\n" +
|
||
76 | "WHEN ASISTENCIA_SANITARIA=2 THEN 'Grave'\n" +
|
||
77 | "WHEN ASISTENCIA_SANITARIA=3 or ASISTENCIA_SANITARIA=4 or ASISTENCIA_SANITARIA=5 or ASISTENCIA_SANITARIA=6 or ASISTENCIA_SANITARIA=7 THEN 'Leve'\n" +
|
||
78 | "WHEN ASISTENCIA_SANITARIA=8 THEN 'Ileso'\n" +
|
||
79 | "ELSE 'Se Desconoce'\n" +
|
||
80 | "END CASE");
|
||
81 | addExtraColumn(eFeatureType, query, "Acc", DataTypes.INTEGER, "1"); |
||
82 | |||
83 | query.getGroupByColumns().add("ano");
|
||
84 | query.getGroupByColumns().add("provincia");
|
||
85 | query.getGroupByColumns().add("lesividad");
|
||
86 | query.getAggregateFunctions().put("Acc", "SUM"); |
||
87 | query.getOrder().add("provincia");
|
||
88 | CountOperation count = operations.createCount(eFeatureType, table, null, query);
|
||
89 | String sqlcount = count.getSQL();
|
||
90 | System.out.println("###### SQL:" + sqlcount); |
||
91 | System.out.println("###### EXP:" + expectedSQLs.get(1)); |
||
92 | assertEquals("Count SQL", expectedSQLs.get(1), sqlcount); |
||
93 | |||
94 | 46050 | omartinez | ResultSetForSetProviderOperation resultSetForSetProvider = operations.createResultSetForSetProvider( |
95 | table, |
||
96 | null,
|
||
97 | null,
|
||
98 | query, |
||
99 | eFeatureType, |
||
100 | eFeatureType, |
||
101 | 0,
|
||
102 | 0,
|
||
103 | 0
|
||
104 | ); |
||
105 | String sql = resultSetForSetProvider.getSQL();
|
||
106 | System.out.println("###### SQL:" + sql); |
||
107 | 46104 | omartinez | System.out.println("###### EXP:" + expectedSQLs.get(0)); |
108 | 46050 | omartinez | |
109 | 46104 | omartinez | assertEquals("Select SQL", expectedSQLs.get(0), sql); |
110 | 46050 | omartinez | } catch (Throwable th) { |
111 | 46104 | omartinez | th.printStackTrace(); |
112 | 46050 | omartinez | throw th;
|
113 | } |
||
114 | } |
||
115 | |||
116 | 46104 | omartinez | private void addExtraColumn(EditableFeatureType eFeatureType, FeatureQuery query, String name, int type, String exp) { |
117 | EditableFeatureAttributeDescriptor extraColumn = query.getExtraColumn().add(name, type); |
||
118 | extraColumn.setFeatureAttributeEmulator(new DefaultFeatureAttributeEmulatorExpression(eFeatureType, ExpressionUtils.createExpression(exp)));
|
||
119 | } |
||
120 | 45472 | jjdelcerro | |
121 | } |