Don't fail on wrong param count
This commit is contained in:
parent
0916a53df3
commit
76ff679f9a
11
src/error.rs
11
src/error.rs
@ -508,6 +508,13 @@ pub enum PostgresError {
|
|||||||
PgStreamDesynchronized,
|
PgStreamDesynchronized,
|
||||||
/// A prepared statement was executed on a connection it does not belong to
|
/// A prepared statement was executed on a connection it does not belong to
|
||||||
PgWrongConnection,
|
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 {
|
impl PostgresError {
|
||||||
@ -522,7 +529,9 @@ impl PostgresError {
|
|||||||
error",
|
error",
|
||||||
PgWrongConnection =>
|
PgWrongConnection =>
|
||||||
~"A statement was executed on a connection it was not \
|
~"A statement was executed on a connection it was not \
|
||||||
prepared on "
|
prepared on ",
|
||||||
|
PgWrongParamCount { expected, actual } =>
|
||||||
|
format!("Expected {} parameters but got {}", expected, actual),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
22
src/lib.rs
22
src/lib.rs
@ -102,6 +102,7 @@ use error::{DnsError,
|
|||||||
PgDbError,
|
PgDbError,
|
||||||
PgStreamDesynchronized,
|
PgStreamDesynchronized,
|
||||||
PgStreamError,
|
PgStreamError,
|
||||||
|
PgWrongParamCount,
|
||||||
PostgresConnectError,
|
PostgresConnectError,
|
||||||
PostgresDbError,
|
PostgresDbError,
|
||||||
PostgresError,
|
PostgresError,
|
||||||
@ -979,8 +980,8 @@ impl<'conn> PostgresTransaction<'conn> {
|
|||||||
///
|
///
|
||||||
/// # Failure
|
/// # Failure
|
||||||
///
|
///
|
||||||
/// Fails if the number or types of the provided parameters do not match
|
/// Fails if the types of the provided parameters do not match the
|
||||||
/// the parameters of the statement.
|
/// parameters of the statement.
|
||||||
pub fn lazy_query<'trans, 'stmt>(&'trans self,
|
pub fn lazy_query<'trans, 'stmt>(&'trans self,
|
||||||
stmt: &'stmt PostgresStatement,
|
stmt: &'stmt PostgresStatement,
|
||||||
params: &[&ToSql],
|
params: &[&ToSql],
|
||||||
@ -1050,9 +1051,12 @@ impl<'conn> PostgresStatement<'conn> {
|
|||||||
-> Result<(), PostgresError> {
|
-> Result<(), PostgresError> {
|
||||||
let mut formats = Vec::new();
|
let mut formats = Vec::new();
|
||||||
let mut values = Vec::new();
|
let mut values = Vec::new();
|
||||||
assert!(self.param_types.len() == params.len(),
|
if self.param_types.len() != params.len() {
|
||||||
"Expected {} parameters but found {}",
|
return Err(PgWrongParamCount {
|
||||||
self.param_types.len(), params.len());
|
expected: self.param_types.len(),
|
||||||
|
actual: params.len(),
|
||||||
|
});
|
||||||
|
}
|
||||||
for (¶m, ty) in params.iter().zip(self.param_types.iter()) {
|
for (¶m, ty) in params.iter().zip(self.param_types.iter()) {
|
||||||
let (format, value) = param.to_sql(ty);
|
let (format, value) = param.to_sql(ty);
|
||||||
formats.push(format as i16);
|
formats.push(format as i16);
|
||||||
@ -1124,8 +1128,8 @@ impl<'conn> PostgresStatement<'conn> {
|
|||||||
///
|
///
|
||||||
/// # Failure
|
/// # Failure
|
||||||
///
|
///
|
||||||
/// Fails if the number or types of the provided parameters do not match
|
/// Fails if the types of the provided parameters do not match the
|
||||||
/// the parameters of the statement.
|
/// parameters of the statement.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
@ -1177,8 +1181,8 @@ impl<'conn> PostgresStatement<'conn> {
|
|||||||
///
|
///
|
||||||
/// # Failure
|
/// # Failure
|
||||||
///
|
///
|
||||||
/// Fails if the number or types of the provided parameters do not match
|
/// Fails if the types of the provided parameters do not match the
|
||||||
/// the parameters of the statement.
|
/// parameters of the statement.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
|
17
src/test.rs
17
src/test.rs
@ -20,6 +20,7 @@ use {PostgresNoticeHandler,
|
|||||||
use error::{PgConnectDbError,
|
use error::{PgConnectDbError,
|
||||||
PgDbError,
|
PgDbError,
|
||||||
PgWrongConnection,
|
PgWrongConnection,
|
||||||
|
PgWrongParamCount,
|
||||||
DnsError,
|
DnsError,
|
||||||
MissingPassword,
|
MissingPassword,
|
||||||
Position,
|
Position,
|
||||||
@ -706,19 +707,23 @@ fn test_wrong_param_type() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_fail]
|
|
||||||
fn test_too_few_params() {
|
fn test_too_few_params() {
|
||||||
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
|
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]
|
#[test]
|
||||||
#[should_fail]
|
|
||||||
fn test_too_many_params() {
|
fn test_too_many_params() {
|
||||||
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
|
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,
|
||||||
&2i32 as &ToSql,
|
&2i32 as &ToSql,
|
||||||
&3i32 as &ToSql]);
|
&3i32 as &ToSql]) {
|
||||||
|
Err(PgWrongParamCount { expected: 2, actual: 3 }) => {},
|
||||||
|
res => fail!("unexpected result {}", res)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user