1 18 package org.objectweb.medor.clone; 19 20 import org.objectweb.jorm.mapper.rdb.adapter.RdbAdapterFactory; 21 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapterException; 22 import org.objectweb.jorm.type.api.PTypeSpace; 23 import org.objectweb.medor.datasource.api.DataStore; 24 import org.objectweb.medor.datasource.lib.BasicDataStore; 25 import org.objectweb.medor.expression.api.Operand; 26 import org.objectweb.medor.expression.api.Operator; 27 import org.objectweb.medor.expression.lib.And; 28 import org.objectweb.medor.expression.lib.BasicOperand; 29 import org.objectweb.medor.expression.lib.BasicVariableOperand; 30 import org.objectweb.medor.expression.lib.Equal; 31 import org.objectweb.medor.expression.lib.Greater; 32 import org.objectweb.medor.expression.lib.Or; 33 import org.objectweb.medor.filter.jorm.lib.SinglePName; 34 import org.objectweb.medor.filter.lib.BasicFieldOperand; 35 import org.objectweb.medor.filter.lib.Count; 36 import org.objectweb.medor.lib.BasicField; 37 import org.objectweb.medor.lib.BasicTupleStructure; 38 import org.objectweb.medor.query.TestQueryTreeHelper; 39 import org.objectweb.medor.query.api.QueryTree; 40 import org.objectweb.medor.query.api.QueryTreeField; 41 import org.objectweb.medor.query.lib.JoinProject; 42 import org.objectweb.medor.query.lib.MedorTCQueryLeaf; 43 import org.objectweb.medor.query.lib.SelectProject; 44 import org.objectweb.medor.query.rdb.api.QualifiedTable; 45 import org.objectweb.medor.query.rdb.api.RdbExpField; 46 import org.objectweb.medor.query.rdb.api.RdbField; 47 import org.objectweb.medor.query.rdb.lib.AggregateRdbQueryNode; 48 import org.objectweb.medor.query.rdb.lib.BasicQualifiedTable; 49 import org.objectweb.medor.query.rdb.lib.BasicRdbExpQueryLeaf; 50 import org.objectweb.medor.query.rdb.lib.BasicRdbStringQueryLeaf; 51 import org.objectweb.medor.tuple.lib.ExplicitTupleCollection; 52 import org.objectweb.medor.tuple.lib.MemoryTuple; 53 import org.objectweb.medor.type.lib.PTypeSpaceMedor; 54 import org.objectweb.medor.type.lib.QType; 55 56 61 public class TestQueryTreeCloning extends TestQueryTreeHelper { 62 63 public TestQueryTreeCloning(String n) { 64 super(n); 65 } 66 67 public void testRdbExpQL() throws Exception { 68 QualifiedTable table = new BasicQualifiedTable("T_REF_SING_A", "T_REF_SING_A_0"); 69 String name = "org.objectweb.medor.optim.jorm.rdb.A"; 70 BasicRdbExpQueryLeaf ql = new BasicRdbExpQueryLeaf(null, new QualifiedTable[] {table}, name); 71 ql.setDataStore(new BasicDataStore(DataStore.JDBC_STORE, "RDB")); 72 ql.setDistinct(true); 73 try { 74 ql.setRdbAdapter(RdbAdapterFactory.getTypeConverter((String ) null)); 75 } catch (RdbAdapterException ex) { 76 fail(ex.getMessage()); 77 } 78 String prefix = name + "."; 79 ql.addRdbField(prefix + "id", PTypeSpace.LONG, "id_col", table); 80 ql.addRdbField(prefix + "field1", PTypeSpace.STRING, "FIELD_1", table); 81 ql.addRdbField(prefix + "field2", PTypeSpace.STRING, "FIELD_2", table); 82 83 Object clone = ql.clone(); 84 assertNotNull("Null clone", clone); 85 assertEquals("Bad class", ql.getClass(), clone.getClass()); 86 equals("testRdbExpQL", ql, (QueryTree) clone, logger); 87 } 88 89 public void testRdbExpQLWithFilter() throws Exception { 90 QualifiedTable table = new BasicQualifiedTable("T_REF_SING_A", "T_REF_SING_A_0"); 91 String name = "org.objectweb.medor.optim.jorm.rdb.A"; 92 BasicRdbExpQueryLeaf ql = new BasicRdbExpQueryLeaf( 93 new BasicDataStore(DataStore.JDBC_STORE, "RDB"), 94 new QualifiedTable[] {table}, 95 name); 96 ql.setDistinct(true); 97 try { 98 ql.setRdbAdapter(RdbAdapterFactory.getTypeConverter((String ) null)); 99 } catch (RdbAdapterException ex) { 100 fail(ex.getMessage()); 101 } 102 String prefix = name + "."; 103 ql.addRdbField(prefix + "id", PTypeSpace.LONG, "id_col", table); 104 RdbExpField f1 = ql.addRdbField(prefix + "field1", PTypeSpace.STRING, "FIELD_1", table); 105 RdbExpField f2 = ql.addRdbField(prefix + "field2", PTypeSpace.STRING, "FIELD_2", table); 106 107 ql.setQueryFilter(new Or( 108 new Greater(new BasicFieldOperand(f1), new BasicOperand(12)), 109 new And( 110 new BasicOperand(true), 111 new Equal(new BasicFieldOperand(f2), new BasicOperand(12)) 112 ))); 113 114 Object clone = ql.clone(); 115 assertNotNull("Null clone", clone); 116 assertEquals("Bad class", ql.getClass(), clone.getClass()); 117 equals("testRdbExpQLWithFilter", ql, (QueryTree) clone, logger); 118 119 BasicRdbExpQueryLeaf cql = (BasicRdbExpQueryLeaf) clone; 122 RdbExpField cf1 = (RdbExpField) cql.getField(prefix + "field1"); 123 RdbExpField cf2 = (RdbExpField) cql.getField(prefix + "field2"); 124 assertTrue("field f1 is not cloned !", f1 != cf1); 125 assertTrue("field f2 is not cloned !", f2 != cf2); 126 assertTrue("adapter cloned !", ql.getRdbAdapter() == cql.getRdbAdapter()); 127 assertEquals("bad adapter name !", ql.getRdbAdapterName(), cql.getRdbAdapterName()); 128 Operator op = (Operator) cql.getQueryFilter(); 129 BasicFieldOperand bfo = (BasicFieldOperand) 130 ((Operator) op.getExpression(0)).getExpression(0); 131 assertTrue("field f1 from filter is not a field f1 from QT", bfo.getField() == cf1); 132 bfo = (BasicFieldOperand) 133 ((Operator) ((Operator) op.getExpression(1)).getExpression(1)).getExpression(0); 134 assertTrue("field f1 from filter is not a field f1 from QT", bfo.getField() == cf2); 135 } 136 137 public void testRdbStringQL() throws Exception { 138 String name = "org.objectweb.medor.optim.jorm.rdb.A"; 139 BasicRdbStringQueryLeaf ql = new BasicRdbStringQueryLeaf( 140 new BasicDataStore(DataStore.JDBC_STORE, "RDB"), 141 "SELECT id_col, FIELD_1, FIELD_2 FROM TA wHERE id_col > 0", 142 name); 143 String prefix = name + "."; 144 ql.addRdbField(prefix + "id", PTypeSpace.LONG, "id_col"); 145 ql.addRdbField(prefix + "field1", PTypeSpace.STRING, "FIELD_1"); 146 ql.addRdbField(prefix + "field2", PTypeSpace.STRING, "FIELD_2"); 147 148 Object clone = ql.clone(); 149 assertNotNull("Null clone", clone); 150 assertEquals("Bad class", ql.getClass(), clone.getClass()); 151 equals("testRdbExpQL", ql, (QueryTree) clone, logger); 152 } 153 154 public void testAggregateRdbQueryNode() throws Exception { 155 QualifiedTable table = new BasicQualifiedTable("T_REF_SING_A", "T_REF_SING_A_0"); 156 String name = "org.objectweb.medor.optim.jorm.rdb.A"; 157 BasicRdbExpQueryLeaf ql = new BasicRdbExpQueryLeaf( 158 new BasicDataStore(DataStore.JDBC_STORE, "RDB"), 159 new QualifiedTable[] {table}, 160 name); 161 String prefix = name + "."; 162 ql.addRdbField(prefix + "id", PTypeSpace.LONG, "id_col", table); 163 RdbExpField f1 = ql.addRdbField(prefix + "f1", PTypeSpace.STRING, "FIELD_1", table); 164 RdbExpField f2 = ql.addRdbField(prefix + "f2", PTypeSpace.STRING, "FIELD_2", table); 165 166 ql.setQueryFilter(new Or( 167 new Greater(new BasicFieldOperand(f1), new BasicOperand(12)), 168 new And( 169 new BasicOperand(true), 170 new Equal(new BasicFieldOperand(f2), new BasicOperand(12)) 171 ))); 172 173 174 QueryTreeField expF2 = (QueryTreeField) 175 ql.getTupleStructure().getField(prefix + "f2"); 176 QueryTreeField expF1 = (QueryTreeField) 177 ql.getTupleStructure().getField(prefix + "f1"); 178 ql.setDistinct(true); 179 ql.setIsSubquery(true); 180 AggregateRdbQueryNode expected = new AggregateRdbQueryNode( 181 new QueryTreeField[]{expF1, expF2}, "grouped", 182 new QueryTreeField[] {}, ql, null, "nestNode"); 183 Count theCount = new Count(new BasicFieldOperand(expF2), true); 184 theCount.setCountAll(); 185 expected.addCalculatedField("count", 186 PTypeSpaceMedor.INT, 187 theCount 188 ); 189 190 Object clone = expected.clone(); 191 assertNotNull("Null clone", clone); 192 assertEquals("Bad class", expected.getClass(), clone.getClass()); 193 equals("testRdbExpQLWithFilter", expected, (QueryTree) clone, logger); 194 } 195 196 public void testMedorTCQueryLeaf() throws Exception { 197 BasicTupleStructure ts = new BasicTupleStructure(new BasicField[] { 198 new BasicField("f1", PTypeSpace.INT), 199 new BasicField("f2", PTypeSpace.STRING), 200 new BasicField("f3", PTypeSpace.LONG), 201 }); 202 ExplicitTupleCollection tc = new ExplicitTupleCollection(ts); 203 tc.insertTuple(new MemoryTuple(new Operand[]{ 204 new BasicOperand(1), 205 new BasicOperand("a"), 206 new BasicOperand(1l), 207 })); 208 tc.insertTuple(new MemoryTuple(new Operand[]{ 209 new BasicOperand(2), 210 new BasicOperand("b"), 211 new BasicOperand(2l), 212 })); 213 tc.insertTuple(new MemoryTuple(new Operand[]{ 214 new BasicOperand(3), 215 new BasicOperand("c"), 216 new BasicOperand(3l), 217 })); 218 MedorTCQueryLeaf ql = new MedorTCQueryLeaf( 219 "toto, ", "TC_NAME", new ExplicitTupleCollection(ts)); 220 221 Object clone = ql.clone(); 222 assertNotNull("Null clone", clone); 223 assertEquals("Bad class", ql.getClass(), clone.getClass()); 224 equals("testRdbExpQL", ql, (QueryTree) clone, logger); 225 } 226 227 public void testJoin() throws Exception { 228 String aname = "org.objectweb.medor.optim.jorm.rdb.A"; 229 BasicRdbStringQueryLeaf aql = new BasicRdbStringQueryLeaf( 230 new BasicDataStore(DataStore.JDBC_STORE, "RDB"), 231 "SELECT AID, AF1, AF2 FROM TA", 232 aname); 233 String aprefix = aname + "."; 234 RdbField aid = aql.addRdbField(aprefix + "aid", PTypeSpace.LONG, "AID"); 235 RdbField af1 = aql.addRdbField(aprefix + "af1", PTypeSpace.STRING, "AF1"); 236 RdbField af2 = aql.addRdbField(aprefix + "af2", PTypeSpace.STRING, "AF2"); 237 238 String bname = "org.objectweb.medor.optim.jorm.rdb.A"; 239 BasicRdbStringQueryLeaf bql = new BasicRdbStringQueryLeaf( 240 new BasicDataStore(DataStore.JDBC_STORE, "RDB"), 241 "SELECT BID, BF1, BF2 FROM TB", 242 bname); 243 String prefix = bname + "."; 244 RdbField bid = bql.addRdbField(prefix + "bid", PTypeSpace.LONG, "BID"); 245 RdbField bf1 = bql.addRdbField(prefix + "bf1", PTypeSpace.STRING, "BF1"); 246 RdbField bf2 = bql.addRdbField(prefix + "bf2", PTypeSpace.STRING, "BF2"); 247 248 JoinProject jp = new JoinProject( 249 new Equal(new BasicFieldOperand(af1), new BasicFieldOperand(bf2)), 250 "J1"); 251 QueryTreeField faid = jp.addPropagatedField(aid.getName(), aid.getType(), new QueryTreeField[]{aid}); 252 QueryTreeField ff1 = jp.addPropagatedField(af1.getName(), af1.getType(), new QueryTreeField[]{af1}); 253 jp.addPropagatedField(af2.getName(), af2.getType(), new QueryTreeField[]{af2}); 254 jp.addPropagatedField(bid.getName(), bid.getType(), new QueryTreeField[]{bid}); 255 jp.addPropagatedField(bf1.getName(), bf1.getType(), new QueryTreeField[]{bf1}); 256 257 SelectProject sp = new SelectProject("SP1"); 258 sp.addCalculatedField("APName", PTypeSpaceMedor.NAMING_CONTEXT, 259 new SinglePName( 260 new BasicFieldOperand(faid), 261 new BasicVariableOperand(QType.TYPECODE_NAMING_CONTEXT), 262 PTypeSpace.BOOLEAN)); 263 sp.setQueryFilter(new Equal(new BasicFieldOperand(ff1), new BasicOperand("azerty"))); 264 265 Object clone = sp.clone(); 266 assertNotNull("Null clone", clone); 267 assertEquals("Bad class", sp.getClass(), clone.getClass()); 268 equals("testRdbExpQL", sp, (QueryTree) clone, logger); 269 } 270 } 271 | Popular Tags |