Repository: apache/shardingsphere (Java)

Issue:
java.sql.SQLFeatureNotSupportedException: addBatch sql
## Bug Report

### Which version of ShardingSphere did you use?

5.3.1

### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?

ShardingSphere-JDBC

### Expected behavior

Batched statements execute succeeded.

### Actual behavior

```
java.sql.SQLFeatureNotSupportedException: addBatch sql
```

### Reproduce

Just use the method `Statement.addBatch(String)`.


## Step

- [x] Implement `addBatch(String)`, `executeBatch()`, `clearBatch()` for `ShardingSphereStatement`.



Fix (patch):
diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/executor/batch/BatchStatementExecutor.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/executor/batch/BatchStatementExecutor.java
new file mode 100644
index 0000000000000..81a703f9ead0f
--- /dev/null
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/executor/batch/BatchStatementExecutor.java
@@ -0,0 +1,67 @@
+/*
+ * 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.driver.executor.batch;
+
+import lombok.RequiredArgsConstructor;
+
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Batch executor for {@link Statement}.
+ */
+@RequiredArgsConstructor
+public final class BatchStatementExecutor {
+    
+    private final Statement statement;
+    
+    private final List<String> batchedSQLs = new LinkedList<>();
+    
+    /**
+     * Add SQL for batched execution.
+     *
+     * @param sql SQL 
+     */
+    public void addBatch(final String sql) {
+        batchedSQLs.add(sql);
+    }
+    
+    /**
+     * Execute batched SQLs.
+     *
+     * @return execute results
+     * @throws SQLException SQL exception
+     */
+    public int[] executeBatch() throws SQLException {
+        int[] result = new int[batchedSQLs.size()];
+        int index = 0;
+        for (String each : batchedSQLs) {
+            result[index++] = statement.executeUpdate(each);
+        }
+        return result;
+    }
+    
+    /**
+     * Clear batched SQLs.
+     */
+    public void clear() {
+        batchedSQLs.clear();
+    }
+}
diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
index 67a1e8a26d678..855a5b107b3a0 100644
--- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
@@ -22,6 +22,7 @@
 import lombok.Getter;
 import org.apache.shardingsphere.dialect.SQLExceptionTransformEngine;
 import org.apache.shardingsphere.driver.executor.DriverExecutor;
+import org.apache.shardingsphere.driver.executor.batch.BatchStatementExecutor;
 import org.apache.shardingsphere.driver.executor.callback.ExecuteCallback;
 import org.apache.shardingsphere.driver.executor.callback.ExecuteUpdateCallback;
 import org.apache.shardingsphere.driver.executor.callback.impl.StatementExecuteQueryCallback;
@@ -126,6 +127,8 @@ public final class ShardingSphereStatement extends AbstractStatementAdapter {
     @Getter(AccessLevel.PROTECTED)
     private final StatementManager statementManager;
     
+    private final BatchStatementExecutor batchStatementExecutor;
+    
     private boolean returnGeneratedKeys;
     
     private ExecutionContext executionContext;
@@ -153,6 +156,7 @@ public ShardingSphereStatement(final ShardingSphereConnection connection, final
         kernelProcessor = new KernelProcessor();
         trafficRule = metaDataContexts.getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class);
         statementManager = new StatementManager();
+        batchStatementExecutor = new BatchStatementExecutor(this);
     }
     
     @Override
@@ -507,6 +511,21 @@ private void clearStatements() throws SQLException {
         statements.clear();
     }
     
+    @Override
+    public void addBatch(final String sql) throws SQLException {
+        batchStatementExecutor.addBatch(sql);
+    }
+    
+    @Override
+    public void clearBatch() {
+        batchStatementExecutor.clear();
+    }
+    
+    @Override
+    public int[] executeBatch() throws SQLException {
+        return batchStatementExecutor.executeBatch();
+    }
+    
     private QueryContext createQueryContext(final String originSQL) {
         SQLParserRule sqlParserRule = metaDataContexts.getMetaData().getGlobalRuleMetaData().getSingleRule(SQLParserRule.class);
         String sql = sqlParserRule.isSqlCommentParseEnabled() ? originSQL : SQLHintUtils.removeHint(originSQL);
diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationPreparedStatement.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationPreparedStatement.java
index 1d14b05e395f8..b3c76b21ead28 100644
--- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationPreparedStatement.java
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationPreparedStatement.java
@@ -34,6 +34,11 @@
  */
 public abstract class AbstractUnsupportedOperationPreparedStatement extends AbstractStatementAdapter implements PreparedStatement {
     
+    @Override
+    public final void addBatch(final String sql) throws SQLException {
+        throw new SQLFeatureNotSupportedException("addBatch sql in PreparedStatement");
+    }
+    
     @Override
     public final ResultSetMetaData getMetaData() throws SQLException {
         throw new SQLFeatureNotSupportedException("getMetaData");
diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationStatement.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationStatement.java
index cb763d3dc9f38..b6c10685931fa 100644
--- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationStatement.java
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationStatement.java
@@ -28,21 +28,6 @@
  */
 public abstract class AbstractUnsupportedOperationStatement extends WrapperAdapter implements Statement {
     
-    @Override
-    public final void addBatch(final String sql) throws SQLException {
-        throw new SQLFeatureNotSupportedException("addBatch sql");
-    }
-    
-    @Override
-    public void clearBatch() throws SQLException {
-        throw new SQLFeatureNotSupportedException("clearBatch");
-    }
-    
-    @Override
-    public int[] executeBatch() throws SQLException {
-        throw new SQLFeatureNotSupportedException("executeBatch");
-    }
-    
     @Override
     public final void closeOnCompletion() throws SQLException {
         throw new SQLFeatureNotSupportedException("closeOnCompletion");
diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/state/circuit/statement/CircuitBreakerStatement.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/state/circuit/statement/CircuitBreakerStatement.java
index 3c4909d7f74ca..31dd09b00678f 100644
--- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/state/circuit/statement/CircuitBreakerStatement.java
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/state/circuit/statement/CircuitBreakerStatement.java
@@ -22,6 +22,7 @@
 
 import java.sql.Connection;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.sql.SQLWarning;
 
 /**
@@ -115,6 +116,19 @@ public int getResultSetType() {
         return ResultSet.TYPE_FORWARD_ONLY;
     }
     
+    @Override
+    public void addBatch(final String sql) {
+    }
+    
+    @Override
+    public void clearBatch() {
+    }
+    
+    @Override
+    public int[] executeBatch() throws SQLException {
+        return new int[0];
+    }
+    
     @Override
     public Connection getConnection() {
         return new CircuitBreakerConnection();