Skip to content

Commit 0ffea4e

Browse files
BillyYcccvietj
authored andcommitted
MySQL column definitions from fetch or execute response should be used prior to prepare response when decoding rows using cursors
1 parent 448bd74 commit 0ffea4e

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/ExtendedQueryCommandCodec.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ExtendedQueryCommandCodec<R> extends ExtendedQueryCommandBaseCodec<R, Exte
3232
ExtendedQueryCommandCodec(ExtendedQueryCommand<R> cmd) {
3333
super(cmd);
3434
if (cmd.fetch() > 0 && statement.isCursorOpen) {
35-
// restore the state we need for decoding fetch response
35+
// restore the state we need for decoding fetch response based on the prepared statement
3636
columnDefinitions = statement.rowDesc.columnDefinitions();
3737
}
3838
}
@@ -42,7 +42,10 @@ void encode(MySQLEncoder encoder) {
4242
super.encode(encoder);
4343

4444
if (statement.isCursorOpen) {
45-
decoder = new RowResultDecoder<>(cmd.collector(), statement.rowDesc);
45+
if (decoder == null) {
46+
// restore the state we need for decoding if column definitions are not included in the fetch response
47+
decoder = new RowResultDecoder<>(cmd.collector(), statement.rowDesc);
48+
}
4649
sendStatementFetchCommand(statement.statementId, cmd.fetch());
4750
} else {
4851
Tuple params = cmd.params();
@@ -96,8 +99,6 @@ void decodePayload(ByteBuf payload, int payloadLength) {
9699
// need to reset packet number so that we can send a fetch request
97100
sequenceId = 0;
98101
// send fetch after cursor opened
99-
decoder = new RowResultDecoder<>(cmd.collector(), statement.rowDesc);
100-
101102
statement.isCursorOpen = true;
102103

103104
sendStatementFetchCommand(statement.statementId, cmd.fetch());

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/QueryCommandBaseCodec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ protected void handleResultsetColumnDefinitions(ByteBuf payload) {
9494

9595
protected void handleResultsetColumnDefinitionsDecodingCompleted() {
9696
commandHandlerState = CommandHandlerState.HANDLING_ROW_DATA_OR_END_PACKET;
97-
decoder = new RowResultDecoder<>(cmd.collector(), /*cmd.isSingleton()*/ new MySQLRowDesc(columnDefinitions, format));
97+
MySQLRowDesc mySQLRowDesc = new MySQLRowDesc(columnDefinitions, format); // use the column definitions if provided by execute or fetch response instead of prepare response
98+
decoder = new RowResultDecoder<>(cmd.collector(), mySQLRowDesc);
9899
}
99100

100101
protected void handleRows(ByteBuf payload, int payloadLength) {

vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLPooledConnectionTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import io.vertx.ext.unit.Async;
1818
import io.vertx.ext.unit.TestContext;
1919
import io.vertx.ext.unit.junit.VertxUnitRunner;
20+
import io.vertx.sqlclient.Cursor;
2021
import io.vertx.sqlclient.PoolOptions;
22+
import io.vertx.sqlclient.Row;
2123
import io.vertx.sqlclient.SqlConnection;
2224
import org.junit.After;
2325
import org.junit.Before;
@@ -84,4 +86,19 @@ public void testTransactionRollbackUnfinishedOnRecycle(TestContext ctx) {
8486
});
8587
}));
8688
}
89+
90+
@Test
91+
public void testQueryConstantWithCursor(TestContext ctx) {
92+
connector.accept(ctx.asyncAssertSuccess(conn -> {
93+
conn.prepare("SELECT 1")
94+
.onComplete(ctx.asyncAssertSuccess(ps -> {
95+
Cursor cursor = ps.cursor();
96+
cursor.read(1024).onComplete(ctx.asyncAssertSuccess(result -> {
97+
ctx.assertEquals(1, result.size());
98+
Row row = result.iterator().next();
99+
ctx.assertEquals(1, row.getInteger(0));
100+
}));
101+
}));
102+
}));
103+
}
87104
}

0 commit comments

Comments
 (0)