Skip to content

Commit 4f76615

Browse files
committed
added testcase, little refactoring
1 parent b5849b6 commit 4f76615

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,50 +26,47 @@
2626
*/
2727
public class Top {
2828

29-
private long rowCount;
30-
private boolean rowCountJdbcParameter = false;
31-
private boolean hasParenthesis = false;
29+
private long rowCount;
30+
private boolean rowCountJdbcParameter = false;
31+
private boolean hasParenthesis = false;
3232

33-
public long getRowCount() {
34-
return rowCount;
35-
}
33+
public long getRowCount() {
34+
return rowCount;
35+
}
3636

3737
// TODO instead of a plain number, an expression should be added, which could be a NumberExpression, a GroupedExpression or a JdbcParameter
3838
public void setRowCount(long rowCount) {
39-
this.rowCount = rowCount;
40-
}
39+
this.rowCount = rowCount;
40+
}
4141

42-
public boolean isRowCountJdbcParameter() {
43-
return rowCountJdbcParameter;
44-
}
42+
public boolean isRowCountJdbcParameter() {
43+
return rowCountJdbcParameter;
44+
}
4545

46-
public void setRowCountJdbcParameter(boolean rowCountJdbcParameter) {
47-
this.rowCountJdbcParameter = rowCountJdbcParameter;
48-
}
46+
public void setRowCountJdbcParameter(boolean rowCountJdbcParameter) {
47+
this.rowCountJdbcParameter = rowCountJdbcParameter;
48+
}
4949

50-
public boolean hasParenthesis()
51-
{
50+
public boolean hasParenthesis() {
5251
return hasParenthesis;
5352
}
5453

55-
public void setParenthesis(boolean hasParenthesis)
56-
{
54+
public void setParenthesis(boolean hasParenthesis) {
5755
this.hasParenthesis = hasParenthesis;
5856
}
5957

6058
@Override
6159
public String toString() {
6260
String result = "TOP ";
6361

64-
if ( hasParenthesis) {
62+
if (hasParenthesis) {
6563
result += "(";
6664
}
6765

6866
result += rowCountJdbcParameter ? "?"
69-
: rowCount;
67+
: rowCount;
7068

71-
if (hasParenthesis)
72-
{
69+
if (hasParenthesis) {
7370
result += ")";
7471
}
7572

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,9 +1097,11 @@ Top Top():
10971097
|
10981098
"?" { top.setRowCountJdbcParameter(true);}
10991099
|
1100-
LOOKAHEAD(2) "(" token=<S_LONG> ")" { top.setRowCount(Long.parseLong(token.image)); top.setParenthesis(true);}
1101-
|
1102-
LOOKAHEAD(2) "(" "?" ")" { top.setRowCountJdbcParameter(true); top.setParenthesis(true);}
1100+
"("
1101+
( token=<S_LONG> { top.setRowCount(Long.parseLong(token.image)); }
1102+
| "?" { top.setRowCountJdbcParameter(true);} )
1103+
{ top.setParenthesis(true);}
1104+
")"
11031105
)
11041106
{
11051107
return top;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,14 @@ public void testTopWithParenthesis() throws JSQLParserException {
272272
assertEquals(5, ((PlainSelect) select.getSelectBody()).getTop().getRowCount());
273273
assertStatementCanBeDeparsedAs(select, statement);
274274
}
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));
279+
280+
assertTrue(((PlainSelect) select.getSelectBody()).getTop().isRowCountJdbcParameter());
281+
assertStatementCanBeDeparsedAs(select, statement);
282+
}
275283

276284
public void testSelectItems() throws JSQLParserException {
277285
String statement = "SELECT myid AS MYID, mycol, tab.*, schema.tab.*, mytab.mycol2, myschema.mytab.mycol, myschema.mytab.* FROM mytable WHERE mytable.col = 9";

0 commit comments

Comments
 (0)