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

zhaojinchao 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 3d85d8b2fc7 Add RULExecuteEngine (#29876)
3d85d8b2fc7 is described below

commit 3d85d8b2fc7507fc1955a98bd181a3ee6fffe452
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jan 27 20:39:50 2024 +0800

    Add RULExecuteEngine (#29876)
---
 .../distsql/handler/type/rul/RULExecuteEngine.java | 91 ++++++++++++++++++++++
 .../handler/distsql/rul/RULBackendHandler.java     | 78 ++++++++++++-------
 2 files changed, 139 insertions(+), 30 deletions(-)

diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rul/RULExecuteEngine.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rul/RULExecuteEngine.java
new file mode 100644
index 00000000000..7852e8ad76c
--- /dev/null
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rul/RULExecuteEngine.java
@@ -0,0 +1,91 @@
+/*
+ * 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.distsql.handler.type.rul;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorConnectionContextAware;
+import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
+import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseProtocolTypeAware;
+import org.apache.shardingsphere.distsql.handler.util.DatabaseNameUtils;
+import org.apache.shardingsphere.distsql.statement.rul.RULStatement;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.DatabaseConnectionManager;
+import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.ExecutorStatementManager;
+import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * RUL execute engine.
+ */
+@RequiredArgsConstructor
+public abstract class RULExecuteEngine {
+    
+    private final RULStatement sqlStatement;
+    
+    private final String currentDatabaseName;
+    
+    private final ContextManager contextManager;
+    
+    @Getter
+    private Collection<String> columnNames;
+    
+    @Getter
+    private Collection<LocalDataQueryResultRow> rows;
+    
+    /**
+     * Execute query.
+     *
+     * @throws SQLException SQL exception
+     */
+    @SuppressWarnings("unchecked")
+    public void executeQuery() throws SQLException {
+        RULExecutor<RULStatement> executor = 
TypedSPILoader.getService(RULExecutor.class, sqlStatement.getClass());
+        columnNames = executor.getColumnNames();
+        if (executor instanceof DistSQLExecutorDatabaseAware) {
+            ((DistSQLExecutorDatabaseAware) 
executor).setDatabase(getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement,
 currentDatabaseName)));
+        }
+        if (executor instanceof DistSQLExecutorDatabaseProtocolTypeAware) {
+            ((DistSQLExecutorDatabaseProtocolTypeAware) 
executor).setDatabaseProtocolType(getDatabaseProtocolType());
+        }
+        if (executor instanceof DistSQLExecutorConnectionContextAware) {
+            ((DistSQLExecutorConnectionContextAware) 
executor).setConnectionContext(getConnectionContext());
+            ((DistSQLExecutorConnectionContextAware) 
executor).setDatabaseConnectionManager(getDatabaseConnectionManager());
+            ((DistSQLExecutorConnectionContextAware) 
executor).setStatementManager(getStatementManager());
+        }
+        rows = executor.getRows(sqlStatement, contextManager);
+    }
+    
+    protected abstract ShardingSphereDatabase getDatabase(String databaseName);
+    
+    protected abstract DatabaseType getDatabaseProtocolType();
+    
+    protected abstract ConnectionContext getConnectionContext();
+    
+    @SuppressWarnings("rawtypes")
+    protected abstract DatabaseConnectionManager 
getDatabaseConnectionManager();
+    
+    @SuppressWarnings("rawtypes")
+    protected abstract ExecutorStatementManager getStatementManager();
+}
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/RULBackendHandler.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/RULBackendHandler.java
index d7ad04282e3..ca2886ebd10 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/RULBackendHandler.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/RULBackendHandler.java
@@ -17,16 +17,16 @@
 
 package org.apache.shardingsphere.proxy.backend.handler.distsql.rul;
 
-import lombok.RequiredArgsConstructor;
-import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorConnectionContextAware;
-import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
-import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseProtocolTypeAware;
-import org.apache.shardingsphere.distsql.handler.type.rul.RULExecutor;
-import org.apache.shardingsphere.distsql.handler.util.DatabaseNameUtils;
+import org.apache.shardingsphere.distsql.handler.type.rul.RULExecuteEngine;
 import org.apache.shardingsphere.distsql.statement.rul.RULStatement;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.DatabaseConnectionManager;
+import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.ExecutorStatementManager;
 import org.apache.shardingsphere.infra.merge.result.MergedResult;
 import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataMergedResult;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import 
org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLBackendHandler;
 import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseCell;
@@ -39,16 +39,14 @@ import 
org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 import java.sql.SQLException;
 import java.sql.Types;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.stream.Collectors;
 
 /**
  * RUL backend handler.
  */
-@RequiredArgsConstructor
-public final class RULBackendHandler implements DistSQLBackendHandler {
-    
-    private final RULStatement sqlStatement;
+public final class RULBackendHandler extends RULExecuteEngine implements 
DistSQLBackendHandler {
     
     private final ConnectionSession connectionSession;
     
@@ -56,32 +54,25 @@ public final class RULBackendHandler implements 
DistSQLBackendHandler {
     
     private MergedResult mergedResult;
     
-    @SuppressWarnings("unchecked")
+    public RULBackendHandler(final RULStatement sqlStatement, final 
ConnectionSession connectionSession) {
+        super(sqlStatement, connectionSession.getDatabaseName(), 
ProxyContext.getInstance().getContextManager());
+        this.connectionSession = connectionSession;
+    }
+    
     @Override
     public ResponseHeader execute() throws SQLException {
-        RULExecutor<RULStatement> executor = 
TypedSPILoader.getService(RULExecutor.class, sqlStatement.getClass());
-        queryHeaders = createQueryHeader(executor);
-        mergedResult = createMergedResult(executor);
+        executeQuery();
+        queryHeaders = createQueryHeader(getColumnNames());
+        mergedResult = createMergedResult(getRows());
         return new QueryResponseHeader(queryHeaders);
     }
     
-    private List<QueryHeader> createQueryHeader(final 
RULExecutor<RULStatement> executor) {
-        return executor.getColumnNames().stream().map(each -> new 
QueryHeader("", "", each, each, Types.CHAR, "CHAR", 255, 0, false, false, 
false, false)).collect(Collectors.toList());
+    private List<QueryHeader> createQueryHeader(final Collection<String> 
columnNames) {
+        return columnNames.stream().map(each -> new QueryHeader("", "", each, 
each, Types.CHAR, "CHAR", 255, 0, false, false, false, 
false)).collect(Collectors.toList());
     }
     
-    private MergedResult createMergedResult(final RULExecutor<RULStatement> 
executor) throws SQLException {
-        if (executor instanceof DistSQLExecutorDatabaseAware) {
-            ((DistSQLExecutorDatabaseAware) 
executor).setDatabase(ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement,
 connectionSession.getDatabaseName())));
-        }
-        if (executor instanceof DistSQLExecutorDatabaseProtocolTypeAware) {
-            ((DistSQLExecutorDatabaseProtocolTypeAware) 
executor).setDatabaseProtocolType(connectionSession.getProtocolType());
-        }
-        if (executor instanceof DistSQLExecutorConnectionContextAware) {
-            ((DistSQLExecutorConnectionContextAware) 
executor).setConnectionContext(connectionSession.getConnectionContext());
-            ((DistSQLExecutorConnectionContextAware) 
executor).setDatabaseConnectionManager(connectionSession.getDatabaseConnectionManager());
-            ((DistSQLExecutorConnectionContextAware) 
executor).setStatementManager(connectionSession.getStatementManager());
-        }
-        return new LocalDataMergedResult(executor.getRows(sqlStatement, 
ProxyContext.getInstance().getContextManager()));
+    private MergedResult createMergedResult(final 
Collection<LocalDataQueryResultRow> rows) {
+        return new LocalDataMergedResult(rows);
     }
     
     @Override
@@ -97,4 +88,31 @@ public final class RULBackendHandler implements 
DistSQLBackendHandler {
         }
         return new QueryResponseRow(cells);
     }
+    
+    @Override
+    protected ShardingSphereDatabase getDatabase(final String databaseName) {
+        return ProxyContext.getInstance().getDatabase(databaseName);
+    }
+    
+    @Override
+    protected DatabaseType getDatabaseProtocolType() {
+        return connectionSession.getProtocolType();
+    }
+    
+    @Override
+    protected ConnectionContext getConnectionContext() {
+        return connectionSession.getConnectionContext();
+    }
+    
+    @SuppressWarnings("rawtypes")
+    @Override
+    protected DatabaseConnectionManager getDatabaseConnectionManager() {
+        return connectionSession.getDatabaseConnectionManager();
+    }
+    
+    @SuppressWarnings("rawtypes")
+    @Override
+    protected ExecutorStatementManager getStatementManager() {
+        return connectionSession.getStatementManager();
+    }
 }

Reply via email to