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/09/01 11:16:51 UTC

[incubator-baremaps] branch 764-sql-script created (now de6010e9)

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

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


      at de6010e9 Add task to execute sql script

This branch includes the following new commits:

     new de6010e9 Add task to execute sql script

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: Add task to execute sql script

Posted by bc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit de6010e9918396028967774fb475ae3f9ef29a1a
Author: Bertil Chapuis <bc...@gmail.com>
AuthorDate: Fri Sep 1 13:16:24 2023 +0200

    Add task to execute sql script
---
 .../org/apache/baremaps/utils/PostgresUtils.java   | 26 ++++++++++++---
 .../java/org/apache/baremaps/workflow/Task.java    |  1 +
 .../apache/baremaps/workflow/tasks/ExecuteSql.java |  5 +--
 .../baremaps/workflow/tasks/ExecuteSqlScript.java  | 37 ++++++++++++++++++++++
 .../workflow/tasks/ExecuteSqlScriptTest.java       | 31 ++++++++++++++++++
 baremaps-core/src/test/resources/script.sql        |  5 +++
 6 files changed, 99 insertions(+), 6 deletions(-)

diff --git a/baremaps-core/src/main/java/org/apache/baremaps/utils/PostgresUtils.java b/baremaps-core/src/main/java/org/apache/baremaps/utils/PostgresUtils.java
index e1768a15..e9679483 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/utils/PostgresUtils.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/utils/PostgresUtils.java
@@ -55,20 +55,38 @@ public final class PostgresUtils {
   /**
    * Creates a data source from a JDBC url with a pool size defined by the user.
    *
-   * @param url the JDBC url
+   * @param jdbcUrl the JDBC url
    * @param poolSize the pool size
    * @return the data source
    */
-  public static DataSource createDataSource(String url, int poolSize) {
+  public static DataSource createDataSource(String jdbcUrl, int poolSize) {
     if (poolSize < 1) {
       throw new IllegalArgumentException("PoolSize cannot be inferior to 1");
     }
-    HikariConfig config = new HikariConfig();
-    config.setJdbcUrl(url);
+    var multiQueriesJdbcUrl = withAllowMultiQueriesParameter(jdbcUrl);
+    var config = new HikariConfig();
+    config.setJdbcUrl(multiQueriesJdbcUrl);
     config.setMaximumPoolSize(poolSize);
     return new HikariDataSource(config);
   }
 
+  /**
+   * Appends the multi-queries parameter to the given JDBC URL.
+   *
+   * @param jdbcUrl the JDBC URL
+   * @return the JDBC URL with the multi-queries parameter
+   */
+  private static String withAllowMultiQueriesParameter(String jdbcUrl) {
+    if (jdbcUrl == null || jdbcUrl.isEmpty()) {
+      return jdbcUrl;
+    }
+    if (jdbcUrl.contains("?")) {
+      return jdbcUrl + "&allowMultiQueries=true";
+    } else {
+      return jdbcUrl + "?allowMultiQueries=true";
+    }
+  }
+
   /**
    * Executes the queries contained in a resource file.
    *
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/workflow/Task.java b/baremaps-core/src/main/java/org/apache/baremaps/workflow/Task.java
index 65676fef..901e130d 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/workflow/Task.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/workflow/Task.java
@@ -30,6 +30,7 @@ import org.apache.baremaps.workflow.tasks.*;
 @JsonSubTypes({@JsonSubTypes.Type(value = DownloadUrl.class, name = "DownloadUrl"),
     @JsonSubTypes.Type(value = ExecuteCommand.class, name = "ExecuteCommand"),
     @JsonSubTypes.Type(value = ExecuteSql.class, name = "ExecuteSql"),
+    @JsonSubTypes.Type(value = ExecuteSqlScript.class, name = "ExecuteSqlScript"),
     @JsonSubTypes.Type(value = ExportVectorTiles.class, name = "ExportVectorTiles"),
     @JsonSubTypes.Type(value = ImportGeoPackage.class, name = "ImportGeoPackage"),
     @JsonSubTypes.Type(value = ImportOpenStreetMap.class, name = "ImportOpenStreetMap"),
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 306ef5fb..95af43e4 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
@@ -34,8 +34,9 @@ public record ExecuteSql(String database, Path file, boolean parallel) implement
     queries.forEach(
         query -> {
           var dataSource = context.getDataSource(database);
-          try (var connection = dataSource.getConnection()) {
-            connection.createStatement().execute(query);
+          try (var connection = dataSource.getConnection();
+              var statement = connection.createStatement()) {
+            statement.execute(query);
           } catch (SQLException e) {
             throw new WorkflowException(e);
           }
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ExecuteSqlScript.java b/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ExecuteSqlScript.java
new file mode 100644
index 00000000..168433b9
--- /dev/null
+++ b/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ExecuteSqlScript.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package org.apache.baremaps.workflow.tasks;
+
+import org.apache.baremaps.workflow.Task;
+import org.apache.baremaps.workflow.WorkflowContext;
+import org.apache.baremaps.workflow.WorkflowException;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.SQLException;
+
+public record ExecuteSqlScript(String database, Path file) implements Task {
+
+  @Override
+  public void execute(WorkflowContext context) throws Exception {
+    var script = Files.readString(file);
+    var dataSource = context.getDataSource(database);
+    try (var connection = dataSource.getConnection();
+        var statement = connection.createStatement()) {
+      statement.execute(script);
+    } catch (SQLException e) {
+      throw new WorkflowException(e);
+    }
+  }
+
+}
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlScriptTest.java b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlScriptTest.java
new file mode 100644
index 00000000..0d6de566
--- /dev/null
+++ b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ExecuteSqlScriptTest.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+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 ExecuteSqlScriptTest extends PostgresContainerTest {
+
+  @Test
+  @Tag("integration")
+  void execute() throws Exception {
+    var task = new ExecuteSqlScript(jdbcUrl(), TestFiles.resolve("script.sql"));
+    task.execute(new WorkflowContext());
+  }
+}
diff --git a/baremaps-core/src/test/resources/script.sql b/baremaps-core/src/test/resources/script.sql
new file mode 100644
index 00000000..4577e1e1
--- /dev/null
+++ b/baremaps-core/src/test/resources/script.sql
@@ -0,0 +1,5 @@
+DO $$
+    BEGIN
+        PERFORM 'Hello, World!';
+    END
+$$;
\ No newline at end of file