You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@baremaps.apache.org by bc...@apache.org on 2023/08/17 09:30:24 UTC

[incubator-baremaps] branch 748-split-sql-file created (now c28c1f5d)

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

bchapuis pushed a change to branch 748-split-sql-file
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git


      at c28c1f5d Split sql files correctly

This branch includes the following new commits:

     new c28c1f5d Split sql files correctly

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-baremaps] 01/01: Split sql files correctly

Posted by bc...@apache.org.
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 c28c1f5d7981279bb1b52a48ef446747edfa4b40
Author: Bertil Chapuis <bc...@gmail.com>
AuthorDate: Thu Aug 17 11:30:11 2023 +0200

    Split sql files correctly
---
 .../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..4ac7f4ae 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;