You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/06/05 07:09:31 UTC
[shardingsphere] branch master updated: Support Connection.prepareCall passthrough (#26051)
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 5accb86c05f Support Connection.prepareCall passthrough (#26051)
5accb86c05f is described below
commit 5accb86c05f7f7defa8d75969eb5f6a466f06ab6
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Mon Jun 5 15:09:23 2023 +0800
Support Connection.prepareCall passthrough (#26051)
* Support single DataSource scenario Connection.prepareCall
* Fix checkstyle
* Perform code format in ShardingSphereConnection
* Complete UnsupportedOperationConnectionTest
---
.../core/connection/ShardingSphereConnection.java | 19 +++++++++++++++++++
.../AbstractUnsupportedOperationConnection.java | 16 ----------------
.../circuit/connection/CircuitBreakerConnection.java | 20 ++++++++++++++++++--
.../connection/ShardingSphereConnectionTest.java | 11 +++++++++++
.../UnsupportedOperationConnectionTest.java | 15 ---------------
5 files changed, 48 insertions(+), 33 deletions(-)
diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
index 1a401718c6b..902d83534b6 100644
--- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
@@ -29,6 +29,7 @@ import org.apache.shardingsphere.infra.util.exception.ShardingSpherePrecondition
import org.apache.shardingsphere.mode.manager.ContextManager;
import java.sql.Array;
+import java.sql.CallableStatement;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@@ -127,6 +128,24 @@ public final class ShardingSphereConnection extends AbstractConnectionAdapter {
return new ShardingSphereStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability);
}
+ @Override
+ public CallableStatement prepareCall(final String sql) throws SQLException {
+ // TODO Support single DataSource scenario for now. Implement ShardingSphereCallableStatement to support multi DataSource scenarios.
+ return databaseConnectionManager.getRandomConnection().prepareCall(sql);
+ }
+
+ @Override
+ public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
+ // TODO Support single DataSource scenario for now. Implement ShardingSphereCallableStatement to support multi DataSource scenarios.
+ return databaseConnectionManager.getRandomConnection().prepareCall(sql, resultSetType, resultSetConcurrency);
+ }
+
+ @Override
+ public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
+ // TODO Support single DataSource scenario for now. Implement ShardingSphereCallableStatement to support multi DataSource scenarios.
+ return databaseConnectionManager.getRandomConnection().prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
+ }
+
@Override
public boolean getAutoCommit() {
return autoCommit;
diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java
index aa713ddfe02..04749c98e5c 100644
--- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java
@@ -21,7 +21,6 @@ import org.apache.shardingsphere.driver.jdbc.adapter.WrapperAdapter;
import org.apache.shardingsphere.infra.util.exception.external.sql.type.generic.UnsupportedSQLOperationException;
import java.sql.Blob;
-import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.NClob;
@@ -38,21 +37,6 @@ import java.util.concurrent.Executor;
*/
public abstract class AbstractUnsupportedOperationConnection extends WrapperAdapter implements Connection {
- @Override
- public final CallableStatement prepareCall(final String sql) throws SQLException {
- throw new SQLFeatureNotSupportedException("prepareCall");
- }
-
- @Override
- public final CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
- throw new SQLFeatureNotSupportedException("prepareCall");
- }
-
- @Override
- public final CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
- throw new SQLFeatureNotSupportedException("prepareCall");
- }
-
@Override
public final String nativeSQL(final String sql) throws SQLException {
throw new SQLFeatureNotSupportedException("nativeSQL");
diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/state/circuit/connection/CircuitBreakerConnection.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/state/circuit/connection/CircuitBreakerConnection.java
index 26c7b36ac6a..06526247f68 100644
--- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/state/circuit/connection/CircuitBreakerConnection.java
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/state/circuit/connection/CircuitBreakerConnection.java
@@ -17,12 +17,13 @@
package org.apache.shardingsphere.driver.state.circuit.connection;
-import java.sql.Array;
+import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationConnection;
import org.apache.shardingsphere.driver.state.circuit.metadata.CircuitBreakerDatabaseMetaData;
import org.apache.shardingsphere.driver.state.circuit.statement.CircuitBreakerPreparedStatement;
import org.apache.shardingsphere.driver.state.circuit.statement.CircuitBreakerStatement;
-import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationConnection;
+import java.sql.Array;
+import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
@@ -154,6 +155,21 @@ public final class CircuitBreakerConnection extends AbstractUnsupportedOperation
return new CircuitBreakerPreparedStatement();
}
+ @Override
+ public CallableStatement prepareCall(final String sql) throws SQLException {
+ throw new SQLFeatureNotSupportedException("prepareCall");
+ }
+
+ @Override
+ public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
+ throw new SQLFeatureNotSupportedException("prepareCall");
+ }
+
+ @Override
+ public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
+ throw new SQLFeatureNotSupportedException("prepareCall");
+ }
+
@Override
public boolean isValid(final int timeout) {
return true;
diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
index ddaed0077c3..3ae0c18372c 100644
--- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
+++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
@@ -36,6 +36,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.internal.configuration.plugins.Plugins;
import javax.sql.DataSource;
+import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
@@ -220,6 +221,16 @@ class ShardingSphereConnectionTest {
verify(physicalConnection).createArrayOf("int", null);
}
+ @Test
+ void assertPrepareCall() throws SQLException {
+ CallableStatement expected = mock(CallableStatement.class);
+ Connection physicalConnection = mock(Connection.class);
+ when(physicalConnection.prepareCall("")).thenReturn(expected);
+ when(connection.getContextManager().getDataSourceMap(DefaultDatabase.LOGIC_NAME).get("ds").getConnection()).thenReturn(physicalConnection);
+ CallableStatement actual = connection.prepareCall("");
+ assertThat(actual, is(expected));
+ }
+
@Test
void assertClose() throws SQLException {
connection.close();
diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java
index f01f65a054a..d274f8d02b8 100644
--- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java
+++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java
@@ -47,21 +47,6 @@ class UnsupportedOperationConnectionTest {
shardingSphereConnection = new ShardingSphereConnection(DefaultDatabase.LOGIC_NAME, contextManager, mock(JDBCContext.class));
}
- @Test
- void assertPrepareCall() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereConnection.prepareCall(""));
- }
-
- @Test
- void assertPrepareCallWithResultSetTypeAndResultSetConcurrency() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereConnection.prepareCall("", 0, 0));
- }
-
- @Test
- void assertPrepareCallWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereConnection.prepareCall("", 0, 0, 0));
- }
-
@Test
void assertNativeSQL() {
assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereConnection.nativeSQL(""));