Correctly handle ErrorResponse after one or more DataRows

Previously, if a query failed with an ErrorResponse after the server had
sent one or more DataRows, stmt.query() incorrectly returned
PgBadResponse.  Fix by handling the error correctly and returning it to
the caller.
This commit is contained in:
Marko Tiikkaja
2014-07-25 03:00:28 +02:00
parent a2c597eb38
commit b95299e47c
2 changed files with 23 additions and 1 deletions

View File

@@ -1316,6 +1316,10 @@ impl<'stmt> PostgresRows<'stmt> {
break;
},
DataRow { row } => self.data.push_back(row),
ErrorResponse { fields } => {
try!(self.stmt.conn.wait_for_ready());
return Err(PgDbError(PostgresDbError::new(fields)));
}
_ => {
self.stmt.conn.conn.borrow_mut().desynchronized = true;
return Err(PgBadResponse);

View File

@@ -39,7 +39,8 @@ use postgres::error::{PgConnectDbError,
QueryCanceled,
UndefinedTable,
InvalidCatalogName,
PgWrongTransaction};
PgWrongTransaction,
CardinalityViolation};
use postgres::types::{ToSql, FromSql, PgInt4, PgVarchar};
use postgres::types::array::{ArrayBase};
use postgres::types::range::{Range, Inclusive, Exclusive, RangeBound};
@@ -361,6 +362,23 @@ fn test_query() {
assert_eq!(vec![1i64, 2], result.map(|row| row.get(0u)).collect());
}
#[test]
fn test_error_after_datarow() {
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
let stmt = or_fail!(conn.prepare("
SELECT
(SELECT generate_series(1, ss.i))
FROM (SELECT gs.i
FROM generate_series(1, 2) gs(i)
ORDER BY gs.i
LIMIT 2) ss"));
match stmt.query([]) {
Err(PgDbError(PostgresDbError { code: CardinalityViolation, .. })) => {}
Err(err) => fail!("Unexpected error {}", err),
Ok(_) => fail!("Expected failure"),
}
}
#[test]
fn test_result_finish() {
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));