use of net.sf.jsqlparser.expression.Expression in project JSqlParser by JSQLParser.
the class StatementDeParserTest method shouldUseProvidedDeparsersWhenDeParsingInsert.
@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeparsersWhenDeParsingInsert() throws JSQLParserException {
Insert insert = new Insert();
Table table = new Table();
List<Column> duplicateUpdateColumns = new ArrayList<Column>();
List<Expression> duplicateUpdateExpressionList = new ArrayList<Expression>();
Column duplicateUpdateColumn1 = new Column();
Column duplicateUpdateColumn2 = new Column();
Expression duplicateUpdateExpression1 = mock(Expression.class);
Expression duplicateUpdateExpression2 = mock(Expression.class);
Select select = new Select();
List<WithItem> withItemsList = new ArrayList<WithItem>();
WithItem withItem1 = spy(new WithItem());
WithItem withItem2 = spy(new WithItem());
SelectBody withItem1SelectBody = mock(SelectBody.class);
SelectBody withItem2SelectBody = mock(SelectBody.class);
SelectBody selectBody = mock(SelectBody.class);
insert.setSelect(select);
insert.setTable(table);
insert.setUseDuplicate(true);
insert.setDuplicateUpdateColumns(duplicateUpdateColumns);
insert.setDuplicateUpdateExpressionList(duplicateUpdateExpressionList);
duplicateUpdateColumns.add(duplicateUpdateColumn1);
duplicateUpdateColumns.add(duplicateUpdateColumn2);
duplicateUpdateExpressionList.add(duplicateUpdateExpression1);
duplicateUpdateExpressionList.add(duplicateUpdateExpression2);
insert.setDuplicateUpdateExpressionList(duplicateUpdateExpressionList);
select.setWithItemsList(withItemsList);
select.setSelectBody(selectBody);
withItemsList.add(withItem1);
withItemsList.add(withItem2);
withItem1.setSelectBody(withItem1SelectBody);
withItem2.setSelectBody(withItem2SelectBody);
statementDeParser.visit(insert);
then(withItem1).should().accept(selectDeParser);
then(withItem2).should().accept(selectDeParser);
then(selectBody).should().accept(selectDeParser);
then(duplicateUpdateExpression1).should().accept(expressionDeParser);
then(duplicateUpdateExpression1).should().accept(expressionDeParser);
}
use of net.sf.jsqlparser.expression.Expression 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(")");
}
use of net.sf.jsqlparser.expression.Expression in project JSqlParser by JSQLParser.
the class ExpressionDeParser method visit.
@Override
public void visit(ExpressionList expressionList) {
if (useBracketsInExprList) {
buffer.append("(");
}
for (Iterator<Expression> iter = expressionList.getExpressions().iterator(); iter.hasNext(); ) {
Expression expression = iter.next();
expression.accept(this);
if (iter.hasNext()) {
buffer.append(", ");
}
}
if (useBracketsInExprList) {
buffer.append(")");
}
}
use of net.sf.jsqlparser.expression.Expression in project JSqlParser by JSQLParser.
the class InsertDeParser method visit.
@Override
public void visit(MultiExpressionList multiExprList) {
buffer.append(" VALUES ");
for (Iterator<ExpressionList> it = multiExprList.getExprList().iterator(); it.hasNext(); ) {
buffer.append("(");
for (Iterator<Expression> iter = it.next().getExpressions().iterator(); iter.hasNext(); ) {
Expression expression = iter.next();
expression.accept(expressionVisitor);
if (iter.hasNext()) {
buffer.append(", ");
}
}
buffer.append(")");
if (it.hasNext()) {
buffer.append(", ");
}
}
}
use of net.sf.jsqlparser.expression.Expression in project JSqlParser by JSQLParser.
the class ReplaceDeParser method deParse.
public void deParse(Replace replace) {
buffer.append("REPLACE ");
if (replace.isUseIntoTables()) {
buffer.append("INTO ");
}
buffer.append(replace.getTable().getFullyQualifiedName());
if (replace.getItemsList() != null) {
if (replace.getColumns() != null) {
buffer.append(" (");
for (int i = 0; i < replace.getColumns().size(); i++) {
Column column = replace.getColumns().get(i);
buffer.append(column.getFullyQualifiedName());
if (i < replace.getColumns().size() - 1) {
buffer.append(", ");
}
}
buffer.append(") ");
} else {
buffer.append(" ");
}
} else {
buffer.append(" SET ");
for (int i = 0; i < replace.getColumns().size(); i++) {
Column column = replace.getColumns().get(i);
buffer.append(column.getFullyQualifiedName()).append("=");
Expression expression = replace.getExpressions().get(i);
expression.accept(expressionVisitor);
if (i < replace.getColumns().size() - 1) {
buffer.append(", ");
}
}
}
if (replace.getItemsList() != null) {
// REPLACE mytab SELECT * FROM mytab2
// or VALUES ('as', ?, 565)
replace.getItemsList().accept(this);
}
}
Aggregations