This is an automated email from the ASF dual-hosted git repository.

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new bec2550f1a9 Replace `LogicalTableResultSet` with 
`ShowLogicalTableExecutor` (#23778)
bec2550f1a9 is described below

commit bec2550f1a97c488aac4832c51620d5f3016f5d0
Author: Zichao <[email protected]>
AuthorDate: Sat Jan 28 21:53:35 2023 +1300

    Replace `LogicalTableResultSet` with `ShowLogicalTableExecutor` (#23778)
---
 ...esultSet.java => ShowLogicalTableExecutor.java} | 37 ++++++++--------------
 ...hardingsphere.distsql.handler.query.RQLExecutor |  1 +
 ...here.distsql.handler.resultset.DistSQLResultSet |  1 -
 ...Test.java => ShowLogicalTableExecutorTest.java} | 37 ++++++++++++++++------
 4 files changed, 43 insertions(+), 33 deletions(-)

diff --git 
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/LogicalTableResultSet.java
 
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowLogicalTableExecutor.java
similarity index 69%
rename from 
proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/LogicalTableResultSet.java
rename to 
proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowLogicalTableExecutor.java
index df24818a2cd..0bc197e2740 100644
--- 
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/LogicalTableResultSet.java
+++ 
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowLogicalTableExecutor.java
@@ -17,39 +17,41 @@
 
 package org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule;
 
-import 
org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
 import 
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowLogicalTablesStatement;
 import 
org.apache.shardingsphere.infra.database.type.SchemaSupportedDatabaseType;
+import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.proxy.backend.util.RegularUtil;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
 
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.stream.Collectors;
 
 /**
- * Result set for show logical table.
+ * Show logical table executor.
  */
-public final class LogicalTableResultSet implements DatabaseDistSQLResultSet {
-    
-    private Iterator<String> data = Collections.emptyIterator();
+public final class ShowLogicalTableExecutor implements 
RQLExecutor<ShowLogicalTablesStatement> {
     
     @Override
-    public void init(final ShardingSphereDatabase database, final SQLStatement 
sqlStatement) {
+    public Collection<LocalDataQueryResultRow> getRows(final 
ShardingSphereDatabase database, final ShowLogicalTablesStatement sqlStatement) 
{
         String schemaName = database.getName();
         if (database.getProtocolType() instanceof SchemaSupportedDatabaseType) 
{
             schemaName = ((SchemaSupportedDatabaseType) 
database.getProtocolType()).getDefaultSchema();
         }
+        if (null == database.getSchema(schemaName)) {
+            return Collections.emptyList();
+        }
         Collection<String> tables = 
database.getSchema(schemaName).getAllTableNames();
-        ShowLogicalTablesStatement showLogicalTablesStatement = 
(ShowLogicalTablesStatement) sqlStatement;
-        if (showLogicalTablesStatement.getLikePattern().isPresent()) {
-            String pattern = 
SQLUtil.convertLikePatternToRegex(showLogicalTablesStatement.getLikePattern().get());
+        if (sqlStatement.getLikePattern().isPresent()) {
+            String pattern = 
SQLUtil.convertLikePatternToRegex(sqlStatement.getLikePattern().get());
             tables = tables.stream().filter(each -> 
RegularUtil.matchesCaseInsensitive(pattern, each)).collect(Collectors.toList());
         }
-        data = tables.iterator();
+        Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+        tables.forEach(each -> result.add(new LocalDataQueryResultRow(each)));
+        return result;
     }
     
     @Override
@@ -57,17 +59,6 @@ public final class LogicalTableResultSet implements 
DatabaseDistSQLResultSet {
         return Collections.singletonList("table_name");
     }
     
-    @Override
-    public boolean next() {
-        return data.hasNext();
-    }
-    
-    @Override
-    public Collection<Object> getRowData() {
-        String next = data.next();
-        return Collections.singletonList(next);
-    }
-    
     @Override
     public String getType() {
         return ShowLogicalTablesStatement.class.getName();
diff --git 
a/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
 
b/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
index da3749f1cb3..64ad1e2e869 100644
--- 
a/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
+++ 
b/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
@@ -20,3 +20,4 @@ 
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.ShowSingleTable
 
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.ShowRulesUsedStorageUnitExecutor
 
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.ShowDefaultSingleTableStorageUnitExecutor
 
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.CountSingleTableExecutor
+org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.ShowLogicalTableExecutor
diff --git 
a/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
 
b/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
index e55e745997c..7a91893abbf 100644
--- 
a/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++ 
b/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
@@ -15,5 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.LogicalTableResultSet
 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.ShowMigrationRuleResultSet
diff --git 
a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/LogicalTableResultSetTest.java
 
b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/ShowLogicalTableExecutorTest.java
similarity index 58%
rename from 
proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/LogicalTableResultSetTest.java
rename to 
proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/ShowLogicalTableExecutorTest.java
index 79a7985b437..a6e598e9bf2 100644
--- 
a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/LogicalTableResultSetTest.java
+++ 
b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/ShowLogicalTableExecutorTest.java
@@ -17,11 +17,13 @@
 
 package org.apache.shardingsphere.proxy.backend.handler.distsql.rql;
 
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
 import 
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowLogicalTablesStatement;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereSchema;
-import 
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.LogicalTableResultSet;
+import 
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.ShowLogicalTableExecutor;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -29,6 +31,8 @@ import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -36,7 +40,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
-public final class LogicalTableResultSetTest {
+public final class ShowLogicalTableExecutorTest {
     
     @Mock
     private ShardingSphereDatabase database;
@@ -52,16 +56,31 @@ public final class LogicalTableResultSetTest {
     
     @Test
     public void assertGetRowData() {
-        LogicalTableResultSet resultSet = new LogicalTableResultSet();
-        resultSet.init(database, mock(ShowLogicalTablesStatement.class));
-        assertThat(resultSet.getRowData().iterator().next(), is("t_order"));
-        assertThat(resultSet.getRowData().iterator().next(), 
is("t_order_item"));
+        RQLExecutor<ShowLogicalTablesStatement> executor = new 
ShowLogicalTableExecutor();
+        Collection<LocalDataQueryResultRow> actual = 
executor.getRows(database, mock(ShowLogicalTablesStatement.class));
+        assertThat(actual.size(), is(2));
+        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+        LocalDataQueryResultRow row = iterator.next();
+        assertThat(row.getCell(1), is("t_order"));
+        row = iterator.next();
+        assertThat(row.getCell(1), is("t_order_item"));
     }
     
     @Test
     public void assertRowDataWithLike() {
-        LogicalTableResultSet resultSet = new LogicalTableResultSet();
-        resultSet.init(database, new ShowLogicalTablesStatement("t_order_%", 
null));
-        assertThat(resultSet.getRowData().iterator().next(), 
is("t_order_item"));
+        RQLExecutor<ShowLogicalTablesStatement> executor = new 
ShowLogicalTableExecutor();
+        Collection<LocalDataQueryResultRow> actual = 
executor.getRows(database, new ShowLogicalTablesStatement("t_order_%", null));
+        assertThat(actual.size(), is(1));
+        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+        assertThat(iterator.next().getCell(1), is("t_order_item"));
+    }
+    
+    @Test
+    public void assertGetColumnNames() {
+        RQLExecutor<ShowLogicalTablesStatement> executor = new 
ShowLogicalTableExecutor();
+        Collection<String> columns = executor.getColumnNames();
+        assertThat(columns.size(), is(1));
+        Iterator<String> iterator = columns.iterator();
+        assertThat(iterator.next(), is("table_name"));
     }
 }

Reply via email to