From 76ff679f9a524a2771ffbe06e2e8350d8373b369 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Thu, 27 Mar 2014 21:39:03 -0700 Subject: [PATCH] Don't fail on wrong param count --- src/error.rs | 11 ++++++++++- src/lib.rs | 22 +++++++++++++--------- src/test.rs | 17 +++++++++++------ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/error.rs b/src/error.rs index a30ffa2d..13b1092d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -508,6 +508,13 @@ pub enum PostgresError { PgStreamDesynchronized, /// A prepared statement was executed on a connection it does not belong to PgWrongConnection, + /// An incorrect number of parameters were bound to a statement + PgWrongParamCount { + /// The expected number of parameters + expected: uint, + /// The actual number of parameters + actual: uint, + } } impl PostgresError { @@ -522,7 +529,9 @@ impl PostgresError { error", PgWrongConnection => ~"A statement was executed on a connection it was not \ - prepared on " + prepared on ", + PgWrongParamCount { expected, actual } => + format!("Expected {} parameters but got {}", expected, actual), } } } diff --git a/src/lib.rs b/src/lib.rs index bdf702e3..d91f0526 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,6 +102,7 @@ use error::{DnsError, PgDbError, PgStreamDesynchronized, PgStreamError, + PgWrongParamCount, PostgresConnectError, PostgresDbError, PostgresError, @@ -979,8 +980,8 @@ impl<'conn> PostgresTransaction<'conn> { /// /// # Failure /// - /// Fails if the number or types of the provided parameters do not match - /// the parameters of the statement. + /// Fails if the types of the provided parameters do not match the + /// parameters of the statement. pub fn lazy_query<'trans, 'stmt>(&'trans self, stmt: &'stmt PostgresStatement, params: &[&ToSql], @@ -1050,9 +1051,12 @@ impl<'conn> PostgresStatement<'conn> { -> Result<(), PostgresError> { let mut formats = Vec::new(); let mut values = Vec::new(); - assert!(self.param_types.len() == params.len(), - "Expected {} parameters but found {}", - self.param_types.len(), params.len()); + if self.param_types.len() != params.len() { + return Err(PgWrongParamCount { + expected: self.param_types.len(), + actual: params.len(), + }); + } for (¶m, ty) in params.iter().zip(self.param_types.iter()) { let (format, value) = param.to_sql(ty); formats.push(format as i16); @@ -1124,8 +1128,8 @@ impl<'conn> PostgresStatement<'conn> { /// /// # Failure /// - /// Fails if the number or types of the provided parameters do not match - /// the parameters of the statement. + /// Fails if the types of the provided parameters do not match the + /// parameters of the statement. /// /// # Example /// @@ -1177,8 +1181,8 @@ impl<'conn> PostgresStatement<'conn> { /// /// # Failure /// - /// Fails if the number or types of the provided parameters do not match - /// the parameters of the statement. + /// Fails if the types of the provided parameters do not match the + /// parameters of the statement. /// /// # Example /// diff --git a/src/test.rs b/src/test.rs index 7a0c0042..1d36344f 100644 --- a/src/test.rs +++ b/src/test.rs @@ -20,6 +20,7 @@ use {PostgresNoticeHandler, use error::{PgConnectDbError, PgDbError, PgWrongConnection, + PgWrongParamCount, DnsError, MissingPassword, Position, @@ -706,19 +707,23 @@ fn test_wrong_param_type() { } #[test] -#[should_fail] fn test_too_few_params() { let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl)); - let _ = conn.execute("SELECT $1::INT, $2::INT", [&1i32 as &ToSql]); + match conn.execute("SELECT $1::INT, $2::INT", [&1i32 as &ToSql]) { + Err(PgWrongParamCount { expected: 2, actual: 1 }) => {}, + res => fail!("unexpected result {}", res) + } } #[test] -#[should_fail] fn test_too_many_params() { let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl)); - let _ = conn.execute("SELECT $1::INT, $2::INT", [&1i32 as &ToSql, - &2i32 as &ToSql, - &3i32 as &ToSql]); + match conn.execute("SELECT $1::INT, $2::INT", [&1i32 as &ToSql, + &2i32 as &ToSql, + &3i32 as &ToSql]) { + Err(PgWrongParamCount { expected: 2, actual: 3 }) => {}, + res => fail!("unexpected result {}", res) + } } #[test]