KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > optim > jorm > rdb > TestRdbJormQueryRewriter


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2005 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, S. Chassande-Barrioz, A. Lefebvre
22  */

23 package org.objectweb.medor.optim.jorm.rdb;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27 import junit.textui.TestRunner;
28 import org.objectweb.jorm.api.PException;
29 import org.objectweb.jorm.api.PMapper;
30 import org.objectweb.jorm.mapper.rdb.lib.ConnectionSpecJDBC;
31 import org.objectweb.jorm.mapper.rdb.lib.MapperJDBC;
32 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter;
33 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapterException;
34 import org.objectweb.jorm.mapper.rdb.metainfo.RdbClassMapping;
35 import org.objectweb.jorm.mapper.rdb.adapter.RdbAdapterFactory;
36 import org.objectweb.jorm.type.api.PTypeSpace;
37 import org.objectweb.medor.api.MedorException;
38 import org.objectweb.medor.expression.lib.And;
39 import org.objectweb.medor.expression.lib.BasicOperand;
40 import org.objectweb.medor.expression.lib.BasicParameterOperand;
41 import org.objectweb.medor.expression.lib.Equal;
42 import org.objectweb.medor.expression.lib.Or;
43 import org.objectweb.medor.filter.lib.BasicFieldOperand;
44 import org.objectweb.medor.filter.lib.MemberOf;
45 import org.objectweb.medor.lib.Log;
46 import org.objectweb.medor.optim.api.QueryRewriter;
47 import org.objectweb.medor.optim.jorm.Jorm2Rdb;
48 import org.objectweb.medor.optim.jorm.JormFilteredInheritance;
49 import org.objectweb.medor.optim.jorm.TestJormQueryRewriter;
50 import org.objectweb.medor.optim.lib.BasicQueryRewriter;
51 import org.objectweb.medor.optim.lib.FlattenQueryTreeRule;
52 import org.objectweb.medor.query.api.OrderField;
53 import org.objectweb.medor.query.api.QueryLeaf;
54 import org.objectweb.medor.query.lib.BasicOrderField;
55 import org.objectweb.medor.query.rdb.api.QualifiedTable;
56 import org.objectweb.medor.query.rdb.api.RdbExpField;
57 import org.objectweb.medor.query.rdb.lib.BasicQualifiedTable;
58 import org.objectweb.medor.query.rdb.lib.BasicRdbExpQueryLeaf;
59
60 import java.util.ArrayList JavaDoc;
61
62 /**
63  * @author S.Chassande-Barrioz
64  */

65 public class TestRdbJormQueryRewriter extends TestJormQueryRewriter {
66
67     RdbAdapter adapter;
68
69     /**
70      * main method to launch the tests manually
71      */

72     public static void main(String JavaDoc[] args) {
73         TestRunner.run(suite());
74     }
75
76     /**
77      * This method creates a TestSuite object with the current tests
78      */

79     public static Test suite() {
80         return new TestSuite(TestRdbJormQueryRewriter.class);
81     }
82
83     public TestRdbJormQueryRewriter() {
84         this("TestRdbJormQueryRewriter");
85     }
86
87     public TestRdbJormQueryRewriter(String JavaDoc testName) {
88         super(testName, Log.MEDOR_PREFIX + ".optim.jorm.rdb.queryrewriter");
89         try {
90             adapter = RdbAdapterFactory.getTypeConverter((String JavaDoc) null);
91         } catch (RdbAdapterException ex) {
92             fail(ex.getMessage());
93         }
94     }
95
96     public PMapper newMappper() {
97         PMapper m = null;
98         try {
99             m = new MapperJDBC();
100             ConnectionSpecJDBC cs = new ConnectionSpecJDBC("jdbc:product:basename", "java.lang.String");
101             m.setConnectionFactory(cs);
102         } catch (PException e) {
103             e.printStackTrace();
104         }
105         return m;
106     }
107
108     public QueryRewriter createQueryRewriter() {
109         ArrayList JavaDoc rules = new ArrayList JavaDoc(2);
110         rules.add(new JormFilteredInheritance());
111         rules.add(new FlattenQueryTreeRule());
112         rules.add(new Jorm2Rdb());
113         return new BasicQueryRewriter(rules);
114     }
115
116     public QueryLeaf getQueryLeafA(Example e) throws MedorException {
117         RdbClassMapping cm = (RdbClassMapping)
118                 e.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
119         String JavaDoc name = e.clazz.getFQName();
120         String JavaDoc prefix = name + ".";
121
122         QualifiedTable qt = new BasicQualifiedTable(cm.getRdbTable().getName(), "T_REF_SING_A_0");
123         BasicRdbExpQueryLeaf leaf = new BasicRdbExpQueryLeaf(
124                 null, new QualifiedTable[]{qt}, "");
125         leaf.addRdbField(prefix + "id", PTypeSpace.LONG, "id_col", qt);
126         RdbExpField f1 = leaf.addRdbField(prefix + "field1", PTypeSpace.STRING, "FIELD_1", qt);
127         leaf.removeRdbField(f1);
128         leaf.setQueryFilter(new Equal(
129                 new BasicFieldOperand(f1),
130                 new BasicParameterOperand(PTypeSpace.STRING, "?1")));
131         leaf.setRdbAdapter(adapter);
132         return leaf;
133     }
134
135     public QueryLeaf getQueryLeafLongLong(Example e) throws MedorException {
136         RdbClassMapping cm = (RdbClassMapping)
137                 e.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
138         String JavaDoc name = e.clazz.getFQName();
139         String JavaDoc prefix = name + ".";
140
141         QualifiedTable qt = new BasicQualifiedTable(cm.getRdbTable().getName(), "T_B_0");
142         BasicRdbExpQueryLeaf leaf = new BasicRdbExpQueryLeaf(
143                 null, new QualifiedTable[]{qt}, "");
144         RdbExpField f1 = leaf.addRdbField(prefix + "c1", PTypeSpace.LONG, "C1", qt);
145         RdbExpField f2 = leaf.addRdbField(prefix + "c2", PTypeSpace.LONG, "C2", qt);
146         leaf.setQueryFilter(new And(
147                 new Equal(
148                     new BasicFieldOperand(f1),
149                     new BasicOperand(-1)),
150                 new Equal(
151                     new BasicFieldOperand(f2),
152                     new BasicOperand(-1))
153                 ));
154         leaf.setRdbAdapter(adapter);
155         return leaf;
156     }
157
158     public QueryLeaf getQueryLeafAgg(Example e) throws MedorException {
159         RdbClassMapping cm = (RdbClassMapping)
160                 e.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
161         String JavaDoc name = e.clazz.getFQName();
162         String JavaDoc prefix = name + ".";
163
164         QualifiedTable qt = new BasicQualifiedTable(cm.getRdbTable().getName(), "T_Agg_0");
165         BasicRdbExpQueryLeaf leaf = new BasicRdbExpQueryLeaf(
166                 null, new QualifiedTable[]{qt}, "");
167         leaf.addRdbField(prefix + "c2", PTypeSpace.LONG, "C2", qt);
168         leaf.addRdbField(prefix + "c1", PTypeSpace.INT, "C1", qt);
169         leaf.addRdbField(prefix + "y", PTypeSpace.FLOAT, "Y", qt);
170         leaf.setRdbAdapter(adapter);
171         return leaf;
172     }
173
174     public QueryLeaf getQueryLeafAggParam(Example e) throws MedorException {
175         RdbClassMapping cm = (RdbClassMapping)
176                 e.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
177         String JavaDoc name = e.clazz.getFQName();
178         String JavaDoc prefix = name + ".";
179
180         QualifiedTable qt = new BasicQualifiedTable(cm.getRdbTable().getName(), "T_REF_SING_A_0");
181         BasicRdbExpQueryLeaf leaf = new BasicRdbExpQueryLeaf(
182                 null, new QualifiedTable[]{qt}, "");
183         leaf.addRdbField(prefix + "id", PTypeSpace.LONG, "id_col", qt);
184         leaf.setRdbAdapter(adapter);
185         return leaf;
186     }
187
188     public QueryLeaf getQueryLeafAggDirect(Example e) throws MedorException {
189         RdbClassMapping cm = (RdbClassMapping)
190                 e.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
191         String JavaDoc name = e.clazz.getFQName();
192         String JavaDoc prefix = name + ".";
193
194         QualifiedTable qt = new BasicQualifiedTable(cm.getRdbTable().getName(), "T_Agg_0");
195         BasicRdbExpQueryLeaf leaf = new BasicRdbExpQueryLeaf(
196                 null, new QualifiedTable[]{qt}, "");
197         leaf.addRdbField(prefix + "c1", PTypeSpace.INT, "C1", qt);
198         leaf.addRdbField(prefix + "y", PTypeSpace.FLOAT, "Y", qt);
199         leaf.setRdbAdapter(adapter);
200         return leaf;
201     }
202
203     public QueryLeaf getQueryLeafAggWithWhere(Example e) throws MedorException {
204         RdbClassMapping cm = (RdbClassMapping)
205                 e.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
206         String JavaDoc name = e.clazz.getFQName();
207         String JavaDoc prefix = name + ".";
208
209         QualifiedTable qt = new BasicQualifiedTable(cm.getRdbTable().getName(), "T_Agg_0");
210         BasicRdbExpQueryLeaf leaf = new BasicRdbExpQueryLeaf(
211                 null, new QualifiedTable[]{qt}, "");
212         RdbExpField c2 = leaf.addRdbField(prefix + "c2", PTypeSpace.LONG, "C2", qt);
213         leaf.removeRdbField(c2);
214         leaf.addRdbField(prefix + "c1", PTypeSpace.INT, "C1", qt);
215         leaf.addRdbField(prefix + "y", PTypeSpace.FLOAT, "Y", qt);
216         leaf.setQueryFilter(new Equal(
217                 new BasicFieldOperand(c2),
218                 new BasicParameterOperand(PTypeSpace.STRING, "?1")));
219         leaf.setRdbAdapter(adapter);
220         return leaf;
221     }
222
223     public QueryLeaf getQueryLeafOrderByA(Example e) throws MedorException {
224         RdbClassMapping cm = (RdbClassMapping)
225                 e.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
226         String JavaDoc name = e.clazz.getFQName();
227         String JavaDoc prefix = name + ".";
228
229         QualifiedTable qt = new BasicQualifiedTable(cm.getRdbTable().getName(), "T_REF_SING_A_0");
230         BasicRdbExpQueryLeaf leaf = new BasicRdbExpQueryLeaf(
231                 null, new QualifiedTable[]{qt}, "");
232         leaf.addRdbField(prefix + "id", PTypeSpace.LONG, "id_col", qt);
233         RdbExpField f1 = leaf.addRdbField(prefix + "field1", PTypeSpace.STRING, "FIELD_1", qt);
234         leaf.removeRdbField(f1);
235         RdbExpField f2 = leaf.addRdbField(prefix + "field2", PTypeSpace.STRING, "FIELD_2", qt);
236         leaf.removeRdbField(f2);
237         OrderField of1 = new BasicOrderField(f1, true);
238         OrderField of2 = new BasicOrderField(f2);
239         leaf.setOrderBy(new OrderField[] {of1, of2});
240         leaf.setQueryFilter(new Equal(
241                 new BasicFieldOperand(f1),
242                 new BasicParameterOperand(PTypeSpace.STRING, "?1")));
243         leaf.setRdbAdapter(adapter);
244         return leaf;
245     }
246
247     public QueryLeaf getQueryLeafMemberOfBD(Example eB, Example eD) throws Exception JavaDoc {
248         RdbClassMapping cmB = (RdbClassMapping)
249                 eB.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
250         RdbClassMapping cmD = (RdbClassMapping)
251                 eD.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
252         String JavaDoc name = eB.clazz.getFQName();
253         String JavaDoc prefix = name + ".";
254
255         QualifiedTable qtB = new BasicQualifiedTable(cmB.getRdbTable().getName(), "T_B_0");
256         BasicRdbExpQueryLeaf leafB = new BasicRdbExpQueryLeaf(
257                 null, new QualifiedTable[]{qtB}, "");
258         RdbExpField fc2 = leafB.addRdbField(prefix + "c2", PTypeSpace.CHAR, "C2", qtB);
259         RdbExpField fc1 = leafB.addRdbField(prefix + "c1", PTypeSpace.BYTE, "C1", qtB);
260
261         QualifiedTable qtD = new BasicQualifiedTable(cmD.getRdbTable().getName(), "T_D_1");
262         BasicRdbExpQueryLeaf leafD = new BasicRdbExpQueryLeaf(
263                 null, new QualifiedTable[]{qtD}, "");
264         leafD.setRdbAdapter(adapter);
265         RdbExpField fb = leafD.addRdbField(prefix + "b", PTypeSpace.BYTE, "B", qtD);
266         RdbExpField fa = leafD.addRdbField(prefix + "a", PTypeSpace.CHAR, "A", qtD);
267
268         ArrayList JavaDoc left = new ArrayList JavaDoc(2);
269         ArrayList JavaDoc right = new ArrayList JavaDoc(2);
270         left.add(new BasicFieldOperand(fc2));
271         left.add(new BasicFieldOperand(fc1));
272         right.add(new BasicFieldOperand(fb));
273         right.add(new BasicFieldOperand(fa));
274         MemberOf mo = new MemberOf(left, right);
275         leafB.setQueryFilter(mo);
276         leafB.setRdbAdapter(adapter);
277         return leafB;
278     }
279
280     public QueryLeaf getQueryLeafMemberOfBC(Example eB, Example eC) throws Exception JavaDoc {
281         RdbClassMapping cmB = (RdbClassMapping)
282                 eB.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
283         RdbClassMapping cmC = (RdbClassMapping)
284                 eC.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
285         String JavaDoc name = eB.clazz.getFQName();
286         String JavaDoc prefix = name + ".";
287
288         QualifiedTable qtB = new BasicQualifiedTable(cmB.getRdbTable().getName(), "T_B_0");
289         BasicRdbExpQueryLeaf leafB = new BasicRdbExpQueryLeaf(
290                 null, new QualifiedTable[]{qtB}, "");
291         leafB.setRdbAdapter(adapter);
292         RdbExpField fc2 = leafB.addRdbField(prefix + "c2", PTypeSpace.CHAR, "C2", qtB);
293         RdbExpField fc1 = leafB.addRdbField(prefix + "c1", PTypeSpace.BYTE, "C1", qtB);
294         RdbExpField fy = leafB.addRdbField(prefix + "y", PTypeSpace.LONG, "Y", qtB);
295         leafB.removeRdbField(fy);
296
297         QualifiedTable qtC = new BasicQualifiedTable(cmC.getRdbTable().getName(), "T_C_1");
298         BasicRdbExpQueryLeaf leafC = new BasicRdbExpQueryLeaf(
299                 null, new QualifiedTable[]{qtC}, "");
300         leafC.setRdbAdapter(adapter);
301         RdbExpField fx = leafC.addRdbField(prefix + "x", PTypeSpace.LONG, "X", qtC);
302         leafC.setQueryFilter(new Equal(new BasicFieldOperand(fx), new BasicOperand((long) 2)));
303         ArrayList JavaDoc left = new ArrayList JavaDoc(1);
304         ArrayList JavaDoc right = new ArrayList JavaDoc(1);
305         left.add(new BasicFieldOperand(fy));
306         right.add(new BasicFieldOperand(fx));
307         MemberOf mo = new MemberOf(left, right);
308         leafB.setQueryFilter(mo);
309         return leafB;
310     }
311
312     public QueryLeaf getQueryLeafMOfParamBD(Example eB, Example eD) throws Exception JavaDoc {
313         RdbClassMapping cmB = (RdbClassMapping)
314                 eB.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
315         RdbClassMapping cmD = (RdbClassMapping)
316                 eD.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
317         String JavaDoc nameB = eB.clazz.getFQName();
318         String JavaDoc prefixB = nameB + ".";
319         String JavaDoc nameD = eD.clazz.getFQName();
320         String JavaDoc prefixD = nameD + ".";
321
322         QualifiedTable qtB = new BasicQualifiedTable(cmB.getRdbTable().getName(), "T_B_0");
323         QualifiedTable qtD = new BasicQualifiedTable(cmD.getRdbTable().getName(), "T_D_1");
324         BasicRdbExpQueryLeaf leafB = new BasicRdbExpQueryLeaf(
325                 null, new QualifiedTable[]{qtB}, "");
326         leafB.setRdbAdapter(adapter);
327         BasicRdbExpQueryLeaf leafD = new BasicRdbExpQueryLeaf(
328                 null, new QualifiedTable[]{qtD}, "");
329         leafD.setRdbAdapter(adapter);
330         RdbExpField fy = leafB.addRdbField(prefixB + "y", PTypeSpace.LONG, "Y", qtB);
331         leafB.removeRdbField(fy);
332
333         RdbExpField fb = leafD.addRdbField(prefixD + "b", PTypeSpace.CHAR, "B", qtD);
334         RdbExpField fa = leafD.addRdbField(prefixD + "a", PTypeSpace.BYTE, "A", qtD);
335
336         ArrayList JavaDoc left = new ArrayList JavaDoc(1);
337         BasicOperand value = new BasicOperand(1013);
338         left.add(value);
339         ArrayList JavaDoc right = new ArrayList JavaDoc(1);
340         right.add(new BasicFieldOperand(fy));
341         MemberOf mo = new MemberOf(left, right);
342         leafD.setQueryFilter(mo);
343         return leafD;
344     }
345
346     public QueryLeaf getQueryLeafAnimal(Example e) throws MedorException {
347         RdbClassMapping cm = (RdbClassMapping)
348                 e.clazz.getClassProject(getProjectName()).getMapping(getMapperName()).getClassMapping();
349         String JavaDoc name = e.clazz.getFQName();
350         String JavaDoc prefix = name + ".";
351
352         QualifiedTable qt = new BasicQualifiedTable(cm.getRdbTable().getName(), "Animal_0");
353         BasicRdbExpQueryLeaf leaf = new BasicRdbExpQueryLeaf(
354                 null, new QualifiedTable[]{qt}, "");
355         leaf.setRdbAdapter(adapter);
356         leaf.addRdbField(prefix + "animal_id", PTypeSpace.LONG, "a_id", qt);
357         RdbExpField f1 = leaf.addRdbField(prefix + "species", PTypeSpace.STRING, "speciesCol", qt);
358         RdbExpField f2 = leaf.addRdbField(prefix + "type", PTypeSpace.STRING, "typeCol", qt);
359         leaf.removeRdbField(f1);
360         leaf.removeRdbField(f2);
361         leaf.setQueryFilter(
362                 new And(
363                     new Equal(
364                         new BasicFieldOperand(f1),
365                         new BasicOperand("marsupial")),
366                     new Or(
367                         new Equal(
368                             new BasicFieldOperand(f2),
369                             new BasicOperand("koala")),
370                         new Equal(
371                             new BasicFieldOperand(f2),
372                             new BasicOperand("kangaroo"))
373                     )
374                 )
375         );
376         return leaf;
377     }
378
379
380 }
381
Popular Tags