Search in sources :

Example 1 with WindowElement

use of net.sf.jsqlparser.expression.WindowElement 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 2 with WindowElement

use of net.sf.jsqlparser.expression.WindowElement in project JSqlParser by JSQLParser.

the class ExpressionDeParser method visit.

@Override
public void visit(AnalyticExpression aexpr) {
    String name = aexpr.getName();
    Expression expression = aexpr.getExpression();
    Expression offset = aexpr.getOffset();
    Expression defaultValue = aexpr.getDefaultValue();
    boolean isAllColumns = aexpr.isAllColumns();
    KeepExpression keep = aexpr.getKeep();
    ExpressionList partitionExpressionList = aexpr.getPartitionExpressionList();
    List<OrderByElement> orderByElements = aexpr.getOrderByElements();
    WindowElement windowElement = aexpr.getWindowElement();
    buffer.append(name).append("(");
    if (expression != null) {
        expression.accept(this);
        if (offset != null) {
            buffer.append(", ");
            offset.accept(this);
            if (defaultValue != null) {
                buffer.append(", ");
                defaultValue.accept(this);
            }
        }
    } else if (isAllColumns) {
        buffer.append("*");
    }
    buffer.append(") ");
    if (keep != null) {
        keep.accept(this);
        buffer.append(" ");
    }
    switch(aexpr.getType()) {
        case WITHIN_GROUP:
            buffer.append("WITHIN GROUP");
            break;
        default:
            buffer.append("OVER");
    }
    buffer.append(" (");
    if (partitionExpressionList != null && !partitionExpressionList.getExpressions().isEmpty()) {
        buffer.append("PARTITION BY ");
        List<Expression> expressions = partitionExpressionList.getExpressions();
        for (int i = 0; i < expressions.size(); i++) {
            if (i > 0) {
                buffer.append(", ");
            }
            expressions.get(i).accept(this);
        }
        buffer.append(" ");
    }
    if (orderByElements != null && !orderByElements.isEmpty()) {
        buffer.append("ORDER BY ");
        orderByDeParser.setExpressionVisitor(this);
        orderByDeParser.setBuffer(buffer);
        for (int i = 0; i < orderByElements.size(); i++) {
            if (i > 0) {
                buffer.append(", ");
            }
            orderByDeParser.deParseElement(orderByElements.get(i));
        }
        if (windowElement != null) {
            buffer.append(' ');
            buffer.append(windowElement);
        }
    }
    buffer.append(")");
}
Also used : KeepExpression(net.sf.jsqlparser.expression.KeepExpression) WindowElement(net.sf.jsqlparser.expression.WindowElement) KeepExpression(net.sf.jsqlparser.expression.KeepExpression) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) ValueListExpression(net.sf.jsqlparser.expression.ValueListExpression) CastExpression(net.sf.jsqlparser.expression.CastExpression) OracleHierarchicalExpression(net.sf.jsqlparser.expression.OracleHierarchicalExpression) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) Expression(net.sf.jsqlparser.expression.Expression) IntervalExpression(net.sf.jsqlparser.expression.IntervalExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) JsonExpression(net.sf.jsqlparser.expression.JsonExpression) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) ExistsExpression(net.sf.jsqlparser.expression.operators.relational.ExistsExpression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) AnalyticExpression(net.sf.jsqlparser.expression.AnalyticExpression) AllComparisonExpression(net.sf.jsqlparser.expression.AllComparisonExpression) ExtractExpression(net.sf.jsqlparser.expression.ExtractExpression) AnyComparisonExpression(net.sf.jsqlparser.expression.AnyComparisonExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) OldOracleJoinBinaryExpression(net.sf.jsqlparser.expression.operators.relational.OldOracleJoinBinaryExpression) DateTimeLiteralExpression(net.sf.jsqlparser.expression.DateTimeLiteralExpression) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) OracleHint(net.sf.jsqlparser.expression.OracleHint)

Aggregations

AnalyticExpression (net.sf.jsqlparser.expression.AnalyticExpression)2 WindowElement (net.sf.jsqlparser.expression.WindowElement)2 OrderByElement (net.sf.jsqlparser.statement.select.OrderByElement)2 ArrayList (java.util.ArrayList)1 AllComparisonExpression (net.sf.jsqlparser.expression.AllComparisonExpression)1 AnyComparisonExpression (net.sf.jsqlparser.expression.AnyComparisonExpression)1 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)1 CaseExpression (net.sf.jsqlparser.expression.CaseExpression)1 CastExpression (net.sf.jsqlparser.expression.CastExpression)1 DateTimeLiteralExpression (net.sf.jsqlparser.expression.DateTimeLiteralExpression)1 Expression (net.sf.jsqlparser.expression.Expression)1 ExtractExpression (net.sf.jsqlparser.expression.ExtractExpression)1 IntervalExpression (net.sf.jsqlparser.expression.IntervalExpression)1 JsonExpression (net.sf.jsqlparser.expression.JsonExpression)1 KeepExpression (net.sf.jsqlparser.expression.KeepExpression)1 NotExpression (net.sf.jsqlparser.expression.NotExpression)1 OracleHierarchicalExpression (net.sf.jsqlparser.expression.OracleHierarchicalExpression)1 OracleHint (net.sf.jsqlparser.expression.OracleHint)1 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)1 TimeKeyExpression (net.sf.jsqlparser.expression.TimeKeyExpression)1