This is an automated email from the ASF dual-hosted git repository.

bchapuis pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git


The following commit(s) were added to refs/heads/main by this push:
     new f25ab673 Split sql files correctly (#749)
f25ab673 is described below

commit f25ab67306041add391e374d6d5a4be702ee26c9
Author: Bertil Chapuis <[email protected]>
AuthorDate: Thu Aug 17 16:35:52 2023 +0200

    Split sql files correctly (#749)
---
 .../apache/baremaps/workflow/tasks/ExecuteSql.java | 22 +++++++-----
 ...ileTest.java => ExecuteSqlIntegrationTest.java} |  3 +-
 .../baremaps/workflow/tasks/ExecuteSqlTest.java    | 39 +++++++++++++---------
 .../src/test/resources/queries/queries.sql         |  1 +
 4 files changed, 40 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..306ef5fb 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,23 @@ 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("\\s*;\\s*(?=(?:[^']*'[^']*')*[^']*$)"));
+  }
+
   /**
    * 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 +73,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..fed0b851 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
@@ -18,22 +18,31 @@ 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;

Reply via email to