Search in sources :

Example 1 with OrderByElement

use of net.sf.jsqlparser.statement.select.OrderByElement in project herddb by diennea.

the class JSQLParserPlanner method planSort.

private PlannerOp planSort(PlannerOp input, OpSchema columns, List<OrderByElement> fieldCollations) {
    boolean[] directions = new boolean[fieldCollations.size()];
    boolean[] nullLastdirections = new boolean[fieldCollations.size()];
    int[] fields = new int[fieldCollations.size()];
    int i = 0;
    for (OrderByElement col : fieldCollations) {
        OrderByElement.NullOrdering nullDirection = col.getNullOrdering();
        CompiledSQLExpression columnPos = SQLParserExpressionCompiler.compileExpression(col.getExpression(), columns);
        checkSupported(columnPos instanceof AccessCurrentRowExpression);
        AccessCurrentRowExpression pos = (AccessCurrentRowExpression) columnPos;
        int index = pos.getIndex();
        directions[i] = col.isAsc();
        // default is NULL LAST
        nullLastdirections[i] = nullDirection == OrderByElement.NullOrdering.NULLS_LAST || nullDirection == null;
        fields[i++] = index;
    }
    return new SortOp(input, directions, fields, nullLastdirections);
}
Also used : AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) SortOp(herddb.model.planner.SortOp)

Example 2 with OrderByElement

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

the class StatementDeParserTest method shouldUseProvidedDeParsersWhenDeParsingUpdateUsingSelect.

@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeParsersWhenDeParsingUpdateUsingSelect() {
    Update update = new Update();
    List<Column> columns = new ArrayList<Column>();
    Select select = new Select();
    Expression where = mock(Expression.class);
    List<OrderByElement> orderByElements = new ArrayList<OrderByElement>();
    Column column1 = new Column();
    Column column2 = new Column();
    SelectBody selectBody = mock(SelectBody.class);
    OrderByElement orderByElement1 = new OrderByElement();
    OrderByElement orderByElement2 = new OrderByElement();
    Expression orderByElement1Expression = mock(Expression.class);
    Expression orderByElement2Expression = mock(Expression.class);
    update.setUseSelect(true);
    update.setColumns(columns);
    update.setSelect(select);
    update.setWhere(where);
    update.setOrderByElements(orderByElements);
    columns.add(column1);
    columns.add(column2);
    select.setSelectBody(selectBody);
    orderByElements.add(orderByElement1);
    orderByElements.add(orderByElement2);
    orderByElement1.setExpression(orderByElement1Expression);
    orderByElement2.setExpression(orderByElement2Expression);
    statementDeParser.visit(update);
    then(expressionDeParser).should().visit(column1);
    then(expressionDeParser).should().visit(column2);
    then(selectBody).should().accept(selectDeParser);
    then(where).should().accept(expressionDeParser);
    then(orderByElement1Expression).should().accept(expressionDeParser);
    then(orderByElement2Expression).should().accept(expressionDeParser);
}
Also used : Column(net.sf.jsqlparser.schema.Column) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) Select(net.sf.jsqlparser.statement.select.Select) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) SelectBody(net.sf.jsqlparser.statement.select.SelectBody) Update(net.sf.jsqlparser.statement.update.Update) Test(org.junit.Test)

Example 3 with OrderByElement

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

the class StatementDeParserTest method shouldUseProvidedDeParsersWhenDeParsingUpdateNotUsingSelect.

@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeParsersWhenDeParsingUpdateNotUsingSelect() {
    Update update = new Update();
    List<Column> columns = new ArrayList<Column>();
    List<Expression> expressions = new ArrayList<Expression>();
    Expression where = mock(Expression.class);
    List<OrderByElement> orderByElements = new ArrayList<OrderByElement>();
    Column column1 = new Column();
    Column column2 = new Column();
    Expression expression1 = mock(Expression.class);
    Expression expression2 = mock(Expression.class);
    OrderByElement orderByElement1 = new OrderByElement();
    OrderByElement orderByElement2 = new OrderByElement();
    Expression orderByElement1Expression = mock(Expression.class);
    Expression orderByElement2Expression = mock(Expression.class);
    update.setColumns(columns);
    update.setExpressions(expressions);
    update.setWhere(where);
    update.setOrderByElements(orderByElements);
    columns.add(column1);
    columns.add(column2);
    expressions.add(expression1);
    expressions.add(expression2);
    orderByElements.add(orderByElement1);
    orderByElements.add(orderByElement2);
    orderByElement1.setExpression(orderByElement1Expression);
    orderByElement2.setExpression(orderByElement2Expression);
    statementDeParser.visit(update);
    then(expressionDeParser).should().visit(column1);
    then(expressionDeParser).should().visit(column2);
    then(expression1).should().accept(expressionDeParser);
    then(expression2).should().accept(expressionDeParser);
    then(where).should().accept(expressionDeParser);
    then(orderByElement1Expression).should().accept(expressionDeParser);
    then(orderByElement2Expression).should().accept(expressionDeParser);
}
Also used : Column(net.sf.jsqlparser.schema.Column) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) Update(net.sf.jsqlparser.statement.update.Update) Test(org.junit.Test)

Example 4 with OrderByElement

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

the class SelectASTTest method testSelectASTColumn.

@Test
public void testSelectASTColumn() throws JSQLParserException {
    String sql = "SELECT  a,  b FROM  mytable  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().beginColumn - 1, '*');
    }
    for (OrderByElement item : ps.getOrderByElements()) {
        Column c = (Column) item.getExpression();
        SimpleNode astNode = c.getASTNode();
        assertNotNull(astNode);
        b.setCharAt(astNode.jjtGetFirstToken().beginColumn - 1, '#');
    }
    assertEquals("SELECT  *,  * FROM  mytable  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 5 with OrderByElement

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

the class ExpressionVisitorAdapter method visit.

@Override
public void visit(AnalyticExpression expr) {
    if (expr.getExpression() != null) {
        expr.getExpression().accept(this);
    }
    if (expr.getDefaultValue() != null) {
        expr.getDefaultValue().accept(this);
    }
    if (expr.getOffset() != null) {
        expr.getOffset().accept(this);
    }
    if (expr.getKeep() != null) {
        expr.getKeep().accept(this);
    }
    for (OrderByElement element : expr.getOrderByElements()) {
        element.getExpression().accept(this);
    }
    if (expr.getWindowElement() != null) {
        expr.getWindowElement().getRange().getStart().getExpression().accept(this);
        expr.getWindowElement().getRange().getEnd().getExpression().accept(this);
        expr.getWindowElement().getOffset().getExpression().accept(this);
    }
}
Also used : OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement)

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