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());
}
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(")");
}
Aggregations