public void testExists() throws JSQLParserException { String statement = "SELECT * FROM tab1 WHERE"; String where = " EXISTS (SELECT * FROM tab2)"; statement += where; Statement parsed = parserManager.parse(new StringReader(statement)); assertEquals(statement, "" + parsed); PlainSelect plainSelect = (PlainSelect) ((Select) parsed).getSelectBody(); ExpressionDeParser expressionDeParser = new ExpressionDeParser(); StringBuffer stringBuffer = new StringBuffer(); expressionDeParser.setBuffer(stringBuffer); SelectDeParser deParser = new SelectDeParser(expressionDeParser, stringBuffer); expressionDeParser.setSelectVisitor(deParser); plainSelect.getWhere().accept(expressionDeParser); assertEquals(where, stringBuffer.toString()); }
public void testOrderBy() throws JSQLParserException { // TODO: should there be a DESC marker in the OrderByElement class? String statement = "SELECT * FROM tab1 WHERE a > 34 GROUP BY tab1.b ORDER BY tab1.a DESC, tab1.b ASC"; String statementToString = "SELECT * FROM tab1 WHERE a > 34 GROUP BY tab1.b ORDER BY tab1.a DESC, tab1.b"; PlainSelect plainSelect = (PlainSelect) ((Select) parserManager.parse(new StringReader(statement))).getSelectBody(); assertEquals(2, plainSelect.getOrderByElements().size()); assertEquals( "tab1.a", ((Column) ((OrderByElement) plainSelect.getOrderByElements().get(0)).getColumnReference()) .getWholeColumnName()); assertEquals( "b", ((Column) ((OrderByElement) plainSelect.getOrderByElements().get(1)).getColumnReference()) .getColumnName()); assertTrue(((OrderByElement) plainSelect.getOrderByElements().get(1)).isAsc()); assertFalse(((OrderByElement) plainSelect.getOrderByElements().get(0)).isAsc()); assertEquals(statementToString, "" + plainSelect); ExpressionDeParser expressionDeParser = new ExpressionDeParser(); StringBuffer stringBuffer = new StringBuffer(); SelectDeParser deParser = new SelectDeParser(expressionDeParser, stringBuffer); expressionDeParser.setSelectVisitor(deParser); expressionDeParser.setBuffer(stringBuffer); plainSelect.accept(deParser); assertEquals(statement, stringBuffer.toString()); statement = "SELECT * FROM tab1 WHERE a > 34 GROUP BY tab1.b ORDER BY tab1.a, 2"; plainSelect = (PlainSelect) ((Select) parserManager.parse(new StringReader(statement))).getSelectBody(); assertEquals(2, plainSelect.getOrderByElements().size()); assertEquals( "a", ((Column) ((OrderByElement) plainSelect.getOrderByElements().get(0)).getColumnReference()) .getColumnName()); assertEquals( 2, ((ColumnIndex) ((OrderByElement) plainSelect.getOrderByElements().get(1)).getColumnReference()) .getIndex()); assertEquals(statement, "" + plainSelect); }