You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/06/24 16:56:12 UTC
[shardingsphere] branch master updated: Move PostgreSQLPreparedStatementRegistry into ConnectionSession (#18568)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 2ba91cc7461 Move PostgreSQLPreparedStatementRegistry into ConnectionSession (#18568)
2ba91cc7461 is described below
commit 2ba91cc7461c59d3d556f356a09d4cc401a3d222
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Sat Jun 25 00:56:04 2022 +0800
Move PostgreSQLPreparedStatementRegistry into ConnectionSession (#18568)
* Move PostgreSQLPreparedStatementRegistry into ConnectionSession
Signed-off-by: 吴伟杰 <wu...@apache.org>
* Complete OpenGaussComBatchBindPacketTest
Signed-off-by: 吴伟杰 <wu...@apache.org>
* Complete PostgreSQLCommandPacketFactoryTest
Signed-off-by: 吴伟杰 <wu...@apache.org>
* Refactor PostgreSQLComBindExecutorTest
Signed-off-by: 吴伟杰 <wu...@apache.org>
* Complete OpenGaussComBatchBindExecutorTest
Signed-off-by: 吴伟杰 <wu...@apache.org>
* Complete PostgreSQLComCloseExecutorTest
Signed-off-by: 吴伟杰 <wu...@apache.org>
* Complete PostgreSQLComDescribeExecutorTest
Signed-off-by: 吴伟杰 <wu...@apache.org>
* Complete PostgreSQLAggregatedBatchedStatementsCommandExecutorTest
Signed-off-by: 吴伟杰 <wu...@apache.org>
* Complete PostgreSQLBatchedStatementsExecutorTest
Signed-off-by: 吴伟杰 <wu...@apache.org>
* Complete PostgreSQLComParseExecutorTest
Signed-off-by: 吴伟杰 <wu...@apache.org>
* Complete JDBCPortalTest
Signed-off-by: 吴伟杰 <wu...@apache.org>
* Fix failed to prepare DistSQL
Signed-off-by: 吴伟杰 <wu...@apache.org>
---
.../bind/OpenGaussComBatchBindPacketTest.java | 9 --
.../PostgreSQLPreparedStatementRegistry.java | 107 ---------------------
.../PostgreSQLCommandPacketFactoryTest.java | 13 ---
.../PostgreSQLPreparedStatementRegistryTest.java | 65 -------------
.../OpenGaussAuthenticationEngine.java | 5 +-
.../bind/OpenGaussComBatchBindExecutor.java | 5 +-
.../bind/OpenGaussComBatchBindExecutorTest.java | 11 ++-
.../postgresql/PostgreSQLFrontendEngine.java | 2 -
.../PostgreSQLAuthenticationEngine.java | 5 +-
.../command/query/extended/JDBCPortal.java | 1 -
...AggregatedBatchedStatementsCommandExecutor.java | 4 +-
.../PostgreSQLBatchedStatementsExecutor.java | 1 -
.../extended/PostgreSQLPreparedStatement.java | 9 +-
.../extended/bind/PostgreSQLComBindExecutor.java | 5 +-
.../extended/close/PostgreSQLComCloseExecutor.java | 3 +-
.../describe/PostgreSQLComDescribeExecutor.java | 5 +-
.../extended/parse/PostgreSQLComParseExecutor.java | 11 ++-
.../command/query/extended/JDBCPortalTest.java | 1 -
...egatedBatchedStatementsCommandExecutorTest.java | 8 +-
.../PostgreSQLBatchedStatementsExecutorTest.java | 3 +-
.../bind/PostgreSQLComBindExecutorTest.java | 51 +++-------
.../close/PostgreSQLComCloseExecutorTest.java | 10 +-
.../PostgreSQLComDescribeExecutorTest.java | 28 ++----
.../parse/PostgreSQLComParseExecutorTest.java | 46 +++++++--
24 files changed, 95 insertions(+), 313 deletions(-)
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-opengauss/src/test/java/org/apache/shardingsphere/db/protocol/opengauss/packet/command/query/extended/bind/OpenGaussComBatchBindPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-opengauss/src/test/java/org/apache/shardingsphere/db/protocol/opengauss/packet/command/query/extended/bind/OpenGaussComBatchBindPacketTest.java
index 9cf45727dd6..d10a28ebb79 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-opengauss/src/test/java/org/apache/shardingsphere/db/protocol/opengauss/packet/command/query/extended/bind/OpenGaussComBatchBindPacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-opengauss/src/test/java/org/apache/shardingsphere/db/protocol/opengauss/packet/command/query/extended/bind/OpenGaussComBatchBindPacketTest.java
@@ -19,9 +19,7 @@ package org.apache.shardingsphere.db.protocol.opengauss.packet.command.query.ext
import io.netty.buffer.Unpooled;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.junit.Test;
import java.nio.charset.StandardCharsets;
@@ -31,7 +29,6 @@ import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mock;
public final class OpenGaussComBatchBindPacketTest {
@@ -49,14 +46,8 @@ public final class OpenGaussComBatchBindPacketTest {
0x45, 0x00, 0x00, 0x00, 0x00, 0x00,
};
- private static final int CONNECTION_ID = 1;
-
@Test
public void assertConstructOpenGaussComBatchBindPacket() {
- PostgreSQLPreparedStatementRegistry.getInstance().register(CONNECTION_ID);
- SQLStatement expectedSQLStatement = mock(SQLStatement.class);
- List<PostgreSQLColumnType> columnTypes = Arrays.asList(PostgreSQLColumnType.POSTGRESQL_TYPE_INT4, PostgreSQLColumnType.POSTGRESQL_TYPE_VARCHAR, PostgreSQLColumnType.POSTGRESQL_TYPE_INT4);
- PostgreSQLPreparedStatementRegistry.getInstance().register(CONNECTION_ID, "S_1", "", expectedSQLStatement, columnTypes);
PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(Unpooled.wrappedBuffer(BATCH_BIND_MESSAGE_BYTES), StandardCharsets.UTF_8);
assertThat(payload.readInt1(), is((int) 'U'));
OpenGaussComBatchBindPacket actual = new OpenGaussComBatchBindPacket(payload);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/PostgreSQLPreparedStatementRegistry.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/PostgreSQLPreparedStatementRegistry.java
deleted file mode 100644
index 8f70c535011..00000000000
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/PostgreSQLPreparedStatementRegistry.java
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- * 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.db.protocol.postgresql.packet.command.query.extended;
-
-import lombok.AccessLevel;
-import lombok.Getter;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-
-import java.util.List;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-
-/**
- * PostgreSQL prepared statement registry.
- */
-@NoArgsConstructor(access = AccessLevel.NONE)
-public final class PostgreSQLPreparedStatementRegistry {
-
- private static final PostgreSQLPreparedStatementRegistry INSTANCE = new PostgreSQLPreparedStatementRegistry();
-
- private final ConcurrentMap<Integer, PostgreSQLConnectionPreparedStatementRegistry> connectionPreparedStatements = new ConcurrentHashMap<>(65535, 1);
-
- /**
- * Get prepared statement registry instance.
- *
- * @return prepared statement registry instance
- */
- public static PostgreSQLPreparedStatementRegistry getInstance() {
- return INSTANCE;
- }
-
- /**
- * Register.
- *
- * @param connectionId connection ID
- */
- public void register(final int connectionId) {
- connectionPreparedStatements.put(connectionId, new PostgreSQLConnectionPreparedStatementRegistry());
- }
-
- /**
- * Register.
- *
- * @param connectionId connection ID
- * @param statementId statement ID
- * @param sql SQL
- * @param sqlStatement sql statement
- * @param columnTypes column types
- */
- public void register(final int connectionId, final String statementId, final String sql, final SQLStatement sqlStatement, final List<PostgreSQLColumnType> columnTypes) {
- connectionPreparedStatements.get(connectionId).getPreparedStatements().put(statementId, new PostgreSQLPreparedStatement(sql, sqlStatement, columnTypes));
- }
-
- /**
- * Get prepared statement.
- *
- * @param connectionId connection ID
- * @param statementId statement ID
- * @return prepared statement
- */
- public PostgreSQLPreparedStatement get(final int connectionId, final String statementId) {
- return connectionPreparedStatements.get(connectionId).preparedStatements.get(statementId);
- }
-
- /**
- * Unregister.
- *
- * @param connectionId connection ID
- */
- public void unregister(final int connectionId) {
- connectionPreparedStatements.remove(connectionId);
- }
-
- /**
- * Close statement.
- *
- * @param connectionId connection ID
- * @param statementId statement ID
- */
- public void closeStatement(final int connectionId, final String statementId) {
- if (connectionPreparedStatements.containsKey(connectionId)) {
- connectionPreparedStatements.get(connectionId).getPreparedStatements().remove(statementId);
- }
- }
-
- @Getter
- private static class PostgreSQLConnectionPreparedStatementRegistry {
-
- private final ConcurrentMap<String, PostgreSQLPreparedStatement> preparedStatements = new ConcurrentHashMap<>(65535, 1);
- }
-}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/PostgreSQLCommandPacketFactoryTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/PostgreSQLCommandPacketFactoryTest.java
index 2ccdef33ce6..0652a7ef748 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/PostgreSQLCommandPacketFactoryTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/PostgreSQLCommandPacketFactoryTest.java
@@ -19,20 +19,14 @@ package org.apache.shardingsphere.db.protocol.postgresql.packet.command;
import io.netty.buffer.ByteBuf;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLAggregatedCommandPacket;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.simple.PostgreSQLComQueryPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLComTerminationPacket;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.EmptyStatement;
-import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
-import java.util.Collections;
-
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
@@ -44,13 +38,6 @@ public final class PostgreSQLCommandPacketFactoryTest {
@Mock
private PostgreSQLPacketPayload payload;
- @Before
- public void init() {
- PostgreSQLPreparedStatementRegistry.getInstance().register(1);
- PostgreSQLPreparedStatementRegistry.getInstance().register(1, "sts-id", "", new EmptyStatement(),
- Collections.singletonList(PostgreSQLColumnType.POSTGRESQL_TYPE_INT8));
- }
-
@Test
public void assertNewInstanceWithQueryComPacket() {
when(payload.getByteBuf()).thenReturn(mock(ByteBuf.class));
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/PostgreSQLPreparedStatementRegistryTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/PostgreSQLPreparedStatementRegistryTest.java
deleted file mode 100644
index aa739879cca..00000000000
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/PostgreSQLPreparedStatementRegistryTest.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * 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.db.protocol.postgresql.packet.command.query.extended;
-
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import org.junit.Test;
-
-import java.util.Collections;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mock;
-
-public final class PostgreSQLPreparedStatementRegistryTest {
-
- @Test
- public void assertRegister() {
- String statementId = "stat-id";
- String sql = "select * from t_order";
- PostgreSQLPreparedStatementRegistry.getInstance().register(1);
- PostgreSQLPreparedStatementRegistry.getInstance().register(1, statementId, sql, mock(SQLStatement.class), Collections.emptyList());
- PostgreSQLPreparedStatement preparedStatement = PostgreSQLPreparedStatementRegistry.getInstance().get(1, statementId);
- assertThat(preparedStatement.getSql(), is(sql));
- assertTrue(preparedStatement.getParameterTypes().isEmpty());
- }
-
- @Test
- public void assertCloseStatement() {
- final int connectionId = 2;
- final String statementId = "S_2";
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId);
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId, statementId, "", mock(SQLStatement.class), Collections.emptyList());
- assertNotNull(PostgreSQLPreparedStatementRegistry.getInstance().get(connectionId, statementId));
- PostgreSQLPreparedStatementRegistry.getInstance().closeStatement(connectionId, statementId);
- assertNull(PostgreSQLPreparedStatementRegistry.getInstance().get(connectionId, statementId));
- }
-
- @Test(expected = NullPointerException.class)
- public void assertUnregister() {
- final int connectionId = 3;
- final String statementId = "";
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId);
- PostgreSQLPreparedStatementRegistry.getInstance().get(connectionId, statementId);
- PostgreSQLPreparedStatementRegistry.getInstance().unregister(connectionId);
- PostgreSQLPreparedStatementRegistry.getInstance().get(connectionId, statementId);
- }
-}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java
index 59f35b36397..0364f4451be 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java
@@ -25,7 +25,6 @@ import org.apache.shardingsphere.db.protocol.payload.PacketPayload;
import org.apache.shardingsphere.proxy.backend.text.admin.postgresql.PostgreSQLCharacterSets;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLErrorCode;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLServerInfo;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLReadyForQueryPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLAuthenticationOKPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLComStartupPacket;
@@ -83,9 +82,7 @@ public final class OpenGaussAuthenticationEngine implements AuthenticationEngine
@Override
public int handshake(final ChannelHandlerContext context) {
- int result = ConnectionIdGenerator.getInstance().nextId();
- PostgreSQLPreparedStatementRegistry.getInstance().register(result);
- return result;
+ return ConnectionIdGenerator.getInstance().nextId();
}
@Override
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutor.java
index 56f22b0e041..e3c02135759 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutor.java
@@ -20,8 +20,7 @@ package org.apache.shardingsphere.proxy.frontend.opengauss.command.query.extende
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.db.protocol.opengauss.packet.command.query.extended.bind.OpenGaussComBatchBindPacket;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatement;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLBindCompletePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLCommandCompletePacket;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
@@ -47,7 +46,7 @@ public final class OpenGaussComBatchBindExecutor implements CommandExecutor {
@Override
public Collection<DatabasePacket<?>> execute() throws SQLException {
connectionSession.getBackendConnection().handleAutoCommit();
- PostgreSQLPreparedStatement preparedStatement = PostgreSQLPreparedStatementRegistry.getInstance().get(connectionSession.getConnectionId(), packet.getStatementId());
+ PostgreSQLPreparedStatement preparedStatement = connectionSession.getPreparedStatementRegistry().getPreparedStatement(packet.getStatementId());
int updateCount = new PostgreSQLBatchedStatementsExecutor(connectionSession, preparedStatement, packet.readParameterSets(preparedStatement.getParameterTypes())).executeBatch();
return Arrays.asList(PostgreSQLBindCompletePacket.getInstance(), createCommandComplete(preparedStatement.getSqlStatement(), updateCount));
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
index 1c0637c812a..72a86116cc4 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.proxy.frontend.opengauss.command.query.extende
import org.apache.shardingsphere.db.protocol.opengauss.packet.command.query.extended.bind.OpenGaussComBatchBindPacket;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLBindCompletePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLCommandCompletePacket;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
@@ -33,7 +32,9 @@ import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.JDB
import org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.JDBCBackendStatement;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.apache.shardingsphere.proxy.backend.session.PreparedStatementRegistry;
import org.apache.shardingsphere.proxy.frontend.opengauss.ProxyContextRestorer;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule;
@@ -76,7 +77,6 @@ public final class OpenGaussComBatchBindExecutorTest extends ProxyContextRestore
ShardingSphereRuleMetaData globalRuleMetaData = mock(ShardingSphereRuleMetaData.class);
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
when(globalRuleMetaData.getSingleRule(SQLTranslatorRule.class)).thenReturn(new SQLTranslatorRule(new DefaultSQLTranslatorRuleConfigurationBuilder().build()));
- int connectionId = 1;
String statement = "S_1";
OpenGaussComBatchBindPacket packet = mock(OpenGaussComBatchBindPacket.class);
when(packet.getStatementId()).thenReturn("S_1");
@@ -93,9 +93,10 @@ public final class OpenGaussComBatchBindExecutorTest extends ProxyContextRestore
when(backendStatement.createStorageResource(any(ExecutionUnit.class), any(Connection.class), any(ConnectionMode.class), any(StatementOption.class))).thenReturn(preparedStatement);
when(connectionSession.getStatementManager()).thenReturn(backendStatement);
when(connectionSession.getBackendConnection()).thenReturn(backendConnection);
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId);
- SQLStatement sqlStatement = SQL_PARSER_ENGINE.parse("insert into bmsql (id) values (?)", false);
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId, statement, "", sqlStatement, Collections.emptyList());
+ when(connectionSession.getPreparedStatementRegistry()).thenReturn(new PreparedStatementRegistry());
+ String sql = "insert into bmsql (id) values (?)";
+ SQLStatement sqlStatement = SQL_PARSER_ENGINE.parse(sql, false);
+ connectionSession.getPreparedStatementRegistry().addPreparedStatement(statement, new PostgreSQLPreparedStatement(sql, sqlStatement, null, Collections.emptyList()));
OpenGaussComBatchBindExecutor executor = new OpenGaussComBatchBindExecutor(packet, connectionSession);
Iterator<DatabasePacket<?>> actualPacketsIterator = executor.execute().iterator();
assertThat(actualPacketsIterator.next(), is(PostgreSQLBindCompletePacket.getInstance()));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
index 93a0b89802f..a589ab1b741 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
@@ -22,7 +22,6 @@ import org.apache.shardingsphere.db.protocol.codec.DatabasePacketCodecEngine;
import org.apache.shardingsphere.db.protocol.postgresql.codec.PostgreSQLPacketCodecEngine;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLServerInfo;
import org.apache.shardingsphere.db.protocol.postgresql.packet.PostgreSQLPacket;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.proxy.backend.exception.InTransactionException;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.frontend.authentication.AuthenticationEngine;
@@ -54,7 +53,6 @@ public final class PostgreSQLFrontendEngine implements DatabaseProtocolFrontendE
@Override
public void release(final ConnectionSession connectionSession) {
- PostgreSQLPreparedStatementRegistry.getInstance().unregister(connectionSession.getConnectionId());
PostgreSQLConnectionContextRegistry.getInstance().remove(connectionSession.getConnectionId());
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
index f29dda38b5b..ab8305071d8 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
@@ -25,7 +25,6 @@ import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLAuthe
import org.apache.shardingsphere.proxy.backend.text.admin.postgresql.PostgreSQLCharacterSets;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLErrorCode;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLServerInfo;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLReadyForQueryPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLAuthenticationOKPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLComStartupPacket;
@@ -68,9 +67,7 @@ public final class PostgreSQLAuthenticationEngine implements AuthenticationEngin
@Override
public int handshake(final ChannelHandlerContext context) {
- int result = ConnectionIdGenerator.getInstance().nextId();
- PostgreSQLPreparedStatementRegistry.getInstance().register(result);
- return result;
+ return ConnectionIdGenerator.getInstance().nextId();
}
@Override
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortal.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortal.java
index 0c7c4cb8dc8..0b2ea7952c9 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortal.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortal.java
@@ -30,7 +30,6 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.Pos
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLNoDataPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLRowDescriptionPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.execute.PostgreSQLPortalSuspendedPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLCommandCompletePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLErrorResponsePacket;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutor.java
index ae212fb3be4..c0df323b5fa 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutor.java
@@ -22,8 +22,6 @@ import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLNoDataPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatement;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLBindCompletePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLComBindPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.describe.PostgreSQLComDescribePacket;
@@ -73,7 +71,7 @@ public final class PostgreSQLAggregatedBatchedStatementsCommandExecutor implemen
private PostgreSQLPreparedStatement getPreparedStatement() {
PostgreSQLComBindPacket bindPacket = (PostgreSQLComBindPacket) packets.get(0);
- return PostgreSQLPreparedStatementRegistry.getInstance().get(connectionSession.getConnectionId(), bindPacket.getStatementId());
+ return connectionSession.getPreparedStatementRegistry().getPreparedStatement(bindPacket.getStatementId());
}
private List<List<Object>> readParameterSets(final List<PostgreSQLColumnType> parameterTypes) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutor.java
index 8deae21382d..aa3e7417070 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutor.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLTypeUnspecifiedSQLParameter;
import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/PostgreSQLPreparedStatement.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLPreparedStatement.java
similarity index 80%
rename from shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/PostgreSQLPreparedStatement.java
rename to shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLPreparedStatement.java
index 15419608de8..ff13967eb84 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/PostgreSQLPreparedStatement.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLPreparedStatement.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended;
+package org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended;
import lombok.AccessLevel;
import lombok.Getter;
@@ -23,6 +23,9 @@ import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.db.protocol.postgresql.packet.PostgreSQLPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLParameterDescriptionPacket;
+import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.proxy.backend.session.PreparedStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.List;
@@ -34,12 +37,14 @@ import java.util.Optional;
@RequiredArgsConstructor
@Getter
@Setter
-public final class PostgreSQLPreparedStatement {
+public final class PostgreSQLPreparedStatement implements PreparedStatement {
private final String sql;
private final SQLStatement sqlStatement;
+ private final SQLStatementContext<?> sqlStatementContext;
+
private final List<PostgreSQLColumnType> parameterTypes;
@Getter(AccessLevel.NONE)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutor.java
index b3e9cd88998..6333578f525 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutor.java
@@ -19,8 +19,7 @@ package org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extend
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatement;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLBindCompletePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLComBindPacket;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.JDBCBackendConnection;
@@ -48,7 +47,7 @@ public final class PostgreSQLComBindExecutor implements CommandExecutor {
@Override
public Collection<DatabasePacket<?>> execute() throws SQLException {
connectionSession.getBackendConnection().handleAutoCommit();
- PostgreSQLPreparedStatement preparedStatement = PostgreSQLPreparedStatementRegistry.getInstance().get(connectionSession.getConnectionId(), packet.getStatementId());
+ PostgreSQLPreparedStatement preparedStatement = connectionSession.getPreparedStatementRegistry().getPreparedStatement(packet.getStatementId());
JDBCBackendConnection backendConnection = (JDBCBackendConnection) connectionSession.getBackendConnection();
JDBCPortal portal = new JDBCPortal(packet.getPortal(), preparedStatement, packet.readParameters(preparedStatement.getParameterTypes()), packet.readResultFormats(), backendConnection);
connectionContext.addPortal(portal);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/close/PostgreSQLComCloseExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/close/PostgreSQLComCloseExecutor.java
index 6930d2fcceb..e01fe9ab657 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/close/PostgreSQLComCloseExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/close/PostgreSQLComCloseExecutor.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extend
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.close.PostgreSQLCloseCompletePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.close.PostgreSQLComClosePacket;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
@@ -46,7 +45,7 @@ public final class PostgreSQLComCloseExecutor implements CommandExecutor {
public Collection<DatabasePacket<?>> execute() throws SQLException {
switch (packet.getType()) {
case PREPARED_STATEMENT:
- PostgreSQLPreparedStatementRegistry.getInstance().closeStatement(connectionSession.getConnectionId(), packet.getName());
+ connectionSession.getPreparedStatementRegistry().removePreparedStatement(packet.getName());
break;
case PORTAL:
closePortal();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutor.java
index 6d0e5df7bd5..5b627c05616 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutor.java
@@ -25,8 +25,7 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.Pos
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLNoDataPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLRowDescriptionPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatement;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.describe.PostgreSQLComDescribePacket;
import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
@@ -94,7 +93,7 @@ public final class PostgreSQLComDescribeExecutor implements CommandExecutor {
private List<DatabasePacket<?>> describePreparedStatement() throws SQLException {
List<DatabasePacket<?>> result = new ArrayList<>(2);
- PostgreSQLPreparedStatement preparedStatement = PostgreSQLPreparedStatementRegistry.getInstance().get(connectionSession.getConnectionId(), packet.getName());
+ PostgreSQLPreparedStatement preparedStatement = connectionSession.getPreparedStatementRegistry().getPreparedStatement(packet.getName());
result.add(preparedStatement.describeParameters());
Optional<PostgreSQLPacket> rowDescription = preparedStatement.describeRows();
if (rowDescription.isPresent()) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutor.java
index 68e359f3442..8e4cfd718ba 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutor.java
@@ -20,9 +20,11 @@ package org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extend
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.parse.PostgreSQLComParsePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.parse.PostgreSQLParseCompletePacket;
+import org.apache.shardingsphere.distsql.parser.statement.DistSQLStatement;
+import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
@@ -30,6 +32,7 @@ import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.frontend.command.executor.CommandExecutor;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.sql.parser.sql.common.constant.ParameterMarkerType;
import org.apache.shardingsphere.sql.parser.sql.common.segment.SQLSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.ParameterMarkerSegment;
@@ -63,7 +66,11 @@ public final class PostgreSQLComParseExecutor implements CommandExecutor {
sqlStatement = sqlParserEngine.parse(sql, true);
}
List<PostgreSQLColumnType> paddedColumnTypes = paddingColumnTypes(sqlStatement.getParameterCount(), packet.readParameterTypes());
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionSession.getConnectionId(), packet.getStatementId(), sql, sqlStatement, paddedColumnTypes);
+ SQLStatementContext<?> sqlStatementContext = !(sqlStatement instanceof DistSQLStatement)
+ ? SQLStatementContextFactory.newInstance(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases(),
+ sqlStatement, connectionSession.getDefaultDatabaseName())
+ : null;
+ connectionSession.getPreparedStatementRegistry().addPreparedStatement(packet.getStatementId(), new PostgreSQLPreparedStatement(sql, sqlStatement, sqlStatementContext, paddedColumnTypes));
return Collections.singletonList(PostgreSQLParseCompletePacket.getInstance());
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortalTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortalTest.java
index 745e2318721..f4608d00b3f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortalTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortalTest.java
@@ -24,7 +24,6 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.Pos
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLEmptyQueryResponsePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLNoDataPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLRowDescriptionPacket;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.execute.PostgreSQLPortalSuspendedPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLCommandCompletePacket;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java
index 5b882cc93ae..a555d10aafd 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java
@@ -21,7 +21,6 @@ import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLNoDataPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLBindCompletePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLComBindPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.describe.PostgreSQLComDescribePacket;
@@ -38,6 +37,7 @@ import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.JDB
import org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.JDBCBackendStatement;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.apache.shardingsphere.proxy.backend.session.PreparedStatementRegistry;
import org.apache.shardingsphere.proxy.frontend.postgresql.ProxyContextRestorer;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule;
@@ -87,10 +87,10 @@ public final class PostgreSQLAggregatedBatchedStatementsCommandExecutorTest exte
ShardingSphereRuleMetaData globalRuleMetaData = mock(ShardingSphereRuleMetaData.class);
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
when(globalRuleMetaData.getSingleRule(SQLTranslatorRule.class)).thenReturn(new SQLTranslatorRule(new DefaultSQLTranslatorRuleConfigurationBuilder().build()));
- PostgreSQLPreparedStatementRegistry.getInstance().register(CONNECTION_ID);
- PostgreSQLPreparedStatementRegistry.getInstance().register(CONNECTION_ID, STATEMENT_ID, SQL, SQL_PARSER_ENGINE.parse(SQL, false),
- Collections.singletonList(PostgreSQLColumnType.POSTGRESQL_TYPE_INT4));
ConnectionSession connectionSession = mock(ConnectionSession.class);
+ when(connectionSession.getPreparedStatementRegistry()).thenReturn(new PreparedStatementRegistry());
+ connectionSession.getPreparedStatementRegistry().addPreparedStatement(STATEMENT_ID,
+ new PostgreSQLPreparedStatement(SQL, SQL_PARSER_ENGINE.parse(SQL, false), null, Collections.singletonList(PostgreSQLColumnType.POSTGRESQL_TYPE_INT4)));
when(connectionSession.getConnectionId()).thenReturn(CONNECTION_ID);
JDBCBackendConnection backendConnection = mock(JDBCBackendConnection.class);
Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java
index 17094e159cc..b1c38592ab0 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extend
import lombok.SneakyThrows;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLTypeUnspecifiedSQLParameter;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
@@ -95,7 +94,7 @@ public final class PostgreSQLBatchedStatementsExecutorTest extends ProxyContextR
public void assertExecuteBatch() throws SQLException {
PostgreSQLInsertStatement insertStatement = mock(PostgreSQLInsertStatement.class, RETURNS_DEEP_STUBS);
when(insertStatement.getTable().getTableName().getIdentifier().getValue()).thenReturn("t");
- PostgreSQLPreparedStatement postgreSQLPreparedStatement = new PostgreSQLPreparedStatement("insert into t (id, col) values (?, ?)", insertStatement,
+ PostgreSQLPreparedStatement postgreSQLPreparedStatement = new PostgreSQLPreparedStatement("insert into t (id, col) values (?, ?)", insertStatement, null,
Arrays.asList(PostgreSQLColumnType.POSTGRESQL_TYPE_INT4, PostgreSQLColumnType.POSTGRESQL_TYPE_VARCHAR));
List<List<Object>> parameterSets = Arrays.asList(Arrays.asList(1, new PostgreSQLTypeUnspecifiedSQLParameter("foo")),
Arrays.asList(2, new PostgreSQLTypeUnspecifiedSQLParameter("bar")), Arrays.asList(3, new PostgreSQLTypeUnspecifiedSQLParameter("baz")));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutorTest.java
index 493dbc7e5b1..fd7d0ca37d5 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/bind/PostgreSQLComBindExecutorTest.java
@@ -18,15 +18,15 @@
package org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.bind;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLBindCompletePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLComBindPacket;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.JDBCBackendConnection;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.apache.shardingsphere.proxy.backend.session.PreparedStatementRegistry;
import org.apache.shardingsphere.proxy.frontend.postgresql.command.PostgreSQLConnectionContext;
import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.JDBCPortal;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.EmptyStatement;
-import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
@@ -36,7 +36,6 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
-import java.util.Iterator;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -61,46 +60,18 @@ public final class PostgreSQLComBindExecutorTest {
@InjectMocks
private PostgreSQLComBindExecutor executor;
- @Before
- public void setup() {
- PostgreSQLPreparedStatementRegistry.getInstance().register(1);
- PostgreSQLPreparedStatementRegistry.getInstance().register(1, "1", "", new EmptyStatement(), Collections.emptyList());
- when(bindPacket.getStatementId()).thenReturn("1");
+ @Test
+ public void assertExecuteBind() throws SQLException {
+ when(connectionSession.getPreparedStatementRegistry()).thenReturn(new PreparedStatementRegistry());
+ JDBCBackendConnection backendConnection = mock(JDBCBackendConnection.class);
+ when(connectionSession.getBackendConnection()).thenReturn(backendConnection);
+ when(backendConnection.getConnectionSession()).thenReturn(connectionSession);
+ String statementId = "S_1";
+ connectionSession.getPreparedStatementRegistry().addPreparedStatement(statementId, new PostgreSQLPreparedStatement("", new EmptyStatement(), null, Collections.emptyList()));
+ when(bindPacket.getStatementId()).thenReturn(statementId);
when(bindPacket.getPortal()).thenReturn("C_1");
when(bindPacket.readParameters(anyList())).thenReturn(Collections.emptyList());
when(bindPacket.readResultFormats()).thenReturn(Collections.emptyList());
- when(connectionSession.getConnectionId()).thenReturn(1);
- JDBCBackendConnection backendConnection = mock(JDBCBackendConnection.class);
- when(connectionSession.getBackendConnection()).thenReturn(backendConnection);
- }
-
- @Test
- public void assertExecuteEmptyBindPacket() throws SQLException {
- Collection<DatabasePacket<?>> actual = executor.execute();
- assertThat(actual.size(), is(1));
- assertThat(actual.iterator().next(), is(PostgreSQLBindCompletePacket.getInstance()));
- verify(connectionContext).addPortal(any(JDBCPortal.class));
- }
-
- @Test
- public void assertExecuteBindPacketWithQuerySQLAndReturnEmptyResult() throws SQLException {
- Collection<DatabasePacket<?>> actual = executor.execute();
- assertThat(actual.size(), is(1));
- assertThat(actual.iterator().next(), is(PostgreSQLBindCompletePacket.getInstance()));
- verify(connectionContext).addPortal(any(JDBCPortal.class));
- }
-
- @Test
- public void assertExecuteBindPacketWithQuerySQL() throws SQLException {
- Collection<DatabasePacket<?>> actual = executor.execute();
- assertThat(actual.size(), is(1));
- Iterator<DatabasePacket<?>> actualPackets = actual.iterator();
- assertThat(actualPackets.next(), is(PostgreSQLBindCompletePacket.getInstance()));
- verify(connectionContext).addPortal(any(JDBCPortal.class));
- }
-
- @Test
- public void assertExecuteBindPacketWithUpdateSQL() throws SQLException {
Collection<DatabasePacket<?>> actual = executor.execute();
assertThat(actual.size(), is(1));
assertThat(actual.iterator().next(), is(PostgreSQLBindCompletePacket.getInstance()));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/close/PostgreSQLComCloseExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/close/PostgreSQLComCloseExecutorTest.java
index be8e6f75d5a..01b2d0f3e7f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/close/PostgreSQLComCloseExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/close/PostgreSQLComCloseExecutorTest.java
@@ -18,12 +18,11 @@
package org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.close;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.close.PostgreSQLCloseCompletePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.close.PostgreSQLComClosePacket;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.apache.shardingsphere.proxy.backend.session.PreparedStatementRegistry;
import org.apache.shardingsphere.proxy.frontend.postgresql.command.PostgreSQLConnectionContext;
-import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -50,14 +49,9 @@ public final class PostgreSQLComCloseExecutorTest {
@Mock
private ConnectionSession connectionSession;
- @Before
- public void setup() {
- PostgreSQLPreparedStatementRegistry.getInstance().register(1);
- when(connectionSession.getConnectionId()).thenReturn(1);
- }
-
@Test
public void assertExecuteClosePreparedStatement() throws SQLException {
+ when(connectionSession.getPreparedStatementRegistry()).thenReturn(new PreparedStatementRegistry());
when(packet.getType()).thenReturn(PostgreSQLComClosePacket.Type.PREPARED_STATEMENT);
when(packet.getName()).thenReturn("S_1");
PostgreSQLComCloseExecutor closeExecutor = new PostgreSQLComCloseExecutor(connectionContext, packet, connectionSession);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java
index ae46e695e4f..1dffcbf0664 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java
@@ -25,7 +25,6 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.Pos
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLParameterDescriptionPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.PostgreSQLRowDescriptionPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.describe.PostgreSQLComDescribePacket;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
@@ -40,9 +39,11 @@ import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.JDBCBackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.apache.shardingsphere.proxy.backend.session.PreparedStatementRegistry;
import org.apache.shardingsphere.proxy.frontend.postgresql.ProxyContextRestorer;
import org.apache.shardingsphere.proxy.frontend.postgresql.command.PostgreSQLConnectionContext;
import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.Portal;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule;
@@ -109,6 +110,7 @@ public final class PostgreSQLComDescribeExecutorTest extends ProxyContextRestore
ProxyContext.init(contextManager);
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().getValue(ConfigurationPropertyKey.SQL_SHOW)).thenReturn(false);
when(connectionSession.getDatabaseName()).thenReturn(DATABASE_NAME);
+ when(connectionSession.getPreparedStatementRegistry()).thenReturn(new PreparedStatementRegistry());
ShardingSphereRuleMetaData globalRuleMetaData = mock(ShardingSphereRuleMetaData.class);
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
when(globalRuleMetaData.getSingleRule(SQLTranslatorRule.class)).thenReturn(new SQLTranslatorRule(new DefaultSQLTranslatorRuleConfigurationBuilder().build()));
@@ -145,16 +147,13 @@ public final class PostgreSQLComDescribeExecutorTest extends ProxyContextRestore
when(packet.getType()).thenReturn('S');
final String statementId = "S_1";
when(packet.getName()).thenReturn(statementId);
- final int connectionId = 1;
- when(connectionSession.getConnectionId()).thenReturn(connectionId);
String sql = "insert into t_order values (?, 0, 'char', ?), (2, ?, ?, '')";
SQLStatement sqlStatement = SQL_PARSER_ENGINE.parse(sql, false);
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId);
List<PostgreSQLColumnType> parameterTypes = new ArrayList<>(sqlStatement.getParameterCount());
for (int i = 0; i < sqlStatement.getParameterCount(); i++) {
parameterTypes.add(PostgreSQLColumnType.POSTGRESQL_TYPE_UNSPECIFIED);
}
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId, statementId, sql, sqlStatement, parameterTypes);
+ connectionSession.getPreparedStatementRegistry().addPreparedStatement(statementId, new PostgreSQLPreparedStatement(sql, sqlStatement, null, parameterTypes));
Collection<DatabasePacket<?>> actualPackets = executor.execute();
assertThat(actualPackets.size(), is(2));
Iterator<DatabasePacket<?>> actualPacketsIterator = actualPackets.iterator();
@@ -172,16 +171,13 @@ public final class PostgreSQLComDescribeExecutorTest extends ProxyContextRestore
when(packet.getType()).thenReturn('S');
final String statementId = "S_2";
when(packet.getName()).thenReturn(statementId);
- final int connectionId = 1;
- when(connectionSession.getConnectionId()).thenReturn(connectionId);
String sql = "insert into t_order (id, k, c, pad) values (1, ?, ?, ?), (?, 2, ?, '')";
SQLStatement sqlStatement = SQL_PARSER_ENGINE.parse(sql, false);
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId);
List<PostgreSQLColumnType> parameterTypes = new ArrayList<>(sqlStatement.getParameterCount());
for (int i = 0; i < sqlStatement.getParameterCount(); i++) {
parameterTypes.add(PostgreSQLColumnType.POSTGRESQL_TYPE_UNSPECIFIED);
}
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId, statementId, sql, sqlStatement, parameterTypes);
+ connectionSession.getPreparedStatementRegistry().addPreparedStatement(statementId, new PostgreSQLPreparedStatement(sql, sqlStatement, null, parameterTypes));
Collection<DatabasePacket<?>> actualPackets = executor.execute();
assertThat(actualPackets.size(), is(2));
Iterator<DatabasePacket<?>> actualPacketsIterator = actualPackets.iterator();
@@ -199,16 +195,13 @@ public final class PostgreSQLComDescribeExecutorTest extends ProxyContextRestore
when(packet.getType()).thenReturn('S');
final String statementId = "S_2";
when(packet.getName()).thenReturn(statementId);
- final int connectionId = 1;
- when(connectionSession.getConnectionId()).thenReturn(connectionId);
String sql = "insert into t_order (iD, k, c, PaD) values (1, ?, ?, ?), (?, 2, ?, '')";
SQLStatement sqlStatement = SQL_PARSER_ENGINE.parse(sql, false);
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId);
List<PostgreSQLColumnType> parameterTypes = new ArrayList<>(sqlStatement.getParameterCount());
for (int i = 0; i < sqlStatement.getParameterCount(); i++) {
parameterTypes.add(PostgreSQLColumnType.POSTGRESQL_TYPE_UNSPECIFIED);
}
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId, statementId, sql, sqlStatement, parameterTypes);
+ connectionSession.getPreparedStatementRegistry().addPreparedStatement(statementId, new PostgreSQLPreparedStatement(sql, sqlStatement, null, parameterTypes));
Collection<DatabasePacket<?>> actualPackets = executor.execute();
assertThat(actualPackets.size(), is(2));
Iterator<DatabasePacket<?>> actualPacketsIterator = actualPackets.iterator();
@@ -226,16 +219,13 @@ public final class PostgreSQLComDescribeExecutorTest extends ProxyContextRestore
when(packet.getType()).thenReturn('S');
final String statementId = "S_2";
when(packet.getName()).thenReturn(statementId);
- final int connectionId = 1;
- when(connectionSession.getConnectionId()).thenReturn(connectionId);
String sql = "insert into t_order (undefined_column, k, c, pad) values (1, ?, ?, ?), (?, 2, ?, '')";
SQLStatement sqlStatement = SQL_PARSER_ENGINE.parse(sql, false);
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId);
List<PostgreSQLColumnType> parameterTypes = new ArrayList<>(sqlStatement.getParameterCount());
for (int i = 0; i < sqlStatement.getParameterCount(); i++) {
parameterTypes.add(PostgreSQLColumnType.POSTGRESQL_TYPE_UNSPECIFIED);
}
- PostgreSQLPreparedStatementRegistry.getInstance().register(connectionId, statementId, sql, sqlStatement, parameterTypes);
+ connectionSession.getPreparedStatementRegistry().addPreparedStatement(statementId, new PostgreSQLPreparedStatement(sql, sqlStatement, null, parameterTypes));
SQLException actual = null;
try {
executor.execute();
@@ -251,13 +241,11 @@ public final class PostgreSQLComDescribeExecutorTest extends ProxyContextRestore
when(packet.getType()).thenReturn('S');
String statementId = "S_3";
when(packet.getName()).thenReturn(statementId);
- when(connectionSession.getConnectionId()).thenReturn(1);
final String sql = "select id, k, c, pad from t_order where id = ?";
SQLStatement sqlStatement = SQL_PARSER_ENGINE.parse(sql, false);
prepareJDBCBackendConnection(sql);
- PostgreSQLPreparedStatementRegistry.getInstance().register(1);
List<PostgreSQLColumnType> parameterTypes = new ArrayList<>(Collections.singleton(PostgreSQLColumnType.POSTGRESQL_TYPE_UNSPECIFIED));
- PostgreSQLPreparedStatementRegistry.getInstance().register(1, statementId, sql, sqlStatement, parameterTypes);
+ connectionSession.getPreparedStatementRegistry().addPreparedStatement(statementId, new PostgreSQLPreparedStatement(sql, sqlStatement, null, parameterTypes));
Collection<DatabasePacket<?>> actual = executor.execute();
assertThat(actual.size(), is(2));
Iterator<DatabasePacket<?>> actualPacketsIterator = actual.iterator();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutorTest.java
index b41f47bd2f3..a2942abf569 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutorTest.java
@@ -19,10 +19,10 @@ package org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extend
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatement;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.parse.PostgreSQLComParsePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.parse.PostgreSQLParseCompletePacket;
+import org.apache.shardingsphere.distsql.parser.statement.ral.common.queryable.ShowVariableStatement;
+import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.mode.manager.ContextManager;
@@ -30,7 +30,9 @@ import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.apache.shardingsphere.proxy.backend.session.PreparedStatementRegistry;
import org.apache.shardingsphere.proxy.frontend.postgresql.ProxyContextRestorer;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.PostgreSQLPreparedStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.EmptyStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml.PostgreSQLInsertStatement;
import org.junit.Before;
@@ -39,6 +41,7 @@ import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
+import org.mockito.MockedConstruction;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
@@ -47,15 +50,15 @@ import java.util.Collections;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLComParseExecutorTest extends ProxyContextRestorer {
- private static final int CONNECTION_ID = 1;
-
private final SQLParserRule sqlParserRule = new SQLParserRule(new DefaultSQLParserRuleConfigurationBuilder().build());
@Mock
@@ -73,20 +76,23 @@ public final class PostgreSQLComParseExecutorTest extends ProxyContextRestorer {
@Before
public void setup() {
ProxyContext.init(mockedContextManager);
- PostgreSQLPreparedStatementRegistry.getInstance().register(CONNECTION_ID);
- when(connectionSession.getConnectionId()).thenReturn(CONNECTION_ID);
+ when(connectionSession.getPreparedStatementRegistry()).thenReturn(new PreparedStatementRegistry());
}
+ @SuppressWarnings("rawtypes")
@Test
public void assertExecuteWithEmptySQL() {
final String expectedSQL = "";
final String statementId = "S_1";
when(parsePacket.getSql()).thenReturn(expectedSQL);
when(parsePacket.getStatementId()).thenReturn(statementId);
- Collection<DatabasePacket<?>> actualPackets = executor.execute();
+ Collection<DatabasePacket<?>> actualPackets;
+ try (MockedConstruction<CommonSQLStatementContext> ignored = mockConstruction(CommonSQLStatementContext.class)) {
+ actualPackets = executor.execute();
+ }
assertThat(actualPackets.size(), is(1));
assertThat(actualPackets.iterator().next(), is(PostgreSQLParseCompletePacket.getInstance()));
- PostgreSQLPreparedStatement actualPreparedStatement = PostgreSQLPreparedStatementRegistry.getInstance().get(CONNECTION_ID, statementId);
+ PostgreSQLPreparedStatement actualPreparedStatement = connectionSession.getPreparedStatementRegistry().getPreparedStatement(statementId);
assertThat(actualPreparedStatement.getSqlStatement(), instanceOf(EmptyStatement.class));
assertThat(actualPreparedStatement.getSql(), is(expectedSQL));
assertThat(actualPreparedStatement.getParameterTypes(), is(Collections.emptyList()));
@@ -109,9 +115,31 @@ public final class PostgreSQLComParseExecutorTest extends ProxyContextRestorer {
Collection<DatabasePacket<?>> actualPackets = executor.execute();
assertThat(actualPackets.size(), is(1));
assertThat(actualPackets.iterator().next(), is(PostgreSQLParseCompletePacket.getInstance()));
- PostgreSQLPreparedStatement actualPreparedStatement = PostgreSQLPreparedStatementRegistry.getInstance().get(CONNECTION_ID, statementId);
+ PostgreSQLPreparedStatement actualPreparedStatement = connectionSession.getPreparedStatementRegistry().getPreparedStatement(statementId);
assertThat(actualPreparedStatement.getSqlStatement(), instanceOf(PostgreSQLInsertStatement.class));
assertThat(actualPreparedStatement.getSql(), is(expectedSQL));
assertThat(actualPreparedStatement.getParameterTypes(), is(Arrays.asList(PostgreSQLColumnType.POSTGRESQL_TYPE_INT4, PostgreSQLColumnType.POSTGRESQL_TYPE_UNSPECIFIED)));
}
+
+ @Test
+ public void assertExecuteWithDistSQL() {
+ String sql = "SHOW VARIABLE sql_show";
+ String statementId = "";
+ when(parsePacket.getSql()).thenReturn(sql);
+ when(parsePacket.getStatementId()).thenReturn(statementId);
+ when(connectionSession.getDatabaseName()).thenReturn("db");
+ when(mockedContextManager.getMetaDataContexts().getMetaData().getDatabases().get("db").getResource().getDatabaseType()).thenReturn(new PostgreSQLDatabaseType());
+ when(mockedContextManager.getMetaDataContexts().getMetaData().getDatabases().get("db").getProtocolType()).thenReturn(new PostgreSQLDatabaseType());
+ ShardingSphereRuleMetaData globalRuleMetaData = mock(ShardingSphereRuleMetaData.class);
+ when(mockedContextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
+ when(globalRuleMetaData.getSingleRule(SQLParserRule.class)).thenReturn(sqlParserRule);
+ Collection<DatabasePacket<?>> actualPackets = executor.execute();
+ assertThat(actualPackets.size(), is(1));
+ assertThat(actualPackets.iterator().next(), is(PostgreSQLParseCompletePacket.getInstance()));
+ PostgreSQLPreparedStatement actualPreparedStatement = connectionSession.getPreparedStatementRegistry().getPreparedStatement(statementId);
+ assertThat(actualPreparedStatement.getSql(), is(sql));
+ assertThat(actualPreparedStatement.getSqlStatement(), instanceOf(ShowVariableStatement.class));
+ assertNull(actualPreparedStatement.getSqlStatementContext());
+ assertThat(actualPreparedStatement.getParameterTypes(), is(Collections.emptyList()));
+ }
}