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 b37f1ec0 Add task to execute sql script (#769)
b37f1ec0 is described below
commit b37f1ec029b1b4e600f47a9159b3a5be7c6da5ae
Author: Bertil Chapuis <[email protected]>
AuthorDate: Fri Sep 1 15:45:51 2023 +0200
Add task to execute sql script (#769)
---
.../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 | 36 ++++++++++++++++++++++
.../workflow/tasks/ExecuteSqlScriptTest.java | 31 +++++++++++++++++++
baremaps-core/src/test/resources/script.sql | 5 +++
6 files changed, 98 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..9e641590
--- /dev/null
+++
b/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ExecuteSqlScript.java
@@ -0,0 +1,36 @@
+/*
+ * 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 java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.SQLException;
+import org.apache.baremaps.workflow.Task;
+import org.apache.baremaps.workflow.WorkflowContext;
+import org.apache.baremaps.workflow.WorkflowException;
+
+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