From 4bb1b8191170fd7865f73a51a7a180b038abe976 Mon Sep 17 00:00:00 2001 From: Mateusz Witkowski Date: Fri, 22 Nov 2024 14:55:15 +0100 Subject: [PATCH 1/4] Added H2 database connection type --- .../DataSourceConnectionFactory.java | 2 + .../datasource/DatabaseProductName.java | 1 + .../spring/datasource/h2/H2Connection.java | 55 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java diff --git a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DataSourceConnectionFactory.java b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DataSourceConnectionFactory.java index d3d0128..6f067e9 100644 --- a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DataSourceConnectionFactory.java +++ b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DataSourceConnectionFactory.java @@ -1,6 +1,7 @@ package dev.logchange.hofund.connection.spring.datasource; import dev.logchange.hofund.connection.HofundDatabaseConnection; +import dev.logchange.hofund.connection.spring.datasource.h2.H2Connection; import dev.logchange.hofund.connection.spring.datasource.oracle.OracleConnection; import dev.logchange.hofund.connection.spring.datasource.postgresql.PostgreSQLConnection; import lombok.extern.slf4j.Slf4j; @@ -22,6 +23,7 @@ public static HofundDatabaseConnection of(DataSource dataSource) { return switch (dbType) { case POSTGRESQL -> new PostgreSQLConnection(metaData, dataSource).toHofundConnection(); case ORACLE -> new OracleConnection(metaData, dataSource).toHofundConnection(); + case H2 -> new H2Connection(metaData, dataSource).toHofundConnection(); default -> { log.warn("Currently there is no support for DataSource: {} please create issue at: https://github.com/logchange/hofund", productName); yield null; diff --git a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DatabaseProductName.java b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DatabaseProductName.java index 61d4f9e..b530de4 100644 --- a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DatabaseProductName.java +++ b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DatabaseProductName.java @@ -10,6 +10,7 @@ public enum DatabaseProductName { POSTGRESQL("PostgreSQL"), ORACLE("Oracle"), + H2("H2"), NOT_RECOGNIZED("Not recognized"); private final String name; diff --git a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java new file mode 100644 index 0000000..fab9e67 --- /dev/null +++ b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java @@ -0,0 +1,55 @@ +package dev.logchange.hofund.connection.spring.datasource.h2; + +import dev.logchange.hofund.connection.spring.datasource.DatasourceConnection; +import lombok.extern.slf4j.Slf4j; + +import javax.sql.DataSource; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; +import java.util.Locale; + +@Slf4j +public class H2Connection extends DatasourceConnection { + + private static final String TEST_QUERY = "SELECT 1"; + + private String target; + private String url; + private String dbVendor; + + public H2Connection(DatabaseMetaData metaData, DataSource dataSource) { + super(dataSource, TEST_QUERY); + try { + this.url = metaData.getURL(); + int slashIndex = url.lastIndexOf('/'); + int to = url.length(); + if (url.lastIndexOf("?") != -1) { + to = url.lastIndexOf("?"); + } + this.target = url.substring(slashIndex + 1, to).toLowerCase(Locale.ROOT); + this.dbVendor = metaData.getDatabaseProductName(); + log.info("H2 info: target: {}, url: {}, dbVendor: {}", target, url, dbVendor); + } catch (SQLException e) { + log.warn("Error getting db information", e); + this.target = "ERROR"; + this.url = "ERROR"; + this.dbVendor = "ERROR"; + } + } + + @Override + protected String getTarget() { + return target; + } + + @Override + protected String getUrl() { + return url; + } + + @Override + protected String getDbVendor() { + return dbVendor; + } + +} From 4b6039e1499fdbd3b407cdd663824a5fd58daf8a Mon Sep 17 00:00:00 2001 From: witx98 Date: Sat, 23 Nov 2024 20:22:12 +0100 Subject: [PATCH 2/4] Added H2 connection target --- .../connection/spring/datasource/h2/H2Connection.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java index fab9e67..6b0e34b 100644 --- a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java +++ b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java @@ -21,14 +21,9 @@ public H2Connection(DatabaseMetaData metaData, DataSource dataSource) { super(dataSource, TEST_QUERY); try { this.url = metaData.getURL(); - int slashIndex = url.lastIndexOf('/'); - int to = url.length(); - if (url.lastIndexOf("?") != -1) { - to = url.lastIndexOf("?"); - } - this.target = url.substring(slashIndex + 1, to).toLowerCase(Locale.ROOT); + int colonIndex = url.lastIndexOf(':'); + this.target = url.substring(colonIndex + 1).toLowerCase(Locale.ROOT); this.dbVendor = metaData.getDatabaseProductName(); - log.info("H2 info: target: {}, url: {}, dbVendor: {}", target, url, dbVendor); } catch (SQLException e) { log.warn("Error getting db information", e); this.target = "ERROR"; From 01cb6624c4837121d2575b3a4ae817c314e27c4f Mon Sep 17 00:00:00 2001 From: witx98 Date: Sat, 23 Nov 2024 20:25:12 +0100 Subject: [PATCH 3/4] Added tests --- .../datasource/h2/H2ConnectionTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 hofund-spring/src/test/java/dev/logchange/hofund/connection/spring/datasource/h2/H2ConnectionTest.java diff --git a/hofund-spring/src/test/java/dev/logchange/hofund/connection/spring/datasource/h2/H2ConnectionTest.java b/hofund-spring/src/test/java/dev/logchange/hofund/connection/spring/datasource/h2/H2ConnectionTest.java new file mode 100644 index 0000000..504c8b3 --- /dev/null +++ b/hofund-spring/src/test/java/dev/logchange/hofund/connection/spring/datasource/h2/H2ConnectionTest.java @@ -0,0 +1,42 @@ +package dev.logchange.hofund.connection.spring.datasource.h2; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import javax.sql.DataSource; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class H2ConnectionTest { + + @Mock + private DatabaseMetaData databaseMetaData; + + @Mock + private DataSource dataSource; + + + @Test + void givenConnectionUrl_whenGetTarget_databaseNameReturned() throws SQLException { + //given: + String url = "spring.datasource.url=jdbc:h2:mem:17ebc6e8-e833-4157-b8e2-35f113eb404a"; + String productName = "H2"; + when(databaseMetaData.getURL()).thenReturn(url); + when(databaseMetaData.getDatabaseProductName()).thenReturn(productName); + + //when: + H2Connection h2Connection = new H2Connection(databaseMetaData, dataSource); + String resultTarget = h2Connection.getTarget(); + String resultVendor = h2Connection.getDbVendor(); + + //then: + assertThat(resultTarget).isEqualTo("17ebc6e8-e833-4157-b8e2-35f113eb404a"); + assertThat(resultVendor).isEqualTo(productName); + } +} From 4cf52527f344be672aa2a9ca78fb7c8cf185fd33 Mon Sep 17 00:00:00 2001 From: witx98 Date: Sat, 23 Nov 2024 20:39:21 +0100 Subject: [PATCH 4/4] Changelog update --- changelog/unreleased/000001-h2-connection.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 changelog/unreleased/000001-h2-connection.yml diff --git a/changelog/unreleased/000001-h2-connection.yml b/changelog/unreleased/000001-h2-connection.yml new file mode 100644 index 0000000..771fd24 --- /dev/null +++ b/changelog/unreleased/000001-h2-connection.yml @@ -0,0 +1,12 @@ +# This file is used by logchange tool to generate CHANGELOG.md 🌳 🪓 => 🪵 +# Visit https://github.com/logchange/logchange and leave a star 🌟 +# More info about configuration you can find https://github.com/logchange/logchange#yaml-format ⬅️⬅ ️ +title: Added support for H2 datasource type +authors: + - name: Mateusz Witkowski + nick: witx98 + url: https://github.com/witx98 +merge_requests: + - 56 +type: added +