Skip to content

Commit

Permalink
[inner-2340] fix: explain' lost connection....
Browse files Browse the repository at this point in the history
(cherry picked from commit 2b87829)
  • Loading branch information
wenyh1 committed Feb 5, 2024
1 parent c561fc8 commit dfacfa8
Showing 1 changed file with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.sql.SQLSyntaxErrorException;
import java.util.LinkedList;
import java.util.List;

/**
Expand Down Expand Up @@ -202,6 +203,7 @@ private static boolean isInsertSeq(ShardingService service, String stmt, SchemaC
}

public static void writeOutHeadAndEof(ShardingService service, RouteResultset rrs) {
final List<RowDataPacket> rows = getRows(service, rrs);
ByteBuffer buffer = service.allocate();
// writeDirectly header
ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
Expand All @@ -219,12 +221,23 @@ public static void writeOutHeadAndEof(ShardingService service, RouteResultset rr
eof.setPacketId(service.nextPacketId());
buffer = eof.write(buffer, service, true);

// writeDirectly rows
for (RowDataPacket row : rows) {
row.setPacketId(service.nextPacketId());
buffer = row.write(buffer, service, true);
}

// writeDirectly last eof
EOFRowPacket lastEof = new EOFRowPacket();
lastEof.setPacketId(service.nextPacketId());
lastEof.write(buffer, service);
}

private static List<RowDataPacket> getRows(ShardingService service, RouteResultset rrs) {
LinkedList<RowDataPacket> rows = new LinkedList<>();
if (!rrs.isNeedOptimizer()) {
// writeDirectly rows
for (RouteResultsetNode node : rrs.getNodes()) {
RowDataPacket row = getRow(node, service.getCharset().getResults());
row.setPacketId(service.nextPacketId());
buffer = row.write(buffer, service, true);
rows.add(getRow(node, service.getCharset().getResults()));
}
} else {
BaseHandlerBuilder builder = buildNodes(rrs, service);
Expand All @@ -238,9 +251,7 @@ public static void writeOutHeadAndEof(ShardingService service, RouteResultset rr
if (!StringUtil.isBlank(routeNode)) {
RouteResultsetNode[] nodes = {new RouteResultsetNode(routeNode, rrs.getSqlType(), builder.getNode().getSql())};
for (RouteResultsetNode node : nodes) {
RowDataPacket row = getRow(node, service.getCharset().getResults());
row.setPacketId(service.nextPacketId());
buffer = row.write(buffer, service, true);
rows.add(getRow(node, service.getCharset().getResults()));
}
} else {
List<ReferenceHandlerInfo> results = ComplexQueryPlanUtil.getComplexQueryResult(builder);
Expand All @@ -250,13 +261,10 @@ public static void writeOutHeadAndEof(ShardingService service, RouteResultset rr
row.add(StringUtil.encode(result.getType(), service.getCharset().getResults()));
row.add(StringUtil.encode(result.getRefOrSQL(), service.getCharset().getResults()));
row.setPacketId(service.nextPacketId());
buffer = row.write(buffer, service, true);
rows.add(row);
}
}
}
// writeDirectly last eof
EOFRowPacket lastEof = new EOFRowPacket();
lastEof.setPacketId(service.nextPacketId());
lastEof.write(buffer, service);
return rows;
}
}

0 comments on commit dfacfa8

Please sign in to comment.