KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MedorQueriesExample


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2004 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
24 import org.objectweb.medor.api.Field;
25 import org.objectweb.medor.api.MedorException;
26 import org.objectweb.medor.api.TupleStructure;
27 import org.objectweb.medor.datasource.api.DataStore;
28 import org.objectweb.medor.datasource.lib.BasicDataStore;
29 import org.objectweb.medor.eval.api.ConnectionResources;
30 import org.objectweb.medor.eval.api.QueryEvaluator;
31 import org.objectweb.medor.eval.lib.MedorEvaluator;
32 import org.objectweb.medor.expression.api.Expression;
33 import org.objectweb.medor.expression.api.ExpressionException;
34 import org.objectweb.medor.expression.api.Operand;
35 import org.objectweb.medor.expression.api.ParameterOperand;
36 import org.objectweb.medor.expression.lib.BasicOperand;
37 import org.objectweb.medor.expression.lib.BasicParameterOperand;
38 import org.objectweb.medor.expression.lib.Lower;
39 import org.objectweb.medor.expression.lib.Minus;
40 import org.objectweb.medor.expression.lib.Plus;
41 import org.objectweb.medor.filter.api.FieldOperand;
42 import org.objectweb.medor.filter.lib.BasicFieldOperand;
43 import org.objectweb.medor.lib.BasicField;
44 import org.objectweb.medor.lib.BasicTupleStructure;
45 import org.objectweb.medor.optim.api.ExecPlanGenerator;
46 import org.objectweb.medor.optim.lib.IndexesGenerator;
47 import org.objectweb.medor.query.api.CalculatedField;
48 import org.objectweb.medor.query.api.PropagatedField;
49 import org.objectweb.medor.query.api.QueryLeaf;
50 import org.objectweb.medor.query.api.QueryTree;
51 import org.objectweb.medor.query.api.QueryTreeField;
52 import org.objectweb.medor.query.api.TCQueryLeaf;
53 import org.objectweb.medor.query.lib.JoinProject;
54 import org.objectweb.medor.query.lib.MedorTCQueryLeaf;
55 import org.objectweb.medor.query.lib.SelectProject;
56 import org.objectweb.medor.query.rdb.api.QualifiedTable;
57 import org.objectweb.medor.query.rdb.api.RdbField;
58 import org.objectweb.medor.query.rdb.api.RdbStringQueryLeaf;
59 import org.objectweb.medor.query.rdb.lib.BasicQualifiedTable;
60 import org.objectweb.medor.query.rdb.lib.BasicRdbStringQueryLeaf;
61 import org.objectweb.medor.tuple.api.Tuple;
62 import org.objectweb.medor.tuple.api.TupleCollection;
63 import org.objectweb.medor.tuple.lib.ExplicitTupleCollection;
64 import org.objectweb.medor.tuple.lib.MemoryTuple;
65 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
66
67 import java.sql.Connection JavaDoc;
68 import java.sql.SQLException JavaDoc;
69
70 /**
71  * Here is a very simple exemple for constructing, compiling and evaluating
72  * Medor Queries...
73  */

74 public class MedorQueriesExample {
75
76     public static void main(String JavaDoc arg[])
77             throws MedorException, ExpressionException, SQLException JavaDoc {
78         //Creating a QueryTree...
79
// THE URL OF THE DATA BASE
80
String JavaDoc dbURL = new String JavaDoc("jdbc:idb:c:/EnhydraDB/Examples/sample.prp");
81         // THE SQL STRING QUERY
82
String JavaDoc sqlQuery1 = new String JavaDoc("SELECT * FROM Sample$db$Cols");
83         // THE SQL STRING QUERY
84
String JavaDoc sqlQuery2 = new String JavaDoc("SELECT * FROM Sample$db$Cols$pKey");
85
86         DataStore ds = new BasicDataStore(DataStore.JDBC_STORE, "sample.prp");
87         QualifiedTable table1 = new BasicQualifiedTable("sample$db$Cols", "sampledbCols");
88
89         // We creates a QueryLeaf with an empty TupleStructure
90
RdbStringQueryLeaf sqlLeaf = new BasicRdbStringQueryLeaf(ds, sqlQuery1, "");
91
92         // Adding Fields that will be projected
93
RdbField field1 = sqlLeaf.addRdbField("ColID", PTypeSpaceMedor.INT, "ColID");
94         RdbField field2 = sqlLeaf.addRdbField("TableId", PTypeSpaceMedor.INT, "TableId");
95         RdbField field3 = sqlLeaf.addRdbField("ColName", PTypeSpaceMedor.STRING, "ColName");
96
97
98         // Creating a SelectProject Operation
99
// At first the filter:
100
FieldOperand fieldOp = new BasicFieldOperand(field1);
101         BasicOperand value = new BasicOperand(1013);
102         Expression filter = new Lower(fieldOp, value);
103         // Then the Operation QueryNode
104
SelectProject sp = new SelectProject(filter, "");
105
106         // Adding Fields wich will be projected
107
PropagatedField pf1 = sp.addPropagatedField("Col_Att1", PTypeSpaceMedor.INT, new QueryTreeField[]{field1});
108         PropagatedField pf2 = sp.addPropagatedField("TableID_Att2", PTypeSpaceMedor.INT, new QueryTreeField[]{field2});
109         PropagatedField pf3 = sp.addPropagatedField("ColName_Att3", PTypeSpaceMedor.STRING, new QueryTreeField[]{field3});
110
111         // Adding CalculatedFields
112
// The Arithmetic axpression of the first CalculatedField = Col_Att1 + TableID_Att2
113
ParameterOperand x = new BasicParameterOperand(PTypeSpaceMedor.INT, "x");
114         Expression exp1 = new Plus(new Plus(new BasicFieldOperand(field1), new BasicFieldOperand(field2)), x);
115         // The Arithmetic axpression of the second CalculatedField = Col_Att1 - TableID_Att2
116
Expression exp2 = new Minus(new BasicFieldOperand(field1), new BasicFieldOperand(field2));
117         // Add The first calculedfield
118
CalculatedField cf1 = sp.addCalculatedField("Col_Att1 + TableID_Att2 + x", PTypeSpaceMedor.INT, exp1);
119         // Add the second CalculatedField
120
CalculatedField cf2 = sp.addCalculatedField("Col_Att1 -TableID_Att2", PTypeSpaceMedor.INT, exp2);
121
122         // Creating a TCQueryLeaf
123
//Creating Fields
124
Field[] fields = new Field[4];
125         fields[0] = new BasicField("NSS", PTypeSpaceMedor.INT);
126         fields[1] = new BasicField("NAME", PTypeSpaceMedor.STRING);
127         fields[2] = new BasicField("ADRESS", PTypeSpaceMedor.STRING);
128         fields[3] = new BasicField("SEX", PTypeSpaceMedor.CHAR);
129
130         // Creating a TupleStructure
131
TupleStructure ts = new BasicTupleStructure(fields);
132
133         // Creating an arrays of VariableOperand
134
Operand[] vops1 = new Operand[4];
135         // Creating a simple variableOperand
136
vops1[0] = new BasicOperand(201);
137         vops1[1] = new BasicOperand("MEDOR");
138         vops1[2] = new BasicOperand("MARS");
139         vops1[3] = new BasicOperand('M');
140
141         Operand[] vops2 = new Operand[4];
142         vops2[0] = new BasicOperand(101);
143         vops2[1] = new BasicOperand("JORM");
144         vops2[2] = new BasicOperand("MARS");
145         vops2[3] = new BasicOperand('M');
146
147         // Creating Tuples
148
Tuple t1 = new MemoryTuple(vops1);
149         Tuple t2 = new MemoryTuple(vops2);
150
151         // Creating an ExplicitTupleCollection
152
ExplicitTupleCollection tc = new ExplicitTupleCollection(ts);
153
154         //Inserting Tuples
155
tc.insertTuple(t1);
156         tc.insertTuple(t2);
157
158         TCQueryLeaf medorQTLeaf = new MedorTCQueryLeaf("", "jorm_medor", tc);
159         QueryTreeField mfield1 = medorQTLeaf.addField("NSS", PTypeSpaceMedor.INT, fields[0]);
160         QueryTreeField mfield2 = medorQTLeaf.addField("NAME", PTypeSpaceMedor.STRING, fields[1]);
161         QueryTreeField mfield3 = medorQTLeaf.addField("ADRESS", PTypeSpaceMedor.STRING, fields[2]);
162
163         // The filter expression : a filtering....
164
FieldOperand mfieldOp = new BasicFieldOperand(new BasicField("NSS", PTypeSpaceMedor.INT));
165         BasicOperand mvalue = new BasicOperand(1000);
166         Expression mFilter = new Lower(mfieldOp, mvalue);
167         // Setting the filter
168
medorQTLeaf.setQueryFilter(mFilter);
169
170         Expression jpFilter = new Lower(new BasicFieldOperand(mfield1), new BasicFieldOperand(cf1));
171         // Creating a JoinProject Operation
172
JoinProject jp = new JoinProject(jpFilter, "");
173
174         // Adding fields to project
175
jp.addPropagatedField("NSS", PTypeSpaceMedor.INT, new QueryTreeField[]{mfield1});
176         jp.addPropagatedField("NAME", PTypeSpaceMedor.STRING, new QueryTreeField[]{mfield2});
177         jp.addPropagatedField("Col_Att1", PTypeSpaceMedor.INT, new QueryTreeField[]{pf1});
178         jp.addPropagatedField("Col_Att1 + TableID_Att2 + x", PTypeSpaceMedor.INT, new QueryTreeField[]{cf1});
179
180         // This variable contains the path driver
181
String JavaDoc idbdriver = "org.enhydra.instantdb.jdbc.idbDriver";
182         JDBCConnection jdbcConn = new JDBCConnection();
183         Connection JavaDoc connection = jdbcConn.getConnection(dbURL, idbdriver);
184
185         // Preparing execution, generate indexes and setting TupleLoader
186
ExecPlanGenerator indexesGen = new IndexesGenerator();
187
188         System.out.print(" Generating indexes...");
189         QueryTree query = null;
190         try {
191             query = indexesGen.transform(jp);
192         }
193         catch (Exception JavaDoc e) {
194             System.out.println("Fail:");
195             e.printStackTrace();
196         }
197         System.out.println("success");
198
199         // Creates an evaluator associated to the QueryTree
200
QueryEvaluator evaluator = new MedorEvaluator(query, 0);
201
202         // Calculates and gets the required connection ressources for this query
203
ConnectionResources connRess = evaluator.getRequiredConnectionResources();
204
205         // Gets the QueryLeafs that requierd connections
206
QueryLeaf[] leafs = connRess.getRequiredQueryLeafConnection();
207
208         // Setting QueryLeaf's appropriated connection Object
209
for (int cpt = 0; (cpt < leafs.length); cpt++) {
210             connRess.setConnection(leafs[cpt], connection);// In our simple case
211
}
212
213         // Setting Parameter Values
214
x.setValue(-1800);
215
216         // Launching Medor Evaluator...
217
TupleCollection queryResult = evaluator.evaluate(new ParameterOperand[]{x}, connRess, null);
218
219         // Browsing results
220
System.out.println("RESULTS :");
221         int cpt = 0;
222
223         if (!queryResult.isEmpty()) {
224             do {
225                 cpt++;
226                 System.out.println("Tuple " + cpt + " :");
227                 System.out.println(queryResult.getMetaData().getField(1).getName() +
228                         " = " + queryResult.getTuple().getObject(1));
229                 System.out.println(queryResult.getMetaData().getField(2).getName() +
230                         " = " + queryResult.getTuple().getObject(2));
231                 System.out.println(queryResult.getMetaData().getField(3).getName() +
232                         " = " + queryResult.getTuple().getObject(3));
233                 System.out.println(queryResult.getMetaData().getField(4).getName() +
234                         " = " + queryResult.getTuple().getObject(4));
235                 System.out.println();
236             } while (queryResult.next());
237         } else
238             System.out.println("No result for this query");
239         connection.close();
240     }
241 }
Popular Tags