diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutor.java index 5d0c7135c5bfb..e818a8941bc7b 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutor.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutor.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.single.distsql.handler.query; -import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor; +import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor; import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -28,9 +28,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Comparator; -import java.util.LinkedList; import java.util.Map; -import java.util.Map.Entry; import java.util.Optional; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -38,7 +36,11 @@ /** * Show single table executor. */ -public final class ShowSingleTableExecutor implements RQLExecutor { +public final class ShowSingleTableExecutor extends RuleAwareRQLExecutor { + + public ShowSingleTableExecutor() { + super(SingleRule.class); + } @Override public Collection getColumnNames() { @@ -46,45 +48,28 @@ public Collection getColumnNames() { } @Override - public Collection getRows(final ShardingSphereDatabase database, final ShowSingleTableStatement sqlStatement) { - SingleRule singleRule = database.getRuleMetaData().getSingleRule(SingleRule.class); - Map> singleTableNodes = singleRule.getSingleTableDataNodes(); - return getRows(singleTableNodes, sqlStatement); - } - - private Collection getRows(final Map> singleTableNodes, final ShowSingleTableStatement sqlStatement) { - Optional pattern = getPattern(sqlStatement); - Collection resultDataNodes = pattern.map(optional -> getDataNodesWithLikePattern(singleTableNodes, optional)).orElseGet(() -> getDataNodes(singleTableNodes, sqlStatement)); + public Collection getRows(final ShardingSphereDatabase database, final ShowSingleTableStatement sqlStatement, final SingleRule rule) { + Collection resultDataNodes = getPattern(sqlStatement) + .map(optional -> getDataNodesWithLikePattern(rule.getSingleTableDataNodes(), optional)).orElseGet(() -> getDataNodes(rule.getSingleTableDataNodes(), sqlStatement)); Collection sortedDataNodes = resultDataNodes.stream().sorted(Comparator.comparing(DataNode::getTableName)).collect(Collectors.toList()); return sortedDataNodes.stream().map(each -> new LocalDataQueryResultRow(each.getTableName(), each.getDataSourceName())).collect(Collectors.toList()); } - private Collection getDataNodesWithLikePattern(final Map> singleTableNodes, final Pattern pattern) { - Collection result = new LinkedList<>(); - for (final Entry> entry : singleTableNodes.entrySet()) { - if (pattern.matcher(entry.getKey()).matches()) { - result.add(entry.getValue().iterator().next()); - } - } - return result; - } - - private Collection getDataNodes(final Map> singleTableNodes, final ShowSingleTableStatement sqlStatement) { - Collection result = new LinkedList<>(); - for (final Entry> entry : singleTableNodes.entrySet()) { - if (!sqlStatement.getTableName().isPresent() || sqlStatement.getTableName().get().equalsIgnoreCase(entry.getKey())) { - result.add(entry.getValue().iterator().next()); - } - } - return result; - } - private Optional getPattern(final ShowSingleTableStatement sqlStatement) { return sqlStatement.getLikePattern().isPresent() ? Optional.of(Pattern.compile(SQLUtils.convertLikePatternToRegex(sqlStatement.getLikePattern().get()), Pattern.CASE_INSENSITIVE)) : Optional.empty(); } + private Collection getDataNodesWithLikePattern(final Map> singleTableNodes, final Pattern pattern) { + return singleTableNodes.entrySet().stream().filter(entry -> pattern.matcher(entry.getKey()).matches()).map(entry -> entry.getValue().iterator().next()).collect(Collectors.toList()); + } + + private Collection getDataNodes(final Map> singleTableNodes, final ShowSingleTableStatement sqlStatement) { + return singleTableNodes.entrySet().stream().filter(entry -> !sqlStatement.getTableName().isPresent() || sqlStatement.getTableName().get().equalsIgnoreCase(entry.getKey())) + .map(entry -> entry.getValue().iterator().next()).collect(Collectors.toList()); + } + @Override public Class getType() { return ShowSingleTableStatement.class;