userkdg commented on a change in pull request #16268:
URL: https://github.com/apache/shardingsphere/pull/16268#discussion_r836978370



##########
File path: 
shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandler.java
##########
@@ -0,0 +1,198 @@
+/*
+ * 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.frontend.mysql.command.query.text.query;
+
+import org.apache.shardingsphere.infra.binder.LogicSQL;
+import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
+import org.apache.shardingsphere.infra.context.kernel.KernelProcessor;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
+import org.apache.shardingsphere.infra.executor.check.SQLCheckEngine;
+import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroup;
+import 
org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
+import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
+import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.engine.SQLExecutorExceptionHandler;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
+import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.DriverExecutionPrepareEngine;
+import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.JDBCDriverType;
+import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption;
+import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.parser.rule.SQLParserRule;
+import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.JDBCBackendConnection;
+import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.JDBCBackendStatement;
+import org.apache.shardingsphere.proxy.backend.context.BackendExecutorContext;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import 
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.regex.Pattern;
+
+/**
+ * Handler for MySQL multi statements.
+ */
+public final class MySQLMultiStatementsHandler implements 
TextProtocolBackendHandler {
+    
+    private static final Pattern MULTI_UPDATE_STATEMENTS = 
Pattern.compile(";(?=\\s+update)", Pattern.CASE_INSENSITIVE);
+    
+    private static final Pattern MULTI_DELETE_STATEMENTS = 
Pattern.compile(";(?=\\s+delete)", Pattern.CASE_INSENSITIVE);
+    
+    private final KernelProcessor kernelProcessor = new KernelProcessor();
+    
+    private final JDBCExecutor jdbcExecutor = new 
JDBCExecutor(BackendExecutorContext.getInstance().getExecutorEngine(), false);
+    
+    private final ConnectionSession connectionSession;
+    
+    private final SQLStatement sqlStatementSample;
+    
+    private final MetaDataContexts metaDataContexts = 
ProxyContext.getInstance().getContextManager().getMetaDataContexts();
+    
+    private final Map<String, List<ExecutionUnit>> dataSourcesToExecutionUnits 
= new HashMap<>();
+    
+    private ExecutionContext anyExecutionContext;
+    
+    public MySQLMultiStatementsHandler(final ConnectionSession 
connectionSession, final SQLStatement sqlStatementSample, final String sql) {
+        this.connectionSession = connectionSession;
+        this.sqlStatementSample = sqlStatementSample;
+        Pattern pattern = sqlStatementSample instanceof UpdateStatement ? 
MULTI_UPDATE_STATEMENTS : MULTI_DELETE_STATEMENTS;
+        ShardingSphereSQLParserEngine sqlParserEngine = getSQLParserEngine();
+        for (String each : extractMultiStatements(pattern, sql)) {
+            SQLStatement eachSQLStatement = sqlParserEngine.parse(each, false);
+            ExecutionContext executionContext = 
createExecutionContext(createLogicSQL(each, eachSQLStatement));
+            if (null == anyExecutionContext) {
+                anyExecutionContext = executionContext;
+            }
+            for (ExecutionUnit eachExecutionUnit : 
executionContext.getExecutionUnits()) {
+                
dataSourcesToExecutionUnits.computeIfAbsent(eachExecutionUnit.getDataSourceName(),
 unused -> new LinkedList<>()).add(eachExecutionUnit);
+            }
+        }
+    }
+    
+    private ShardingSphereSQLParserEngine getSQLParserEngine() {
+        MetaDataContexts metaDataContexts = 
ProxyContext.getInstance().getContextManager().getMetaDataContexts();
+        return new 
ShardingSphereSQLParserEngine(DatabaseTypeRegistry.getTrunkDatabaseTypeName(metaDataContexts.getMetaData(connectionSession.getSchemaName()).getResource().getDatabaseType()),
+                
metaDataContexts.getGlobalRuleMetaData().findSingleRule(SQLParserRule.class).orElse(null));
+    }
+    
+    private List<String> extractMultiStatements(final Pattern pattern, final 
String sql) {

Review comment:
       yes, connect url must add allowMultiQueries=true | 
rewriteBatchedStatements=true
   However, what I want to say is that this problem will still exist in 
shardingsphere-jdbc, we should support this feature by sql parser or satisfies 
both proxy and jdbc on basic module.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to