KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > query > RdbExpLeaf


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2002 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, A. Lefebvre
22  */

23 package org.objectweb.medor.query;
24
25 import org.objectweb.jorm.api.PException;
26 import org.objectweb.medor.filter.api.FieldOperand;
27 import org.objectweb.medor.expression.api.ParameterOperand;
28 import org.objectweb.medor.expression.api.Expression;
29 import org.objectweb.medor.expression.lib.And;
30 import org.objectweb.medor.filter.lib.BasicFieldOperand;
31 import org.objectweb.medor.expression.lib.BasicParameterOperand;
32 import org.objectweb.medor.expression.lib.Greater;
33 import org.objectweb.medor.expression.lib.Lower;
34 import org.objectweb.medor.expression.lib.BasicOperand;
35 import org.objectweb.medor.query.rdb.api.QualifiedTable;
36 import org.objectweb.medor.query.rdb.api.RdbExpField;
37 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf;
38 import org.objectweb.medor.query.rdb.lib.BasicQualifiedTable;
39 import org.objectweb.medor.query.rdb.lib.BasicRdbExpQueryLeaf;
40 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
41
42 /**
43  * @author <A HREF="mailto:alia.mourad@rd.francetelecom.com><b>
44  * Mourad Alia
45  * </b></A>
46  * <A HREF="mailto:alexandre.lefebvre@rd.francetelecom.com><b>
47  * Alexandre Lefebvre
48  </b></A>
49  */

50 public class RdbExpLeaf {
51
52     public static void main(String JavaDoc[] arg) {
53         try {
54             //create qualified tables
55
QualifiedTable t1 = new BasicQualifiedTable("t1", null);
56             QualifiedTable t2 = new BasicQualifiedTable("t2", "al2");
57             QualifiedTable t3 = new BasicQualifiedTable("t3", "al3");
58             QualifiedTable[] tables = {t1, t2};
59             //create a new leaf
60
RdbExpQueryLeaf ql = new BasicRdbExpQueryLeaf(null, tables, "");
61             //add fields
62
RdbExpField f1 = ql.addRdbField("f1", null, "col1", t1);
63             RdbExpField f2 = ql.addRdbField("f2", null, "col2", t2);
64             //create a filter to be added to the query leaf
65
FieldOperand fieldOp1 = new BasicFieldOperand(f1);
66             FieldOperand fieldOp2 = new BasicFieldOperand(f2);
67             BasicOperand valueL = new BasicOperand(1013);
68             BasicOperand valueR = new BasicOperand("coucou");
69             Expression left = new Lower(fieldOp1, valueL);
70             Expression right = new Greater(fieldOp2, valueR);
71             Expression filter = new And(left, right);
72             //assigne the filter to the query leaf
73
ql.setQueryFilter(filter);
74             System.out.println("First request");
75             //computes the SQL query
76
System.out.println(ql.getSqlRequest(null, false, false));
77
78             ParameterOperand fieldOp3 =
79                 new BasicParameterOperand(PTypeSpaceMedor.STRING, "x");
80             Expression left2 = new Lower(fieldOp3, valueL);
81             filter = new And(left2, right);
82             ql.setQueryFilter(filter);
83             ql.addGroupBy(f2);
84             System.out.println(ql.getSqlRequest(null, false, false));
85         }
86         catch (Exception JavaDoc e) {
87             System.err.println("Exit with exception: " + e.getMessage());
88             do {
89                 e.printStackTrace();
90                 if (e instanceof PException)
91                     e = ((PException) e).getNestedException();
92                 else
93                     e = null;
94             }
95             while (e != null);
96         }
97     }
98 }
99
Popular Tags