Skip to content

Commit 594705a

Browse files
committed
fixes #134 - preserve order of query
1 parent eab6098 commit 594705a

File tree

3 files changed

+66
-44
lines changed

3 files changed

+66
-44
lines changed

src/main/java/net/sf/jsqlparser/expression/OracleHierarchicalExpression.java

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,50 +27,70 @@
2727
*/
2828
public class OracleHierarchicalExpression implements Expression {
2929

30-
private Expression startExpression;
31-
private Expression connectExpression;
32-
private boolean noCycle = false;
30+
private Expression startExpression;
31+
private Expression connectExpression;
32+
private boolean noCycle = false;
33+
boolean connectFirst = false;
3334

34-
public Expression getStartExpression() {
35-
return startExpression;
36-
}
35+
public Expression getStartExpression() {
36+
return startExpression;
37+
}
3738

38-
public void setStartExpression(Expression startExpression) {
39-
this.startExpression = startExpression;
40-
}
39+
public void setStartExpression(Expression startExpression) {
40+
this.startExpression = startExpression;
41+
}
4142

42-
public Expression getConnectExpression() {
43-
return connectExpression;
44-
}
43+
public Expression getConnectExpression() {
44+
return connectExpression;
45+
}
4546

46-
public void setConnectExpression(Expression connectExpression) {
47-
this.connectExpression = connectExpression;
48-
}
47+
public void setConnectExpression(Expression connectExpression) {
48+
this.connectExpression = connectExpression;
49+
}
4950

50-
public boolean isNoCycle() {
51-
return noCycle;
52-
}
51+
public boolean isNoCycle() {
52+
return noCycle;
53+
}
5354

54-
public void setNoCycle(boolean noCycle) {
55-
this.noCycle = noCycle;
56-
}
55+
public void setNoCycle(boolean noCycle) {
56+
this.noCycle = noCycle;
57+
}
5758

58-
@Override
59-
public void accept(ExpressionVisitor expressionVisitor) {
60-
expressionVisitor.visit(this);
61-
}
59+
public boolean isConnectFirst() {
60+
return connectFirst;
61+
}
6262

63-
@Override
64-
public String toString() {
65-
StringBuilder b = new StringBuilder();
66-
if (startExpression != null) {
67-
b.append(" START WITH ").append(startExpression.toString());
68-
}
69-
b.append(" CONNECT BY ");
70-
if (isNoCycle()) {
71-
b.append("NOCYCLE ");
72-
}
73-
b.append(connectExpression.toString());
74-
return b.toString();
75-
}
63+
public void setConnectFirst(boolean connectFirst) {
64+
this.connectFirst = connectFirst;
65+
}
66+
67+
@Override
68+
public void accept(ExpressionVisitor expressionVisitor) {
69+
expressionVisitor.visit(this);
70+
}
71+
72+
@Override
73+
public String toString() {
74+
StringBuilder b = new StringBuilder();
75+
if (isConnectFirst()) {
76+
b.append(" CONNECT BY ");
77+
if (isNoCycle()) {
78+
b.append("NOCYCLE ");
79+
}
80+
b.append(connectExpression.toString());
81+
if (startExpression != null) {
82+
b.append(" START WITH ").append(startExpression.toString());
83+
}
84+
} else {
85+
if (startExpression != null) {
86+
b.append(" START WITH ").append(startExpression.toString());
87+
}
88+
b.append(" CONNECT BY ");
89+
if (isNoCycle()) {
90+
b.append("NOCYCLE ");
91+
}
92+
b.append(connectExpression.toString());
93+
}
94+
return b.toString();
95+
}
7696
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,18 +1114,21 @@ OracleHierarchicalExpression OracleHierarchicalQueryClause():
11141114
{
11151115
OracleHierarchicalExpression result = new OracleHierarchicalExpression();
11161116
Expression expr;
1117-
Expression connectExpr;
11181117
}
11191118
{
11201119
(
11211120
<K_START> <K_WITH> expr=AndExpression() {result.setStartExpression(expr);}
1122-
<K_CONNECT> <K_BY> [ <K_NOCYCLE> { result.setNoCycle(true); } ] connectExpr=AndExpression()
1121+
<K_CONNECT> <K_BY> [ <K_NOCYCLE> { result.setNoCycle(true); } ] expr=AndExpression()
1122+
{ result.setConnectExpression(expr); }
11231123
|
1124-
<K_CONNECT> <K_BY> [ <K_NOCYCLE> { result.setNoCycle(true); } ] connectExpr=AndExpression()
1124+
<K_CONNECT> <K_BY> [ <K_NOCYCLE> { result.setNoCycle(true); } ] expr=AndExpression()
1125+
{
1126+
result.setConnectExpression(expr);
1127+
result.setConnectFirst(true);
1128+
}
11251129
[ <K_START> <K_WITH> expr=AndExpression() {result.setStartExpression(expr);} ]
11261130
)
11271131
{
1128-
result.setConnectExpression(connectExpr);
11291132
return result;
11301133
}
11311134
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,8 +1615,7 @@ public void testOracleHierarchicalQuery3() throws JSQLParserException {
16151615

16161616
public void testOracleHierarchicalQuery4() throws JSQLParserException {
16171617
String stmt = "SELECT last_name, employee_id, manager_id, LEVEL FROM employees CONNECT BY PRIOR employee_id = manager_id START WITH employee_id = 100 ORDER SIBLINGS BY last_name";
1618-
Statement parsed = CCJSqlParserUtil.parse(new StringReader(stmt));
1619-
assertStatementCanBeDeparsedAs(parsed, "SELECT last_name, employee_id, manager_id, LEVEL FROM employees START WITH employee_id = 100 CONNECT BY PRIOR employee_id = manager_id ORDER SIBLINGS BY last_name");
1618+
assertSqlCanBeParsedAndDeparsed(stmt);
16201619
}
16211620

16221621
public void testPostgreSQLRegExpCaseSensitiveMatch() throws JSQLParserException {

0 commit comments

Comments
 (0)