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