Skip to content

Commit 5e3e8e6

Browse files
committed
odbc: skip result counts in fetch_optional
1 parent 4d1a1d7 commit 5e3e8e6

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

‎sqlx-core/src/odbc/connection/executor.rs‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::odbc::{Odbc, OdbcConnection, OdbcQueryResult, OdbcRow, OdbcStatement,
55
use either::Either;
66
use futures_core::future::BoxFuture;
77
use futures_core::stream::BoxStream;
8-
use futures_util::{future, FutureExt, StreamExt};
8+
use futures_util::TryStreamExt;
99

1010
impl<'c> Executor<'c> for &'c mut OdbcConnection {
1111
type Database = Odbc;
@@ -30,12 +30,17 @@ impl<'c> Executor<'c> for &'c mut OdbcConnection {
3030
'c: 'e,
3131
E: Execute<'q, Self::Database> + 'q,
3232
{
33-
Box::pin(self.fetch_many(query).into_future().then(|(v, _)| match v {
34-
Some(Ok(Either::Right(r))) => future::ok(Some(r)),
35-
Some(Ok(Either::Left(_))) => future::ok(None),
36-
Some(Err(e)) => future::err(e),
37-
None => future::ok(None),
38-
}))
33+
let mut stream = self.fetch_many(query);
34+
35+
Box::pin(async move {
36+
while let Some(step) = stream.try_next().await? {
37+
if let Either::Right(row) = step {
38+
return Ok(Some(row));
39+
}
40+
}
41+
42+
Ok(None)
43+
})
3944
}
4045

4146
fn prepare_with<'e, 'q: 'e>(

‎tests/odbc/odbc.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ async fn it_streams_multiple_result_sets() -> anyhow::Result<()> {
174174
Ok(())
175175
}
176176

177+
#[tokio::test]
178+
async fn fetch_optional_skips_empty_result_sets() -> anyhow::Result<()> {
179+
let mut conn = new::<Odbc>().await?;
180+
181+
if conn.dbms_name().await? != "PostgreSQL" {
182+
return Ok(());
183+
}
184+
185+
let row = conn
186+
.fetch_optional("SELECT 1 WHERE 1=0; SELECT 2 AS v")
187+
.await?
188+
.expect("second result set should contain one row");
189+
190+
assert_eq!(row.try_get::<i64, _>("v")?, 2);
191+
Ok(())
192+
}
193+
177194
#[tokio::test]
178195
async fn it_handles_empty_result() -> anyhow::Result<()> {
179196
let mut conn = new::<Odbc>().await?;

0 commit comments

Comments
 (0)