Skip to content

Commit 7819a2f

Browse files
committed
Merge origin/master
2 parents fd25ab4 + 96798f2 commit 7819a2f

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public SelectDeParser(ExpressionVisitor expressionVisitor, StringBuilder buffer)
5353
@Override
5454
public void visit(PlainSelect plainSelect) {
5555
buffer.append("SELECT ");
56-
Top top = plainSelect.getTop();
57-
if (top != null) {
58-
buffer.append(top).append(" ");
59-
}
6056
if (plainSelect.getDistinct() != null) {
6157
buffer.append("DISTINCT ");
6258
if (plainSelect.getDistinct().getOnSelectItems() != null) {
@@ -72,6 +68,10 @@ public void visit(PlainSelect plainSelect) {
7268
}
7369

7470
}
71+
Top top = plainSelect.getTop();
72+
if (top != null) {
73+
buffer.append(top).append(" ");
74+
}
7575

7676
for (Iterator<SelectItem> iter = plainSelect.getSelectItems().iterator(); iter.hasNext();) {
7777
SelectItem selectItem = iter.next();

src/test/java/net/sf/jsqlparser/test/select/SelectTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,19 @@ public void testDistinct() throws JSQLParserException {
408408
assertStatementCanBeDeparsedAs(select, statement);
409409
}
410410

411+
public void testDistinctTop() throws JSQLParserException {
412+
String statement = "SELECT DISTINCT TOP 5 myid, mycol FROM mytable WHERE mytable.col = 9";
413+
Select select = (Select) parserManager.parse(new StringReader(statement));
414+
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
415+
assertEquals("myid",
416+
((Column) ((SelectExpressionItem) plainSelect.getSelectItems().get(0)).getExpression())
417+
.getColumnName());
418+
assertEquals("mycol",
419+
((Column) ((SelectExpressionItem) plainSelect.getSelectItems().get(1)).getExpression()).getColumnName());
420+
assertNotNull(plainSelect.getTop());
421+
assertStatementCanBeDeparsedAs(select, statement);
422+
}
423+
411424
public void testFrom() throws JSQLParserException {
412425
String statement = "SELECT * FROM mytable as mytable0, mytable1 alias_tab1, mytable2 as alias_tab2, (SELECT * FROM mytable3) AS mytable4 WHERE mytable.col = 9";
413426
String statementToString = "SELECT * FROM mytable AS mytable0, mytable1 alias_tab1, mytable2 AS alias_tab2, (SELECT * FROM mytable3) AS mytable4 WHERE mytable.col = 9";

0 commit comments

Comments
 (0)