Skip to content

Commit 780ba12

Browse files
committed
fixes #243
1 parent 10e68b5 commit 780ba12

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ Also I would like to know about needed examples or documentation stuff.
4242

4343
## Extensions in the latest SNAPSHOT version 0.9.6
4444

45+
* improved **top** expression
46+
47+
```java
48+
SELECT TOP (? + 1) * FROM MyTable
49+
```
50+
4551
* allowed negative interval expressions like **INTERVAL -420 MINUTES**.
4652
* support for **ALTER VIEW** statements
4753
* improved merge statement

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,23 @@
2121
*/
2222
package net.sf.jsqlparser.statement.select;
2323

24+
import net.sf.jsqlparser.expression.Expression;
25+
2426
/**
2527
* A top clause in the form [TOP (row_count) or TOP row_count]
2628
*/
2729
public class Top {
2830

29-
private long rowCount;
30-
private boolean rowCountJdbcParameter = false;
3131
private boolean hasParenthesis = false;
3232
private boolean isPercentage = false;
33+
private Expression expression;
3334

34-
public long getRowCount() {
35-
return rowCount;
36-
}
37-
38-
// TODO instead of a plain number, an expression should be added, which could be a NumberExpression, a GroupedExpression or a JdbcParameter
39-
public void setRowCount(long rowCount) {
40-
this.rowCount = rowCount;
41-
}
42-
43-
public boolean isRowCountJdbcParameter() {
44-
return rowCountJdbcParameter;
35+
public Expression getExpression() {
36+
return expression;
4537
}
4638

47-
public void setRowCountJdbcParameter(boolean rowCountJdbcParameter) {
48-
this.rowCountJdbcParameter = rowCountJdbcParameter;
39+
public void setExpression(Expression expression) {
40+
this.expression = expression;
4941
}
5042

5143
public boolean hasParenthesis() {
@@ -72,8 +64,7 @@ public String toString() {
7264
result += "(";
7365
}
7466

75-
result += rowCountJdbcParameter ? "?"
76-
: rowCount;
67+
result += expression.toString();
7768

7869
if (hasParenthesis) {
7970
result += ")";

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,17 +1439,17 @@ Top Top():
14391439
{
14401440
Top top = new Top();
14411441
Token token = null;
1442+
Expression expr = null;
14421443
}
14431444
{
14441445
<K_TOP>
14451446
(
1446-
token=<S_LONG> { top.setRowCount(Long.parseLong(token.image)); }
1447+
token=<S_LONG> { top.setExpression(new LongValue(token.image)); }
14471448
|
1448-
"?" { top.setRowCountJdbcParameter(true);}
1449+
"?" { top.setExpression(new JdbcParameter()); }
14491450
|
14501451
"("
1451-
( token=<S_LONG> { top.setRowCount(Long.parseLong(token.image)); }
1452-
| "?" { top.setRowCountJdbcParameter(true);} )
1452+
expr=AdditiveExpression() { top.setExpression(expr); }
14531453
{ top.setParenthesis(true);}
14541454
")"
14551455
) [ LOOKAHEAD(2) <K_PERCENT> { top.setPercentage(true); } ]

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,12 @@ public void testTop() throws JSQLParserException {
398398

399399
Select select = (Select) parserManager.parse(new StringReader(statement));
400400

401-
assertEquals(3, ((PlainSelect) select.getSelectBody()).getTop().getRowCount());
401+
assertEquals(3, ((LongValue)((PlainSelect) select.getSelectBody()).getTop().getExpression()).getValue());
402402
assertStatementCanBeDeparsedAs(select, statement);
403403

404404
statement = "select top 5 foo from bar";
405405
select = (Select) parserManager.parse(new StringReader(statement));
406-
assertEquals(5, ((PlainSelect) select.getSelectBody()).getTop().getRowCount());
406+
assertEquals(5, ((LongValue)((PlainSelect) select.getSelectBody()).getTop().getExpression()).getValue());
407407
}
408408

409409
public void testTopWithParenthesis() throws JSQLParserException {
@@ -415,8 +415,7 @@ public void testTopWithParenthesis() throws JSQLParserException {
415415
final PlainSelect selectBody = (PlainSelect) select.getSelectBody();
416416

417417
final Top top = selectBody.getTop();
418-
assertEquals(5, top.getRowCount());
419-
assertFalse(top.isRowCountJdbcParameter());
418+
assertEquals("5", top.getExpression().toString());
420419
assertTrue(top.hasParenthesis());
421420
assertTrue(top.isPercentage());
422421

@@ -2188,4 +2187,12 @@ public void testEscapedBackslashIssue253() throws JSQLParserException {
21882187
public void testKeywordTableIssue261() throws JSQLParserException {
21892188
assertSqlCanBeParsedAndDeparsed("SELECT column_value FROM table(VARCHAR_LIST_TYPE())");
21902189
}
2190+
2191+
public void testTopExpressionIssue243() throws JSQLParserException {
2192+
assertSqlCanBeParsedAndDeparsed("SELECT TOP (? + 1) * FROM MyTable");
2193+
}
2194+
2195+
public void testTopExpressionIssue243_2() throws JSQLParserException {
2196+
assertSqlCanBeParsedAndDeparsed("SELECT TOP (CAST(? AS INT)) * FROM MyTable");
2197+
}
21912198
}

0 commit comments

Comments
 (0)