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 / TestArena2OrderByExtraValue.java @ 46105
History | View | Annotate | Download (6.02 KB)
1 | 46104 | omartinez | package org.gvsig.fmap.dal.store.h2.operations.usecases; |
---|---|---|---|
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 | 46105 | omartinez | public class TestArena2OrderByExtraValue extends TestCase { |
26 | 45472 | jjdelcerro | |
27 | 46105 | omartinez | private static final Logger LOGGER = LoggerFactory.getLogger(TestArena2OrderByExtraValue.class); |
28 | 45472 | jjdelcerro | |
29 | 46105 | omartinez | public TestArena2OrderByExtraValue(String testName) { |
30 | super(testName);
|
||
31 | } |
||
32 | 45472 | jjdelcerro | |
33 | 46105 | omartinez | @Override
|
34 | protected void setUp() throws Exception { |
||
35 | super.setUp();
|
||
36 | new DefaultLibrariesInitializer().fullInitialize();
|
||
37 | } |
||
38 | 45472 | jjdelcerro | |
39 | 46105 | omartinez | @Override
|
40 | protected void tearDown() throws Exception { |
||
41 | super.tearDown();
|
||
42 | } |
||
43 | 46050 | omartinez | |
44 | 46105 | omartinez | protected List<String> getExpectedSQLs(String name) throws Exception { |
45 | return TestUtils.getSQLs(name);
|
||
46 | } |
||
47 | |||
48 | public void testOrderByExtraValue() 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 | 46105 | omartinez | List<String> expectedSQLs = getExpectedSQLs("usecases/arena2/testOrderByExtraValue.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 | 46105 | omartinez | query.setFilter("( ( ( (FOREING_VALUE('ID_ACCIDENTE.TITULARIDAD_VIA') = 2) )) AND \n" +
|
70 | 46104 | omartinez | "( (( (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 | 46105 | omartinez | 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 | 46104 | omartinez | 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 | 46105 | omartinez | query.getOrder().add("ano");
|
88 | query.getOrder().add("provincia");
|
||
89 | query.getOrder().add("lesividad");
|
||
90 | |||
91 | |||
92 | CountOperation count = operations.createCount(eFeatureType, table, null, query);
|
||
93 | String sqlcount = count.getSQL();
|
||
94 | System.out.println("###### SQL:" + sqlcount); |
||
95 | System.out.println("###### EXP:" + expectedSQLs.get(1)); |
||
96 | 46104 | omartinez | assertEquals("Count SQL", expectedSQLs.get(1), sqlcount); |
97 | 46105 | omartinez | |
98 | 46050 | omartinez | ResultSetForSetProviderOperation resultSetForSetProvider = operations.createResultSetForSetProvider( |
99 | table, |
||
100 | null,
|
||
101 | null,
|
||
102 | query, |
||
103 | eFeatureType, |
||
104 | eFeatureType, |
||
105 | 46105 | omartinez | 15,
|
106 | 46050 | omartinez | 0,
|
107 | 0
|
||
108 | ); |
||
109 | String sql = resultSetForSetProvider.getSQL();
|
||
110 | System.out.println("###### SQL:" + sql); |
||
111 | 46104 | omartinez | System.out.println("###### EXP:" + expectedSQLs.get(0)); |
112 | 46050 | omartinez | |
113 | 46104 | omartinez | assertEquals("Select SQL", expectedSQLs.get(0), sql); |
114 | 46105 | omartinez | |
115 | |||
116 | 46050 | omartinez | } catch (Throwable th) { |
117 | 46105 | omartinez | th.printStackTrace(); |
118 | 46050 | omartinez | throw th;
|
119 | } |
||
120 | } |
||
121 | |||
122 | 46105 | omartinez | private void addExtraColumn(EditableFeatureType eFeatureType, FeatureQuery query, String name, int type, String exp) { |
123 | EditableFeatureAttributeDescriptor extraColumn = query.getExtraColumn().add(name, type); |
||
124 | extraColumn.setFeatureAttributeEmulator(new DefaultFeatureAttributeEmulatorExpression(eFeatureType, ExpressionUtils.createExpression(exp)));
|
||
125 | } |
||
126 | 45472 | jjdelcerro | |
127 | } |