Exemplo n.º 1
0
  /**
   * Author the generated AST.
   *
   * <pre>
   *              OR
   *    p1>0                  AND
   *              p2<=10.2            =
   *                          p1-p2        5
   * </pre>
   *
   * TODO: add tests for DateLiteral, other operators, paranthesys etc
   */
  public void testAST() {
    SQLQuery query =
        SQLQueryParser.parse("SELECT p1, p2 FROM table WHERE p1 > 0 OR p2 <= 10.2 AND p1 - p2 = 5");

    SelectList select = query.getSelectClause().elements;
    assertEquals(2, select.size());

    Expression where = query.getWhereClause().predicate;
    assertEquals(Operator.OR, where.operator);

    Expression expr = (Expression) where.lvalue;
    assertEquals(Operator.GT, expr.operator);
    assertEquals("p1", ((Reference) expr.lvalue).name);
    assertEquals(0, ((IntegerLiteral) expr.rvalue).value);

    // root expr
    expr = (Expression) where.rvalue;
    assertEquals(Operator.AND, expr.operator);

    Expression expr1 = (Expression) expr.lvalue;
    assertEquals(Operator.LTEQ, expr1.operator);
    assertEquals("p2", ((Reference) expr1.lvalue).name);
    assertEquals(10.2, ((DoubleLiteral) expr1.rvalue).value);

    Expression expr2 = (Expression) expr.rvalue;
    assertEquals(Operator.EQ, expr2.operator);
    assertEquals(5, ((IntegerLiteral) expr2.rvalue).value);

    Expression expr3 = (Expression) expr2.lvalue;
    assertEquals(Operator.SUB, expr3.operator);
    assertEquals("p1", ((Reference) expr3.lvalue).name);
    assertEquals("p2", ((Reference) expr3.rvalue).name);
  }