1 23 24 package org.objectweb.medor.query; 25 26 import junit.framework.Test; 27 import junit.framework.TestSuite; 28 import junit.textui.TestRunner; 29 import org.objectweb.medor.datasource.api.DataStore; 30 import org.objectweb.medor.datasource.lib.BasicDataStore; 31 import org.objectweb.medor.filter.api.FieldOperand; 32 import org.objectweb.medor.expression.lib.And; 33 import org.objectweb.medor.filter.lib.BasicFieldOperand; 34 import org.objectweb.medor.expression.lib.Equal; 35 import org.objectweb.medor.expression.lib.Greater; 36 import org.objectweb.medor.expression.lib.Lower; 37 import org.objectweb.medor.expression.lib.BasicOperand; 38 import org.objectweb.medor.expression.api.Expression; 39 import org.objectweb.medor.filter.lib.Avg; 40 import org.objectweb.medor.lib.Log; 41 import org.objectweb.medor.lib.TestMedorHelper; 42 import org.objectweb.medor.optim.api.RewriteRule; 43 import org.objectweb.medor.optim.rdb.GroupSameDBRule; 44 import org.objectweb.medor.query.api.QueryTreeField; 45 import org.objectweb.medor.query.lib.JoinProject; 46 import org.objectweb.medor.query.lib.QueryTreePrinter; 47 import org.objectweb.medor.query.lib.Nest; 48 import org.objectweb.medor.query.rdb.api.QualifiedTable; 49 import org.objectweb.medor.query.rdb.api.RdbExpField; 50 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf; 51 import org.objectweb.medor.query.rdb.lib.BasicQualifiedTable; 52 import org.objectweb.medor.query.rdb.lib.BasicRdbExpQueryLeaf; 53 import org.objectweb.medor.type.lib.PTypeSpaceMedor; 54 import org.objectweb.util.monolog.api.BasicLevel; 55 56 61 public class TestNest extends TestMedorHelper { 62 63 66 public static void main(String [] args) { 67 TestRunner.run(suite()); 68 } 69 70 73 public static Test suite() { 74 return new TestSuite(TestNest.class); 75 } 76 77 public TestNest() { 78 super("TestNest", Log.MEDOR_PREFIX + ".query.nest"); 79 } 80 81 public TestNest(String testName) { 82 super(testName, Log.MEDOR_PREFIX + ".query.nest"); 83 } 84 85 protected void setUp() { 86 } 87 88 protected void tearDown() { 89 } 90 91 public void testA() { 92 try { 93 logger.log(BasicLevel.DEBUG, "Entering A"); 94 QualifiedTable t1 = new BasicQualifiedTable("t1", null); 96 QualifiedTable t2 = new BasicQualifiedTable("t2", "al2"); 97 QualifiedTable[] tables1 = {t1, t2}; 98 DataStore ds = new BasicDataStore(DataStore.JDBC_STORE, "RDB"); 100 RdbExpQueryLeaf ql1 = 102 new BasicRdbExpQueryLeaf(ds, tables1, "" + "leaf1"); 103 RdbExpField f1 = 105 ql1.addRdbField("f1", PTypeSpaceMedor.INT, "col1", t1); 106 RdbExpField f2 = 107 ql1.addRdbField("f2", PTypeSpaceMedor.STRING, "col2", t2); 108 FieldOperand fieldOp1 = new BasicFieldOperand(f1); 110 FieldOperand fieldOp2 = new BasicFieldOperand(f2); 111 BasicOperand valueL = new BasicOperand(1013); 112 BasicOperand valueR = new BasicOperand("coucou"); 113 Expression left = new Lower(fieldOp1, valueL); 114 Expression right = new Greater(fieldOp2, valueR); 115 Expression filter1 = new And(left, right); 116 ql1.setQueryFilter(filter1); 118 QualifiedTable[] tables2 = {t2, t1}; 120 RdbExpQueryLeaf ql2 = 121 new BasicRdbExpQueryLeaf(ds, tables2, "leaf2"); 122 RdbExpField f3 = 123 ql2.addRdbField("f3", PTypeSpaceMedor.STRING, "col3", t2); 124 RdbExpField f4 = 125 ql2.addRdbField("f4", PTypeSpaceMedor.SHORT, "col4", t1); 126 FieldOperand fieldOp3 = new BasicFieldOperand(f3); 128 BasicOperand valueR1 = new BasicOperand("ayemayema"); 129 Expression filter2 = new Equal(fieldOp3, valueR1); 130 ql2.setQueryFilter(filter2); 131 JoinProject jp = new JoinProject("join"); 133 QueryTreeField pf5 = jp.addPropagatedField("f5", 134 PTypeSpaceMedor.INT, 135 new QueryTreeField[]{f1}); 136 QueryTreeField pf6 = jp.addPropagatedField("f6", 137 PTypeSpaceMedor.STRING, 138 new QueryTreeField[]{f2, f3}); 139 QueryTreeField pf7 = jp.addPropagatedField("f7", 140 PTypeSpaceMedor.SHORT, 141 new QueryTreeField[]{f4}); 142 FieldOperand fieldOp4 = new BasicFieldOperand(f2); 144 FieldOperand fieldOp5 = new BasicFieldOperand(f3); 145 Expression filter3 = new Equal(fieldOp4, fieldOp5); 146 jp.setQueryFilter(filter3); 147 Nest nest = new Nest( 149 new QueryTreeField[] {pf5, pf6, pf7}, 150 "grouped", 151 new QueryTreeField[] {}, 152 "nestNode"); 153 nest.addCalculatedField("avg", 154 PTypeSpaceMedor.FLOAT, 155 new Avg( 156 new BasicFieldOperand(pf7) 157 )); 158 logger.log(BasicLevel.DEBUG, "Original tree:"); 159 QueryTreePrinter.printQueryTree(nest, logger); 160 logger.log(BasicLevel.DEBUG, ql1.getSqlRequest(null, false, false)); 161 logger.log(BasicLevel.DEBUG, ql2.getSqlRequest(null, false, false)); 162 163 RewriteRule gqr = new GroupSameDBRule(); 164 RdbExpQueryLeaf rewrittenQuery = 165 (RdbExpQueryLeaf) (gqr.rewrite(jp)); 166 logger.log(BasicLevel.DEBUG, "Rewritten tree:"); 167 QueryTreePrinter.printQueryTree(rewrittenQuery, logger); 168 169 logger.log(BasicLevel.DEBUG, rewrittenQuery.getSqlRequest(null, 170 false, false)); 171 } 172 catch (Exception e) { 173 e.printStackTrace(); 174 fail("fail to create the request" + e.getMessage()); 175 } 176 } 177 } 178 | Popular Tags |