cdbc/
arguments.rs

1//! Types and traits for passing arguments to SQL queries.
2
3use crate::database::{Database, HasArguments};
4use crate::encode::Encode;
5use crate::types::Type;
6
7/// A tuple of arguments to be sent to the database.
8pub trait Arguments<'q>: Send + Sized + Default {
9    type Database: Database;
10
11    /// Reserves the capacity for at least `additional` more values (of `size` total bytes) to
12    /// be added to the arguments without a reallocation.
13    fn reserve(&mut self, additional: usize, size: usize);
14
15    /// Add the value to the end of the arguments.
16    fn add<T>(&mut self, value: T)
17    where
18        T: 'q + Send + Encode<'q, Self::Database> + Type<Self::Database>;
19}
20
21pub trait IntoArguments<'q, DB: HasArguments<'q>>: Sized + Send {
22    fn into_arguments(self) -> <DB as HasArguments<'q>>::Arguments;
23}
24
25// NOTE: required due to lack of lazy normalization
26#[macro_export]
27#[allow(unused_macros)]
28macro_rules! impl_into_arguments_for_arguments {
29    ($Arguments:path) => {
30        impl<'q>
31            cdbc::arguments::IntoArguments<
32                'q,
33                <$Arguments as cdbc::arguments::Arguments<'q>>::Database,
34            > for $Arguments
35        {
36            fn into_arguments(self) -> $Arguments {
37                self
38            }
39        }
40    };
41}
42
43/// used by the query macros to prevent supernumerary `.bind()` calls
44pub struct ImmutableArguments<'q, DB: HasArguments<'q>>(pub <DB as HasArguments<'q>>::Arguments);
45
46impl<'q, DB: HasArguments<'q>> IntoArguments<'q, DB> for ImmutableArguments<'q, DB> {
47    fn into_arguments(self) -> <DB as HasArguments<'q>>::Arguments {
48        self.0
49    }
50}
51
52// TODO: Impl `IntoArguments` for &[&dyn Encode]
53// TODO: Impl `IntoArguments` for (impl Encode, ...) x16