Skip to content

Commit

Permalink
Optimize ShardingSphereDatabaseMetaData getActualSchema and getActual…
Browse files Browse the repository at this point in the history
…Catalog logic (#29660)
  • Loading branch information
tuichenchuxin authored Jan 5, 2024
1 parent 645c63b commit 99a5340
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 99a5340

Please sign in to comment.