Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,17 @@ private static Optional<DatabaseAdminExecutor> mockExecutor(final SelectStatemen
return Optional.of(new NoResourceShowExecutor(sqlStatement));
}
boolean isUseDatabase = null != databaseName || sqlStatement.getFrom().isPresent();
if (!isUseDatabase && hasMultipleProjections(sqlStatement)) {
return Optional.empty();
}
return isUseDatabase ? Optional.empty() : Optional.of(new UnicastResourceShowExecutor(sqlStatement, sql));
}

private static boolean hasMultipleProjections(final SelectStatement sqlStatement) {
Collection<ProjectionSegment> projections = sqlStatement.getProjections().getProjections();
return projections.size() > 1;
}

private static boolean isEmptyResource(final ShardingSphereMetaData metaData) {
Collection<ShardingSphereDatabase> databases = metaData.getAllDatabases();
return databases.isEmpty() || databases.stream().noneMatch(ShardingSphereDatabase::containsDataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
Expand Down Expand Up @@ -405,4 +406,48 @@ void assertCreateWithDMLStatement() {
Optional<DatabaseAdminExecutor> actual = new MySQLAdminExecutorCreator().create(sqlStatementContext, "DELETE FROM t", "", Collections.emptyList());
assertThat(actual, is(Optional.empty()));
}

@Test
void assertCreateWithNoFromAndMultiProjectionsSkipsAdmin() {
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds_0", new MockedDataSource()));
ShardingSphereDatabase database = new ShardingSphereDatabase("db_0", databaseType, resourceMetaData, mock(RuleMetaData.class), Collections.emptyList());
initProxyContext(Collections.singleton(database));

SelectStatement selectStatement = mock(SelectStatement.class);
when(selectStatement.getFrom()).thenReturn(Optional.empty());
ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class);
when(projectionsSegment.getProjections()).thenReturn(Arrays.asList(
new ExpressionProjectionSegment(0, 10, "database()"),
new ExpressionProjectionSegment(0, 10, "schema()"),
new ExpressionProjectionSegment(0, 10, "left(user(),instr(concat(user(),'@'),'@')-1)")));
when(selectStatement.getProjections()).thenReturn(projectionsSegment);
SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class);
when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement);

Optional<DatabaseAdminExecutor> actual =
new MySQLAdminExecutorCreator().create(sqlStatementContext, "SELECT database(),schema(),left(user(),instr(concat(user(),'@'),'@')-1)", null, Collections.emptyList());
assertThat(actual, is(Optional.empty()));
}

@Test
void assertCreateWithMultiSystemVariablesUseSysVarExecutor() {
initProxyContext(Collections.emptyList());
SelectStatement selectStatement = mock(SelectStatement.class);
when(selectStatement.getFrom()).thenReturn(Optional.empty());
ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class);
VariableSegment v1 = new VariableSegment(0, 0, "version", "SESSION");
VariableSegment v2 = new VariableSegment(0, 0, "transaction_isolation", "SESSION");
when(projectionsSegment.getProjections()).thenReturn(Arrays.asList(
new ExpressionProjectionSegment(0, 10, "@@session.version", v1),
new ExpressionProjectionSegment(0, 10, "@@session.transaction_isolation", v2)));
when(selectStatement.getProjections()).thenReturn(projectionsSegment);

SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class);
when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement);

Optional<DatabaseAdminExecutor> actual = new MySQLAdminExecutorCreator().create(
sqlStatementContext, "SELECT @@session.version, @@session.transaction_isolation", null, Collections.emptyList());
assertTrue(actual.isPresent());
assertThat(actual.get(), isA(MySQLSystemVariableQueryExecutor.class));
}
}