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/12/23 06:57:11 UTC

[shardingsphere] branch master updated: Add information_schema and performance_schema's init framework (#8730)

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

zhangyonglun 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 bdf43c3  Add information_schema and performance_schema's init framework (#8730)
bdf43c3 is described below

commit bdf43c3c7d3bc37243ff68ffd6e9601ff5b8050c
Author: Liang Zhang <te...@163.com>
AuthorDate: Wed Dec 23 14:55:25 2020 +0800

    Add information_schema and performance_schema's init framework (#8730)
---
 .../admin/DatabaseAdminBackendHandlerFactory.java  |  2 +-
 .../executor/DatabaseAdminExecutorFactory.java     |  3 +-
 .../admin/mysql/MySQLAdminExecutorFactory.java     | 44 ++++++++++++++++++++--
 3 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/DatabaseAdminBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/DatabaseAdminBackendHandlerFactory.java
index 028b5489f..24433ab 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/DatabaseAdminBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/DatabaseAdminBackendHandlerFactory.java
@@ -55,7 +55,7 @@ public final class DatabaseAdminBackendHandlerFactory {
         if (!executorFactory.isPresent()) {
             return Optional.empty();
         }
-        Optional<DatabaseAdminExecutor> executor = executorFactory.get().newInstance(sqlStatement);
+        Optional<DatabaseAdminExecutor> executor = executorFactory.get().newInstance(backendConnection.getSchemaName(), sqlStatement);
         return executor.map(optional -> createTextProtocolBackendHandler(sqlStatement, backendConnection, optional));
     }
     
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/executor/DatabaseAdminExecutorFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/executor/DatabaseAdminExecutorFactory.java
index cbeab09..92fa95c 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/executor/DatabaseAdminExecutorFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/executor/DatabaseAdminExecutorFactory.java
@@ -30,8 +30,9 @@ public interface DatabaseAdminExecutorFactory extends TypedSPI {
     /**
      * New instance of database admin executor.
      * 
+     * @param currentSchema current schema
      * @param sqlStatement SQL statement
      * @return instance of database admin executor
      */
-    Optional<DatabaseAdminExecutor> newInstance(SQLStatement sqlStatement);
+    Optional<DatabaseAdminExecutor> newInstance(String currentSchema, SQLStatement sqlStatement);
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/MySQLAdminExecutorFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/MySQLAdminExecutorFactory.java
index 0a2bbcd..ce5bb3f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/MySQLAdminExecutorFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/MySQLAdminExecutorFactory.java
@@ -25,6 +25,8 @@ import org.apache.shardingsphere.proxy.backend.text.admin.mysql.executor.ShowTab
 import org.apache.shardingsphere.proxy.backend.text.admin.mysql.executor.UseDatabaseExecutor;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowDatabasesStatement;
@@ -38,8 +40,12 @@ import java.util.Optional;
  */
 public final class MySQLAdminExecutorFactory implements DatabaseAdminExecutorFactory {
     
+    private static final String INFORMATION_SCHEMA = "information_schema";
+    
+    private static final String PERFORMANCE_SCHEMA = "performance_schema";
+    
     @Override
-    public Optional<DatabaseAdminExecutor> newInstance(final SQLStatement sqlStatement) {
+    public Optional<DatabaseAdminExecutor> newInstance(final String currentSchema, final SQLStatement sqlStatement) {
         if (sqlStatement instanceof MySQLUseStatement) {
             return Optional.of(new UseDatabaseExecutor((MySQLUseStatement) sqlStatement));
         }
@@ -50,15 +56,45 @@ public final class MySQLAdminExecutorFactory implements DatabaseAdminExecutorFac
             return Optional.of(new ShowTablesExecutor());
         }
         if (sqlStatement instanceof SelectStatement) {
-            ProjectionSegment firstProjection = ((SelectStatement) sqlStatement).getProjections().getProjections().iterator().next();
-            if (firstProjection instanceof ExpressionProjectionSegment
-                    && ShowCurrentDatabaseExecutor.FUNCTION_NAME.equalsIgnoreCase(((ExpressionProjectionSegment) firstProjection).getText())) {
+            if (isShowCurrentDatabaseStatement((SelectStatement) sqlStatement)) {
                 return Optional.of(new ShowCurrentDatabaseExecutor());
+            } 
+            if (isQueryInformationSchema(currentSchema, (SelectStatement) sqlStatement)) {
+                // TODO
+                return Optional.empty();
+            }
+            if (isQueryPerformanceSchema(currentSchema, (SelectStatement) sqlStatement)) {
+                // TODO
+                return Optional.empty();
             }
         }
         return Optional.empty();
     }
     
+    private boolean isShowCurrentDatabaseStatement(final SelectStatement sqlStatement) {
+        ProjectionSegment firstProjection = sqlStatement.getProjections().getProjections().iterator().next();
+        return firstProjection instanceof ExpressionProjectionSegment && ShowCurrentDatabaseExecutor.FUNCTION_NAME.equalsIgnoreCase(((ExpressionProjectionSegment) firstProjection).getText());
+    }
+    
+    private boolean isQueryInformationSchema(final String currentSchema, final SelectStatement sqlStatement) {
+        return isQuerySpecialSchema(currentSchema, sqlStatement, INFORMATION_SCHEMA);
+    }
+    
+    private boolean isQueryPerformanceSchema(final String currentSchema, final SelectStatement sqlStatement) {
+        return isQuerySpecialSchema(currentSchema, sqlStatement, PERFORMANCE_SCHEMA);
+    }
+    
+    private boolean isQuerySpecialSchema(final String currentSchema, final SelectStatement sqlStatement, final String specialSchemaName) {
+        TableSegment tableSegment = sqlStatement.getFrom();
+        if (!(tableSegment instanceof SimpleTableSegment)) {
+            return false;
+        }
+        if (specialSchemaName.equalsIgnoreCase(currentSchema) && !((SimpleTableSegment) tableSegment).getOwner().isPresent()) {
+            return true;
+        }
+        return ((SimpleTableSegment) tableSegment).getOwner().isPresent() && specialSchemaName.equalsIgnoreCase(((SimpleTableSegment) tableSegment).getOwner().get().getIdentifier().getValue());
+    }
+    
     @Override
     public String getType() {
         return "MySQL";