/** * Processes an AST node that describes a HAVING condition. * * @param node an AST node to process */ private void processHavingClause(AST node) { AST ast = node; do { switch (ast.getType()) { case SqlTokenTypes.AND_CONSTRAINT_SET: // Recurse - it's an AND set, equivalent to a HAVING_CLAUSE processHavingClause(ast.getFirstChild()); break; case SqlTokenTypes.OR_CONSTRAINT_SET: case SqlTokenTypes.CONSTRAINT: case SqlTokenTypes.NOT_CONSTRAINT: case SqlTokenTypes.SUBQUERY_CONSTRAINT: case SqlTokenTypes.INLIST_CONSTRAINT: case SqlTokenTypes.NULL_CONSTRAINT: addHaving(processNewAbstractConstraint(ast)); break; default: throw (new IllegalArgumentException("Unknown AST node: " + ast.getText() + " [" + ast.getType() + "]")); } ast = ast.getNextSibling(); } while (ast != null); }
/** * Processes an AST node that describes a HAVING condition. * * @param node an AST node to process */ private void processHavingClause(AST node) { AST ast = node; do { switch (ast.getType()) { case SqlTokenTypes.AND_CONSTRAINT_SET: // Recurse - it's an AND set, equivalent to a HAVING_CLAUSE processHavingClause(ast.getFirstChild()); break; case SqlTokenTypes.OR_CONSTRAINT_SET: case SqlTokenTypes.CONSTRAINT: case SqlTokenTypes.NOT_CONSTRAINT: case SqlTokenTypes.SUBQUERY_CONSTRAINT: case SqlTokenTypes.INLIST_CONSTRAINT: case SqlTokenTypes.NULL_CONSTRAINT: addHaving(processNewAbstractConstraint(ast)); break; default: throw (new IllegalArgumentException("Unknown AST node: " + ast.getText() + " [" + ast.getType() + "]")); } ast = ast.getNextSibling(); } while (ast != null); }
public void testHavingTwoEqualConstraints() throws Exception { q1 = new Query("select table1.field1 from table1 group by table1.field1 having table1.field1 = 1 and table1.field2 = 2"); q2 = new Query(); Table t1 = new Table("table1"); Field f1 = new Field("field1", t1); Field f2 = new Field("field2", t1); Constant c1 = new Constant("1"); Constant c2 = new Constant("2"); SelectValue sv1 = new SelectValue(f1, null); q2.addSelect(sv1); q2.addFrom(t1); q2.addGroupBy(f1); q2.addHaving(new Constraint(f1, Constraint.EQ, c1)); q2.addHaving(new Constraint(f2, Constraint.EQ, c2)); assertEquals(q2, q1); }
public void testHavingNottedConstraintSet() throws Exception { q1 = new Query("select table1.field1 from table1 group by table1.field1 having not (table1.field1 = table1.field2 or table1.field1 = table1.field3)"); q2 = new Query(); Table t1 = new Table("table1"); Field f1 = new Field("field1", t1); Field f2 = new Field("field2", t1); Field f3 = new Field("field3", t1); SelectValue sv1 = new SelectValue(f1, null); q2.addSelect(sv1); q2.addFrom(t1); q2.addGroupBy(f1); q2.addHaving(new NotConstraint(new Constraint(f1, Constraint.EQ, f2))); q2.addHaving(new NotConstraint(new Constraint(f1, Constraint.EQ, f3))); assertEquals(q2, q1); }
public void testHavingOneLessThanConstraint() throws Exception { q1 = new Query("select table1.field1 from table1 group by table1.field1 having 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.addGroupBy(f1); q2.addHaving(new Constraint(f1, Constraint.LT, c)); assertEquals(q2, q1); }
public void testHavingFieldLessThanField() throws Exception { q1 = new Query("select table1.field1 from table1 group by table1.field1 having 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.addGroupBy(f1); q2.addHaving(new Constraint(f1, Constraint.LT, f2)); assertEquals(q2, q1); }
public void testHavingNottedFieldLessThanField() throws Exception { q1 = new Query("select table1.field1 from table1 group by table1.field1 having 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.addGroupBy(f1); q2.addHaving(new NotConstraint(new Constraint(f1, Constraint.LT, f2))); assertEquals(q2, q1); }
public void testHavingFieldLessThanFunction() throws Exception { q1 = new Query("select table1.field1 from table1 group by table1.field1 having table1.field1 < avg(table1.field2)"); q2 = new Query(); Table t1 = new Table("table1"); Field f1 = new Field("field1", t1); Field f2 = new Field("field2", t1); Function func1 = new Function(Function.AVG); func1.add(f2); SelectValue sv1 = new SelectValue(f1, null); q2.addSelect(sv1); q2.addFrom(t1); q2.addGroupBy(f1); q2.addHaving(new Constraint(f1, Constraint.LT, func1)); assertEquals(q2, q1); }
public void testHavingFieldLessThanPlusFunction() throws Exception { q1 = new Query("select table1.field1 from table1 group by table1.field1 having table1.field1 < table1.field2 + table1.field3"); q2 = new Query(); Table t1 = new Table("table1"); Field f1 = new Field("field1", t1); Field f2 = new Field("field2", t1); Field f3 = new Field("field3", t1); Function func1 = new Function(Function.PLUS); func1.add(f2); func1.add(f3); SelectValue sv1 = new SelectValue(f1, null); q2.addSelect(sv1); q2.addFrom(t1); q2.addGroupBy(f1); q2.addHaving(new Constraint(f1, Constraint.LT, func1)); assertEquals(q2, q1); }
public void testHavingConstraintSet() throws Exception { q1 = new Query("select table1.field1 from table1 group by table1.field1 having (table1.field1 = table1.field2 or table1.field1 = table1.field3)"); q2 = new Query(); Table t1 = new Table("table1"); Field f1 = new Field("field1", t1); Field f2 = new Field("field2", t1); Field f3 = new Field("field3", t1); SelectValue sv1 = new SelectValue(f1, null); q2.addSelect(sv1); q2.addFrom(t1); q2.addGroupBy(f1); ConstraintSet cs1 = new ConstraintSet(); cs1.add(new Constraint(f1, Constraint.EQ, f2)); cs1.add(new Constraint(f1, Constraint.EQ, f3)); q2.addHaving(cs1); assertEquals(q2, q1); }
public void testHavingFieldInSubQuery() throws Exception { q1 = new Query("select table1.field1 from table1 group by table1.field1 having table1.field1 in (select table2.field2 from table2)"); q2 = new Query(); q3 = new Query(); Table t1 = new Table("table1"); Table t2 = new Table("table2"); Field f1 = new Field("field1", t1); Field f2 = new Field("field2", t2); SelectValue sv1 = new SelectValue(f1, null); SelectValue sv2 = new SelectValue(f2, null); q2.addSelect(sv1); q2.addFrom(t1); q2.addGroupBy(f1); q3.addSelect(sv2); q3.addFrom(t2); q2.addHaving(new SubQueryConstraint(f1, q3)); assertEquals(q2, q1); }
q2.addGroupBy(t2f1); q2.addHaving(cs2);