You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/08/18 10:30:46 UTC

[shardingsphere] branch master updated: Sharding proxy support show tables (#6916)

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

zhangliang 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 6c6d131  Sharding proxy support show tables (#6916)
6c6d131 is described below

commit 6c6d13100cac46e833008cdbb44ee05539def3ec
Author: Juan Pan(Trista) <pa...@apache.org>
AuthorDate: Tue Aug 18 18:30:32 2020 +0800

    Sharding proxy support show tables (#6916)
    
    * sharding proxy support show tables
    
    * add test
    
    * rename function
---
 .../kernel/context/SchemaContext.java              |   9 ++
 .../text/TextProtocolBackendHandlerFactory.java    |   5 +
 .../ShowTablesBackendHandler.java}                 |  24 +++--
 .../backend/text/query/QueryBackendHandler.java    |  13 ++-
 .../text/admin/ShowTablesBackendHandlerTest.java   | 101 +++++++++++++++++++++
 5 files changed, 143 insertions(+), 9 deletions(-)

diff --git a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContext.java b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContext.java
index 6b6f557..f441b6d 100644
--- a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContext.java
+++ b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContext.java
@@ -38,4 +38,13 @@ public final class SchemaContext {
         this.schema = schema;
         this.runtimeContext = runtimeContext;
     }
+    
+    /**
+     * Is complete schema Context.
+     *
+     * @return is complete schema Context or not
+     */
+    public boolean isComplete() {
+        return null != schema && null != runtimeContext && !schema.getRules().isEmpty() && !schema.getDataSources().isEmpty();
+    }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
index dd6e2cd..31d2644 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
@@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
 import org.apache.shardingsphere.proxy.backend.text.admin.BroadcastBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.admin.ShowDatabasesBackendHandler;
+import org.apache.shardingsphere.proxy.backend.text.admin.ShowTablesBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.admin.UnicastBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.admin.UseDatabaseBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.query.QueryBackendHandler;
@@ -36,6 +37,7 @@ import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
 import org.apache.shardingsphere.sql.parser.sql.statement.dal.DALStatement;
 import org.apache.shardingsphere.sql.parser.sql.statement.dal.SetStatement;
 import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowDatabasesStatement;
+import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowTablesStatement;
 import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.UseStatement;
 import org.apache.shardingsphere.sql.parser.sql.statement.tcl.BeginTransactionStatement;
 import org.apache.shardingsphere.sql.parser.sql.statement.tcl.CommitStatement;
@@ -103,6 +105,9 @@ public final class TextProtocolBackendHandlerFactory {
         if (dalStatement instanceof ShowDatabasesStatement) {
             return new ShowDatabasesBackendHandler(backendConnection);
         }
+        if (dalStatement instanceof ShowTablesStatement) {
+            return new ShowTablesBackendHandler(sql, dalStatement, backendConnection);
+        }
         // FIXME: There are three SetStatement classes.
         if (dalStatement instanceof SetStatement) {
             return new BroadcastBackendHandler(sql, dalStatement, backendConnection);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/query/QueryBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/ShowTablesBackendHandler.java
similarity index 71%
copy from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/query/QueryBackendHandler.java
copy to shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/ShowTablesBackendHandler.java
index e53dc84..01f33a1 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/query/QueryBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/ShowTablesBackendHandler.java
@@ -15,9 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.proxy.backend.text.query;
+package org.apache.shardingsphere.proxy.backend.text.admin;
 
 import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.QueryHeader;
 import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
 import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
@@ -25,17 +26,19 @@ import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedExcep
 import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
 import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
 import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
+import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
 import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
-import org.apache.shardingsphere.rdl.parser.statement.rdl.CreateSchemaStatement;
 import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
 
 import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Collections;
 
 /**
- * Backend handler with query.
+ * Backend handler for show tables.
  */
 @RequiredArgsConstructor
-public final class QueryBackendHandler implements TextProtocolBackendHandler {
+public final class ShowTablesBackendHandler implements TextProtocolBackendHandler {
     
     private final DatabaseCommunicationEngineFactory databaseCommunicationEngineFactory = DatabaseCommunicationEngineFactory.getInstance();
     
@@ -49,20 +52,25 @@ public final class QueryBackendHandler implements TextProtocolBackendHandler {
     
     @Override
     public BackendResponse execute() {
-        if (!hasSelectedOrNewSchema()) {
+        if (null == backendConnection.getSchema()) {
             return new ErrorResponse(new NoDatabaseSelectedException());
         }
+        if (!backendConnection.getSchema().isComplete()) {
+            return getDefaultQueryResponse(backendConnection.getSchema().getName());
+        }
+        // TODO Get all tables from meta data.
         databaseCommunicationEngine = databaseCommunicationEngineFactory.newTextProtocolInstance(sqlStatement, sql, backendConnection);
         return databaseCommunicationEngine.execute();
     }
     
-    private boolean hasSelectedOrNewSchema() {
-        return null != backendConnection.getSchema() || sqlStatement instanceof CreateSchemaStatement;
+    private QueryResponse getDefaultQueryResponse(final String schemaName) {
+        return new QueryResponse(Collections.singletonList(
+                new QueryHeader(schemaName, "", "Tables_in_" + schemaName, "Tables_in_" + schemaName, 64, Types.VARCHAR, 0, false, false, false, false)));
     }
     
     @Override
     public boolean next() throws SQLException {
-        return databaseCommunicationEngine.next();
+        return null != databaseCommunicationEngine && databaseCommunicationEngine.next();
     }
     
     @Override
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/query/QueryBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/query/QueryBackendHandler.java
index e53dc84..f20ab11 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/query/QueryBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/query/QueryBackendHandler.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.proxy.backend.text.query;
 
 import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.QueryHeader;
 import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
 import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
@@ -25,11 +26,14 @@ import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedExcep
 import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
 import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
 import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
+import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
 import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
 import org.apache.shardingsphere.rdl.parser.statement.rdl.CreateSchemaStatement;
 import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
 
 import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Collections;
 
 /**
  * Backend handler with query.
@@ -52,17 +56,24 @@ public final class QueryBackendHandler implements TextProtocolBackendHandler {
         if (!hasSelectedOrNewSchema()) {
             return new ErrorResponse(new NoDatabaseSelectedException());
         }
+        if (!backendConnection.getSchema().isComplete()) {
+            return getDefaultQueryResponse(backendConnection.getSchema().getName());
+        }
         databaseCommunicationEngine = databaseCommunicationEngineFactory.newTextProtocolInstance(sqlStatement, sql, backendConnection);
         return databaseCommunicationEngine.execute();
     }
     
+    private QueryResponse getDefaultQueryResponse(final String schemaName) {
+        return new QueryResponse(Collections.singletonList(new QueryHeader(schemaName, "", "", "", 255, Types.VARCHAR, 0, false, false, false, false)));
+    }
+    
     private boolean hasSelectedOrNewSchema() {
         return null != backendConnection.getSchema() || sqlStatement instanceof CreateSchemaStatement;
     }
     
     @Override
     public boolean next() throws SQLException {
-        return databaseCommunicationEngine.next();
+        return null != databaseCommunicationEngine && databaseCommunicationEngine.next();
     }
     
     @Override
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/ShowTablesBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/ShowTablesBackendHandlerTest.java
new file mode 100644
index 0000000..7626234
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/ShowTablesBackendHandlerTest.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.text.admin;
+
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.auth.ProxyUser;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.kernel.context.SchemaContext;
+import org.apache.shardingsphere.kernel.context.StandardSchemaContexts;
+import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
+import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
+import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
+import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class ShowTablesBackendHandlerTest {
+    
+    private ShowTablesBackendHandler tablesBackendHandler;
+    
+    @Before
+    @SneakyThrows(ReflectiveOperationException.class)
+    public void setUp() {
+        BackendConnection backendConnection = mock(BackendConnection.class);
+        when(backendConnection.getUserName()).thenReturn("root");
+        tablesBackendHandler = new ShowTablesBackendHandler("show tables", mock(SQLStatement.class), backendConnection);
+        Map<String, SchemaContext> schemaContextMap = getSchemaContextMap();
+        when(backendConnection.getSchema()).thenReturn(schemaContextMap.values().iterator().next());
+        Field schemaContexts = ProxySchemaContexts.getInstance().getClass().getDeclaredField("schemaContexts");
+        schemaContexts.setAccessible(true);
+        schemaContexts.set(ProxySchemaContexts.getInstance(),
+                new StandardSchemaContexts(schemaContextMap, getAuthentication(), new ConfigurationProperties(new Properties()), new MySQLDatabaseType()));
+    }
+    
+    private Map<String, SchemaContext> getSchemaContextMap() {
+        Map<String, SchemaContext> result = new HashMap<>(10);
+        for (int i = 0; i < 10; i++) {
+            SchemaContext context = mock(SchemaContext.class);
+            when(context.isComplete()).thenReturn(false);
+            result.put("schema_" + i, context);
+        }
+        return result;
+    }
+    
+    private Authentication getAuthentication() {
+        ProxyUser proxyUser = new ProxyUser("root", Arrays.asList("schema_0", "schema_1"));
+        Authentication result = new Authentication();
+        result.getUsers().put("root", proxyUser);
+        return result;
+    }
+    
+    @Test
+    public void assertExecuteShowTablesBackendHandler() {
+        QueryResponse actual = (QueryResponse) tablesBackendHandler.execute();
+        assertThat(actual, instanceOf(QueryResponse.class));
+        assertThat(actual.getQueryHeaders().size(), is(1));
+    }
+    
+    @Test
+    public void assertShowTablesUsingStream() throws SQLException {
+        tablesBackendHandler.execute();
+        while (tablesBackendHandler.next()) {
+            QueryData queryData = tablesBackendHandler.getQueryData();
+            assertThat(queryData.getColumnTypes().size(), is(1));
+            assertThat(queryData.getColumnTypes().iterator().next(), is(Types.VARCHAR));
+            assertThat(queryData.getData().size(), is(1));
+        }
+    }
+}