Skip to content

Commit 624a768

Browse files
feat: Oracle hierarchical queries to except Expression in the operator
- fixes #2300 Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
1 parent 1fdfd1b commit 624a768

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,26 @@
3535
* @author are
3636
*/
3737
public class ConnectByPriorOperator extends ASTNodeAccessImpl implements Expression {
38-
private final Column column;
38+
private final Expression expression;
3939

40+
@Deprecated
4041
public ConnectByPriorOperator(Column column) {
41-
this.column = Objects.requireNonNull(column,
42+
this.expression = Objects.requireNonNull(column,
4243
"The COLUMN of the ConnectByPrior Operator must not be null");
4344
}
4445

45-
public Column getColumn() {
46-
return column;
46+
public ConnectByPriorOperator(Expression column) {
47+
this.expression = Objects.requireNonNull(column,
48+
"The COLUMN of the ConnectByPrior Operator must not be null");
49+
}
50+
51+
@Deprecated
52+
public Expression getColumn() {
53+
return getExpression();
54+
}
55+
56+
public Expression getExpression() {
57+
return expression;
4758
}
4859

4960
@Override
@@ -52,12 +63,12 @@ public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
5263
}
5364

5465
public StringBuilder appendTo(StringBuilder builder) {
55-
builder.append("PRIOR ").append(column);
66+
builder.append("PRIOR ").append(expression);
5667
return builder;
5768
}
5869

5970
@Override
6071
public String toString() {
6172
return appendTo(new StringBuilder()).toString();
6273
}
63-
}
74+
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,26 @@
3434
* @author are
3535
*/
3636
public class ConnectByRootOperator extends ASTNodeAccessImpl implements Expression {
37-
private final Column column;
37+
private final Expression expression;
3838

39+
@Deprecated
3940
public ConnectByRootOperator(Column column) {
40-
this.column = Objects.requireNonNull(column,
41+
this.expression = Objects.requireNonNull(column,
4142
"The COLUMN of the ConnectByRoot Operator must not be null");
4243
}
4344

44-
public Column getColumn() {
45-
return column;
45+
public ConnectByRootOperator(Expression column) {
46+
this.expression = Objects.requireNonNull(column,
47+
"The EXPRESSION of the ConnectByRoot Operator must not be null");
48+
}
49+
50+
@Deprecated
51+
public Expression getColumn() {
52+
return expression;
53+
}
54+
55+
public Expression getExpression() {
56+
return expression;
4657
}
4758

4859
@Override
@@ -51,7 +62,7 @@ public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
5162
}
5263

5364
public StringBuilder appendTo(StringBuilder builder) {
54-
builder.append("CONNECT_BY_ROOT ").append(column);
65+
builder.append("CONNECT_BY_ROOT ").append(expression);
5566
return builder;
5667
}
5768

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6545,23 +6545,30 @@ Expression PrimaryExpression() #PrimaryExpression:
65456545
}
65466546
}
65476547

6548+
/* https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/img_text/hierarchical_query_clause.html
6549+
6550+
{ CONNECT BY [ NOCYCLE ] condition [ START WITH condition ]
6551+
| START WITH condition CONNECT BY [ NOCYCLE ] condition
6552+
}
6553+
*/
6554+
65486555
ConnectByRootOperator ConnectByRootOperator() #ConnectByRootOperator: {
6549-
Column column;
6556+
Expression expression;
65506557
}
65516558
{
6552-
<K_CONNECT_BY_ROOT> column = Column()
6559+
<K_CONNECT_BY_ROOT> expression = Expression()
65536560
{
6554-
return new ConnectByRootOperator(column);
6561+
return new ConnectByRootOperator(expression);
65556562
}
65566563
}
65576564

65586565
ConnectByPriorOperator ConnectByPriorOperator() #ConnectByPriorOperator: {
6559-
Column column;
6566+
Expression expression;
65606567
}
65616568
{
6562-
<K_PRIOR> column = Column()
6569+
<K_PRIOR> expression = Expression()
65636570
{
6564-
return new ConnectByPriorOperator(column);
6571+
return new ConnectByPriorOperator(expression);
65656572
}
65666573
}
65676574

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.sf.jsqlparser.expression;
2+
3+
import net.sf.jsqlparser.JSQLParserException;
4+
import net.sf.jsqlparser.test.TestUtils;
5+
import org.junit.jupiter.api.Test;
6+
7+
8+
class ConnectByRootOperatorTest {
9+
10+
@Test
11+
void testCondition() throws JSQLParserException {
12+
//@formatter:off
13+
String sqlStr=
14+
"SELECT EMP_ID, EMP_NAME,\n" +
15+
" \t CONNECT_BY_ROOT (EMP_NAME || '_' || EMP_ID) AS ROOT_MANAGER,\n" +
16+
" \t SYS_CONNECT_BY_PATH(EMP_NAME, ' -> ') AS PATH\n" +
17+
" FROM EMPLOYEES\n" +
18+
" START WITH MANAGER_ID IS NULL\n" +
19+
" CONNECT BY PRIOR EMP_ID = MANAGER_ID";
20+
//@formatter:on
21+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
22+
}
23+
}

0 commit comments

Comments
 (0)