Ensure empty array Params impl can trigger Error::InvalidParameterCount when needed

This commit is contained in:
Thom Chiovoloni 2020-11-03 01:34:18 -08:00
parent 22121772a2
commit a312582d11
2 changed files with 8 additions and 5 deletions

View File

@ -161,7 +161,7 @@ pub const NO_PARAMS: &[&dyn ToSql] = &[];
#[macro_export] #[macro_export]
macro_rules! params { macro_rules! params {
() => { () => {
(&[] as &[&dyn $crate::ToSql]) &[] as &[&dyn $crate::ToSql]
}; };
($($param:expr),+ $(,)?) => { ($($param:expr),+ $(,)?) => {
&[$(&$param as &dyn $crate::ToSql),+] as &[&dyn $crate::ToSql] &[$(&$param as &dyn $crate::ToSql),+] as &[&dyn $crate::ToSql]
@ -198,12 +198,12 @@ macro_rules! params {
#[macro_export] #[macro_export]
macro_rules! named_params { macro_rules! named_params {
() => { () => {
(&[] as &[(&str, &dyn $crate::ToSql)]) &[] as &[(&str, &dyn $crate::ToSql)]
}; };
// Note: It's a lot more work to support this as part of the same macro as // Note: It's a lot more work to support this as part of the same macro as
// `params!`, unfortunately. // `params!`, unfortunately.
($($param_name:literal: $param_val:expr),+ $(,)?) => { ($($param_name:literal: $param_val:expr),+ $(,)?) => {
(&[$(($param_name, &$param_val as &dyn $crate::ToSql)),+] as &[(&str, &dyn $crate::ToSql)]) &[$(($param_name, &$param_val as &dyn $crate::ToSql)),+] as &[(&str, &dyn $crate::ToSql)]
}; };
} }

View File

@ -172,8 +172,11 @@ pub trait Params: Sealed {
impl Sealed for [&dyn ToSql; 0] {} impl Sealed for [&dyn ToSql; 0] {}
impl Params for [&dyn ToSql; 0] { impl Params for [&dyn ToSql; 0] {
#[inline] #[inline]
fn bind_in(self, _: &mut Statement<'_>) -> Result<()> { fn bind_in(self, stmt: &mut Statement<'_>) -> Result<()> {
Ok(()) // Note: Can't just return `Ok(())` — `Statement::bind_parameters`
// checks that the right number of params were passed too.
// TODO: we should have tests for `Error::InvalidParameterCount`...
stmt.bind_parameters(crate::params![])
} }
} }