|
9 | 9 | */ |
10 | 10 | package net.sf.jsqlparser.statement.create; |
11 | 11 |
|
| 12 | +import static net.sf.jsqlparser.test.TestUtils.assertDeparse; |
| 13 | +import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed; |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
12 | 16 | import java.util.Arrays; |
13 | 17 | import net.sf.jsqlparser.JSQLParserException; |
14 | 18 | import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 19 | +import net.sf.jsqlparser.statement.Statements; |
15 | 20 | import net.sf.jsqlparser.statement.create.function.CreateFunction; |
16 | 21 | import net.sf.jsqlparser.statement.create.procedure.CreateProcedure; |
17 | | -import static net.sf.jsqlparser.test.TestUtils.assertDeparse; |
18 | | -import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed; |
19 | | -import static org.assertj.core.api.Assertions.assertThat; |
20 | 22 | import org.junit.jupiter.api.Test; |
21 | 23 |
|
22 | 24 | /** |
@@ -86,4 +88,66 @@ public void createOrReplaceFunctionMinimal() throws JSQLParserException { |
86 | 88 | func.setOrReplace(true); |
87 | 89 | assertDeparse(func, statement); |
88 | 90 | } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void createFunctionWithPositionalParametersAcrossStatementsIssue2322() |
| 94 | + throws JSQLParserException { |
| 95 | + String sql = "create table if not exists test_table (\n" |
| 96 | + + " id bigint not null\n" |
| 97 | + + ");\n" |
| 98 | + + "\n" |
| 99 | + + "create or replace function test_fn_1(\n" |
| 100 | + + " target text,\n" |
| 101 | + + " characters text\n" |
| 102 | + + ") returns boolean as $$\n" |
| 103 | + + " select trim($2 from $1) <> $1\n" |
| 104 | + + "$$ language sql immutable;\n" |
| 105 | + + "\n" |
| 106 | + + "create or replace function test_fn_2(\n" |
| 107 | + + " target text,\n" |
| 108 | + + " characters text\n" |
| 109 | + + ") returns boolean as $$\n" |
| 110 | + + " select position(repeat(first_char, 2) in translate(\n" |
| 111 | + + " $1, $2, repeat(first_char, length($2))\n" |
| 112 | + + " )) > 0\n" |
| 113 | + + " from (values (left($2, 1))) params(first_char)\n" |
| 114 | + + "$$ language sql immutable;\n" |
| 115 | + + "\n" |
| 116 | + + "create table if not exists test_table_2 (\n" |
| 117 | + + " id bigint not null\n" |
| 118 | + + ");"; |
| 119 | + |
| 120 | + Statements statements = CCJSqlParserUtil.parseStatements(sql); |
| 121 | + |
| 122 | + assertThat(statements.getStatements()).hasSize(4); |
| 123 | + assertThat(statements.getStatements().get(1)).isInstanceOf(CreateFunction.class); |
| 124 | + assertThat(statements.getStatements().get(2)).isInstanceOf(CreateFunction.class); |
| 125 | + |
| 126 | + CreateFunction function1 = (CreateFunction) statements.getStatements().get(1); |
| 127 | + CreateFunction function2 = (CreateFunction) statements.getStatements().get(2); |
| 128 | + |
| 129 | + assertThat(function1.getFunctionDeclarationParts()).anySatisfy( |
| 130 | + token -> assertThat(token).startsWith("$$").endsWith("$$")); |
| 131 | + assertThat(function1.getFunctionDeclarationParts()).containsSequence("language", "sql", |
| 132 | + "immutable", ";"); |
| 133 | + assertThat(String.join(" ", function1.getFunctionDeclarationParts())) |
| 134 | + .contains("test_fn_1") |
| 135 | + .contains("$2") |
| 136 | + .contains("$1") |
| 137 | + .doesNotContain("create or replace function test_fn_2"); |
| 138 | + |
| 139 | + assertThat(function2.getFunctionDeclarationParts()).anySatisfy( |
| 140 | + token -> assertThat(token).startsWith("$$").endsWith("$$")); |
| 141 | + assertThat(function2.getFunctionDeclarationParts()).containsSequence("language", "sql", |
| 142 | + "immutable", ";"); |
| 143 | + assertThat(String.join(" ", function2.getFunctionDeclarationParts())) |
| 144 | + .contains("test_fn_2") |
| 145 | + .contains("params") |
| 146 | + .doesNotContain("create table if not exists test_table_2"); |
| 147 | + |
| 148 | + assertThat(function1.formatDeclaration()).contains("test_fn_1"); |
| 149 | + assertThat(function1.formatDeclaration()).doesNotContain("test_fn_2"); |
| 150 | + assertThat(function2.formatDeclaration()).contains("test_fn_2"); |
| 151 | + assertThat(function2.formatDeclaration()).doesNotContain("test_table_2"); |
| 152 | + } |
89 | 153 | } |
0 commit comments