diff --git a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/DialectMetaDataLoader.java b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/DialectMetaDataLoader.java index e21ce11eeb06e..7dcf98736c9b5 100644 --- a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/DialectMetaDataLoader.java +++ b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/DialectMetaDataLoader.java @@ -21,7 +21,6 @@ import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPI; import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; -import javax.sql.DataSource; import java.sql.SQLException; import java.util.Collection; @@ -34,11 +33,9 @@ public interface DialectMetaDataLoader extends DatabaseTypedSPI { /** * Load schema meta data. * - * @param dataSource data source - * @param tables tables - * @param defaultSchemaName default schema name + * @param material meta data loader material * @return schema meta data collection * @throws SQLException SQL exception */ - Collection load(DataSource dataSource, Collection tables, String defaultSchemaName) throws SQLException; + Collection load(MetaDataLoaderMaterial material) throws SQLException; } diff --git a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/MetaDataLoader.java b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/MetaDataLoader.java index f51cac2a0a321..13765ad4d5289 100644 --- a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/MetaDataLoader.java +++ b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/MetaDataLoader.java @@ -83,7 +83,7 @@ private static Collection load(final MetaDataLoaderMaterial mate Optional dialectLoader = DatabaseTypedSPILoader.findService(DialectMetaDataLoader.class, material.getStorageType()); if (dialectLoader.isPresent()) { try { - return dialectLoader.get().load(material.getDataSource(), material.getActualTableNames(), material.getDefaultSchemaName()); + return dialectLoader.get().load(material); } catch (final SQLException ex) { log.debug("Dialect load schema meta data error.", ex); } diff --git a/infra/database/type/h2/src/main/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoader.java b/infra/database/type/h2/src/main/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoader.java index 50b3251d42bc4..656f7bc4f2827 100644 --- a/infra/database/type/h2/src/main/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoader.java +++ b/infra/database/type/h2/src/main/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoader.java @@ -18,13 +18,13 @@ package org.apache.shardingsphere.infra.database.h2.metadata.data.loader; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData; import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeLoader; -import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -65,17 +65,17 @@ public final class H2MetaDataLoader implements DialectMetaDataLoader { private static final String GENERATED_INFO_SQL_IN_TABLES = GENERATED_INFO_SQL + " AND UPPER(C.TABLE_NAME) IN (%s)"; @Override - public Collection load(final DataSource dataSource, final Collection tables, final String defaultSchemaName) throws SQLException { + public Collection load(final MetaDataLoaderMaterial material) throws SQLException { Collection tableMetaDataList = new LinkedList<>(); - try (Connection connection = dataSource.getConnection()) { - Map> columnMetaDataMap = loadColumnMetaDataMap(connection, tables); + try (Connection connection = material.getDataSource().getConnection()) { + Map> columnMetaDataMap = loadColumnMetaDataMap(connection, material.getActualTableNames()); Map> indexMetaDataMap = columnMetaDataMap.isEmpty() ? Collections.emptyMap() : loadIndexMetaData(connection, columnMetaDataMap.keySet()); for (Entry> entry : columnMetaDataMap.entrySet()) { Collection indexMetaDataList = indexMetaDataMap.getOrDefault(entry.getKey(), Collections.emptyList()); tableMetaDataList.add(new TableMetaData(entry.getKey(), entry.getValue(), indexMetaDataList, Collections.emptyList())); } } - return Collections.singleton(new SchemaMetaData(defaultSchemaName, tableMetaDataList)); + return Collections.singleton(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList)); } private Map> loadColumnMetaDataMap(final Connection connection, final Collection tables) throws SQLException { diff --git a/infra/database/type/h2/src/test/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoaderTest.java b/infra/database/type/h2/src/test/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoaderTest.java index 38e548485aed2..52dfd69d72ed8 100644 --- a/infra/database/type/h2/src/test/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoaderTest.java +++ b/infra/database/type/h2/src/test/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoaderTest.java @@ -18,12 +18,14 @@ package org.apache.shardingsphere.infra.database.h2.metadata.data.loader; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData; import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.database.h2.type.H2DatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.junit.jupiter.api.Test; @@ -65,7 +67,7 @@ void assertLoadWithoutTables() throws SQLException { "SELECT C.TABLE_NAME TABLE_NAME, C.COLUMN_NAME COLUMN_NAME, COALESCE(I.IS_GENERATED, FALSE) IS_GENERATED FROM INFORMATION_SCHEMA.COLUMNS C RIGHT JOIN" + " INFORMATION_SCHEMA.INDEXES I ON C.TABLE_NAME=I.TABLE_NAME WHERE C.TABLE_CATALOG=? AND C.TABLE_SCHEMA=?") .executeQuery()).thenReturn(generatedInfo); - assertTableMetaDataMap(getDialectTableMetaDataLoader().load(dataSource, Collections.emptyList(), "sharding_db")); + assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.emptyList(), dataSource, new H2DatabaseType(), "sharding_db"))); } @Test @@ -89,7 +91,7 @@ void assertLoadWithTables() throws SQLException { "SELECT C.TABLE_NAME TABLE_NAME, C.COLUMN_NAME COLUMN_NAME, COALESCE(I.IS_GENERATED, FALSE) IS_GENERATED FROM INFORMATION_SCHEMA.COLUMNS C" + " RIGHT JOIN INFORMATION_SCHEMA.INDEXES I ON C.TABLE_NAME=I.TABLE_NAME WHERE C.TABLE_CATALOG=? AND C.TABLE_SCHEMA=? AND C.TABLE_NAME IN ('tbl')") .executeQuery()).thenReturn(generatedInfo); - assertTableMetaDataMap(getDialectTableMetaDataLoader().load(dataSource, Collections.singletonList("tbl"), "sharding_db")); + assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), dataSource, new H2DatabaseType(), "sharding_db"))); } private DataSource mockDataSource() throws SQLException { diff --git a/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoader.java b/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoader.java index 251e23436908f..c070f0bf213b8 100644 --- a/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoader.java +++ b/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoader.java @@ -19,6 +19,7 @@ import org.apache.shardingsphere.infra.database.core.GlobalDataSourceRegistry; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ConstraintMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; @@ -62,17 +63,18 @@ public final class MySQLMetaDataLoader implements DialectMetaDataLoader { + "WHERE TABLE_NAME IN (%s) AND REFERENCED_TABLE_SCHEMA IS NOT NULL"; @Override - public Collection load(final DataSource dataSource, final Collection tables, final String defaultSchemaName) throws SQLException { + public Collection load(final MetaDataLoaderMaterial material) throws SQLException { Collection tableMetaDataList = new LinkedList<>(); - Map> columnMetaDataMap = loadColumnMetaDataMap(dataSource, tables); - Map> indexMetaDataMap = columnMetaDataMap.isEmpty() ? Collections.emptyMap() : loadIndexMetaData(dataSource, columnMetaDataMap.keySet()); - Map> constraintMetaDataMap = columnMetaDataMap.isEmpty() ? Collections.emptyMap() : loadConstraintMetaDataMap(dataSource, columnMetaDataMap.keySet()); + Map> columnMetaDataMap = loadColumnMetaDataMap(material.getDataSource(), material.getActualTableNames()); + Map> indexMetaDataMap = columnMetaDataMap.isEmpty() ? Collections.emptyMap() : loadIndexMetaData(material.getDataSource(), columnMetaDataMap.keySet()); + Map> constraintMetaDataMap = + columnMetaDataMap.isEmpty() ? Collections.emptyMap() : loadConstraintMetaDataMap(material.getDataSource(), columnMetaDataMap.keySet()); for (Entry> entry : columnMetaDataMap.entrySet()) { Collection indexMetaDataList = indexMetaDataMap.getOrDefault(entry.getKey(), Collections.emptyList()); Collection constraintMetaDataList = constraintMetaDataMap.getOrDefault(entry.getKey(), Collections.emptyList()); tableMetaDataList.add(new TableMetaData(entry.getKey(), entry.getValue(), indexMetaDataList, constraintMetaDataList)); } - return Collections.singletonList(new SchemaMetaData(defaultSchemaName, tableMetaDataList)); + return Collections.singletonList(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList)); } private Map> loadConstraintMetaDataMap(final DataSource dataSource, final Collection tables) throws SQLException { diff --git a/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoaderTest.java b/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoaderTest.java index ab41b9ce4f50a..12ac455db5970 100644 --- a/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoaderTest.java +++ b/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoaderTest.java @@ -18,12 +18,14 @@ package org.apache.shardingsphere.infra.database.mysql.metadata.data.loader; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData; import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.junit.jupiter.api.Test; @@ -55,7 +57,7 @@ void assertLoadWithoutTables() throws SQLException { ResultSet indexResultSet = mockIndexMetaDataResultSet(); when(dataSource.getConnection().prepareStatement("SELECT TABLE_NAME, INDEX_NAME, NON_UNIQUE, COLUMN_NAME FROM information_schema.statistics " + "WHERE TABLE_SCHEMA=? and TABLE_NAME IN ('tbl') ORDER BY NON_UNIQUE, INDEX_NAME, SEQ_IN_INDEX").executeQuery()).thenReturn(indexResultSet); - assertTableMetaDataMap(dialectMetaDataLoader.load(dataSource, Collections.emptyList(), "sharding_db")); + assertTableMetaDataMap(dialectMetaDataLoader.load(new MetaDataLoaderMaterial(Collections.emptyList(), dataSource, new MySQLDatabaseType(), "sharding_db"))); } @Test @@ -69,7 +71,7 @@ void assertLoadWithTables() throws SQLException { when(dataSource.getConnection().prepareStatement("SELECT TABLE_NAME, INDEX_NAME, NON_UNIQUE, COLUMN_NAME FROM information_schema.statistics WHERE TABLE_SCHEMA=? and TABLE_NAME IN ('tbl') " + "ORDER BY NON_UNIQUE, INDEX_NAME, SEQ_IN_INDEX") .executeQuery()).thenReturn(indexResultSet); - assertTableMetaDataMap(dialectMetaDataLoader.load(dataSource, Collections.singletonList("tbl"), "sharding_db")); + assertTableMetaDataMap(dialectMetaDataLoader.load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), dataSource, new MySQLDatabaseType(), "sharding_db"))); } private DataSource mockDataSource() throws SQLException { diff --git a/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/data/loader/OpenGaussMetaDataLoader.java b/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/data/loader/OpenGaussMetaDataLoader.java index 56d586b019e37..58159925aacb7 100644 --- a/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/data/loader/OpenGaussMetaDataLoader.java +++ b/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/data/loader/OpenGaussMetaDataLoader.java @@ -20,6 +20,7 @@ import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Multimap; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.type.SchemaMetaDataLoader; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; @@ -29,7 +30,6 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -61,11 +61,11 @@ public final class OpenGaussMetaDataLoader implements DialectMetaDataLoader { private static final String BASIC_INDEX_META_DATA_SQL = "SELECT tablename, indexname, schemaname FROM pg_indexes WHERE schemaname IN (%s)"; @Override - public Collection load(final DataSource dataSource, final Collection tables, final String defaultSchemaName) throws SQLException { - try (Connection connection = dataSource.getConnection()) { + public Collection load(final MetaDataLoaderMaterial material) throws SQLException { + try (Connection connection = material.getDataSource().getConnection()) { Collection schemaNames = SchemaMetaDataLoader.loadSchemaNames(connection, TypedSPILoader.getService(DatabaseType.class, "openGauss")); Map> schemaIndexMetaDataMap = loadIndexMetaDataMap(connection, schemaNames); - Map> schemaColumnMetaDataMap = loadColumnMetaDataMap(connection, tables, schemaNames); + Map> schemaColumnMetaDataMap = loadColumnMetaDataMap(connection, material.getActualTableNames(), schemaNames); Collection result = new LinkedList<>(); for (String each : schemaNames) { Multimap tableIndexMetaDataMap = schemaIndexMetaDataMap.getOrDefault(each, LinkedHashMultimap.create()); diff --git a/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java b/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java index 7ff5e560c4b8d..057c9b4e80864 100644 --- a/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java +++ b/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java @@ -20,6 +20,7 @@ import com.google.common.collect.Lists; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderConnection; +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData; @@ -28,7 +29,6 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import javax.sql.DataSource; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; @@ -76,11 +76,11 @@ public final class OracleMetaDataLoader implements DialectMetaDataLoader { private static final int MAX_EXPRESSION_SIZE = 1000; @Override - public Collection load(final DataSource dataSource, final Collection tables, final String defaultSchemaName) throws SQLException { - Map> columnMetaDataMap = new HashMap<>(tables.size(), 1F); - Map> indexMetaDataMap = new HashMap<>(tables.size(), 1F); - try (Connection connection = new MetaDataLoaderConnection(TypedSPILoader.getService(DatabaseType.class, "Oracle"), dataSource.getConnection())) { - for (List each : Lists.partition(new ArrayList<>(tables), MAX_EXPRESSION_SIZE)) { + public Collection load(final MetaDataLoaderMaterial material) throws SQLException { + Map> columnMetaDataMap = new HashMap<>(material.getActualTableNames().size(), 1F); + Map> indexMetaDataMap = new HashMap<>(material.getActualTableNames().size(), 1F); + try (Connection connection = new MetaDataLoaderConnection(TypedSPILoader.getService(DatabaseType.class, "Oracle"), material.getDataSource().getConnection())) { + for (List each : Lists.partition(new ArrayList<>(material.getActualTableNames()), MAX_EXPRESSION_SIZE)) { columnMetaDataMap.putAll(loadColumnMetaDataMap(connection, each)); indexMetaDataMap.putAll(loadIndexMetaData(connection, each)); } @@ -90,7 +90,7 @@ public Collection load(final DataSource dataSource, final Collec tableMetaDataList.add(new TableMetaData(entry.getKey(), entry.getValue(), indexMetaDataMap.getOrDefault(entry.getKey(), Collections.emptyList()), Collections.emptyList())); } // TODO Load views from Oracle database. - return Collections.singletonList(new SchemaMetaData(defaultSchemaName, tableMetaDataList)); + return Collections.singletonList(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList)); } private Map> loadColumnMetaDataMap(final Connection connection, final Collection tables) throws SQLException { diff --git a/infra/database/type/oracle/src/test/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoaderTest.java b/infra/database/type/oracle/src/test/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoaderTest.java index 0b56585ef1257..004efe546bfc7 100644 --- a/infra/database/type/oracle/src/test/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoaderTest.java +++ b/infra/database/type/oracle/src/test/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoaderTest.java @@ -18,12 +18,14 @@ package org.apache.shardingsphere.infra.database.oracle.metadata.data.loader; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData; import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.database.oracle.type.OracleDatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.junit.jupiter.api.Test; @@ -86,7 +88,7 @@ void assertLoadCondition1() throws SQLException { when(dataSource.getConnection().prepareStatement(ALL_CONSTRAINTS_SQL_WITHOUT_TABLES).executeQuery()).thenReturn(primaryKeys); when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(12); when(dataSource.getConnection().getMetaData().getDatabaseMinorVersion()).thenReturn(2); - Collection actual = getDialectTableMetaDataLoader().load(dataSource, Collections.singleton("tbl"), "sharding_db"); + Collection actual = getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singleton("tbl"), dataSource, new OracleDatabaseType(), "sharding_db")); assertTableMetaDataMap(actual); TableMetaData actualTableMetaData = actual.iterator().next().getTables().iterator().next(); Iterator columnsIterator = actualTableMetaData.getColumns().iterator(); @@ -105,7 +107,7 @@ void assertLoadCondition2() throws SQLException { when(dataSource.getConnection().prepareStatement(ALL_CONSTRAINTS_SQL_WITHOUT_TABLES).executeQuery()).thenReturn(primaryKeys); when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(12); when(dataSource.getConnection().getMetaData().getDatabaseMinorVersion()).thenReturn(1); - Collection actual = getDialectTableMetaDataLoader().load(dataSource, Collections.singleton("tbl"), "sharding_db"); + Collection actual = getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singleton("tbl"), dataSource, new OracleDatabaseType(), "sharding_db")); assertTableMetaDataMap(actual); TableMetaData actualTableMetaData = actual.iterator().next().getTables().iterator().next(); Iterator columnsIterator = actualTableMetaData.getColumns().iterator(); @@ -124,7 +126,7 @@ void assertLoadCondition3() throws SQLException { when(dataSource.getConnection().prepareStatement(ALL_CONSTRAINTS_SQL_WITHOUT_TABLES).executeQuery()).thenReturn(primaryKeys); when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(11); when(dataSource.getConnection().getMetaData().getDatabaseMinorVersion()).thenReturn(2); - Collection actual = getDialectTableMetaDataLoader().load(dataSource, Collections.singleton("tbl"), "sharding_db"); + Collection actual = getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singleton("tbl"), dataSource, new OracleDatabaseType(), "sharding_db")); assertTableMetaDataMap(actual); TableMetaData actualTableMetaData = actual.iterator().next().getTables().iterator().next(); Iterator columnsIterator = actualTableMetaData.getColumns().iterator(); @@ -144,7 +146,7 @@ void assertLoadCondition4() throws SQLException { when(dataSource.getConnection().prepareStatement(ALL_CONSTRAINTS_SQL_WITH_TABLES).executeQuery()).thenReturn(primaryKeys); when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(12); when(dataSource.getConnection().getMetaData().getDatabaseMinorVersion()).thenReturn(2); - Collection actual = getDialectTableMetaDataLoader().load(dataSource, Collections.singleton("tbl"), "sharding_db"); + Collection actual = getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singleton("tbl"), dataSource, new OracleDatabaseType(), "sharding_db")); assertTableMetaDataMap(actual); TableMetaData actualTableMetaData = actual.iterator().next().getTables().iterator().next(); Iterator columnsIterator = actualTableMetaData.getColumns().iterator(); @@ -164,7 +166,7 @@ void assertLoadCondition5() throws SQLException { when(dataSource.getConnection().prepareStatement(ALL_CONSTRAINTS_SQL_WITH_TABLES).executeQuery()).thenReturn(primaryKeys); when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(12); when(dataSource.getConnection().getMetaData().getDatabaseMinorVersion()).thenReturn(1); - Collection actual = getDialectTableMetaDataLoader().load(dataSource, Collections.singleton("tbl"), "sharding_db"); + Collection actual = getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singleton("tbl"), dataSource, new OracleDatabaseType(), "sharding_db")); assertTableMetaDataMap(actual); TableMetaData actualTableMetaData = actual.iterator().next().getTables().iterator().next(); Iterator columnsIterator = actualTableMetaData.getColumns().iterator(); @@ -184,7 +186,7 @@ void assertLoadCondition6() throws SQLException { when(dataSource.getConnection().prepareStatement(ALL_CONSTRAINTS_SQL_WITH_TABLES).executeQuery()).thenReturn(primaryKeys); when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(11); when(dataSource.getConnection().getMetaData().getDatabaseMinorVersion()).thenReturn(2); - Collection actual = getDialectTableMetaDataLoader().load(dataSource, Collections.singleton("tbl"), "sharding_db"); + Collection actual = getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singleton("tbl"), dataSource, new OracleDatabaseType(), "sharding_db")); assertTableMetaDataMap(actual); TableMetaData actualTableMetaData = actual.iterator().next().getTables().iterator().next(); Iterator columnsIterator = actualTableMetaData.getColumns().iterator(); @@ -204,7 +206,7 @@ void assertLoadCondition7() throws SQLException { when(dataSource.getConnection().prepareStatement(ALL_CONSTRAINTS_SQL_WITH_TABLES).executeQuery()).thenReturn(primaryKeys); when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(12); when(dataSource.getConnection().getMetaData().getDatabaseMinorVersion()).thenReturn(2); - Collection actual = getDialectTableMetaDataLoader().load(dataSource, Collections.singleton("tbl"), "sharding_db"); + Collection actual = getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singleton("tbl"), dataSource, new OracleDatabaseType(), "sharding_db")); assertTableMetaDataMap(actual); TableMetaData actualTableMetaData = actual.iterator().next().getTables().iterator().next(); Iterator columnsIterator = actualTableMetaData.getColumns().iterator(); diff --git a/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/data/loader/PostgreSQLMetaDataLoader.java b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/data/loader/PostgreSQLMetaDataLoader.java index 6c987b3cf08f5..75af122c54d9d 100644 --- a/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/data/loader/PostgreSQLMetaDataLoader.java +++ b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/data/loader/PostgreSQLMetaDataLoader.java @@ -20,6 +20,7 @@ import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Multimap; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.type.SchemaMetaDataLoader; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ConstraintMetaData; @@ -30,7 +31,6 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -77,11 +77,11 @@ public final class PostgreSQLMetaDataLoader implements DialectMetaDataLoader { private static final String LOAD_FILTERED_ROLE_TABLE_GRANTS_SQL = LOAD_ALL_ROLE_TABLE_GRANTS_SQL + " WHERE table_name IN (%s)"; @Override - public Collection load(final DataSource dataSource, final Collection tables, final String defaultSchemaName) throws SQLException { - try (Connection connection = dataSource.getConnection()) { + public Collection load(final MetaDataLoaderMaterial material) throws SQLException { + try (Connection connection = material.getDataSource().getConnection()) { Collection schemaNames = SchemaMetaDataLoader.loadSchemaNames(connection, TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); Map> schemaIndexMetaDataMap = loadIndexMetaDataMap(connection, schemaNames); - Map> schemaColumnMetaDataMap = loadColumnMetaDataMap(connection, tables, schemaNames); + Map> schemaColumnMetaDataMap = loadColumnMetaDataMap(connection, material.getActualTableNames(), schemaNames); Map> schemaConstraintMetaDataMap = loadConstraintMetaDataMap(connection, schemaNames); Collection result = new LinkedList<>(); for (String each : schemaNames) { diff --git a/infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/metadata/data/loader/PostgreSQLMetaDataLoaderTest.java b/infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/metadata/data/loader/PostgreSQLMetaDataLoaderTest.java index 3b8d7960f8490..61e78b3b7cf60 100644 --- a/infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/metadata/data/loader/PostgreSQLMetaDataLoaderTest.java +++ b/infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/metadata/data/loader/PostgreSQLMetaDataLoaderTest.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.infra.database.postgresql.metadata.data.loader; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ConstraintMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; @@ -25,6 +26,7 @@ import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData; import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.database.postgresql.type.PostgreSQLDatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.junit.jupiter.api.Test; @@ -89,7 +91,7 @@ void assertLoadWithoutTables() throws SQLException { when(dataSource.getConnection().prepareStatement(BASIC_CONSTRAINT_META_DATA_SQL).executeQuery()).thenReturn(constraintResultSet); ResultSet roleTableGrantsResultSet = mockRoleTableGrantsResultSet(); when(dataSource.getConnection().prepareStatement(startsWith(LOAD_ALL_ROLE_TABLE_GRANTS_SQL)).executeQuery()).thenReturn(roleTableGrantsResultSet); - assertTableMetaDataMap(getDialectTableMetaDataLoader().load(dataSource, Collections.emptyList(), "sharding_db")); + assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.emptyList(), dataSource, new PostgreSQLDatabaseType(), "sharding_db"))); } private ResultSet mockSchemaMetaDataResultSet() throws SQLException { @@ -116,7 +118,7 @@ void assertLoadWithTables() throws SQLException { when(dataSource.getConnection().prepareStatement(BASIC_CONSTRAINT_META_DATA_SQL).executeQuery()).thenReturn(constraintResultSet); ResultSet roleTableGrantsResultSet = mockRoleTableGrantsResultSet(); when(dataSource.getConnection().prepareStatement(startsWith(LOAD_ALL_ROLE_TABLE_GRANTS_SQL)).executeQuery()).thenReturn(roleTableGrantsResultSet); - assertTableMetaDataMap(getDialectTableMetaDataLoader().load(dataSource, Collections.singletonList("tbl"), "sharding_db")); + assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), dataSource, new PostgreSQLDatabaseType(), "sharding_db"))); } private ResultSet mockRoleTableGrantsResultSet() throws SQLException { diff --git a/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoader.java b/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoader.java index 46df79e10aef4..7e9115ccd5227 100644 --- a/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoader.java +++ b/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoader.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.infra.database.sqlserver.metadata.data.loader; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData; @@ -64,17 +65,17 @@ public final class SQLServerMetaDataLoader implements DialectMetaDataLoader { private static final int HIDDEN_COLUMN_START_MAJOR_VERSION = 15; @Override - public Collection load(final DataSource dataSource, final Collection tables, final String defaultSchemaName) throws SQLException { + public Collection load(final MetaDataLoaderMaterial material) throws SQLException { Collection tableMetaDataList = new LinkedList<>(); - Map> columnMetaDataMap = loadColumnMetaDataMap(dataSource, tables); + Map> columnMetaDataMap = loadColumnMetaDataMap(material.getDataSource(), material.getActualTableNames()); if (!columnMetaDataMap.isEmpty()) { - Map> indexMetaDataMap = loadIndexMetaData(dataSource, columnMetaDataMap.keySet()); + Map> indexMetaDataMap = loadIndexMetaData(material.getDataSource(), columnMetaDataMap.keySet()); for (Entry> entry : columnMetaDataMap.entrySet()) { Collection indexMetaDataList = indexMetaDataMap.getOrDefault(entry.getKey(), Collections.emptyList()); tableMetaDataList.add(new TableMetaData(entry.getKey(), entry.getValue(), indexMetaDataList, Collections.emptyList())); } } - return Collections.singleton(new SchemaMetaData(defaultSchemaName, tableMetaDataList)); + return Collections.singleton(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList)); } private Map> loadColumnMetaDataMap(final DataSource dataSource, final Collection tables) throws SQLException { diff --git a/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoaderTest.java b/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoaderTest.java index 752d9eb05bd10..8fd6d30639687 100644 --- a/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoaderTest.java +++ b/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoaderTest.java @@ -18,12 +18,14 @@ package org.apache.shardingsphere.infra.database.sqlserver.metadata.data.loader; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader; +import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData; import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData; import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.database.sqlserver.type.SQLServerDatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.junit.jupiter.api.Test; @@ -87,7 +89,7 @@ void assertLoadWithoutTablesWithHighVersion() throws SQLException { when(dataSource.getConnection().prepareStatement(LOAD_INDEX_META_DATA) .executeQuery()).thenReturn(indexResultSet); when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(15); - Collection actual = getDialectTableMetaDataLoader().load(dataSource, Collections.emptyList(), "sharding_db"); + Collection actual = getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.emptyList(), dataSource, new SQLServerDatabaseType(), "sharding_db")); assertTableMetaDataMap(actual); TableMetaData actualTableMetaData = actual.iterator().next().getTables().iterator().next(); Iterator columnsIterator = actualTableMetaData.getColumns().iterator(); @@ -105,7 +107,7 @@ void assertLoadWithoutTablesWithLowVersion() throws SQLException { when(dataSource.getConnection().prepareStatement(LOAD_INDEX_META_DATA) .executeQuery()).thenReturn(indexResultSet); when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(14); - Collection actual = getDialectTableMetaDataLoader().load(dataSource, Collections.emptyList(), "sharding_db"); + Collection actual = getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.emptyList(), dataSource, new SQLServerDatabaseType(), "sharding_db")); assertTableMetaDataMap(actual); TableMetaData actualTableMetaData = actual.iterator().next().getTables().iterator().next(); Iterator columnsIterator = actualTableMetaData.getColumns().iterator(); @@ -122,7 +124,7 @@ void assertLoadWithTablesWithHighVersion() throws SQLException { when(dataSource.getConnection().prepareStatement(LOAD_INDEX_META_DATA) .executeQuery()).thenReturn(indexResultSet); when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(15); - Collection actual = getDialectTableMetaDataLoader().load(dataSource, Collections.singletonList("tbl"), "sharding_db"); + Collection actual = getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), dataSource, new SQLServerDatabaseType(), "sharding_db")); assertTableMetaDataMap(actual); TableMetaData actualTableMetaData = actual.iterator().next().getTables().iterator().next(); Iterator columnsIterator = actualTableMetaData.getColumns().iterator(); @@ -139,7 +141,7 @@ void assertLoadWithTablesWithLowVersion() throws SQLException { when(dataSource.getConnection().prepareStatement(LOAD_INDEX_META_DATA) .executeQuery()).thenReturn(indexResultSet); when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(14); - Collection actual = getDialectTableMetaDataLoader().load(dataSource, Collections.singletonList("tbl"), "sharding_db"); + Collection actual = getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), dataSource, new SQLServerDatabaseType(), "sharding_db")); assertTableMetaDataMap(actual); TableMetaData actualTableMetaData = actual.iterator().next().getTables().iterator().next(); Iterator columnsIterator = actualTableMetaData.getColumns().iterator();