Search in sources :

Example 6 with OrderByElement

use of net.sf.jsqlparser.statement.select.OrderByElement in project JSqlParser by JSQLParser.

the class ExpressionDeParserTest method shouldDeParseAnalyticExpressionWithOrderByElements.

@Test
public void shouldDeParseAnalyticExpressionWithOrderByElements() {
    AnalyticExpression analyticExpression = new AnalyticExpression();
    List<OrderByElement> orderByElements = new ArrayList<OrderByElement>();
    OrderByElement orderByElement1 = mock(OrderByElement.class);
    OrderByElement orderByElement2 = mock(OrderByElement.class);
    analyticExpression.setName("name");
    analyticExpression.setOrderByElements(orderByElements);
    orderByElements.add(orderByElement1);
    orderByElements.add(orderByElement2);
    will(appendToBuffer("order by element 1")).given(orderByDeParser).deParseElement(orderByElement1);
    will(appendToBuffer("order by element 2")).given(orderByDeParser).deParseElement(orderByElement2);
    expressionDeParser.visit(analyticExpression);
    assertEquals("name() OVER (ORDER BY order by element 1, order by element 2)", buffer.toString());
}
Also used : ArrayList(java.util.ArrayList) AnalyticExpression(net.sf.jsqlparser.expression.AnalyticExpression) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) Test(org.junit.Test)

Example 7 with OrderByElement

use of net.sf.jsqlparser.statement.select.OrderByElement in project JSqlParser by JSQLParser.

the class ExpressionDeParserTest method shouldDeParseAnalyticExpressionWithWindowElement.

@Test
public void shouldDeParseAnalyticExpressionWithWindowElement() {
    AnalyticExpression analyticExpression = new AnalyticExpression();
    List<OrderByElement> orderByElements = new ArrayList<OrderByElement>();
    OrderByElement orderByElement1 = mock(OrderByElement.class);
    OrderByElement orderByElement2 = mock(OrderByElement.class);
    WindowElement windowElement = mock(WindowElement.class);
    analyticExpression.setName("name");
    analyticExpression.setOrderByElements(orderByElements);
    analyticExpression.setWindowElement(windowElement);
    orderByElements.add(orderByElement1);
    orderByElements.add(orderByElement2);
    will(appendToBuffer("order by element 1")).given(orderByDeParser).deParseElement(orderByElement1);
    will(appendToBuffer("order by element 2")).given(orderByDeParser).deParseElement(orderByElement2);
    given(windowElement.toString()).willReturn("window element");
    expressionDeParser.visit(analyticExpression);
    assertEquals("name() OVER (ORDER BY order by element 1, order by element 2 window element)", buffer.toString());
}
Also used : WindowElement(net.sf.jsqlparser.expression.WindowElement) ArrayList(java.util.ArrayList) AnalyticExpression(net.sf.jsqlparser.expression.AnalyticExpression) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) Test(org.junit.Test)

Example 8 with OrderByElement

use of net.sf.jsqlparser.statement.select.OrderByElement in project JSqlParser by JSQLParser.

the class StatementDeParserTest method shouldUseProvidedDeparsersWhenDeParsingDelete.

@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeparsersWhenDeParsingDelete() {
    Delete delete = new Delete();
    Table table = new Table();
    Expression where = mock(Expression.class);
    List<OrderByElement> orderByElements = new ArrayList<OrderByElement>();
    OrderByElement orderByElement1 = new OrderByElement();
    OrderByElement orderByElement2 = new OrderByElement();
    Expression orderByElement1Expression = mock(Expression.class);
    Expression orderByElement2Expression = mock(Expression.class);
    delete.setTable(table);
    delete.setWhere(where);
    delete.setOrderByElements(orderByElements);
    orderByElements.add(orderByElement1);
    orderByElements.add(orderByElement2);
    orderByElement1.setExpression(orderByElement1Expression);
    orderByElement2.setExpression(orderByElement2Expression);
    statementDeParser.visit(delete);
    then(where).should().accept(expressionDeParser);
    then(orderByElement1Expression).should().accept(expressionDeParser);
    then(orderByElement2Expression).should().accept(expressionDeParser);
}
Also used : Delete(net.sf.jsqlparser.statement.delete.Delete) Table(net.sf.jsqlparser.schema.Table) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) Test(org.junit.Test)

Example 9 with OrderByElement

use of net.sf.jsqlparser.statement.select.OrderByElement in project JSqlParser by JSQLParser.

the class SelectASTTest method testSelectASTColumnLF.

@Test
public void testSelectASTColumnLF() throws JSQLParserException {
    String sql = "SELECT  a,  b FROM  mytable \n order by   b,  c";
    StringBuilder b = new StringBuilder(sql);
    Statement stmt = CCJSqlParserUtil.parse(sql);
    Select select = (Select) stmt;
    PlainSelect ps = (PlainSelect) select.getSelectBody();
    for (SelectItem item : ps.getSelectItems()) {
        SelectExpressionItem sei = (SelectExpressionItem) item;
        Column c = (Column) sei.getExpression();
        SimpleNode astNode = c.getASTNode();
        assertNotNull(astNode);
        b.setCharAt(astNode.jjtGetFirstToken().absoluteBegin - 1, '*');
    }
    for (OrderByElement item : ps.getOrderByElements()) {
        Column c = (Column) item.getExpression();
        SimpleNode astNode = c.getASTNode();
        assertNotNull(astNode);
        b.setCharAt(astNode.jjtGetFirstToken().absoluteBegin - 1, '#');
    }
    assertEquals("SELECT  *,  * FROM  mytable \n order by   #,  #", b.toString());
}
Also used : Column(net.sf.jsqlparser.schema.Column) Statement(net.sf.jsqlparser.statement.Statement) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) SimpleNode(net.sf.jsqlparser.parser.SimpleNode) Test(org.junit.Test)

Example 10 with OrderByElement

use of net.sf.jsqlparser.statement.select.OrderByElement in project JSqlParser by JSQLParser.

the class SelectASTTest method testSelectASTCommentCRLF.

@Test
public void testSelectASTCommentCRLF() throws JSQLParserException {
    String sql = "SELECT  /* testcomment */ \r\n a,  b FROM  -- testcomment2 \r\n mytable \r\n order by   b,  c";
    StringBuilder b = new StringBuilder(sql);
    Statement stmt = CCJSqlParserUtil.parse(sql);
    Select select = (Select) stmt;
    PlainSelect ps = (PlainSelect) select.getSelectBody();
    for (SelectItem item : ps.getSelectItems()) {
        SelectExpressionItem sei = (SelectExpressionItem) item;
        Column c = (Column) sei.getExpression();
        SimpleNode astNode = c.getASTNode();
        assertNotNull(astNode);
        b.setCharAt(astNode.jjtGetFirstToken().absoluteBegin - 1, '*');
    }
    for (OrderByElement item : ps.getOrderByElements()) {
        Column c = (Column) item.getExpression();
        SimpleNode astNode = c.getASTNode();
        assertNotNull(astNode);
        b.setCharAt(astNode.jjtGetFirstToken().absoluteBegin - 1, '#');
    }
    assertEquals("SELECT  /* testcomment */ \r\n *,  * FROM  -- testcomment2 \r\n mytable \r\n order by   #,  #", b.toString());
}
Also used : Column(net.sf.jsqlparser.schema.Column) Statement(net.sf.jsqlparser.statement.Statement) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) SimpleNode(net.sf.jsqlparser.parser.SimpleNode) Test(org.junit.Test)

Aggregations

OrderByElement (net.sf.jsqlparser.statement.select.OrderByElement)15 Test (org.junit.Test)9 Select (net.sf.jsqlparser.statement.select.Select)7 ArrayList (java.util.ArrayList)6 Column (net.sf.jsqlparser.schema.Column)6 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)6 Expression (net.sf.jsqlparser.expression.Expression)5 SimpleNode (net.sf.jsqlparser.parser.SimpleNode)4 Statement (net.sf.jsqlparser.statement.Statement)4 SelectExpressionItem (net.sf.jsqlparser.statement.select.SelectExpressionItem)4 SelectItem (net.sf.jsqlparser.statement.select.SelectItem)4 AnalyticExpression (net.sf.jsqlparser.expression.AnalyticExpression)3 WindowElement (net.sf.jsqlparser.expression.WindowElement)2 Update (net.sf.jsqlparser.statement.update.Update)2 ParsedQuery (org.folio.rest.persist.facets.ParsedQuery)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)1 SortOp (herddb.model.planner.SortOp)1 AccessCurrentRowExpression (herddb.sql.expressions.AccessCurrentRowExpression)1 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)1