diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutor.java index 21ab729ef858c..ba4060adcd643 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutor.java @@ -104,9 +104,9 @@ public ProxySQLExecutor(final String type, final ProxyDatabaseConnectionManager regularExecutor = new ProxyJDBCExecutor(type, databaseConnectionManager.getConnectionSession(), databaseConnector, jdbcExecutor); rawExecutor = new RawExecutor(executorEngine, connectionContext); MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts(); - String currentDatabaseName = databaseConnectionManager.getConnectionSession().getCurrentDatabaseName(); - String currentSchemaName = getSchemaName(queryContext.getSqlStatementContext(), metaDataContexts.getMetaData().getDatabase(currentDatabaseName)); - sqlFederationEngine = new SQLFederationEngine(currentDatabaseName, currentSchemaName, metaDataContexts.getMetaData(), metaDataContexts.getStatistics(), jdbcExecutor); + String usedDatabaseName = databaseConnectionManager.getConnectionSession().getUsedDatabaseName(); + String usedSchemaName = getSchemaName(queryContext.getSqlStatementContext(), metaDataContexts.getMetaData().getDatabase(usedDatabaseName)); + sqlFederationEngine = new SQLFederationEngine(usedDatabaseName, usedSchemaName, metaDataContexts.getMetaData(), metaDataContexts.getStatistics(), jdbcExecutor); } private String getSchemaName(final SQLStatementContext sqlStatementContext, final ShardingSphereDatabase database) { diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java index c7258522a90a9..636c0ccdc8d8b 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java @@ -21,7 +21,6 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; -import org.apache.shardingsphere.infra.binder.context.type.TableAvailable; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.executor.sql.prepare.driver.ExecutorStatementManager; import org.apache.shardingsphere.infra.metadata.user.Grantee; @@ -120,12 +119,7 @@ public ConnectionContext getConnectionContext() { * @return used database name */ public String getUsedDatabaseName() { - if (null == queryContext) { - return currentDatabaseName; - } - return queryContext.getSqlStatementContext() instanceof TableAvailable - ? ((TableAvailable) queryContext.getSqlStatementContext()).getTablesContext().getDatabaseName().orElse(currentDatabaseName) - : currentDatabaseName; + return null == queryContext ? currentDatabaseName : queryContext.getUsedDatabaseName(); } /** diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java index 1d0d8bea79a48..42b253c4183ad 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java @@ -58,7 +58,7 @@ class DatabaseConnectorFactoryTest { @Test void assertNewDatabaseConnectorWithoutParameter() { ProxyDatabaseConnectionManager databaseConnectionManager = mock(ProxyDatabaseConnectionManager.class, RETURNS_DEEP_STUBS); - when(databaseConnectionManager.getConnectionSession().getCurrentDatabaseName()).thenReturn("foo_db"); + when(databaseConnectionManager.getConnectionSession().getUsedDatabaseName()).thenReturn("foo_db"); SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, RETURNS_DEEP_STUBS); when(sqlStatementContext.getDatabaseType()).thenReturn(databaseType); ShardingSphereDatabase database = mockDatabase(); @@ -81,7 +81,7 @@ private ConnectionContext mockConnectionContext() { @Test void assertNewDatabaseConnectorWithParameters() { ProxyDatabaseConnectionManager databaseConnectionManager = mock(ProxyDatabaseConnectionManager.class, RETURNS_DEEP_STUBS); - when(databaseConnectionManager.getConnectionSession().getCurrentDatabaseName()).thenReturn("foo_db"); + when(databaseConnectionManager.getConnectionSession().getUsedDatabaseName()).thenReturn("foo_db"); SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, RETURNS_DEEP_STUBS); when(sqlStatementContext.getDatabaseType()).thenReturn(databaseType); ShardingSphereDatabase database = mockDatabase(); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorTest.java index 876f7fd3448a8..13d5e36690c48 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorTest.java @@ -115,6 +115,7 @@ class DatabaseConnectorTest { @BeforeEach void setUp() { when(databaseConnectionManager.getConnectionSession().getCurrentDatabaseName()).thenReturn("foo_db"); + when(databaseConnectionManager.getConnectionSession().getUsedDatabaseName()).thenReturn("foo_db"); ContextManager contextManager = mockContextManager(); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); }