This is an automated email from the ASF dual-hosted git repository.
hucong 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 fb0e0058a28 Refactor create parameter column definition packets
(#38014)
fb0e0058a28 is described below
commit fb0e0058a28e3ace1746c2bddbd3c414588a4463
Author: ZhangCheng <[email protected]>
AuthorDate: Thu Feb 12 10:54:10 2026 +0800
Refactor create parameter column definition packets (#38014)
---
.../prepare/MySQLComStmtPrepareExecutor.java | 26 ++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)
diff --git
a/proxy/frontend/dialect/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java
b/proxy/frontend/dialect/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java
index c25a38f7ffa..2899789aa4e 100644
---
a/proxy/frontend/dialect/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java
+++
b/proxy/frontend/dialect/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java
@@ -56,8 +56,9 @@ import
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatemen
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -100,8 +101,8 @@ public final class MySQLComStmtPrepareExecutor implements
CommandExecutor {
}
private Collection<DatabasePacket> createPackets(final SQLStatementContext
sqlStatementContext, final int statementId, final MySQLServerPreparedStatement
serverPreparedStatement) {
- Collection<DatabasePacket> result = new LinkedList<>();
Collection<Projection> projections =
getProjections(sqlStatementContext);
+ Collection<DatabasePacket> result = new
ArrayList<>(sqlStatementContext.getSqlStatement().getParameterCount() +
projections.size() + 3);
int parameterCount =
sqlStatementContext.getSqlStatement().getParameterCount();
ShardingSpherePreconditions.checkState(parameterCount <=
MAX_PARAMETER_COUNT, TooManyPlaceholdersException::new);
result.add(new MySQLComStmtPrepareOKPacket(statementId,
projections.size(), parameterCount, 0));
@@ -126,6 +127,8 @@ public final class MySQLComStmtPrepareExecutor implements
CommandExecutor {
final MySQLServerPreparedStatement serverPreparedStatement) {
List<ShardingSphereColumn> columnsOfParameterMarkers =
MySQLComStmtPrepareParameterMarkerExtractor.findColumnsOfParameterMarkers(sqlStatementContext.getSqlStatement(),
getSchema(sqlStatementContext));
+ Map<ShardingSphereColumn, MySQLColumnDefinition41Packet>
columnPacketCache = new HashMap<>();
+ MySQLColumnDefinition41Packet defaultColumnPacket = null;
Collection<ParameterMarkerSegment> parameterMarkerSegments =
sqlStatementContext.getSqlStatement().getParameterMarkers();
Collection<MySQLPacket> result = new
ArrayList<>(parameterMarkerSegments.size());
Collection<Integer> paramColumnDefinitionFlags = new
ArrayList<>(parameterMarkerSegments.size());
@@ -137,12 +140,15 @@ public final class MySQLComStmtPrepareExecutor implements
CommandExecutor {
}
if (null != column) {
int columnDefinitionFlag =
calculateColumnDefinitionFlag(column);
+
result.add(createMySQLColumnDefinition41PacketByCache(characterSet,
columnPacketCache, column, columnDefinitionFlag));
MySQLBinaryColumnType columnType =
MySQLBinaryColumnType.valueOfJDBCType(column.getDataType());
- result.add(createMySQLColumnDefinition41Packet(characterSet,
columnDefinitionFlag, columnType));
paramColumnDefinitionFlags.add(columnDefinitionFlag);
parameterColumnTypes.add(columnType);
} else {
- result.add(createMySQLColumnDefinition41Packet(characterSet,
0, MySQLBinaryColumnType.VAR_STRING));
+ if (null == defaultColumnPacket) {
+ defaultColumnPacket =
createMySQLColumnDefinition41Packet(characterSet, 0,
MySQLBinaryColumnType.VAR_STRING);
+ }
+ result.add(defaultColumnPacket);
paramColumnDefinitionFlags.add(0);
parameterColumnTypes.add(MySQLBinaryColumnType.NULL);
}
@@ -152,6 +158,18 @@ public final class MySQLComStmtPrepareExecutor implements
CommandExecutor {
return result;
}
+ private MySQLColumnDefinition41Packet
createMySQLColumnDefinition41PacketByCache(final int characterSet, final
Map<ShardingSphereColumn, MySQLColumnDefinition41Packet> columnPacketCache,
+
final ShardingSphereColumn column,
+
final int columnDefinitionFlag) {
+ MySQLColumnDefinition41Packet cachedPacket =
columnPacketCache.get(column);
+ if (null != cachedPacket) {
+ return cachedPacket;
+ }
+ MySQLColumnDefinition41Packet result =
createMySQLColumnDefinition41Packet(characterSet, columnDefinitionFlag,
MySQLBinaryColumnType.valueOfJDBCType(column.getDataType()));
+ columnPacketCache.put(column, result);
+ return result;
+ }
+
private Collection<MySQLPacket>
createProjectionColumnDefinition41Packets(final SelectStatementContext
selectStatementContext, final int characterSet) {
Collection<Projection> projections =
selectStatementContext.getProjectionsContext().getExpandProjections();
ShardingSphereSchema schema = getSchema(selectStatementContext);