sqlx_core_oldapi/any/
arguments.rs

1use crate::any::Any;
2use crate::arguments::Arguments;
3use crate::encode::Encode;
4use crate::types::Type;
5
6#[derive(Default)]
7pub struct AnyArguments<'q> {
8    values: Vec<Box<dyn Encode<'q, Any> + Send + 'q>>,
9}
10
11impl<'q> Arguments<'q> for AnyArguments<'q> {
12    type Database = Any;
13
14    fn reserve(&mut self, additional: usize, _size: usize) {
15        self.values.reserve(additional);
16    }
17
18    fn add<T>(&mut self, value: T)
19    where
20        T: 'q + Send + Encode<'q, Self::Database> + Type<Self::Database>,
21    {
22        self.values.push(Box::new(value));
23    }
24}
25
26pub struct AnyArgumentBuffer<'q>(pub(crate) AnyArgumentBufferKind<'q>);
27
28pub(crate) enum AnyArgumentBufferKind<'q> {
29    #[cfg(feature = "postgres")]
30    Postgres(
31        crate::postgres::PgArguments,
32        std::marker::PhantomData<&'q ()>,
33    ),
34
35    #[cfg(feature = "mysql")]
36    MySql(
37        crate::mysql::MySqlArguments,
38        std::marker::PhantomData<&'q ()>,
39    ),
40
41    #[cfg(feature = "sqlite")]
42    Sqlite(crate::sqlite::SqliteArguments<'q>),
43
44    #[cfg(feature = "mssql")]
45    Mssql(
46        crate::mssql::MssqlArguments,
47        std::marker::PhantomData<&'q ()>,
48    ),
49}
50
51// control flow inferred type bounds would be fun
52// the compiler should know the branch is totally unreachable
53
54#[cfg(feature = "sqlite")]
55#[allow(irrefutable_let_patterns)]
56impl<'q> From<AnyArguments<'q>> for crate::sqlite::SqliteArguments<'q> {
57    fn from(args: AnyArguments<'q>) -> Self {
58        let mut buf = AnyArgumentBuffer(AnyArgumentBufferKind::Sqlite(Default::default()));
59
60        for value in args.values {
61            let _ = value.encode_by_ref(&mut buf);
62        }
63
64        if let AnyArgumentBufferKind::Sqlite(args) = buf.0 {
65            args
66        } else {
67            unreachable!()
68        }
69    }
70}
71
72#[cfg(feature = "mysql")]
73#[allow(irrefutable_let_patterns)]
74impl<'q> From<AnyArguments<'q>> for crate::mysql::MySqlArguments {
75    fn from(args: AnyArguments<'q>) -> Self {
76        let mut buf = AnyArgumentBuffer(AnyArgumentBufferKind::MySql(
77            Default::default(),
78            std::marker::PhantomData,
79        ));
80
81        for value in args.values {
82            let _ = value.encode_by_ref(&mut buf);
83        }
84
85        if let AnyArgumentBufferKind::MySql(args, _) = buf.0 {
86            args
87        } else {
88            unreachable!()
89        }
90    }
91}
92
93#[cfg(feature = "mssql")]
94#[allow(irrefutable_let_patterns)]
95impl<'q> From<AnyArguments<'q>> for crate::mssql::MssqlArguments {
96    fn from(args: AnyArguments<'q>) -> Self {
97        let mut buf = AnyArgumentBuffer(AnyArgumentBufferKind::Mssql(
98            Default::default(),
99            std::marker::PhantomData,
100        ));
101
102        for value in args.values {
103            let _ = value.encode_by_ref(&mut buf);
104        }
105
106        if let AnyArgumentBufferKind::Mssql(args, _) = buf.0 {
107            args
108        } else {
109            unreachable!()
110        }
111    }
112}
113
114#[cfg(feature = "postgres")]
115#[allow(irrefutable_let_patterns)]
116impl<'q> From<AnyArguments<'q>> for crate::postgres::PgArguments {
117    fn from(args: AnyArguments<'q>) -> Self {
118        let mut buf = AnyArgumentBuffer(AnyArgumentBufferKind::Postgres(
119            Default::default(),
120            std::marker::PhantomData,
121        ));
122
123        for value in args.values {
124            let _ = value.encode_by_ref(&mut buf);
125        }
126
127        if let AnyArgumentBufferKind::Postgres(args, _) = buf.0 {
128            args
129        } else {
130            unreachable!()
131        }
132    }
133}