Skip to content

Commit 0fc4005

Browse files
committed
For query_typed, deal with the no-data case.
If a query returns no data, we receive `Message::NoData`, which signals the completion of the query. However, we treated it as a no-op, leading to processing other messages and eventual failure. This PR fixes the issue and updates the `query_typed` tests to cover this scenario.
1 parent 9f196e7 commit 0fc4005

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

tokio-postgres/src/query.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,15 @@ where
8989

9090
loop {
9191
match responses.next().await? {
92-
Message::ParseComplete
93-
| Message::BindComplete
94-
| Message::ParameterDescription(_)
95-
| Message::NoData => {}
92+
Message::ParseComplete | Message::BindComplete | Message::ParameterDescription(_) => {}
93+
Message::NoData => {
94+
return Ok(RowStream {
95+
statement: Statement::unnamed(vec![], vec![]),
96+
responses,
97+
rows_affected: None,
98+
_p: PhantomPinned,
99+
});
100+
}
96101
Message::RowDescription(row_description) => {
97102
let mut columns: Vec<Column> = vec![];
98103
let mut it = row_description.fields();

tokio-postgres/tests/test/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,13 @@ async fn query_typed_no_transaction() {
997997
assert_eq!(second_row.get::<_, i32>(1), 40);
998998
assert_eq!(second_row.get::<_, &str>(2), "literal");
999999
assert_eq!(second_row.get::<_, i32>(3), 5);
1000+
1001+
// Test for UPDATE that returns no data
1002+
let updated_rows = client
1003+
.query_typed("UPDATE foo set age = 33", &[])
1004+
.await
1005+
.unwrap();
1006+
assert_eq!(updated_rows.len(), 0);
10001007
}
10011008

10021009
#[tokio::test]
@@ -1064,4 +1071,11 @@ async fn query_typed_with_transaction() {
10641071
assert_eq!(second_row.get::<_, i32>(1), 40);
10651072
assert_eq!(second_row.get::<_, &str>(2), "literal");
10661073
assert_eq!(second_row.get::<_, i32>(3), 5);
1074+
1075+
// Test for UPDATE that returns no data
1076+
let updated_rows = transaction
1077+
.query_typed("UPDATE foo set age = 33", &[])
1078+
.await
1079+
.unwrap();
1080+
assert_eq!(updated_rows.len(), 0);
10671081
}

0 commit comments

Comments
 (0)