Skip to content

Commit

Permalink
Modified the TOP expression to accept parentheses also;
Browse files Browse the repository at this point in the history
  • Loading branch information
Pap Lőrinc committed Feb 6, 2014
1 parent e143abb commit b5849b6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
44 changes: 35 additions & 9 deletions src/main/java/net/sf/jsqlparser/statement/select/Top.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,57 @@
package net.sf.jsqlparser.statement.select;

/**
* A top clause in the form [TOP row_count]
* A top clause in the form [TOP (row_count) or TOP row_count]
*/
public class Top {

private long rowCount;
private boolean rowCountJdbcParameter = false;
private boolean hasParenthesis = false;

public long getRowCount() {
return rowCount;
}

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

public boolean isRowCountJdbcParameter() {
return rowCountJdbcParameter;
}

public void setRowCountJdbcParameter(boolean b) {
rowCountJdbcParameter = b;
public void setRowCountJdbcParameter(boolean rowCountJdbcParameter) {
this.rowCountJdbcParameter = rowCountJdbcParameter;
}

@Override
public String toString() {
return "TOP " + (rowCountJdbcParameter ? "?" : rowCount + "");
}
public boolean hasParenthesis()
{
return hasParenthesis;
}

public void setParenthesis(boolean hasParenthesis)
{
this.hasParenthesis = hasParenthesis;
}

@Override
public String toString() {
String result = "TOP ";

if ( hasParenthesis) {
result += "(";
}

result += rowCountJdbcParameter ? "?"
: rowCount;

if (hasParenthesis)
{
result += ")";
}

return result;
}
}
18 changes: 12 additions & 6 deletions src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj
Original file line number Diff line number Diff line change
Expand Up @@ -1083,18 +1083,24 @@ Limit Limit():
}
}

// according to http://technet.microsoft.com/en-us/library/ms189463.aspx
Top Top():
{
Top top = new Top();
Token token = null;
}
{
// TODO the inside should be extracted, ? should be added as a new JdbcParameter and the parenthesis should wrap them with a GroupedExpression
<K_TOP>
(
token=<S_LONG> { top.setRowCount(Long.parseLong(token.image)); }
|
"?" { top.setRowCountJdbcParameter(true);}
)
(
token=<S_LONG> { top.setRowCount(Long.parseLong(token.image)); }
|
"?" { top.setRowCountJdbcParameter(true);}
|
LOOKAHEAD(2) "(" token=<S_LONG> ")" { top.setRowCount(Long.parseLong(token.image)); top.setParenthesis(true);}
|
LOOKAHEAD(2) "(" "?" ")" { top.setRowCountJdbcParameter(true); top.setParenthesis(true);}
)
{
return top;
}
Expand All @@ -1116,7 +1122,7 @@ Expression Expression():
{ return retval; }
}

Expression OrExpression() :
Expression OrExpression():
{
Expression left, right, result;
}
Expand Down
11 changes: 8 additions & 3 deletions src/test/java/net/sf/jsqlparser/test/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import static net.sf.jsqlparser.test.TestUtils.*;

Expand Down Expand Up @@ -265,9 +263,16 @@ public void testTop() throws JSQLParserException {
statement = "select top 5 foo from bar";
select = (Select) parserManager.parse(new StringReader(statement));
assertEquals(5, ((PlainSelect) select.getSelectBody()).getTop().getRowCount());

}

public void testTopWithParenthesis() throws JSQLParserException {
final String statement = "SELECT TOP (5) PERCENT JobTitle, HireDate FROM HumanResources.Employee ORDER BY HireDate DESC";
final Select select = (Select) parserManager.parse(new StringReader(statement));

assertEquals(5, ((PlainSelect) select.getSelectBody()).getTop().getRowCount());
assertStatementCanBeDeparsedAs(select, statement);
}

public void testSelectItems() throws JSQLParserException {
String statement = "SELECT myid AS MYID, mycol, tab.*, schema.tab.*, mytab.mycol2, myschema.mytab.mycol, myschema.mytab.* FROM mytable WHERE mytable.col = 9";
Select select = (Select) parserManager.parse(new StringReader(statement));
Expand Down

0 comments on commit b5849b6

Please sign in to comment.