|
73 | 73 | import org.junit.jupiter.api.Test; |
74 | 74 | import org.junit.jupiter.api.extension.ExtendWith; |
75 | 75 |
|
| 76 | +import java.util.Arrays; |
76 | 77 | import java.util.Collection; |
77 | 78 | import java.util.Collections; |
78 | 79 | import java.util.Optional; |
@@ -405,4 +406,49 @@ void assertCreateWithDMLStatement() { |
405 | 406 | Optional<DatabaseAdminExecutor> actual = new MySQLAdminExecutorCreator().create(sqlStatementContext, "DELETE FROM t", "", Collections.emptyList()); |
406 | 407 | assertThat(actual, is(Optional.empty())); |
407 | 408 | } |
| 409 | + |
| 410 | + @Test |
| 411 | + void assertCreateWithNoFromAndMultiProjectionsSkipsAdmin() { |
| 412 | + ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds_0", new MockedDataSource())); |
| 413 | + ShardingSphereDatabase database = new ShardingSphereDatabase("db_0", databaseType, resourceMetaData, mock(RuleMetaData.class), Collections.emptyList()); |
| 414 | + initProxyContext(Collections.singleton(database)); |
| 415 | + |
| 416 | + SelectStatement selectStatement = mock(SelectStatement.class); |
| 417 | + when(selectStatement.getFrom()).thenReturn(Optional.empty()); |
| 418 | + ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class); |
| 419 | + when(projectionsSegment.getProjections()).thenReturn(Arrays.asList( |
| 420 | + new ExpressionProjectionSegment(0, 10, "database()"), |
| 421 | + new ExpressionProjectionSegment(0, 10, "schema()"), |
| 422 | + new ExpressionProjectionSegment(0, 10, "left(user(),instr(concat(user(),'@'),'@')-1)"))); |
| 423 | + when(selectStatement.getProjections()).thenReturn(projectionsSegment); |
| 424 | + SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class); |
| 425 | + when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement); |
| 426 | + |
| 427 | + Optional<DatabaseAdminExecutor> actual = |
| 428 | + new MySQLAdminExecutorCreator().create(sqlStatementContext, "SELECT database(),schema(),left(user(),instr(concat(user(),'@'),'@')-1)", null, Collections.emptyList()); |
| 429 | + assertThat(actual, is(Optional.empty())); |
| 430 | + } |
| 431 | + |
| 432 | + @Test |
| 433 | + void assertCreateWithMultiSystemVariablesUseSysVarExecutor() { |
| 434 | + initProxyContext(Collections.emptyList()); |
| 435 | + SelectStatement selectStatement = mock(SelectStatement.class); |
| 436 | + when(selectStatement.getFrom()).thenReturn(Optional.empty()); |
| 437 | + ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class); |
| 438 | + VariableSegment v1 = new VariableSegment(0, 0, "version", "SESSION"); |
| 439 | + VariableSegment v2 = new VariableSegment(0, 0, "transaction_isolation", "SESSION"); |
| 440 | + when(projectionsSegment.getProjections()).thenReturn(Arrays.asList( |
| 441 | + new ExpressionProjectionSegment(0, 10, "@@session.version", v1), |
| 442 | + new ExpressionProjectionSegment(0, 10, "@@session.transaction_isolation", v2) |
| 443 | + )); |
| 444 | + when(selectStatement.getProjections()).thenReturn(projectionsSegment); |
| 445 | + |
| 446 | + SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class); |
| 447 | + when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement); |
| 448 | + |
| 449 | + Optional<DatabaseAdminExecutor> actual = new MySQLAdminExecutorCreator().create( |
| 450 | + sqlStatementContext, "SELECT @@session.version, @@session.transaction_isolation", null, Collections.emptyList()); |
| 451 | + assertTrue(actual.isPresent()); |
| 452 | + assertThat(actual.get(), isA(MySQLSystemVariableQueryExecutor.class)); |
| 453 | + } |
408 | 454 | } |
0 commit comments