Skip to content

Commit

Permalink
Parse unadapted Database to SQL92 dialect and add support for JDBC UR…
Browse files Browse the repository at this point in the history
…Ls for commonly used testcontainers
  • Loading branch information
linghengqian committed Dec 5, 2023
1 parent b96b0d4 commit 7214c97
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 23 deletions.
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:")));
}
}

0 comments on commit 7214c97

Please sign in to comment.