Skip to content

Commit 8ff14dc

Browse files
feat(ok): add correct handling of ok packets in MYSQL implementation (#3910)
* feat(ok): add correct handling of ok packet * feat(ok): add unit tests
1 parent 69f9ff9 commit 8ff14dc

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

sqlx-mysql/src/connection/executor.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,22 +208,27 @@ impl MySqlConnection {
208208
loop {
209209
let packet = self.inner.stream.recv_packet().await?;
210210

211-
if packet[0] == 0xfe && packet.len() < 9 {
212-
let eof = packet.eof(self.inner.stream.capabilities)?;
213-
214-
self.inner.status_flags = eof.status;
215-
211+
if packet[0] == 0xfe {
212+
let (rows_affected, last_insert_id, status) = if packet.len() < 9 {
213+
// EOF packet
214+
let eof = packet.eof(self.inner.stream.capabilities)?;
215+
(0, 0, eof.status)
216+
} else {
217+
// OK packet
218+
let ok = packet.ok()?;
219+
(ok.affected_rows, ok.last_insert_id, ok.status)
220+
};
221+
222+
self.inner.status_flags = status;
216223
r#yield!(Either::Left(MySqlQueryResult {
217-
rows_affected: 0,
218-
last_insert_id: 0,
224+
rows_affected,
225+
last_insert_id,
219226
}));
220227

221-
if eof.status.contains(Status::SERVER_MORE_RESULTS_EXISTS) {
222-
// more result sets exist, continue to the next one
228+
if status.contains(Status::SERVER_MORE_RESULTS_EXISTS) {
223229
*self.inner.stream.waiting.front_mut().unwrap() = Waiting::Result;
224230
break;
225231
}
226-
227232
self.inner.stream.waiting.pop_front();
228233
return Ok(());
229234
}

sqlx-mysql/src/protocol/response/ok.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,29 @@ fn test_decode_ok_packet() {
5050
assert!(p.status.contains(Status::SERVER_STATUS_AUTOCOMMIT));
5151
assert!(p.status.contains(Status::SERVER_SESSION_STATE_CHANGED));
5252
}
53+
54+
#[test]
55+
fn test_decode_ok_packet_with_info() {
56+
// OK packet with 0xfe header and length >= 9 (with appended info)
57+
const DATA: &[u8] = b"\xfe\x01\x00\x02\x00\x00\x00\x05\x09info data";
58+
59+
let p = OkPacket::decode(DATA.into()).unwrap();
60+
61+
assert_eq!(p.affected_rows, 1);
62+
assert_eq!(p.last_insert_id, 0);
63+
assert_eq!(p.warnings, 0);
64+
assert!(p.status.contains(Status::SERVER_STATUS_AUTOCOMMIT));
65+
}
66+
67+
#[test]
68+
fn test_decode_ok_packet_with_extended_info() {
69+
// OK packet with 0xfe header, affected rows, last insert id, and extended info
70+
const DATA: &[u8] = b"\xfe\x05\x64\x02\x00\x01\x00\x0e\x14extended information";
71+
72+
let p = OkPacket::decode(DATA.into()).unwrap();
73+
74+
assert_eq!(p.affected_rows, 5);
75+
assert_eq!(p.last_insert_id, 100);
76+
assert_eq!(p.warnings, 1);
77+
assert!(p.status.contains(Status::SERVER_STATUS_AUTOCOMMIT));
78+
}

0 commit comments

Comments
 (0)