1 23 24 package org.objectweb.medor.optim; 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.MemberOf; 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.rdb.api.QualifiedTable; 48 import org.objectweb.medor.query.rdb.api.RdbExpField; 49 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf; 50 import org.objectweb.medor.query.rdb.lib.BasicQualifiedTable; 51 import org.objectweb.medor.query.rdb.lib.BasicRdbExpQueryLeaf; 52 import org.objectweb.medor.type.lib.PTypeSpaceMedor; 53 import org.objectweb.util.monolog.api.BasicLevel; 54 55 import java.util.ArrayList ; 56 57 62 public class TestGroupSameDBRule extends TestMedorHelper { 63 64 67 public static void main(String [] args) { 68 TestRunner.run(suite()); 69 } 70 71 74 public static Test suite() { 75 return new TestSuite(TestGroupSameDBRule.class); 76 } 77 78 public TestGroupSameDBRule() { 79 super("TestGroupSameDBRule", Log.MEDOR_PREFIX + ".optim.groupsamedb"); 80 } 81 82 public TestGroupSameDBRule(String testName) { 83 super(testName, Log.MEDOR_PREFIX + ".optim.groupsamedb"); 84 } 85 86 protected void setUp() { 87 } 88 89 protected void tearDown() { 90 } 91 92 public void testA() { 93 try { 94 logger.log(BasicLevel.DEBUG, "Entering testA"); 95 QualifiedTable t1 = new BasicQualifiedTable("t1", null); 97 QualifiedTable t2 = new BasicQualifiedTable("t2", "al2"); 98 QualifiedTable[] tables1 = {t1, t2}; 99 DataStore ds = new BasicDataStore(DataStore.JDBC_STORE, "RDB"); 101 RdbExpQueryLeaf ql1 = 103 new BasicRdbExpQueryLeaf(ds, tables1, "" + "leaf1"); 104 RdbExpField f1 = 106 ql1.addRdbField("f1", PTypeSpaceMedor.INT, "col1", t1); 107 RdbExpField f2 = 108 ql1.addRdbField("f2", PTypeSpaceMedor.STRING, "col2", t2); 109 FieldOperand fieldOp1 = new BasicFieldOperand(f1); 111 FieldOperand fieldOp2 = new BasicFieldOperand(f2); 112 BasicOperand valueL = new BasicOperand(1013); 113 BasicOperand valueR = new BasicOperand("coucou"); 114 Expression left = new Lower(fieldOp1, valueL); 115 Expression right = new Greater(fieldOp2, valueR); 116 Expression filter1 = new And(left, right); 117 ql1.setQueryFilter(filter1); 119 QualifiedTable[] tables2 = {t2, t1}; 121 RdbExpQueryLeaf ql2 = 122 new BasicRdbExpQueryLeaf(ds, tables2, "leaf2"); 123 RdbExpField f3 = 124 ql2.addRdbField("f3", PTypeSpaceMedor.STRING, "col3", t2); 125 RdbExpField f4 = 126 ql2.addRdbField("f4", PTypeSpaceMedor.SHORT, "col4", t1); 127 FieldOperand fieldOp3 = new BasicFieldOperand(f3); 129 BasicOperand valueR1 = new BasicOperand("ayemayema"); 130 Expression filter2 = new Equal(fieldOp3, valueR1); 131 ql2.setQueryFilter(filter2); 132 JoinProject jp = new JoinProject("join"); 134 jp.addPropagatedField("f5", PTypeSpaceMedor.INT, 135 new QueryTreeField[]{f1}); 136 jp.addPropagatedField("f6", PTypeSpaceMedor.STRING, 137 new QueryTreeField[]{f2, f3}); 138 jp.addPropagatedField("f7", PTypeSpaceMedor.SHORT, 139 new QueryTreeField[]{f4}); 140 FieldOperand fieldOp4 = new BasicFieldOperand(f2); 142 FieldOperand fieldOp5 = new BasicFieldOperand(f3); 143 Expression filter3 = new Equal(fieldOp4, fieldOp5); 144 jp.setQueryFilter(filter3); 145 logger.log(BasicLevel.DEBUG, "Original tree:"); 146 QueryTreePrinter.printQueryTree(jp, logger); 147 logger.log(BasicLevel.DEBUG, 148 ql1.getSqlRequest(null, false, false)); 149 logger.log(BasicLevel.DEBUG, 150 ql2.getSqlRequest(null, false, false)); 151 152 RewriteRule gqr = new GroupSameDBRule(); 153 RdbExpQueryLeaf rewrittenQuery = 154 (RdbExpQueryLeaf) (gqr.rewrite(jp)); 155 logger.log(BasicLevel.DEBUG, "Rewritten tree:"); 156 QueryTreePrinter.printQueryTree(rewrittenQuery, logger); 157 158 logger.log(BasicLevel.DEBUG, 159 rewrittenQuery.getSqlRequest(null, false, false)); 160 } 161 catch (Exception e) { 162 e.printStackTrace(); 163 fail("fail to create the request" + e.getMessage()); 164 } 165 } 166 167 170 public void testMemberOf1() { 171 try { 172 logger.log(BasicLevel.DEBUG, "Entering testMemberOf1"); 173 QualifiedTable t1 = new BasicQualifiedTable("t1", null); 175 QualifiedTable t2 = new BasicQualifiedTable("t2", null); 176 QualifiedTable[] tables1 = {t1}; 177 DataStore ds = new BasicDataStore(DataStore.JDBC_STORE, "RDB"); 179 RdbExpQueryLeaf ql1 = 181 new BasicRdbExpQueryLeaf(ds, tables1, "leaf1"); 182 RdbExpField f1 = 184 ql1.addRdbField("f1", PTypeSpaceMedor.INT, "col1", t1); 185 RdbExpField f2 = 186 ql1.addRdbField("f2", PTypeSpaceMedor.STRING, "col2", t1); 187 QualifiedTable[] tables2 = {t2}; 189 RdbExpQueryLeaf ql2 = 190 new BasicRdbExpQueryLeaf(ds, tables2, "leaf2"); 191 RdbExpField f3 = 192 ql2.addRdbField("f3", PTypeSpaceMedor.STRING, "col3", t2); 193 JoinProject jp = new JoinProject("join"); 195 jp.addPropagatedField("f5", PTypeSpaceMedor.INT, 196 new QueryTreeField[]{f1}); 197 FieldOperand fieldOp4 = new BasicFieldOperand(f2); 199 FieldOperand fieldOp5 = new BasicFieldOperand(f3); 200 ArrayList list1 = new ArrayList (1); 201 ArrayList list2 = new ArrayList (1); 202 list1.add(fieldOp4); 203 list2.add(fieldOp5); 204 Expression filter3 = new MemberOf(list1, list2); 205 jp.setQueryFilter(filter3); 206 logger.log(BasicLevel.DEBUG, "Original tree:"); 207 QueryTreePrinter.printQueryTree(jp, logger); 208 logger.log(BasicLevel.DEBUG, ql1.getSqlRequest(null, false, false)); 209 logger.log(BasicLevel.DEBUG, ql2.getSqlRequest(null, false, false)); 210 RewriteRule gqr = new GroupSameDBRule(); 211 RdbExpQueryLeaf rewrittenQuery = 212 (RdbExpQueryLeaf) (gqr.rewrite(jp)); 213 logger.log(BasicLevel.DEBUG, "Rewritten tree:"); 214 QueryTreePrinter.printQueryTree(rewrittenQuery, logger); 215 216 logger.log(BasicLevel.DEBUG, rewrittenQuery.getSqlRequest(null, 217 false, false)); 218 } 219 catch (Exception e) { 220 e.printStackTrace(); 221 fail("fail to create the request" + e.getMessage()); 222 } 223 } 224 225 public void testMemberOf2() { 226 try { 227 logger.log(BasicLevel.DEBUG, "Entering testMemberOf2"); 228 QualifiedTable t1 = new BasicQualifiedTable("t1", null); 230 QualifiedTable t2 = new BasicQualifiedTable("t2", null); 231 QualifiedTable t3 = new BasicQualifiedTable("t3", null); 232 QualifiedTable[] tables1 = {t1}; 233 DataStore ds = new BasicDataStore(DataStore.JDBC_STORE, "RDB"); 235 RdbExpQueryLeaf ql1 = 237 new BasicRdbExpQueryLeaf(ds, tables1, "leaf1"); 238 RdbExpField f11 = 240 ql1.addRdbField("f11", PTypeSpaceMedor.INT, "col1", t1); 241 RdbExpField f12 = 242 ql1.addRdbField("f12", PTypeSpaceMedor.STRING, "col2", t1); 243 RdbExpField f13 = 244 ql1.addRdbField("f13", PTypeSpaceMedor.STRING, "col3", t1); 245 QualifiedTable[] tables2 = {t2}; 247 RdbExpQueryLeaf ql2 = 248 new BasicRdbExpQueryLeaf(ds, tables2, "leaf2"); 249 RdbExpField f23 = 250 ql2.addRdbField("f23", PTypeSpaceMedor.STRING, "col3", t2); 251 QualifiedTable[] tables3 = {t3}; 253 RdbExpQueryLeaf ql3 = 254 new BasicRdbExpQueryLeaf(ds, tables3, "leaf3"); 255 RdbExpField f31 = 256 ql3.addRdbField("f31", PTypeSpaceMedor.STRING, "col3", t3); 257 JoinProject jp = new JoinProject("join"); 259 jp.addPropagatedField("f5", PTypeSpaceMedor.INT, 260 new QueryTreeField[]{f11}); 261 FieldOperand fieldOp1 = new BasicFieldOperand(f12); 263 FieldOperand fieldOp2 = new BasicFieldOperand(f23); 264 ArrayList list1 = new ArrayList (1); 265 ArrayList list2 = new ArrayList (1); 266 list1.add(fieldOp1); 267 list2.add(fieldOp2); 268 Expression filter1 = new MemberOf(list1, list2); 269 FieldOperand fieldOp3 = new BasicFieldOperand(f13); 271 FieldOperand fieldOp4 = new BasicFieldOperand(f31); 272 ArrayList list3 = new ArrayList (1); 273 ArrayList list4 = new ArrayList (1); 274 list3.add(fieldOp3); 275 list4.add(fieldOp4); 276 Expression filter2 = new MemberOf(list3, list4); 277 jp.setQueryFilter(new And(filter1, filter2)); 278 logger.log(BasicLevel.DEBUG, "Original tree:"); 279 QueryTreePrinter.printQueryTree(jp, logger); 280 logger.log(BasicLevel.DEBUG, ql1.getSqlRequest(null, false, false)); 281 logger.log(BasicLevel.DEBUG, ql2.getSqlRequest(null, false, false)); 282 logger.log(BasicLevel.DEBUG, ql3.getSqlRequest(null, false, false)); 283 RewriteRule gqr = new GroupSameDBRule(); 284 RdbExpQueryLeaf rewrittenQuery = 285 (RdbExpQueryLeaf) (gqr.rewrite(jp)); 286 logger.log(BasicLevel.DEBUG, "Rewritten tree:"); 287 QueryTreePrinter.printQueryTree(rewrittenQuery, logger); 288 logger.log(BasicLevel.DEBUG, rewrittenQuery.getSqlRequest(null, 289 false, false)); 290 } 291 catch (Exception e) { 292 e.printStackTrace(); 293 fail("fail to create the request" + e.getMessage()); 294 } 295 } 296 } 297 | Popular Tags |