This is an automated email from the ASF dual-hosted git repository. bchapuis pushed a commit to branch 748-split-sql-file in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git
commit c5ef6136648db3ead405116178d7ac02394fa6aa Author: Bertil Chapuis <[email protected]> AuthorDate: Thu Aug 17 11:30:11 2023 +0200 Split sql files correctly --- .../apache/baremaps/workflow/tasks/ExecuteSql.java | 24 ++++++++----- ...ileTest.java => ExecuteSqlIntegrationTest.java} | 3 +- .../baremaps/workflow/tasks/ExecuteSqlTest.java | 40 ++++++++++++++-------- .../src/test/resources/queries/queries.sql | 1 + 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ExecuteSql.java b/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ExecuteSql.java index 6bdc7120..5dcc25cd 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ExecuteSql.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ExecuteSql.java @@ -17,20 +17,17 @@ import java.nio.file.Path; import java.sql.SQLException; import java.util.Arrays; import java.util.regex.Pattern; +import java.util.stream.Stream; import org.apache.baremaps.workflow.Task; import org.apache.baremaps.workflow.WorkflowContext; import org.apache.baremaps.workflow.WorkflowException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public record ExecuteSql(String database, Path file, boolean parallel) implements Task { - private static final Logger logger = LoggerFactory.getLogger(ExecuteSql.class); - @Override public void execute(WorkflowContext context) throws Exception { - var sql = removeComments(Files.readString(file)); - var queries = Arrays.stream(sql.split(";")); + var script = clean(Files.readString(file)); + var queries = split(script); if (parallel) { queries = queries.parallel(); } @@ -45,13 +42,25 @@ public record ExecuteSql(String database, Path file, boolean parallel) implement }); } + /** + * Split a SQL string into multiple SQL statements. + * + * @param sql The SQL string. + * @return The SQL statements. + */ + public static Stream<String> split(String sql) { + return Arrays.stream(sql.split(";(?=(?:[^']*'[^']*')*[^']*$)")) + .filter(s -> s != null && !s.isBlank()) + .map(s -> s.trim()); + } + /** * Remove comments from a SQL string. * * @param sql The SQL string. * @return The SQL string without comments. */ - public static String removeComments(String sql) { + public static String clean(String sql) { var result = sql; // remove single line comments @@ -66,5 +75,4 @@ public record ExecuteSql(String database, Path file, boolean parallel) implement return result; } - } diff --git a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlFileTest.java b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlIntegrationTest.java similarity index 94% rename from baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlFileTest.java rename to baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlIntegrationTest.java index 93d4274a..371443f4 100644 --- a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlFileTest.java +++ b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlIntegrationTest.java @@ -13,14 +13,13 @@ package org.apache.baremaps.workflow.tasks; - import org.apache.baremaps.testing.PostgresContainerTest; import org.apache.baremaps.testing.TestFiles; import org.apache.baremaps.workflow.WorkflowContext; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; -class ExecuteSqlFileTest extends PostgresContainerTest { +class ExecuteSqlIntegrationTest extends PostgresContainerTest { @Test @Tag("integration") diff --git a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlTest.java b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlTest.java index 814aeb7a..5c34248c 100644 --- a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlTest.java +++ b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlTest.java @@ -14,26 +14,36 @@ package org.apache.baremaps.workflow.tasks; import static org.junit.jupiter.api.Assertions.*; +import java.util.Arrays; import org.junit.jupiter.api.Test; class ExecuteSqlTest { - private static final String INPUT = """ - SELECT 1; - -- test - SELECT 2; - /* test */ - SELECT 3; - /* - test - */ - """; - - private static final String OUTPUT = "SELECT 1;SELECT 2;SELECT 3;"; + @Test + void split() { + var input = """ + SELECT 1; + SELECT split_part('a;b;c', ';', 1); + """; + var output = new String[] {"SELECT 1", "SELECT split_part('a;b;c', ';', 1)"}; + var queries = ExecuteSql.split(input).toArray(); + assertArrayEquals(output, queries); + } @Test - void removeComments() { - var queriesWithoutComments = ExecuteSql.removeComments(INPUT).strip(); - assertEquals(OUTPUT.trim(), queriesWithoutComments.replace("\n", "").strip()); + void clean() { + var input = """ + SELECT 1; + -- test + SELECT 2; + /* test */ + SELECT 3; + /* + test + */ + """; + var output = "SELECT 1;SELECT 2;SELECT 3;"; + var queriesWithoutComments = ExecuteSql.clean(input).strip(); + assertEquals(output.trim(), queriesWithoutComments.replace("\n", "").strip()); } } diff --git a/baremaps-core/src/test/resources/queries/queries.sql b/baremaps-core/src/test/resources/queries/queries.sql index 1ea13412..a21e7533 100644 --- a/baremaps-core/src/test/resources/queries/queries.sql +++ b/baremaps-core/src/test/resources/queries/queries.sql @@ -8,6 +8,7 @@ -- or implied. See the License for the specific language governing permissions and limitations under -- the License. +-- simple cases SELECT 1; SELECT 2; SELECT 3;
