Skip to content

Commit 5e12719

Browse files
author
Pap Lőrinc
committed
Added PERCENT support for the TOP statement;
1 parent 53aab57 commit 5e12719

File tree

3 files changed

+45
-23
lines changed

3 files changed

+45
-23
lines changed

src/main/java/net/sf/jsqlparser/statement/select/Top.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class Top {
2929
private long rowCount;
3030
private boolean rowCountJdbcParameter = false;
3131
private boolean hasParenthesis = false;
32+
private boolean isPercentage = false;
3233

3334
public long getRowCount() {
3435
return rowCount;
@@ -55,6 +56,16 @@ public void setParenthesis(boolean hasParenthesis) {
5556
this.hasParenthesis = hasParenthesis;
5657
}
5758

59+
public boolean isPercentage()
60+
{
61+
return isPercentage;
62+
}
63+
64+
public void setPercentage(boolean percentage)
65+
{
66+
this.isPercentage = percentage;
67+
}
68+
5869
@Override
5970
public String toString() {
6071
String result = "TOP ";
@@ -64,12 +75,16 @@ public String toString() {
6475
}
6576

6677
result += rowCountJdbcParameter ? "?"
67-
: rowCount;
78+
: rowCount;
6879

6980
if (hasParenthesis) {
7081
result += ")";
7182
}
7283

84+
if (isPercentage) {
85+
result += " PERCENT";
86+
}
87+
7388
return result;
7489
}
7590
}

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,15 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
101101
| <K_IN:"IN">
102102
| <K_OR:"OR">
103103
| <K_ON:"ON">
104-
| <K_ALL: "ALL">
105-
| <K_AND: "AND">
106-
| <K_ANY: "ANY">
107-
| <K_KEY: "KEY">
104+
| <K_ALL:"ALL">
105+
| <K_AND:"AND">
106+
| <K_ANY:"ANY">
107+
| <K_KEY:"KEY">
108108
| <K_NOT:"NOT">
109109
| <K_SET:"SET">
110110
| <K_ASC:"ASC">
111111
| <K_TOP:"TOP">
112+
| <K_PERCENT:"PERCENT">
112113
| <K_END:"END">
113114
| <K_DESC:"DESC">
114115
| <K_INTO:"INTO">
@@ -1097,12 +1098,12 @@ Top Top():
10971098
|
10981099
"?" { top.setRowCountJdbcParameter(true);}
10991100
|
1100-
"("
1101-
( token=<S_LONG> { top.setRowCount(Long.parseLong(token.image)); }
1102-
| "?" { top.setRowCountJdbcParameter(true);} )
1101+
"("
1102+
( token=<S_LONG> { top.setRowCount(Long.parseLong(token.image)); }
1103+
| "?" { top.setRowCountJdbcParameter(true);} )
11031104
{ top.setParenthesis(true);}
11041105
")"
1105-
)
1106+
) [ <K_PERCENT> { top.setPercentage(true); } ] // TODO how would you select a column called 'percent' if it was after the TOP expression?
11061107
{
11071108
return top;
11081109
}

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -266,18 +266,24 @@ public void testTop() throws JSQLParserException {
266266
}
267267

268268
public void testTopWithParenthesis() throws JSQLParserException {
269-
final String statement = "SELECT TOP (5) PERCENT JobTitle, HireDate FROM HumanResources.Employee ORDER BY HireDate DESC";
269+
final String firstColumnName = "alias.columnName1";
270+
final String secondColumnName = "alias.columnName2";
271+
final String statement = "SELECT TOP (5) PERCENT " + firstColumnName + ", " + secondColumnName + " FROM schemaName.tableName alias ORDER BY " + secondColumnName + " DESC";
270272
final Select select = (Select) parserManager.parse(new StringReader(statement));
271273

272-
assertEquals(5, ((PlainSelect) select.getSelectBody()).getTop().getRowCount());
273-
assertStatementCanBeDeparsedAs(select, statement);
274-
}
275-
276-
public void testTopWithParenthesisJDBC() throws JSQLParserException {
277-
final String statement = "SELECT TOP (?) col1 FROM mytab";
278-
final Select select = (Select) parserManager.parse(new StringReader(statement));
274+
final PlainSelect selectBody = (PlainSelect) select.getSelectBody();
275+
276+
final Top top = selectBody.getTop();
277+
assertEquals(5, top.getRowCount());
278+
assertFalse(top.isRowCountJdbcParameter());
279+
assertTrue(top.hasParenthesis());
280+
assertTrue(top.isPercentage());
281+
282+
final List<SelectItem> selectItems = selectBody.getSelectItems();
283+
assertEquals(2, selectItems.size());
284+
assertEquals(firstColumnName, selectItems.get(0).toString());
285+
assertEquals(secondColumnName, selectItems.get(1).toString());
279286

280-
assertTrue(((PlainSelect) select.getSelectBody()).getTop().isRowCountJdbcParameter());
281287
assertStatementCanBeDeparsedAs(select, statement);
282288
}
283289

@@ -551,7 +557,7 @@ public void testOrderBy() throws JSQLParserException {
551557
((Column) plainSelect.getOrderByElements().get(0).getExpression())
552558
.getFullyQualifiedName());
553559
assertEquals("b",
554-
((Column) plainSelect.getOrderByElements().get(1).getExpression()).getColumnName());
560+
((Column) plainSelect.getOrderByElements().get(1).getExpression()).getColumnName());
555561
assertTrue(plainSelect.getOrderByElements().get(1).isAsc());
556562
assertFalse(plainSelect.getOrderByElements().get(0).isAsc());
557563
assertStatementCanBeDeparsedAs(select, statementToString);
@@ -561,7 +567,7 @@ public void testOrderBy() throws JSQLParserException {
561567
plainSelect = (PlainSelect) select.getSelectBody();
562568
assertEquals(2, plainSelect.getOrderByElements().size());
563569
assertEquals("a",
564-
((Column) plainSelect.getOrderByElements().get(0).getExpression()).getColumnName());
570+
((Column) plainSelect.getOrderByElements().get(0).getExpression()).getColumnName());
565571
assertEquals(2,
566572
((LongValue) plainSelect.getOrderByElements().get(1).getExpression()).getValue());
567573
assertStatementCanBeDeparsedAs(select, statement);
@@ -696,23 +702,23 @@ public void testDouble2() throws JSQLParserException {
696702
Select select = (Select) parserManager.parse(new StringReader(statement));
697703

698704
assertEquals(1e22, ((DoubleValue) ((SelectExpressionItem) ((PlainSelect) select.getSelectBody())
699-
.getSelectItems().get(0)).getExpression()).getValue(), 0);
705+
.getSelectItems().get(0)).getExpression()).getValue(), 0);
700706
}
701707

702708
public void testDouble3() throws JSQLParserException {
703709
String statement = "SELECT 1. FROM mytable";
704710
Select select = (Select) parserManager.parse(new StringReader(statement));
705711

706712
assertEquals(1.0, ((DoubleValue) ((SelectExpressionItem) ((PlainSelect) select.getSelectBody())
707-
.getSelectItems().get(0)).getExpression()).getValue(), 0);
713+
.getSelectItems().get(0)).getExpression()).getValue(), 0);
708714
}
709715

710716
public void testDouble4() throws JSQLParserException {
711717
String statement = "SELECT 1.2e22 FROM mytable";
712718
Select select = (Select) parserManager.parse(new StringReader(statement));
713719

714720
assertEquals(1.2e22, ((DoubleValue) ((SelectExpressionItem) ((PlainSelect) select.getSelectBody())
715-
.getSelectItems().get(0)).getExpression()).getValue(), 0);
721+
.getSelectItems().get(0)).getExpression()).getValue(), 0);
716722
}
717723

718724
public void testWith() throws JSQLParserException {

0 commit comments

Comments
 (0)