Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package net.sf.jsqlparser.statement.select;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;

import java.io.Serializable;
import java.util.List;
Expand Down Expand Up @@ -100,6 +101,13 @@ public StringBuilder appendTo(StringBuilder builder) {
.append(returnExpression);
}

public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
if (returnExpression != null) {
return returnExpression.accept(expressionVisitor, context);
}
return null;
}

@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ public <T, S> T accept(SelectVisitor<T> selectVisitor, S context) {
}

public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
return statement.accept(statementVisitor, context);
if (statement != null) {
return statement.accept(statementVisitor, context);
}
return null;
}

public WithItem<?> withWithItemList(List<SelectItem<?>> withItemList) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,12 @@ public Set<String> getTables(Expression expr) {

@Override
public <S> Void visit(WithItem<?> withItem, S context) {
otherItemNames.add(withItem.getAlias().getName());
withItem.getSelect().accept((SelectVisitor<?>) this, context);
if (withItem.getAlias() != null) {
otherItemNames.add(withItem.getAlias().getName());
}
if (withItem.getSelect() != null) {
withItem.getSelect().accept((SelectVisitor<?>) this, context);
}
return null;
}

Expand Down
13 changes: 11 additions & 2 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -4405,11 +4405,20 @@ WithFunctionDeclaration WithFunctionDeclaration() #WithFunctionDeclaration:
WithFunctionParameter WithFunctionParameter() #WithFunctionParameter:
{
String name;
String type;
String type = null;
String arrayType = null;
}
{
name = RelObjectName() type = RelObjectName()
name = RelObjectName()
(
LOOKAHEAD(2) <K_ARRAY_LITERAL> "<" arrayType = RelObjectName() ">"
|
type = RelObjectName()
)
{
if (arrayType != null) {
type = "ARRAY<" + arrayType + ">";
}
return new WithFunctionParameter(name, type);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2025 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.select;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand All @@ -10,6 +20,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -93,4 +105,26 @@ void toStringTestWithNoParameters() {
assertThat(withFunctionDeclaration.toString())
.isEqualTo("FUNCTION func1() RETURNS integer RETURN 1 + 1");
}

@Test
void expressionVisitorIsNotCalledWhenNoReturnExpressionDeclared(
@Mock ExpressionVisitor<Void> expressionVisitor) {
withFunctionDeclaration = new WithFunctionDeclaration();

withFunctionDeclaration.accept(expressionVisitor, "RANDOM_CONTEXT");

verifyNoInteractions(expressionVisitor);
}

@Test
void expressionVisitorCalledWhenReturnExpressionDeclared(
@Mock ExpressionVisitor<Void> expressionVisitor) {
String context = "RANDOM_CONTEXT";
withFunctionDeclaration = new WithFunctionDeclaration()
.withReturnExpression(expression);

withFunctionDeclaration.accept(expressionVisitor, context);

verify(expression).accept(expressionVisitor, context);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2025 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.select;

import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ void testNotMaterializedIssue2251() throws JSQLParserException {
" FUNCTION doubleupplusone(x integer)\n" +
" RETURNS integer\n" +
" RETURN doubleup(x) + 1\n" +
"SELECT doubleupplusone(21);"
"SELECT doubleupplusone(21);",
"WITH\n" +
" FUNCTION takesArray(x array<double>)\n" +
" RETURNS double\n" +
" RETURN x[1] + x[2] + x[3]\n" +
"SELECT takesArray(ARRAY[1.0, 2.0, 3.0]);"

})
void testWithFunction(String sqlStr) throws JSQLParserException {
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -697,5 +698,20 @@ void testIssue2305() throws JSQLParserException {
Set<String> tables = TablesNamesFinder.findTables(sqlStr);
assertThat(tables).containsExactlyInAnyOrder("tbl");
}

@Test
void assertWithItemWithFunctionDeclarationDoesNotThrowException() throws JSQLParserException {
String sqlStr =
"WITH FUNCTION my_with_item(param1 INT) RETURNS INT RETURN param1 + 1 SELECT * FROM my_table;";
assertThatCode(() -> TablesNamesFinder.findTables(sqlStr))
.doesNotThrowAnyException();
}

@Test
void assertWithItemWithFunctionDeclarationReturnsTableInSelect() throws JSQLParserException {
String sqlStr =
"WITH FUNCTION my_with_item(param1 INT) RETURNS INT RETURN param1 + 1 SELECT * FROM my_table;";
assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactly("my_table");
}
}

8 changes: 7 additions & 1 deletion src/test/resources/simple_parsing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,10 @@ WITH
FUNCTION bye(name varchar)
RETURNS varchar
RETURN format('Bye %s!', 'name')
SELECT hello('Finn') || ' and ' || bye('Joe');
SELECT hello('Finn') || ' and ' || bye('Joe');

WITH
FUNCTION takesArray(x array<double>)
RETURNS double
RETURN x[1] + x[2] + x[3]
SELECT takesArray(array[1.0, 2.0, 3.0]);
Loading