1 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 ; 68 import java.sql.SQLException ; 69 70 74 public class MedorQueriesExample { 75 76 public static void main(String arg[]) 77 throws MedorException, ExpressionException, SQLException { 78 String dbURL = new String ("jdbc:idb:c:/EnhydraDB/Examples/sample.prp"); 81 String sqlQuery1 = new String ("SELECT * FROM Sample$db$Cols"); 83 String sqlQuery2 = new String ("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 RdbStringQueryLeaf sqlLeaf = new BasicRdbStringQueryLeaf(ds, sqlQuery1, ""); 91 92 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 FieldOperand fieldOp = new BasicFieldOperand(field1); 101 BasicOperand value = new BasicOperand(1013); 102 Expression filter = new Lower(fieldOp, value); 103 SelectProject sp = new SelectProject(filter, ""); 105 106 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 ParameterOperand x = new BasicParameterOperand(PTypeSpaceMedor.INT, "x"); 114 Expression exp1 = new Plus(new Plus(new BasicFieldOperand(field1), new BasicFieldOperand(field2)), x); 115 Expression exp2 = new Minus(new BasicFieldOperand(field1), new BasicFieldOperand(field2)); 117 CalculatedField cf1 = sp.addCalculatedField("Col_Att1 + TableID_Att2 + x", PTypeSpaceMedor.INT, exp1); 119 CalculatedField cf2 = sp.addCalculatedField("Col_Att1 -TableID_Att2", PTypeSpaceMedor.INT, exp2); 121 122 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 TupleStructure ts = new BasicTupleStructure(fields); 132 133 Operand[] vops1 = new Operand[4]; 135 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 Tuple t1 = new MemoryTuple(vops1); 149 Tuple t2 = new MemoryTuple(vops2); 150 151 ExplicitTupleCollection tc = new ExplicitTupleCollection(ts); 153 154 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 FieldOperand mfieldOp = new BasicFieldOperand(new BasicField("NSS", PTypeSpaceMedor.INT)); 165 BasicOperand mvalue = new BasicOperand(1000); 166 Expression mFilter = new Lower(mfieldOp, mvalue); 167 medorQTLeaf.setQueryFilter(mFilter); 169 170 Expression jpFilter = new Lower(new BasicFieldOperand(mfield1), new BasicFieldOperand(cf1)); 171 JoinProject jp = new JoinProject(jpFilter, ""); 173 174 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 String idbdriver = "org.enhydra.instantdb.jdbc.idbDriver"; 182 JDBCConnection jdbcConn = new JDBCConnection(); 183 Connection connection = jdbcConn.getConnection(dbURL, idbdriver); 184 185 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 e) { 194 System.out.println("Fail:"); 195 e.printStackTrace(); 196 } 197 System.out.println("success"); 198 199 QueryEvaluator evaluator = new MedorEvaluator(query, 0); 201 202 ConnectionResources connRess = evaluator.getRequiredConnectionResources(); 204 205 QueryLeaf[] leafs = connRess.getRequiredQueryLeafConnection(); 207 208 for (int cpt = 0; (cpt < leafs.length); cpt++) { 210 connRess.setConnection(leafs[cpt], connection); } 212 213 x.setValue(-1800); 215 216 TupleCollection queryResult = evaluator.evaluate(new ParameterOperand[]{x}, connRess, null); 218 219 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 |