Skip to content

Deparsing query having DISTINCT and TOP #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ public SelectDeParser(ExpressionVisitor expressionVisitor, StringBuilder buffer)
@Override
public void visit(PlainSelect plainSelect) {
buffer.append("SELECT ");
Top top = plainSelect.getTop();
if (top != null) {
buffer.append(top).append(" ");
}
if (plainSelect.getDistinct() != null) {
buffer.append("DISTINCT ");
if (plainSelect.getDistinct().getOnSelectItems() != null) {
Expand All @@ -72,6 +68,10 @@ public void visit(PlainSelect plainSelect) {
}

}
Top top = plainSelect.getTop();
if (top != null) {
buffer.append(top).append(" ");
}

for (Iterator<SelectItem> iter = plainSelect.getSelectItems().iterator(); iter.hasNext();) {
SelectItem selectItem = iter.next();
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/net/sf/jsqlparser/test/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,19 @@ public void testDistinct() throws JSQLParserException {
assertStatementCanBeDeparsedAs(select, statement);
}

public void testDistinctTop() throws JSQLParserException {
String statement = "SELECT DISTINCT TOP 5 myid, mycol FROM mytable WHERE mytable.col = 9";
Select select = (Select) parserManager.parse(new StringReader(statement));
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
assertEquals("myid",
((Column) ((SelectExpressionItem) plainSelect.getSelectItems().get(0)).getExpression())
.getColumnName());
assertEquals("mycol",
((Column) ((SelectExpressionItem) plainSelect.getSelectItems().get(1)).getExpression()).getColumnName());
assertNotNull(plainSelect.getTop());
assertStatementCanBeDeparsedAs(select, statement);
}

public void testFrom() throws JSQLParserException {
String statement = "SELECT * FROM mytable as mytable0, mytable1 alias_tab1, mytable2 as alias_tab2, (SELECT * FROM mytable3) AS mytable4 WHERE mytable.col = 9";
String statementToString = "SELECT * FROM mytable AS mytable0, mytable1 alias_tab1, mytable2 AS alias_tab2, (SELECT * FROM mytable3) AS mytable4 WHERE mytable.col = 9";
Expand Down