/** * Gets the best Query found so far * * @return the best Query, or null if no Queries added to this object */ @Override public Query getBestQuery() { return (queryString == null ? query : new Query(queryString)); }
/** * Returns the Query of this Candidate, converting from a String if necessary. * * @return a Query */ public Query getQuery() { return (query == null ? new Query(queryString) : query); }
/** * Allows a Query to be added to this tracker. * * @param q a query String to be added to the tracker */ @Override public void add(String q) { if (q == null) { throw new NullPointerException("Cannot add null queries to a BestQueryStorer"); } queries.add(new Query(q)); }
public void testTreeParserRulesForOrConstraint3() throws Exception { // g..OR.. OR (h..OR..) becomes g..OR.. OR h..OR.. Query lq1 = new Query("select t.a from t where t.g = 1 OR (t.ha = 2 OR t.hb = 3)"); Query lq2 = new Query("select t.a from t where t.g = 1 OR t.ha = 2 OR t.hb = 3"); assertEquals(lq1, lq2); lq1 = new Query("select t.a from t where t.ga = 1 OR t.gb = 4 OR (t.ha = 2 OR t.hb = 3)"); lq2 = new Query("select t.a from t where t.ga = 1 OR t.gb = 4 OR t.ha = 2 OR t.hb = 3"); assertEquals(lq1, lq2); }
public void testTreeParserRulesForAndConstraint5() throws Exception { // g..AND.. AND (h..AND..) becomes g..AND.. h..AND.. Query lq1 = new Query("select t.a from t where t.g = 1 AND (t.ha = 2 AND t.hb = 3)"); Query lq2 = new Query("select t.a from t where t.g = 1 AND t.ha = 2 AND t.hb = 3"); assertEquals(lq1, lq2); lq1 = new Query("select t.a from t where t.ga = 1 AND t.gb = 4 AND (t.ha = 2 AND t.hb = 3)"); lq2 = new Query("select t.a from t where t.ga = 1 AND t.gb = 4 AND t.ha = 2 AND t.hb = 3"); assertEquals(lq1, lq2); }
public void testPrecomputedTableWithNullName() throws Exception { try { PrecomputedTable pt = new PrecomputedTable(new Query(), "", null, null, con); fail("Expected: NullPointerException"); } catch (NullPointerException e) { } }
public void testOrderByUnaliasedFieldMultiTable() throws Exception { // if a field (not an alias) appears in order by without a table name we shouldn't add a // table name to it try { q1 = new Query("SELECT field1 FROM table1, table2 WHERE table1.k = table2.fk ORDER BY field1"); fail("Expected an IllegalArgumentExcption"); } catch (IllegalArgumentException e) { // expected } }
public void testMergeOkayDistinct2() throws Exception { Query q1 = new Query("SELECT DISTINCT table1.a AS t1_a, table1.b AS t1_b from table as table1 WHERE table1.c = 'five'"); Query pq1 = new Query("SELECT DISTINCT table2.a AS kjfd, table2.b AS ddfw FROM table as table2 WHERE table2.c = 'five'"); PrecomputedTable pt1 = new PrecomputedTable(pq1, pq1.getSQLString(), "precomp1", null, con); Query eq1 = new Query("SELECT DISTINCT P42.kjfd AS t1_a, P42.ddfw AS t1_b FROM precomp1 AS P42"); Set eSet = new HashSet(); eSet.add(eq1); StringUtil.setNextUniqueNumber(42); Set newSet = QueryOptimiser.merge(pt1, q1, q1); assertEquals(eSet, newSet); }
public void testRemapAliasesToAvoidPrecomputePrefix() throws Exception { Query q1 = new Query("SELECT table1.a, Putty.b FROM table1, table AS Putty"); Query eq1 = new Query("SELECT table1.a, P42.b FROM table1, table AS P42"); StringUtil.setNextUniqueNumber(42); QueryOptimiser.remapAliasesToAvoidPrecomputePrefix(q1); assertEquals(eq1, q1); }
public void testIsNotNullBugFails() throws Exception { String sql1 = "SELECT DISTINCT a2_.identifier AS a1_ FROM Gene AS a2_, Organism AS a3_ WHERE (a2_.organismId = a3_.id OR a2_.identifier IS NOT NULL) OR 'ENSANGG00000018976' < a2_.identifier ORDER BY a2_.identifier LIMIT 10000 OFFSET 1"; Query q1 = new Query(sql1); assertTrue(q1.toString().indexOf("IS NOT NULL") > 0); }
public void testIsNotNullBugWorks() throws Exception { String sql1 = "SELECT DISTINCT a2_.identifier AS a1_ FROM Gene AS a2_, Organism AS a3_ WHERE a2_.organismId = a3_.id OR a2_.identifier IS NOT NULL OR 'ENSANGG00000018976' < a2_.identifier ORDER BY a2_.identifier LIMIT 10000 OFFSET 1"; Query q1 = new Query(sql1); assertTrue(q1.toString().indexOf("IS NOT NULL") > 0); }
public void testMergeGroupByExtraSelect() throws Exception { Query q1 = new Query("SELECT table1.a AS t1_a, table1.b AS t1_b, table1.e AS t1_e, count(*) as stuff from table as table1, somethingelse as table2 WHERE table1.c = table2.a GROUP BY table1.a, table1.b, table1.d HAVING table1.d = 'five' ORDER BY table1.a LIMIT 100 OFFSET 0"); Query pq1 = new Query("SELECT table3.a AS sahjg, table3.b AS aytq, count(*) AS hksf, table3.d AS fdjsa FROM table AS table3, somethingelse AS table4 WHERE table3.c = table4.a GROUP BY table3.a, table3.b, table3.d"); PrecomputedTable pt1 = new PrecomputedTable(pq1, pq1.getSQLString(), "precomp1", null, con); Set eSet = new HashSet(); Set newSet = QueryOptimiser.mergeGroupBy(pt1, q1, q1); assertEquals(eSet, newSet); }
public void testMergeGroupByWrongHaving() throws Exception { Query q1 = new Query("SELECT table1.a AS t1_a, table1.b AS t1_b, count(*) as stuff from table as table1, somethingelse as table2 WHERE table1.c = table2.a GROUP BY table1.a, table1.b, table1.d HAVING table1.d = 'five' ORDER BY table1.a LIMIT 100 OFFSET 0"); Query pq1 = new Query("SELECT table3.a AS sahjg, table3.b AS aytq, count(*) AS hksf, table3.d AS fdjsa FROM table AS table3, somethingelse AS table4 WHERE table3.c = table4.a GROUP BY table3.a, table3.b, table3.d HAVING table3.b = 'five'"); PrecomputedTable pt1 = new PrecomputedTable(pq1, pq1.getSQLString(), "precomp1", null, con); Set eSet = new HashSet(); Set newSet = QueryOptimiser.mergeGroupBy(pt1, q1, q1); assertEquals(eSet, newSet); }
public void testMergeWrongSelect() throws Exception { Query q1 = new Query("SELECT table1.c AS t1_c, table1.b AS t1_b from table as table1"); Query pq1 = new Query("SELECT table2.a AS kjfd, table2.b AS ddfw FROM table as table2"); PrecomputedTable pt1 = new PrecomputedTable(pq1, pq1.getSQLString(), "precomp1", null, con); Set eSet = new HashSet(); Set newSet = QueryOptimiser.merge(pt1, q1, q1); assertEquals(eSet, newSet); }
public void testDeleteInvalid() throws Exception { Connection c = database.getConnection(); c.setAutoCommit(false); PrecomputedTableManager ptm = new PrecomputedTableManager(database); try { ptm.delete(new PrecomputedTable(new Query(), "", "tablenotthere", null, c)); fail("Expected: IllegalArgumentException"); } catch (IllegalArgumentException e) { } finally { c.close(); } }
public void testOrderByAlias() throws Exception { // referring to an alias in the order by should sort by the field q1 = new Query("SELECT field1 AS alias1 FROM table1 ORDER BY alias1"); q2 = new Query(); Table t1 = new Table("table1"); Field f1 = new Field("field1", t1); SelectValue sv1 = new SelectValue(f1, "alias1"); q2.addSelect(sv1); q2.addFrom(t1); q2.addOrderBy(f1); assertEquals(q2, q1); }
public void testSelectFunctionAlias() throws Exception { q1 = new Query("select max(table1.field1) as alias1 from table1"); q2 = new Query(); Table t1 = new Table("table1"); Field f1 = new Field("field1", t1); Function func1 = new Function(Function.MAX); func1.add(f1); SelectValue sv1 = new SelectValue(func1, "alias1"); q2.addSelect(sv1); q2.addFrom(t1); assertEquals(q2, q1); }
public void testWhereOneEqualConstraint() throws Exception { q1 = new Query("select table1.field1 from table1 where table1.field1 = 1"); q2 = new Query(); Table t1 = new Table("table1"); Field f1 = new Field("field1", t1); Constant c = new Constant("1"); SelectValue sv1 = new SelectValue(f1, null); q2.addSelect(sv1); q2.addFrom(t1); q2.addWhere(new Constraint(f1, Constraint.EQ, c)); assertEquals(q2, q1); }
public void testWhereNottedFieldLessThanField() throws Exception { q1 = new Query("select table1.field1 from table1 where not table1.field1 < table1.field2"); q2 = new Query(); Table t1 = new Table("table1"); Field f1 = new Field("field1", t1); Field f2 = new Field("field2", t1); SelectValue sv1 = new SelectValue(f1, null); q2.addSelect(sv1); q2.addFrom(t1); q2.addWhere(new NotConstraint(new Constraint(f1, Constraint.LT, f2))); assertEquals(q2, q1); }