From 9a838ab6d95f2998fbefdfe06280f7d0a41200af Mon Sep 17 00:00:00 2001 From: linghengqian Date: Tue, 5 Dec 2023 18:17:06 +0800 Subject: [PATCH] Convert DatabaseTypes for Clickhouse and common testcontainers --- infra/common/pom.xml | 5 ++ .../type/UnsupportedStorageTypeException.java | 32 ++++++++++++- infra/database/type/clickhouse/pom.xml | 43 +++++++++++++++++ .../type/ClickHouseDatabaseType.java | 47 +++++++++++++++++++ ...here.infra.database.core.type.DatabaseType | 18 +++++++ .../type/ClickHouseDatabaseTypeTest.java | 36 ++++++++++++++ .../mysql/type/MySQLDatabaseType.java | 4 +- .../mysql/type/MySQLDatabaseTypeTest.java | 2 +- .../oracle/type/OracleDatabaseType.java | 6 +-- .../oracle/type/OracleDatabaseTypeTest.java | 4 +- infra/database/type/pom.xml | 1 + .../type/PostgreSQLDatabaseType.java | 6 +-- .../type/PostgreSQLDatabaseTypeTest.java | 4 +- .../sqlserver/type/SQLServerDatabaseType.java | 4 +- .../type/SQLServerDatabaseTypeTest.java | 3 +- 15 files changed, 197 insertions(+), 18 deletions(-) create mode 100644 infra/database/type/clickhouse/pom.xml create mode 100644 infra/database/type/clickhouse/src/main/java/org/apache/shardingsphere/infra/database/clickhouse/type/ClickHouseDatabaseType.java create mode 100644 infra/database/type/clickhouse/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.type.DatabaseType create mode 100644 infra/database/type/clickhouse/src/test/java/org/apache/shardingsphere/infra/database/clickhouse/type/ClickHouseDatabaseTypeTest.java diff --git a/infra/common/pom.xml b/infra/common/pom.xml index 479cd2fdeed874..f53c0713869e15 100644 --- a/infra/common/pom.xml +++ b/infra/common/pom.xml @@ -68,6 +68,11 @@ shardingsphere-infra-database-h2 ${project.version} + + org.apache.shardingsphere + shardingsphere-infra-database-clickhouse + ${project.version} + org.apache.shardingsphere shardingsphere-infra-database-sql92 diff --git a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/type/UnsupportedStorageTypeException.java b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/type/UnsupportedStorageTypeException.java index b18f9ed49a8bfc..23b3f0c26f0ff8 100644 --- a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/type/UnsupportedStorageTypeException.java +++ b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/type/UnsupportedStorageTypeException.java @@ -21,13 +21,41 @@ import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.ConnectionSQLException; /** - * Unsupported storage type exception. + * Unsupported storage type exception. When this exception is thrown, it means that the relevant jdbcUrl lacks the corresponding {@link DatabaseType} SPI implementation. + * Assume that there is a jdbcUrl of `jdbc:vertica://node01.example.com:5433/dbname`, but there is no corresponding implementation of DatabaseType SPI. + * User can temporarily create the class and implement {@link DatabaseType#getJdbcUrlPrefixes()} and {@link DatabaseType#getTrunkDatabaseType()} to require the corresponding jdbcUrl Use the SQL92 dialect. + * + *
+ * public final class VerticaDatabaseType implements DatabaseType {
+ * 

+ * @Override + * public Collection getJdbcUrlPrefixes() { + * return Collections.singleton("jdbc:vertica:"); + * } + *

+ * @Override + * public Optional getTrunkDatabaseType() { + * return Optional.of(TypedSPILoader.getService(DatabaseType.class, "SQL92")); + * } + *

+ * @Override + * public String getType() { + * return "Vertica"; + * } + * } + *

+ * + * To fully support the corresponding database dialect, user need to refer to `org.apache.shardingsphere:shardingsphere-infra-database-mysql` module to implement additional SPIs. + * + * @see org.apache.shardingsphere.infra.database.core.type.DatabaseType */ public final class UnsupportedStorageTypeException extends ConnectionSQLException { private static final long serialVersionUID = 8981789100727786183L; public UnsupportedStorageTypeException(final String url) { - super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 40, "Unsupported storage type of URL `%s`.", url); + super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 40, + "Unsupported storage type of URL `%s`. Add corresponding SPI implementation of `%s` to enable handling. Through reference SQL State: %s. Through reference Vendor Code: %s.", + url, DatabaseType.class.getName(), XOpenSQLState.FEATURE_NOT_SUPPORTED.getValue(), 40); } } diff --git a/infra/database/type/clickhouse/pom.xml b/infra/database/type/clickhouse/pom.xml new file mode 100644 index 00000000000000..4186c28c2376fe --- /dev/null +++ b/infra/database/type/clickhouse/pom.xml @@ -0,0 +1,43 @@ + + + + + 4.0.0 + + org.apache.shardingsphere + shardingsphere-infra-database-type + 5.4.2-SNAPSHOT + + shardingsphere-infra-database-clickhouse + ${project.artifactId} + + + + org.apache.shardingsphere + shardingsphere-infra-database-core + ${project.version} + + + + org.apache.shardingsphere + shardingsphere-test-util + ${project.version} + test + + + diff --git a/infra/database/type/clickhouse/src/main/java/org/apache/shardingsphere/infra/database/clickhouse/type/ClickHouseDatabaseType.java b/infra/database/type/clickhouse/src/main/java/org/apache/shardingsphere/infra/database/clickhouse/type/ClickHouseDatabaseType.java new file mode 100644 index 00000000000000..84b1daacecd735 --- /dev/null +++ b/infra/database/type/clickhouse/src/main/java/org/apache/shardingsphere/infra/database/clickhouse/type/ClickHouseDatabaseType.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.shardingsphere.infra.database.clickhouse.type; + +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Optional; + +/** + * Database type of ClickHouse. Includes verification of Docker Images involved in commonly used testcontainers. + * ClickHouse currently uses MySQL dialect parsing. + */ +public final class ClickHouseDatabaseType implements DatabaseType { + + @Override + public Collection getJdbcUrlPrefixes() { + return Arrays.asList("jdbc:ch:", "jdbc:clickhouse:", "jdbc:tc:clickhouse/clickhouse-server:", "jdbc:tc:yandex/clickhouse-server:"); + } + + @Override + public Optional getTrunkDatabaseType() { + return Optional.of(TypedSPILoader.getService(DatabaseType.class, "MySQL")); + } + + @Override + public String getType() { + return "ClickHouse"; + } +} diff --git a/infra/database/type/clickhouse/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.type.DatabaseType b/infra/database/type/clickhouse/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.type.DatabaseType new file mode 100644 index 00000000000000..c0d3189916dfea --- /dev/null +++ b/infra/database/type/clickhouse/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.type.DatabaseType @@ -0,0 +1,18 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +# + +org.apache.shardingsphere.infra.database.clickhouse.type.ClickHouseDatabaseType diff --git a/infra/database/type/clickhouse/src/test/java/org/apache/shardingsphere/infra/database/clickhouse/type/ClickHouseDatabaseTypeTest.java b/infra/database/type/clickhouse/src/test/java/org/apache/shardingsphere/infra/database/clickhouse/type/ClickHouseDatabaseTypeTest.java new file mode 100644 index 00000000000000..9b91e31b0e3358 --- /dev/null +++ b/infra/database/type/clickhouse/src/test/java/org/apache/shardingsphere/infra/database/clickhouse/type/ClickHouseDatabaseTypeTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.shardingsphere.infra.database.clickhouse.type; + +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +class ClickHouseDatabaseTypeTest { + + @Test + void assertGetJdbcUrlPrefixes() { + assertThat(TypedSPILoader.getService(DatabaseType.class, "ClickHouse").getJdbcUrlPrefixes(), + is(Arrays.asList("jdbc:ch:", "jdbc:clickhouse:", "jdbc:tc:clickhouse/clickhouse-server:", "jdbc:tc:yandex/clickhouse-server:"))); + } +} 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/pom.xml b/infra/database/type/pom.xml index a00e7b29671e1a..0d2e4169b7e62e 100644 --- a/infra/database/type/pom.xml +++ b/infra/database/type/pom.xml @@ -36,5 +36,6 @@ sqlserver h2 sql92 + clickhouse 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/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..e1725acb12bfef 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,13 @@ 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:"); } @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..33e75ae4bea451 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:"))); } }