From 99a53400f5fa0fea38e3adce6193005a6010a98a Mon Sep 17 00:00:00 2001 From: Chuxin Chen Date: Fri, 5 Jan 2024 15:57:41 +0800 Subject: [PATCH] Optimize ShardingSphereDatabaseMetaData getActualSchema and getActualCatalog logic (#29660) --- .../metadata/ShardingSphereDatabaseMetaData.java | 13 ++++++++++--- .../ShardingSphereDatabaseMetaDataTest.java | 5 +++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaData.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaData.java index aa13f33ccd604..29f37c764f3c1 100644 --- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaData.java +++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaData.java @@ -21,7 +21,6 @@ import org.apache.shardingsphere.driver.jdbc.adapter.AdaptedDatabaseMetaData; import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection; import org.apache.shardingsphere.driver.jdbc.core.resultset.DatabaseMetaDataResultSet; -import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule; @@ -222,15 +221,23 @@ private ResultSet createDatabaseMetaDataResultSet(final ResultSet resultSet) thr } private String getActualCatalog(final String catalog) { + if (null == catalog) { + return null; + } + // TODO consider get actual catalog by logic catalog rather than random physical datasource's catalog. ConnectionProperties connectionProps = connection.getContextManager() .getMetaDataContexts().getMetaData().getDatabase(connection.getDatabaseName()).getResourceMetaData().getStorageUnits().get(getDataSourceName()).getConnectionProperties(); - return null == catalog || !catalog.contains(DefaultDatabase.LOGIC_NAME) ? catalog : connectionProps.getCatalog(); + return connectionProps.getCatalog(); } private String getActualSchema(final String schema) { + if (null == schema) { + return null; + } + // TODO consider get actual schema by logic catalog rather than random physical datasource's schema. ConnectionProperties connectionProps = connection.getContextManager() .getMetaDataContexts().getMetaData().getDatabase(connection.getDatabaseName()).getResourceMetaData().getStorageUnits().get(getDataSourceName()).getConnectionProperties(); - return null == schema || !schema.contains(DefaultDatabase.LOGIC_NAME) ? schema : connectionProps.getSchema(); + return Optional.ofNullable(connectionProps.getSchema()).map(String::toUpperCase).orElse(null); } private String getDataSourceName() { diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaDataTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaDataTest.java index 48522a9d5be3a..30ebcad91a72e 100644 --- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaDataTest.java +++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaDataTest.java @@ -21,6 +21,7 @@ import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection; import org.apache.shardingsphere.driver.jdbc.core.resultset.DatabaseMetaDataResultSet; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; @@ -95,6 +96,10 @@ void setUp() throws SQLException { when(metaDataContexts.getMetaData().getDatabase(shardingSphereConnection.getDatabaseName())).thenReturn(database); ShardingRule shardingRule = mockShardingRule(); when(database.getRuleMetaData().getRules()).thenReturn(Collections.singleton(shardingRule)); + ConnectionProperties connectionProperties = mock(ConnectionProperties.class); + when(connectionProperties.getCatalog()).thenReturn("test"); + when(connectionProperties.getSchema()).thenReturn("test"); + when(database.getResourceMetaData().getStorageUnits().get(DATA_SOURCE_NAME).getConnectionProperties()).thenReturn(connectionProperties); shardingSphereDatabaseMetaData = new ShardingSphereDatabaseMetaData(shardingSphereConnection); }