This is an automated email from the ASF dual-hosted git repository.

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new c3173376f18 Fix get first database name error in 
UnicastDatabaseProxyBackendHandler (#37908)
c3173376f18 is described below

commit c3173376f18087af486ae6e6cadd0308c25ee4e1
Author: jiangML <[email protected]>
AuthorDate: Fri Jan 30 23:25:52 2026 +0800

    Fix get first database name error in UnicastDatabaseProxyBackendHandler 
(#37908)
    
    * Fix get first database name error in UnicastDatabaseProxyBackendHandler
    
    * Update
    
    * Fix test error
---
 .../data/type/UnicastDatabaseProxyBackendHandler.java      | 14 +++++++++-----
 .../data/type/UnicastDatabaseProxyBackendHandlerTest.java  | 13 +++++++++----
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/type/UnicastDatabaseProxyBackendHandler.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/type/UnicastDatabaseProxyBackendHandler.java
index d8cdfc9aaf4..e54d9285804 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/type/UnicastDatabaseProxyBackendHandler.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/type/UnicastDatabaseProxyBackendHandler.java
@@ -18,7 +18,7 @@
 package org.apache.shardingsphere.proxy.backend.handler.data.type;
 
 import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.checker.AuthorityChecker;
 import org.apache.shardingsphere.authority.rule.AuthorityRule;
 import 
org.apache.shardingsphere.database.exception.core.exception.syntax.database.NoDatabaseSelectedException;
 import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
@@ -27,6 +27,7 @@ import 
org.apache.shardingsphere.infra.session.query.QueryContext;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import 
org.apache.shardingsphere.proxy.backend.connector.DatabaseProxyConnector;
 import 
org.apache.shardingsphere.proxy.backend.connector.DatabaseProxyConnectorFactory;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import 
org.apache.shardingsphere.proxy.backend.handler.data.DatabaseProxyBackendHandler;
 import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseRow;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
@@ -35,7 +36,7 @@ import 
org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Optional;
-import java.util.stream.Stream;
+import java.util.stream.Collectors;
 
 /**
  * Unicast database proxy backend handler.
@@ -69,9 +70,12 @@ public final class UnicastDatabaseProxyBackendHandler 
implements DatabaseProxyBa
         Collection<String> databaseNames = 
contextManager.getAllDatabaseNames();
         ShardingSpherePreconditions.checkNotEmpty(databaseNames, 
NoDatabaseSelectedException::new);
         AuthorityRule authorityRule = 
queryContext.getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
-        Optional<ShardingSpherePrivileges> privileges = 
authorityRule.findPrivileges(connectionSession.getConnectionContext().getGrantee());
-        Stream<String> storageUnitContainedDatabaseNames = 
databaseNames.stream().filter(each -> 
contextManager.getDatabase(each).containsDataSource());
-        Optional<String> result = privileges.map(optional -> 
storageUnitContainedDatabaseNames.filter(optional::hasPrivileges).findFirst()).orElseGet(storageUnitContainedDatabaseNames::findFirst);
+        Collection<String> storageUnitContainedDatabaseNames = 
databaseNames.stream()
+                .filter(each -> 
ProxyContext.getInstance().getContextManager().getDatabase(each).containsDataSource())
+                .collect(Collectors.toSet());
+        AuthorityChecker authorityChecker = new 
AuthorityChecker(authorityRule, 
connectionSession.getConnectionContext().getGrantee());
+        Collection<String> authorizedDatabases = 
storageUnitContainedDatabaseNames.stream().filter(authorityChecker::isAuthorized).collect(Collectors.toSet());
+        Optional<String> result = authorizedDatabases.isEmpty() ? 
storageUnitContainedDatabaseNames.stream().findFirst() : 
authorizedDatabases.stream().findFirst();
         ShardingSpherePreconditions.checkState(result.isPresent(), 
EmptyStorageUnitException::new);
         return result.get();
     }
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/type/UnicastDatabaseProxyBackendHandlerTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/type/UnicastDatabaseProxyBackendHandlerTest.java
index d5ac667cd95..9d14eca5ffb 100644
--- 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/type/UnicastDatabaseProxyBackendHandlerTest.java
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/type/UnicastDatabaseProxyBackendHandlerTest.java
@@ -28,6 +28,7 @@ import 
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaDa
 import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
 import 
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
 import 
org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsFactory;
+import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
 import org.apache.shardingsphere.infra.session.query.QueryContext;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
@@ -36,6 +37,7 @@ import 
org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import 
org.apache.shardingsphere.proxy.backend.connector.DatabaseProxyConnector;
 import 
org.apache.shardingsphere.proxy.backend.connector.DatabaseProxyConnectorFactory;
 import 
org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
 import 
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
 import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseRow;
@@ -68,7 +70,7 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 @ExtendWith(AutoMockExtension.class)
-@StaticMockSettings(DatabaseProxyConnectorFactory.class)
+@StaticMockSettings({DatabaseProxyConnectorFactory.class, ProxyContext.class})
 @MockitoSettings(strictness = Strictness.LENIENT)
 class UnicastDatabaseProxyBackendHandlerTest {
     
@@ -130,11 +132,14 @@ class UnicastDatabaseProxyBackendHandlerTest {
     void assertExecuteWithNullCurrentDatabaseChoosesFirstAvailable() throws 
SQLException {
         ConnectionSession connectionSession = mock(ConnectionSession.class, 
RETURNS_DEEP_STUBS);
         
when(connectionSession.getConnectionContext().getCurrentDatabaseName()).thenReturn(Optional.empty());
+        ShardingSphereUser user = mock(ShardingSphereUser.class, 
RETURNS_DEEP_STUBS);
+        when(user.isAdmin()).thenReturn(true);
         AuthorityRule authorityRule = mock(AuthorityRule.class, 
RETURNS_DEEP_STUBS);
-        ShardingSpherePrivileges privileges = 
mock(ShardingSpherePrivileges.class);
-        when(privileges.hasPrivileges("bar_db")).thenReturn(true);
-        
when(authorityRule.findPrivileges(any())).thenReturn(Optional.of(privileges));
+        when(authorityRule.findUser(any())).thenReturn(Optional.of(user));
         ContextManager contextManager = 
mockContextManagerWithAuthority(authorityRule, Arrays.asList("foo_db", 
"bar_db"), Collections.singletonList("bar_db"));
+        
when(contextManager.getDatabase("foo_db").containsDataSource()).thenReturn(false);
+        
when(contextManager.getDatabase("bar_db").containsDataSource()).thenReturn(true);
+        
when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
         
when(DatabaseProxyConnectorFactory.newInstance(any(QueryContext.class), 
any(ProxyDatabaseConnectionManager.class), eq(false)))
                 .thenReturn(mock(DatabaseProxyConnector.class, 
RETURNS_DEEP_STUBS));
         QueryContext queryContext = new 
QueryContext(mock(SQLStatementContext.class, RETURNS_DEEP_STUBS), EXECUTE_SQL, 
Collections.emptyList(), new HintValueContext(),

Reply via email to