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)]
32#[allow(clippy::needless_doctest_main)]
33macro_rules! impl_into_arguments_for_arguments {
34    ($Arguments:path) => {
35        impl<'q>
36            crate::arguments::IntoArguments<
37                'q,
38                <$Arguments as crate::arguments::Arguments<'q>>::Database,
39            > for $Arguments
40        {
41            fn into_arguments(self) -> $Arguments {
42                self
43            }
44        }
45    };
46}
47
48/// used by the query macros to prevent supernumerary `.bind()` calls
49pub struct ImmutableArguments<'q, DB: HasArguments<'q>>(pub <DB as HasArguments<'q>>::Arguments);
50
51impl<'q, DB: HasArguments<'q>> IntoArguments<'q, DB> for ImmutableArguments<'q, DB> {
52    fn into_arguments(self) -> <DB as HasArguments<'q>>::Arguments {
53        self.0
54    }
55}
56
57// TODO: Impl `IntoArguments` for &[&dyn Encode]
58// TODO: Impl `IntoArguments` for (impl Encode, ...) x16