Don't fail on wrong param count

This commit is contained in:
Steven Fackler 2014-03-27 21:39:03 -07:00
parent 0916a53df3
commit 76ff679f9a
3 changed files with 34 additions and 16 deletions

View File

@ -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),
}
}
}

View File

@ -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 (&param, 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
///

View File

@ -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]