Skip to content

Commit 6e63214

Browse files
authored
fix fetch present in the end of union query (#1456)
1 parent 11430af commit 6e63214

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public void setFetch(Fetch fetch) {
9797
this.fetch = fetch;
9898
}
9999

100-
public Fetch getWithIsolation() {
101-
return fetch;
100+
public WithIsolation getWithIsolation() {
101+
return this.withIsolation;
102102
}
103103

104104
public void setWithIsolation(WithIsolation withIsolation) {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,16 +1938,24 @@ SelectBody SetOperationList() #SetOperationList: {
19381938
((PlainSelect)selects.get(0)).setUseBrackets(true);
19391939
return selects.get(0);
19401940
} else {
1941-
if (selects.size()>1 && selects.get(selects.size()-1) instanceof PlainSelect) {
1941+
if (selects.size()>1 && selects.get(selects.size()-1) instanceof PlainSelect && !brackets.get(brackets.size() - 1)) {
19421942
PlainSelect ps = (PlainSelect)selects.get(selects.size()-1);
1943-
if (ps.getOrderByElements() != null && !brackets.get(brackets.size() - 1)) {
1943+
if (ps.getOrderByElements() != null) {
19441944
list.setOrderByElements(ps.getOrderByElements());
19451945
list.setLimit(ps.getLimit());
19461946
list.setOffset(ps.getOffset());
19471947
ps.setOrderByElements(null);
19481948
ps.setLimit(null);
19491949
ps.setOffset(null);
19501950
}
1951+
if (ps.getFetch() != null) {
1952+
list.setFetch(ps.getFetch());
1953+
ps.setFetch(null);
1954+
}
1955+
if (ps.getWithIsolation() != null) {
1956+
list.setWithIsolation(ps.getWithIsolation());
1957+
ps.setWithIsolation(null);
1958+
}
19511959
}
19521960
list.setBracketsOpsAndSelects(brackets,selects,operations);
19531961
return list;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,25 @@ public void testUnion() throws JSQLParserException {
898898
+ "SELECT * FROM mytable3 WHERE mytable3.col = ? UNION "
899899
+ "SELECT * FROM mytable2 LIMIT 3, 4";
900900
assertStatementCanBeDeparsedAs(select, statementToString);
901+
902+
//with fetch and with ur
903+
String statement2 = "SELECT * FROM mytable WHERE mytable.col = 9 UNION "
904+
+ "SELECT * FROM mytable3 WHERE mytable3.col = ? UNION " + "SELECT * FROM mytable2 ORDER BY COL DESC FETCH FIRST 1 ROWS ONLY WITH UR";
905+
906+
Select select2 = (Select) parserManager.parse(new StringReader(statement2));
907+
SetOperationList setList2 = (SetOperationList) select2.getSelectBody();
908+
assertEquals(3, setList2.getSelects().size());
909+
assertEquals("mytable", ((Table) ((PlainSelect) setList2.getSelects().get(0)).getFromItem()).
910+
getName());
911+
assertEquals("mytable3", ((Table) ((PlainSelect) setList2.getSelects().get(1)).getFromItem()).
912+
getName());
913+
assertEquals("mytable2", ((Table) ((PlainSelect) setList2.getSelects().get(2)).getFromItem()).
914+
getName());
915+
assertEquals(1, ((SetOperationList) setList2).getFetch().getRowCount());
916+
917+
assertEquals("UR", ((SetOperationList) setList2).getWithIsolation().getIsolation());
918+
919+
assertStatementCanBeDeparsedAs(select2, statement2);
901920
}
902921

903922
@Test

0 commit comments

Comments
 (0)