Skip to content

Commit

Permalink
Refactor ShowCreateTableMergedResult (#33716)
Browse files Browse the repository at this point in the history
* Refactor LogicTablesMergedResult

* Refactor ShowCreateTableMergedResult

* Refactor ShowCreateTableMergedResult

* Refactor ShowCreateTableMergedResult
  • Loading branch information
terrymanu authored Nov 18, 2024
1 parent 4f87279 commit 969dbf9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,42 @@
*/
public class LogicTablesMergedResult extends MemoryMergedResult<ShardingRule> {

public LogicTablesMergedResult(final ShardingRule shardingRule,
public LogicTablesMergedResult(final ShardingRule rule,
final SQLStatementContext sqlStatementContext, final ShardingSphereSchema schema, final List<QueryResult> queryResults) throws SQLException {
super(shardingRule, schema, sqlStatementContext, queryResults);
super(rule, schema, sqlStatementContext, queryResults);
}

@Override
protected final List<MemoryQueryResultRow> init(final ShardingRule shardingRule, final ShardingSphereSchema schema,
protected final List<MemoryQueryResultRow> init(final ShardingRule rule, final ShardingSphereSchema schema,
final SQLStatementContext sqlStatementContext, final List<QueryResult> queryResults) throws SQLException {
List<MemoryQueryResultRow> result = new LinkedList<>();
Set<String> tableNames = new HashSet<>();
for (QueryResult each : queryResults) {
while (each.next()) {
createMemoryQueryResultRow(shardingRule, schema, each, tableNames).ifPresent(result::add);
createMemoryQueryResultRow(rule, schema, each, tableNames).ifPresent(result::add);
}
}
return result;
}

private Optional<MemoryQueryResultRow> createMemoryQueryResultRow(final ShardingRule shardingRule,
private Optional<MemoryQueryResultRow> createMemoryQueryResultRow(final ShardingRule rule,
final ShardingSphereSchema schema, final QueryResult queryResult, final Set<String> tableNames) throws SQLException {
MemoryQueryResultRow memoryResultSetRow = new MemoryQueryResultRow(queryResult);
String actualTableName = memoryResultSetRow.getCell(1).toString();
Optional<ShardingTable> shardingTable = shardingRule.findShardingTableByActualTable(actualTableName);
Optional<ShardingTable> shardingTable = rule.findShardingTableByActualTable(actualTableName);
if (shardingTable.isPresent() && tableNames.add(shardingTable.get().getLogicTable())) {
String logicTableName = shardingTable.get().getLogicTable();
memoryResultSetRow.setCell(1, logicTableName);
setCellValue(memoryResultSetRow, logicTableName, actualTableName, schema.getTable(logicTableName), shardingRule);
setCellValue(memoryResultSetRow, logicTableName, actualTableName, schema.getTable(logicTableName), rule);
return Optional.of(memoryResultSetRow);
}
if (shardingRule.getShardingTables().isEmpty() || tableNames.add(actualTableName)) {
setCellValue(memoryResultSetRow, actualTableName, actualTableName, schema.getTable(actualTableName), shardingRule);
if (rule.getShardingTables().isEmpty() || tableNames.add(actualTableName)) {
setCellValue(memoryResultSetRow, actualTableName, actualTableName, schema.getTable(actualTableName), rule);
return Optional.of(memoryResultSetRow);
}
return Optional.empty();
}

protected void setCellValue(final MemoryQueryResultRow memoryResultSetRow,
final String logicTableName, final String actualTableName, final ShardingSphereTable table, final ShardingRule shardingRule) {
protected void setCellValue(final MemoryQueryResultRow memoryResultSetRow, final String logicTableName, final String actualTableName, final ShardingSphereTable table, final ShardingRule rule) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,53 @@
*/
public final class ShowCreateTableMergedResult extends LogicTablesMergedResult {

public ShowCreateTableMergedResult(final ShardingRule shardingRule,
public ShowCreateTableMergedResult(final ShardingRule rule,
final SQLStatementContext sqlStatementContext, final ShardingSphereSchema schema, final List<QueryResult> queryResults) throws SQLException {
super(shardingRule, sqlStatementContext, schema, queryResults);
super(rule, sqlStatementContext, schema, queryResults);
}

@Override
protected void setCellValue(final MemoryQueryResultRow memoryResultSetRow, final String logicTableName, final String actualTableName,
final ShardingSphereTable table, final ShardingRule shardingRule) {
final ShardingSphereTable table, final ShardingRule rule) {
replaceTables(memoryResultSetRow, logicTableName, actualTableName);
replaceBindingTables(memoryResultSetRow, logicTableName, actualTableName, rule);
replaceIndexes(memoryResultSetRow, actualTableName, table);
replaceConstraints(memoryResultSetRow, actualTableName, table, rule);
}

private void replaceTables(final MemoryQueryResultRow memoryResultSetRow, final String logicTableName, final String actualTableName) {
memoryResultSetRow.setCell(2, memoryResultSetRow.getCell(2).toString().replaceFirst(actualTableName, logicTableName));
setViewCellValue(memoryResultSetRow, logicTableName, actualTableName, shardingRule);
}

private void replaceBindingTables(final MemoryQueryResultRow memoryResultSetRow, final String logicTableName, final String actualTableName, final ShardingRule rule) {
Optional<ShardingTable> shardingTable = rule.findShardingTable(logicTableName);
Optional<BindingTableRule> bindingTableRule = rule.findBindingTableRule(logicTableName);
if (!shardingTable.isPresent() || !bindingTableRule.isPresent()) {
return;
}
Collection<DataNode> actualDataNodes = shardingTable.get().getActualDataNodes().stream().filter(each -> each.getTableName().equalsIgnoreCase(actualTableName)).collect(Collectors.toList());
Map<String, String> logicAndActualTablesFromBindingTables = new CaseInsensitiveMap<>();
for (DataNode each : actualDataNodes) {
logicAndActualTablesFromBindingTables
.putAll(rule.getLogicAndActualTablesFromBindingTable(each.getDataSourceName(), logicTableName, actualTableName, bindingTableRule.get().getAllLogicTables()));
}
for (Entry<String, String> entry : logicAndActualTablesFromBindingTables.entrySet()) {
memoryResultSetRow.setCell(2, memoryResultSetRow.getCell(2).toString().replaceFirst(entry.getValue(), entry.getKey()));
}
}

private void replaceIndexes(final MemoryQueryResultRow memoryResultSetRow, final String actualTableName, final ShardingSphereTable table) {
for (ShardingSphereIndex each : table.getIndexValues()) {
String actualIndexName = IndexMetaDataUtils.getActualIndexName(each.getName(), actualTableName);
memoryResultSetRow.setCell(2, memoryResultSetRow.getCell(2).toString().replace(actualIndexName, each.getName()));
}
}

private void replaceConstraints(final MemoryQueryResultRow memoryResultSetRow, final String actualTableName, final ShardingSphereTable table, final ShardingRule rule) {
for (ShardingSphereConstraint each : table.getConstraintValues()) {
String actualIndexName = IndexMetaDataUtils.getActualIndexName(each.getName(), actualTableName);
memoryResultSetRow.setCell(2, memoryResultSetRow.getCell(2).toString().replace(actualIndexName, each.getName()));
Optional<ShardingTable> shardingTable = shardingRule.findShardingTable(each.getReferencedTableName());
Optional<ShardingTable> shardingTable = rule.findShardingTable(each.getReferencedTableName());
if (!shardingTable.isPresent()) {
continue;
}
Expand All @@ -70,20 +99,4 @@ protected void setCellValue(final MemoryQueryResultRow memoryResultSetRow, final
}
}
}

private void setViewCellValue(final MemoryQueryResultRow memoryResultSetRow, final String logicTableName, final String actualTableName, final ShardingRule shardingRule) {
Optional<ShardingTable> shardingTable = shardingRule.findShardingTable(logicTableName);
Optional<BindingTableRule> bindingTableRule = shardingRule.findBindingTableRule(logicTableName);
if (shardingTable.isPresent() && bindingTableRule.isPresent()) {
Collection<DataNode> actualDataNodes = shardingTable.get().getActualDataNodes().stream().filter(each -> each.getTableName().equalsIgnoreCase(actualTableName)).collect(Collectors.toList());
Map<String, String> logicAndActualTablesFromBindingTables = new CaseInsensitiveMap<>();
for (DataNode each : actualDataNodes) {
logicAndActualTablesFromBindingTables
.putAll(shardingRule.getLogicAndActualTablesFromBindingTable(each.getDataSourceName(), logicTableName, actualTableName, bindingTableRule.get().getAllLogicTables()));
}
for (Entry<String, String> entry : logicAndActualTablesFromBindingTables.entrySet()) {
memoryResultSetRow.setCell(2, memoryResultSetRow.getCell(2).toString().replaceFirst(entry.getValue(), entry.getKey()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@

class LogicTablesMergedResultTest {

private ShardingRule shardingRule;
private ShardingRule rule;

private ShardingSphereSchema schema;

@BeforeEach
void setUp() {
shardingRule = createShardingRule();
rule = createShardingRule();
schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("table",
new ShardingSphereTable("table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyMap());
}
Expand All @@ -61,12 +61,12 @@ private ShardingRule createShardingRule() {

@Test
void assertNextForEmptyQueryResult() throws SQLException {
assertFalse(new LogicTablesMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next());
assertFalse(new LogicTablesMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next());
}

@Test
void assertNextForActualTableNameInTableRule() throws SQLException {
assertTrue(new LogicTablesMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult("table_0"))).next());
assertTrue(new LogicTablesMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult("table_0"))).next());
}

private QueryResult mockQueryResult(final String value) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@

class ShowCreateTableMergedResultTest {

private ShardingRule shardingRule;
private ShardingRule rule;

private ShardingSphereSchema schema;

@BeforeEach
void setUp() {
shardingRule = buildShardingRule();
rule = buildShardingRule();
schema = createSchema();
}

Expand All @@ -74,17 +74,17 @@ private ShardingSphereSchema createSchema() {

@Test
void assertNextForEmptyQueryResult() throws SQLException {
assertFalse(new ShowCreateTableMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next());
assertFalse(new ShowCreateTableMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next());
}

@Test
void assertNextForTableRulePresent() throws SQLException {
assertTrue(new ShowCreateTableMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())).next());
assertTrue(new ShowCreateTableMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())).next());
}

@Test
void assertGetValueForTableRulePresent() throws SQLException {
ShowCreateTableMergedResult actual = new ShowCreateTableMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult()));
ShowCreateTableMergedResult actual = new ShowCreateTableMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult()));
assertTrue(actual.next());
assertThat(actual.getValue(1, String.class), is("t_order"));
assertThat(actual.getValue(2, String.class), is("CREATE TABLE `t_order` (\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@
class ShowIndexMergedResultTest {

@Mock
private ShardingRule shardingRule;
private ShardingRule rule;

@Mock
private ShardingSphereSchema schema;

@Test
void assertNextForEmptyQueryResult() throws SQLException {
assertFalse(new ShowIndexMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next());
assertFalse(new ShowIndexMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next());
}

@Test
void assertNextForTableRuleIsPresent() throws SQLException {
assertTrue(new ShowIndexMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())).next());
assertTrue(new ShowIndexMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())).next());
}

private QueryResult mockQueryResult() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
class ShowTableStatusMergedResultTest {

@Mock
private ShardingRule shardingRule;
private ShardingRule rule;

@Mock
private ShardingSphereSchema schema;

@BeforeEach
void setUp() {
shardingRule = buildShardingRule();
rule = buildShardingRule();
schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("table",
new ShardingSphereTable("table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyMap());
}
Expand All @@ -70,12 +70,12 @@ private ShardingRule buildShardingRule() {

@Test
void assertNextForEmptyQueryResult() throws SQLException {
assertFalse(new ShowTableStatusMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next());
assertFalse(new ShowTableStatusMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next());
}

@Test
void assertNextForTableRuleIsPresent() throws SQLException {
MergedResult mergedResult = new ShowTableStatusMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult()));
MergedResult mergedResult = new ShowTableStatusMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult()));
assertTrue(mergedResult.next());
assertFalse(mergedResult.next());
}
Expand Down

0 comments on commit 969dbf9

Please sign in to comment.