Skip to content

Commit a035b55

Browse files
committed
Fix handling of result sets without outputs within multi-result sets.
Fix #23
1 parent d988095 commit a035b55

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/conn/mod.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ mod test {
453453
use lib_futures::Future;
454454
use test_misc::DATABASE_URL;
455455
use tokio::reactor::Core;
456+
use TransactionOptions;
456457

457458
fn get_opts() -> OptsBuilder {
458459
let mut builder = OptsBuilder::from_opts(&**DATABASE_URL);
@@ -740,6 +741,55 @@ mod test {
740741
}
741742
}
742743

744+
#[test]
745+
fn should_handle_multi_result_sets_where_some_results_have_no_output() {
746+
const QUERY: &str =
747+
r"SELECT 1;
748+
UPDATE time_zone SET Time_zone_id = 1 WHERE Time_zone_id = 1;
749+
SELECT 2;
750+
SELECT 3;
751+
UPDATE time_zone SET Time_zone_id = 1 WHERE Time_zone_id = 1;
752+
UPDATE time_zone SET Time_zone_id = 1 WHERE Time_zone_id = 1;
753+
SELECT 4;";
754+
let mut lp = Core::new().unwrap();
755+
756+
let fut = Conn::new(get_opts(), &lp.handle())
757+
.and_then(|c| {
758+
c.start_transaction(TransactionOptions::new())
759+
.and_then(|t| {
760+
t.drop_query(QUERY)
761+
})
762+
.and_then(|t| {
763+
t.query(QUERY)
764+
.and_then(|r| r.collect_and_drop::<u8>())
765+
})
766+
.and_then(|(t, out)| {
767+
assert_eq!(vec![1], out);
768+
t.query(QUERY)
769+
.and_then(|r| r.for_each_and_drop(|x| assert_eq!(from_row::<u8>(x), 1)))
770+
})
771+
.and_then(|t| {
772+
t.query(QUERY)
773+
.and_then(|r| r.map_and_drop(|row| from_row::<u8>(row)))
774+
})
775+
.and_then(|(t, out)| {
776+
assert_eq!(vec![1], out);
777+
t.query(QUERY)
778+
.and_then(|r| r.reduce_and_drop(0u8, |acc, x| acc + from_row::<u8>(x)))
779+
})
780+
.and_then(|(t, out)| {
781+
assert_eq!(1, out);
782+
t.query(QUERY)
783+
.and_then(|r| r.drop_result())
784+
})
785+
.and_then(|t| t.commit())
786+
})
787+
.and_then(|c| c.first_exec::<_, _, u8>("SELECT 1", ()))
788+
.and_then(|(c, output)| c.disconnect().map(move |_| output));
789+
790+
assert_eq!(Some(1), lp.run(fut).unwrap());
791+
}
792+
743793
#[test]
744794
fn should_iterate_over_resultset() {
745795
use std::cell::RefCell;

src/queryable/query_result/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ where
119119
QueryResult(WithRows(conn_like, _, cached, _)) => {
120120
QueryResult(Empty(conn_like, cached, PhantomData))
121121
}
122-
_ => unreachable!(),
122+
x => x,
123123
}
124124
}
125125

@@ -204,7 +204,9 @@ where
204204
/// One could use it to check if there is more than one result set in this query result.
205205
pub fn is_empty(&self) -> bool {
206206
match *self {
207-
QueryResult(Empty(..)) => true,
207+
QueryResult(Empty(..)) => {
208+
!self.get_status().contains(StatusFlags::SERVER_MORE_RESULTS_EXISTS)
209+
},
208210
_ => false,
209211
}
210212
}

0 commit comments

Comments
 (0)