Search in sources :

Example 6 with AnalyticExpression

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

the class ExpressionDeParserTest method shouldDeParseSimplestAnalyticExpression.

@Test
public void shouldDeParseSimplestAnalyticExpression() {
    AnalyticExpression analyticExpression = new AnalyticExpression();
    analyticExpression.setName("name");
    expressionDeParser.visit(analyticExpression);
    assertEquals("name() OVER ()", buffer.toString());
}
Also used : AnalyticExpression(net.sf.jsqlparser.expression.AnalyticExpression) Test(org.junit.Test)

Example 7 with AnalyticExpression

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

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

the class ExpressionDeParserTest method shouldDeParseAnalyticExpressionWithDefaultValue.

@Test
public void shouldDeParseAnalyticExpressionWithDefaultValue() {
    AnalyticExpression analyticExpression = new AnalyticExpression();
    Expression expression = mock(Expression.class);
    Expression offset = mock(Expression.class);
    Expression defaultValue = mock(Expression.class);
    analyticExpression.setName("name");
    analyticExpression.setExpression(expression);
    analyticExpression.setOffset(offset);
    analyticExpression.setDefaultValue(defaultValue);
    will(appendToBuffer("expression")).given(expression).accept(expressionDeParser);
    will(appendToBuffer("offset")).given(offset).accept(expressionDeParser);
    will(appendToBuffer("default value")).given(defaultValue).accept(expressionDeParser);
    expressionDeParser.visit(analyticExpression);
    assertEquals("name(expression, offset, default value) OVER ()", buffer.toString());
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) KeepExpression(net.sf.jsqlparser.expression.KeepExpression) AnalyticExpression(net.sf.jsqlparser.expression.AnalyticExpression) AnalyticExpression(net.sf.jsqlparser.expression.AnalyticExpression) Test(org.junit.Test)

Example 9 with AnalyticExpression

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

use of net.sf.jsqlparser.expression.AnalyticExpression 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)10 Test (org.junit.Test)9 KeepExpression (net.sf.jsqlparser.expression.KeepExpression)6 Expression (net.sf.jsqlparser.expression.Expression)5 ArrayList (java.util.ArrayList)3 OrderByElement (net.sf.jsqlparser.statement.select.OrderByElement)3 WindowElement (net.sf.jsqlparser.expression.WindowElement)2 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)2 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 ExtractExpression (net.sf.jsqlparser.expression.ExtractExpression)1 IntervalExpression (net.sf.jsqlparser.expression.IntervalExpression)1 JsonExpression (net.sf.jsqlparser.expression.JsonExpression)1 NotExpression (net.sf.jsqlparser.expression.NotExpression)1 OracleHierarchicalExpression (net.sf.jsqlparser.expression.OracleHierarchicalExpression)1 OracleHint (net.sf.jsqlparser.expression.OracleHint)1