From 7214c9752cf18a0b89675a34d80c95e9b1be1a97 Mon Sep 17 00:00:00 2001 From: linghengqian Date: Tue, 5 Dec 2023 18:17:06 +0800 Subject: [PATCH] Parse unadapted Database to SQL92 dialect and add support for JDBC URLs for commonly used testcontainers --- .../infra/database/core/type/DatabaseTypeFactory.java | 8 ++++++-- .../core/type/fixture/TrunkDatabaseTypeFixture.java | 5 ----- .../infra/database/mysql/type/MySQLDatabaseType.java | 4 ++-- .../infra/database/mysql/type/MySQLDatabaseTypeTest.java | 2 +- .../infra/database/oracle/type/OracleDatabaseType.java | 6 +++--- .../database/oracle/type/OracleDatabaseTypeTest.java | 4 ++-- .../database/postgresql/type/PostgreSQLDatabaseType.java | 6 +++--- .../postgresql/type/PostgreSQLDatabaseTypeTest.java | 4 ++-- .../infra/database/sql92/type/SQL92DatabaseTypeTest.java | 9 +++++++++ .../database/sqlserver/type/SQLServerDatabaseType.java | 5 +++-- .../sqlserver/type/SQLServerDatabaseTypeTest.java | 3 ++- 11 files changed, 33 insertions(+), 23 deletions(-) diff --git a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/type/DatabaseTypeFactory.java b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/type/DatabaseTypeFactory.java index a2f3789cb044a3..1b4b958efc35d9 100644 --- a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/type/DatabaseTypeFactory.java +++ b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/type/DatabaseTypeFactory.java @@ -19,10 +19,11 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; -import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import java.util.Collection; +import java.util.Properties; import java.util.stream.Collectors; /** @@ -39,7 +40,10 @@ public final class DatabaseTypeFactory { */ public static DatabaseType get(final String url) { Collection databaseTypes = ShardingSphereServiceLoader.getServiceInstances(DatabaseType.class).stream().filter(each -> matchURLs(url, each)).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(!databaseTypes.isEmpty(), () -> new UnsupportedStorageTypeException(url)); + if (databaseTypes.isEmpty()) { + return TypedSPILoader.findService(DatabaseType.class, null, new Properties()) + .orElseThrow(()-> new UnsupportedStorageTypeException(url)); + } for (DatabaseType each : databaseTypes) { if (each.getTrunkDatabaseType().isPresent()) { return each; diff --git a/infra/database/core/src/test/java/org/apache/shardingsphere/infra/database/core/type/fixture/TrunkDatabaseTypeFixture.java b/infra/database/core/src/test/java/org/apache/shardingsphere/infra/database/core/type/fixture/TrunkDatabaseTypeFixture.java index f1c9b52577ae24..d8bfdca1d77cdd 100644 --- a/infra/database/core/src/test/java/org/apache/shardingsphere/infra/database/core/type/fixture/TrunkDatabaseTypeFixture.java +++ b/infra/database/core/src/test/java/org/apache/shardingsphere/infra/database/core/type/fixture/TrunkDatabaseTypeFixture.java @@ -33,9 +33,4 @@ public Collection getJdbcUrlPrefixes() { public String getType() { return "TRUNK"; } - - @Override - public boolean isDefault() { - return true; - } } diff --git a/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/type/MySQLDatabaseType.java b/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/type/MySQLDatabaseType.java index bd8c6355759903..fd3f14f4258fcf 100644 --- a/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/type/MySQLDatabaseType.java +++ b/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/type/MySQLDatabaseType.java @@ -23,13 +23,13 @@ import java.util.Collection; /** - * Database type of MySQL. + * Database type of MySQL. Includes verification of Docker Images involved in commonly used testcontainers. */ public final class MySQLDatabaseType implements DatabaseType { @Override public Collection getJdbcUrlPrefixes() { - return Arrays.asList("jdbc:mysql:", "jdbc:mysqlx:"); + return Arrays.asList("jdbc:mysql:", "jdbc:mysqlx:", "jdbc:tc:mysql:"); } @Override diff --git a/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/type/MySQLDatabaseTypeTest.java b/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/type/MySQLDatabaseTypeTest.java index 63d7970db38874..64f2927bcd1a64 100644 --- a/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/type/MySQLDatabaseTypeTest.java +++ b/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/type/MySQLDatabaseTypeTest.java @@ -30,6 +30,6 @@ class MySQLDatabaseTypeTest { @Test void assertGetJdbcUrlPrefixes() { - assertThat(TypedSPILoader.getService(DatabaseType.class, "MySQL").getJdbcUrlPrefixes(), is(Arrays.asList("jdbc:mysql:", "jdbc:mysqlx:"))); + assertThat(TypedSPILoader.getService(DatabaseType.class, "MySQL").getJdbcUrlPrefixes(), is(Arrays.asList("jdbc:mysql:", "jdbc:mysqlx:", "jdbc:tc:mysql:"))); } } diff --git a/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/type/OracleDatabaseType.java b/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/type/OracleDatabaseType.java index bcd844a94cc20f..1404fa1a0638a5 100644 --- a/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/type/OracleDatabaseType.java +++ b/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/type/OracleDatabaseType.java @@ -19,17 +19,17 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import java.util.Arrays; import java.util.Collection; -import java.util.Collections; /** - * Database type of Oracle. + * Database type of Oracle. Includes verification of Docker Images involved in commonly used testcontainers. */ public final class OracleDatabaseType implements DatabaseType { @Override public Collection getJdbcUrlPrefixes() { - return Collections.singleton(String.format("jdbc:%s:", getType().toLowerCase())); + return Arrays.asList(String.format("jdbc:%s:", getType().toLowerCase()), "jdbc:tc:gvenzl/oracle-free:", "jdbc:tc:gvenzl/oracle-xe:"); } @Override diff --git a/infra/database/type/oracle/src/test/java/org/apache/shardingsphere/infra/database/oracle/type/OracleDatabaseTypeTest.java b/infra/database/type/oracle/src/test/java/org/apache/shardingsphere/infra/database/oracle/type/OracleDatabaseTypeTest.java index 5d48b9faa4a751..5cca23fdb8e512 100644 --- a/infra/database/type/oracle/src/test/java/org/apache/shardingsphere/infra/database/oracle/type/OracleDatabaseTypeTest.java +++ b/infra/database/type/oracle/src/test/java/org/apache/shardingsphere/infra/database/oracle/type/OracleDatabaseTypeTest.java @@ -21,7 +21,7 @@ import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.junit.jupiter.api.Test; -import java.util.Collections; +import java.util.Arrays; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -30,6 +30,6 @@ class OracleDatabaseTypeTest { @Test void assertGetJdbcUrlPrefixes() { - assertThat(TypedSPILoader.getService(DatabaseType.class, "Oracle").getJdbcUrlPrefixes(), is(Collections.singleton("jdbc:oracle:"))); + assertThat(TypedSPILoader.getService(DatabaseType.class, "Oracle").getJdbcUrlPrefixes(), is(Arrays.asList("jdbc:oracle:", "jdbc:tc:gvenzl/oracle-free:", "jdbc:tc:gvenzl/oracle-xe:"))); } } diff --git a/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/type/PostgreSQLDatabaseType.java b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/type/PostgreSQLDatabaseType.java index ae934f2efa2ab5..049f06edf82dee 100644 --- a/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/type/PostgreSQLDatabaseType.java +++ b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/type/PostgreSQLDatabaseType.java @@ -19,17 +19,17 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import java.util.Arrays; import java.util.Collection; -import java.util.Collections; /** - * Database type of PostgreSQL. + * Database type of PostgreSQL. Includes verification of Docker Images involved in commonly used testcontainers. */ public final class PostgreSQLDatabaseType implements DatabaseType { @Override public Collection getJdbcUrlPrefixes() { - return Collections.singleton(String.format("jdbc:%s:", getType().toLowerCase())); + return Arrays.asList(String.format("jdbc:%s:", getType().toLowerCase()), "jdbc:tc:postgres:"); } @Override diff --git a/infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/type/PostgreSQLDatabaseTypeTest.java b/infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/type/PostgreSQLDatabaseTypeTest.java index 4fb7c837e0aedf..840f5885a11747 100644 --- a/infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/type/PostgreSQLDatabaseTypeTest.java +++ b/infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/type/PostgreSQLDatabaseTypeTest.java @@ -21,7 +21,7 @@ import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.junit.jupiter.api.Test; -import java.util.Collections; +import java.util.Arrays; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -30,6 +30,6 @@ class PostgreSQLDatabaseTypeTest { @Test void assertGetJdbcUrlPrefixes() { - assertThat(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL").getJdbcUrlPrefixes(), is(Collections.singleton("jdbc:postgresql:"))); + assertThat(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL").getJdbcUrlPrefixes(), is(Arrays.asList("jdbc:postgresql:", "jdbc:tc:postgres:"))); } } diff --git a/infra/database/type/sql92/src/test/java/org/apache/shardingsphere/infra/database/sql92/type/SQL92DatabaseTypeTest.java b/infra/database/type/sql92/src/test/java/org/apache/shardingsphere/infra/database/sql92/type/SQL92DatabaseTypeTest.java index 4097f902942b1a..21d473cc8657a2 100644 --- a/infra/database/type/sql92/src/test/java/org/apache/shardingsphere/infra/database/sql92/type/SQL92DatabaseTypeTest.java +++ b/infra/database/type/sql92/src/test/java/org/apache/shardingsphere/infra/database/sql92/type/SQL92DatabaseTypeTest.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.infra.database.sql92.type; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.junit.jupiter.api.Test; @@ -32,4 +33,12 @@ class SQL92DatabaseTypeTest { void assertGetJdbcUrlPrefixes() { assertThat(TypedSPILoader.getService(DatabaseType.class, "SQL92").getJdbcUrlPrefixes(), is(Collections.emptyList())); } + + @Test + void assertGetDatabaseTypeWithUnrecognizedURL() { + assertThat(DatabaseTypeFactory.get("jdbc:not-existed:test").getType(), is("SQL92")); + assertThat(DatabaseTypeFactory.get("jdbc:vertica://VerticaHost:portNumber/databaseName").getType(), is("SQL92")); + assertThat(DatabaseTypeFactory.get("jdbc:ch://my-server/system").getType(), is("SQL92")); + assertThat(DatabaseTypeFactory.get("jdbc:clickhouse://localhost:8123/test").getType(), is("SQL92")); + } } diff --git a/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/type/SQLServerDatabaseType.java b/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/type/SQLServerDatabaseType.java index 69e7e4763d0e49..fc3c424464cb96 100644 --- a/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/type/SQLServerDatabaseType.java +++ b/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/type/SQLServerDatabaseType.java @@ -23,13 +23,14 @@ import java.util.Collection; /** - * Database type of SQLServer. + * Database type of SQLServer. Includes verification of Docker Images involved in commonly used testcontainers. */ public final class SQLServerDatabaseType implements DatabaseType { @Override public Collection getJdbcUrlPrefixes() { - return Arrays.asList("jdbc:microsoft:sqlserver:", "jdbc:sqlserver:"); + return Arrays.asList("jdbc:microsoft:sqlserver:", "jdbc:sqlserver:", + "jdbc:tc:mcr.microsoft.com/mssql/server:", "jdbc:tc:mcr.microsoft.com/mssql/rhel/server:"); } @Override diff --git a/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/type/SQLServerDatabaseTypeTest.java b/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/type/SQLServerDatabaseTypeTest.java index 49e57cdf5905f6..ee7e9f2a081a0f 100644 --- a/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/type/SQLServerDatabaseTypeTest.java +++ b/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/type/SQLServerDatabaseTypeTest.java @@ -30,6 +30,7 @@ class SQLServerDatabaseTypeTest { @Test void assertGetJdbcUrlPrefixes() { - assertThat(TypedSPILoader.getService(DatabaseType.class, "SQLServer").getJdbcUrlPrefixes(), is(Arrays.asList("jdbc:microsoft:sqlserver:", "jdbc:sqlserver:"))); + assertThat(TypedSPILoader.getService(DatabaseType.class, "SQLServer").getJdbcUrlPrefixes(), + is(Arrays.asList("jdbc:microsoft:sqlserver:", "jdbc:sqlserver:", "jdbc:tc:mcr.microsoft.com/mssql/server:", "jdbc:tc:mcr.microsoft.com/mssql/rhel/server:"))); } }