Skip to content

Commit

Permalink
Failure to parse query with PRIOR in select list (#2083)
Browse files Browse the repository at this point in the history
* Added support for PRIOR operator to be used in select list

* Fixed copy paste issue
  • Loading branch information
hannes92 authored Oct 2, 2024
1 parent 5cf281f commit 3046924
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2021 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
/*
* Copyright (C) 2021 JSQLParser.
*
* This library is free software; you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation; either version
* 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this library;
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/

package net.sf.jsqlparser.expression;

import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
import net.sf.jsqlparser.schema.Column;

import java.util.Objects;

/**
*
* @author are
*/
public class ConnectByPriorOperator extends ASTNodeAccessImpl implements Expression {
private final Column column;

public ConnectByPriorOperator(Column column) {
this.column = Objects.requireNonNull(column,
"The COLUMN of the ConnectByPrior Operator must not be null");
}

public Column getColumn() {
return column;
}

@Override
public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
return expressionVisitor.visit(this, context);
}

public StringBuilder appendTo(StringBuilder builder) {
builder.append("PRIOR ").append(column);
return builder;
}

@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,12 @@ default void visit(ConnectByRootOperator connectByRootOperator) {
this.visit(connectByRootOperator, null);
}

<S> T visit(ConnectByPriorOperator connectByPriorOperator, S context);

default void visit(ConnectByPriorOperator connectByPriorOperator) {
this.visit(connectByPriorOperator, null);
}

<S> T visit(OracleNamedFunctionParameter oracleNamedFunctionParameter, S context);

default void visit(OracleNamedFunctionParameter oracleNamedFunctionParameter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,11 @@ public <S> T visit(ConnectByRootOperator connectByRootOperator, S context) {
return connectByRootOperator.getColumn().accept(this, context);
}

@Override
public <S> T visit(ConnectByPriorOperator connectByPriorOperator, S context) {
return connectByPriorOperator.getColumn().accept(this, context);
}

@Override
public <S> T visit(OracleNamedFunctionParameter oracleNamedFunctionParameter, S context) {
return oracleNamedFunctionParameter.getExpression().accept(this, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class ParserKeywordsUtils {
{"CHECK", RESTRICTED_SQL2016},
{"CONNECT", RESTRICTED_ALIAS},
{"CONNECT_BY_ROOT", RESTRICTED_JSQLPARSER},
{"PRIOR", RESTRICTED_JSQLPARSER},
{"CONSTRAINT", RESTRICTED_SQL2016},
{"CREATE", RESTRICTED_ALIAS},
{"CROSS", RESTRICTED_SQL2016},
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.sf.jsqlparser.expression.CastExpression;
import net.sf.jsqlparser.expression.CollateExpression;
import net.sf.jsqlparser.expression.ConnectByRootOperator;
import net.sf.jsqlparser.expression.ConnectByPriorOperator;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.DoubleValue;
Expand Down Expand Up @@ -1722,6 +1723,12 @@ public <S> Void visit(ConnectByRootOperator connectByRootOperator, S context) {
return null;
}

@Override
public <S> Void visit(ConnectByPriorOperator connectByPriorOperator, S context) {
connectByPriorOperator.getColumn().accept(this, context);
return null;
}

@Override
public <S> Void visit(IfElseStatement ifElseStatement, S context) {
ifElseStatement.getIfStatement().accept(this, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.sf.jsqlparser.expression.CastExpression;
import net.sf.jsqlparser.expression.CollateExpression;
import net.sf.jsqlparser.expression.ConnectByRootOperator;
import net.sf.jsqlparser.expression.ConnectByPriorOperator;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.DoubleValue;
Expand Down Expand Up @@ -1583,6 +1584,13 @@ public <S> StringBuilder visit(ConnectByRootOperator connectByRootOperator, S co
return buffer;
}

@Override
public <S> StringBuilder visit(ConnectByPriorOperator connectByPriorOperator, S context) {
buffer.append("PRIOR ");
connectByPriorOperator.getColumn().accept(this, context);
return buffer;
}

@Override
public <S> StringBuilder visit(OracleNamedFunctionParameter oracleNamedFunctionParameter,
S context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.sf.jsqlparser.expression.CastExpression;
import net.sf.jsqlparser.expression.CollateExpression;
import net.sf.jsqlparser.expression.ConnectByRootOperator;
import net.sf.jsqlparser.expression.ConnectByPriorOperator;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.DoubleValue;
Expand Down Expand Up @@ -1014,6 +1015,12 @@ public <S> Void visit(ConnectByRootOperator connectByRootOperator, S context) {
return null;
}

@Override
public <S> Void visit(ConnectByPriorOperator connectByPriorOperator, S context) {
connectByPriorOperator.getColumn().accept(this, context);
return null;
}

@Override
public <S> Void visit(OracleNamedFunctionParameter oracleNamedFunctionParameter, S context) {
oracleNamedFunctionParameter.getExpression().accept(this, context);
Expand Down
13 changes: 13 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2749,6 +2749,8 @@ SelectItem<?> SelectItem() #SelectItem:
(
expression = AllColumns()
|
expression = ConnectByPriorOperator()
|
LOOKAHEAD(AllTableColumns()) expression = AllTableColumns()
|
LOOKAHEAD( 3 ) expression = XorExpression()
Expand Down Expand Up @@ -4899,6 +4901,17 @@ ConnectByRootOperator ConnectByRootOperator() #ConnectByRootOperator: {
}
}

ConnectByPriorOperator ConnectByPriorOperator() #ConnectByPriorOperator: {
Column column;
}
{
<K_PRIOR> column = Column()
{
return new ConnectByPriorOperator(column);
}
}


NextValExpression NextValExpression() : {
ObjectNames data = null;
Token token;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
-- #%L
-- JSQLParser library
-- %%
-- Copyright (C) 2004 - 2019 JSQLParser
-- %%
-- Dual licensed under GNU LGPL 2.1 or Apache License 2.0
-- #L%
---
select t.*, connect_by_root t.id as root_id
from test t
start with t.id = 1
connect by prior t.id = t.parent_id
order siblings by t.some_text
--@SUCCESSFULLY_PARSED_AND_DEPARSED first on Oct 2, 2024, 8:11:58 PM
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
-- #%L
-- JSQLParser library
-- %%
-- Copyright (C) 2004 - 2019 JSQLParser
-- %%
-- Dual licensed under GNU LGPL 2.1 or Apache License 2.0
-- #L%
---
select t.*, prior t.id parent_id
from test t
start with t.id = 1
connect by prior t.id = t.parent_id
order siblings by t.some_text
--@SUCCESSFULLY_PARSED_AND_DEPARSED first on Oct 2, 2024, 8:14:31 PM
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
-- #%L
-- JSQLParser library
-- %%
-- Copyright (C) 2004 - 2019 JSQLParser
-- %%
-- Dual licensed under GNU LGPL 2.1 or Apache License 2.0
-- #L%
---
select t.*, prior t.id as parent_id
from test t
start with t.id = 1
connect by prior t.id = t.parent_id
order siblings by t.some_text
--@SUCCESSFULLY_PARSED_AND_DEPARSED first on Oct 2, 2024, 8:14:33 PM

0 comments on commit 3046924

Please sign in to comment.