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

duanzhengqiang 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 1050fd557c4 Refactor SQLBindEngine (#32041)
1050fd557c4 is described below

commit 1050fd557c425e31c115b10d1c71646140c06333
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Jul 9 11:10:34 2024 +0800

    Refactor SQLBindEngine (#32041)
    
    * Refactor SQLBindEngine
    
    * Refactor SQLBindEngine
---
 .../shardingsphere/infra/binder/SQLBindEngine.java | 58 +++++-----------------
 .../infra/binder/type/DDLStatementBindEngine.java  | 48 ++++++++++++++++++
 .../DMLStatementBindEngine.java}                   | 50 +++----------------
 3 files changed, 67 insertions(+), 89 deletions(-)

diff --git 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/SQLBindEngine.java
 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/SQLBindEngine.java
index 39513f75f24..baa68017f2f 100644
--- 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/SQLBindEngine.java
+++ 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/SQLBindEngine.java
@@ -20,23 +20,13 @@ package org.apache.shardingsphere.infra.binder;
 import lombok.RequiredArgsConstructor;
 import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
 import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContextFactory;
-import 
org.apache.shardingsphere.infra.binder.statement.ddl.CursorStatementBinder;
-import 
org.apache.shardingsphere.infra.binder.statement.dml.DeleteStatementBinder;
-import 
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder;
-import 
org.apache.shardingsphere.infra.binder.statement.dml.MergeStatementBinder;
-import 
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementBinder;
-import 
org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementBinder;
+import org.apache.shardingsphere.infra.binder.type.DDLStatementBindEngine;
+import org.apache.shardingsphere.infra.binder.type.DMLStatementBindEngine;
 import org.apache.shardingsphere.infra.hint.HintValueContext;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CursorStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DMLStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.MergeStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;
 
 import java.util.List;
 
@@ -53,51 +43,27 @@ public final class SQLBindEngine {
     private final HintValueContext hintValueContext;
     
     /**
-     * Bind SQL statement with metadata.
+     * Bind SQL statement.
      *
      * @param sqlStatement SQL statement
      * @param params parameters
      * @return SQL statement context
      */
     public SQLStatementContext bind(final SQLStatement sqlStatement, final 
List<Object> params) {
-        return SQLStatementContextFactory.newInstance(metaData, 
bind(sqlStatement), params, currentDatabaseName);
+        SQLStatement boundSQLStatement = isNeedBind() ? 
bindSQLStatement(sqlStatement) : sqlStatement;
+        return SQLStatementContextFactory.newInstance(metaData, 
boundSQLStatement, params, currentDatabaseName);
     }
     
-    private SQLStatement bind(final SQLStatement statement) {
-        if (hintValueContext.findHintDataSourceName().isPresent()) {
-            return statement;
-        }
-        if (statement instanceof DMLStatement) {
-            return bindDMLStatement(statement);
-        }
-        if (statement instanceof DDLStatement) {
-            return bindDDLStatement(statement);
-        }
-        return statement;
+    private boolean isNeedBind() {
+        return !hintValueContext.findHintDataSourceName().isPresent();
     }
     
-    private SQLStatement bindDMLStatement(final SQLStatement statement) {
-        if (statement instanceof SelectStatement) {
-            return new SelectStatementBinder().bind((SelectStatement) 
statement, metaData, currentDatabaseName);
-        }
-        if (statement instanceof InsertStatement) {
-            return new InsertStatementBinder().bind((InsertStatement) 
statement, metaData, currentDatabaseName);
-        }
-        if (statement instanceof UpdateStatement) {
-            return new UpdateStatementBinder().bind((UpdateStatement) 
statement, metaData, currentDatabaseName);
-        }
-        if (statement instanceof DeleteStatement) {
-            return new DeleteStatementBinder().bind((DeleteStatement) 
statement, metaData, currentDatabaseName);
-        }
-        if (statement instanceof MergeStatement) {
-            return new MergeStatementBinder().bind((MergeStatement) statement, 
metaData, currentDatabaseName);
+    private SQLStatement bindSQLStatement(final SQLStatement statement) {
+        if (statement instanceof DMLStatement) {
+            return new DMLStatementBindEngine(metaData, 
currentDatabaseName).bind((DMLStatement) statement);
         }
-        return statement;
-    }
-    
-    private SQLStatement bindDDLStatement(final SQLStatement statement) {
-        if (statement instanceof CursorStatement) {
-            return new CursorStatementBinder().bind((CursorStatement) 
statement, metaData, currentDatabaseName);
+        if (statement instanceof DDLStatement) {
+            return new DDLStatementBindEngine(metaData, 
currentDatabaseName).bind((DDLStatement) statement);
         }
         return statement;
     }
diff --git 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/type/DDLStatementBindEngine.java
 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/type/DDLStatementBindEngine.java
new file mode 100644
index 00000000000..c1e72c42c0f
--- /dev/null
+++ 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/type/DDLStatementBindEngine.java
@@ -0,0 +1,48 @@
+/*
+ * 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.infra.binder.type;
+
+import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.infra.binder.statement.ddl.CursorStatementBinder;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CursorStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
+
+/**
+ * DDL statement bind engine.
+ */
+@RequiredArgsConstructor
+public final class DDLStatementBindEngine {
+    
+    private final ShardingSphereMetaData metaData;
+    
+    private final String currentDatabaseName;
+    
+    /**
+     * Bind DDL statement.
+     *
+     * @param statement to be bound DDL statement
+     * @return bound DDL statement
+     */
+    public DDLStatement bind(final DDLStatement statement) {
+        if (statement instanceof CursorStatement) {
+            return new CursorStatementBinder().bind((CursorStatement) 
statement, metaData, currentDatabaseName);
+        }
+        return statement;
+    }
+}
diff --git 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/SQLBindEngine.java
 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/type/DMLStatementBindEngine.java
similarity index 60%
copy from 
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/SQLBindEngine.java
copy to 
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/type/DMLStatementBindEngine.java
index 39513f75f24..a8da53fdf01 100644
--- 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/SQLBindEngine.java
+++ 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/type/DMLStatementBindEngine.java
@@ -15,22 +15,15 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.infra.binder;
+package org.apache.shardingsphere.infra.binder.type;
 
 import lombok.RequiredArgsConstructor;
-import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContextFactory;
-import 
org.apache.shardingsphere.infra.binder.statement.ddl.CursorStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.statement.dml.DeleteStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.statement.dml.MergeStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementBinder;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CursorStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DMLStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
@@ -38,45 +31,23 @@ import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.MergeSt
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;
 
-import java.util.List;
-
 /**
- * SQL bind engine.
+ * DML statement bind engine.
  */
 @RequiredArgsConstructor
-public final class SQLBindEngine {
+public final class DMLStatementBindEngine {
     
     private final ShardingSphereMetaData metaData;
     
     private final String currentDatabaseName;
     
-    private final HintValueContext hintValueContext;
-    
     /**
-     * Bind SQL statement with metadata.
+     * Bind DML statement.
      *
-     * @param sqlStatement SQL statement
-     * @param params parameters
-     * @return SQL statement context
+     * @param statement to be bound DML statement
+     * @return bound DML statement
      */
-    public SQLStatementContext bind(final SQLStatement sqlStatement, final 
List<Object> params) {
-        return SQLStatementContextFactory.newInstance(metaData, 
bind(sqlStatement), params, currentDatabaseName);
-    }
-    
-    private SQLStatement bind(final SQLStatement statement) {
-        if (hintValueContext.findHintDataSourceName().isPresent()) {
-            return statement;
-        }
-        if (statement instanceof DMLStatement) {
-            return bindDMLStatement(statement);
-        }
-        if (statement instanceof DDLStatement) {
-            return bindDDLStatement(statement);
-        }
-        return statement;
-    }
-    
-    private SQLStatement bindDMLStatement(final SQLStatement statement) {
+    public DMLStatement bind(final DMLStatement statement) {
         if (statement instanceof SelectStatement) {
             return new SelectStatementBinder().bind((SelectStatement) 
statement, metaData, currentDatabaseName);
         }
@@ -94,11 +65,4 @@ public final class SQLBindEngine {
         }
         return statement;
     }
-    
-    private SQLStatement bindDDLStatement(final SQLStatement statement) {
-        if (statement instanceof CursorStatement) {
-            return new CursorStatementBinder().bind((CursorStatement) 
statement, metaData, currentDatabaseName);
-        }
-        return statement;
-    }
 }

Reply via email to