Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse unadapted Database to SQL92 dialect and add support for JDBC URLs for commonly used testcontainers #29291

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -39,7 +40,10 @@ public final class DatabaseTypeFactory {
*/
public static DatabaseType get(final String url) {
Collection<DatabaseType> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,4 @@ public Collection<String> getJdbcUrlPrefixes() {
public String getType() {
return "TRUNK";
}

@Override
public boolean isDefault() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getJdbcUrlPrefixes() {
return Arrays.asList("jdbc:mysql:", "jdbc:mysqlx:");
return Arrays.asList("jdbc:mysql:", "jdbc:mysqlx:", "jdbc:tc:mysql:");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getJdbcUrlPrefixes() {
return Collections.singleton(String.format("jdbc:%s:", getType().toLowerCase()));
return Arrays.asList(String.format("jdbc:%s:", getType().toLowerCase()), "jdbc:tc:postgres:");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:")));
}
}