Skip to content

Commit

Permalink
Fix NPE when no database is specified(apache#32637) (apache#32639)
Browse files Browse the repository at this point in the history
* Fix NPE when no database is specified

* Fix no database selected exception in the query sql

* Fix no database selected exception in the query sql
  • Loading branch information
FlyingZC authored Aug 23, 2024
1 parent cea4dd2 commit 0af0b6b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 0af0b6b

Please sign in to comment.