1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::any::Any;
use crate::arguments::Arguments;
use crate::encode::Encode;
use crate::types::Type;

#[derive(Default)]
pub struct AnyArguments<'q> {
    values: Vec<Box<dyn Encode<'q, Any> + Send + 'q>>,
}

impl<'q> Arguments<'q> for AnyArguments<'q> {
    type Database = Any;

    fn reserve(&mut self, additional: usize, _size: usize) {
        self.values.reserve(additional);
    }

    fn add<T>(&mut self, value: T)
    where
        T: 'q + Send + Encode<'q, Self::Database> + Type<Self::Database>,
    {
        self.values.push(Box::new(value));
    }
}

pub struct AnyArgumentBuffer<'q>(pub(crate) AnyArgumentBufferKind<'q>);

pub(crate) enum AnyArgumentBufferKind<'q> {
    #[cfg(feature = "postgres")]
    Postgres(
        crate::postgres::PgArguments,
        std::marker::PhantomData<&'q ()>,
    ),

    #[cfg(feature = "mysql")]
    MySql(
        crate::mysql::MySqlArguments,
        std::marker::PhantomData<&'q ()>,
    ),

    #[cfg(feature = "sqlite")]
    Sqlite(crate::sqlite::SqliteArguments<'q>),

    #[cfg(feature = "mssql")]
    Mssql(
        crate::mssql::MssqlArguments,
        std::marker::PhantomData<&'q ()>,
    ),
}

// control flow inferred type bounds would be fun
// the compiler should know the branch is totally unreachable

#[cfg(feature = "sqlite")]
#[allow(irrefutable_let_patterns)]
impl<'q> From<AnyArguments<'q>> for crate::sqlite::SqliteArguments<'q> {
    fn from(args: AnyArguments<'q>) -> Self {
        let mut buf = AnyArgumentBuffer(AnyArgumentBufferKind::Sqlite(Default::default()));

        for value in args.values {
            let _ = value.encode_by_ref(&mut buf);
        }

        if let AnyArgumentBufferKind::Sqlite(args) = buf.0 {
            args
        } else {
            unreachable!()
        }
    }
}

#[cfg(feature = "mysql")]
#[allow(irrefutable_let_patterns)]
impl<'q> From<AnyArguments<'q>> for crate::mysql::MySqlArguments {
    fn from(args: AnyArguments<'q>) -> Self {
        let mut buf = AnyArgumentBuffer(AnyArgumentBufferKind::MySql(
            Default::default(),
            std::marker::PhantomData,
        ));

        for value in args.values {
            let _ = value.encode_by_ref(&mut buf);
        }

        if let AnyArgumentBufferKind::MySql(args, _) = buf.0 {
            args
        } else {
            unreachable!()
        }
    }
}

#[cfg(feature = "mssql")]
#[allow(irrefutable_let_patterns)]
impl<'q> From<AnyArguments<'q>> for crate::mssql::MssqlArguments {
    fn from(args: AnyArguments<'q>) -> Self {
        let mut buf = AnyArgumentBuffer(AnyArgumentBufferKind::Mssql(
            Default::default(),
            std::marker::PhantomData,
        ));

        for value in args.values {
            let _ = value.encode_by_ref(&mut buf);
        }

        if let AnyArgumentBufferKind::Mssql(args, _) = buf.0 {
            args
        } else {
            unreachable!()
        }
    }
}

#[cfg(feature = "postgres")]
#[allow(irrefutable_let_patterns)]
impl<'q> From<AnyArguments<'q>> for crate::postgres::PgArguments {
    fn from(args: AnyArguments<'q>) -> Self {
        let mut buf = AnyArgumentBuffer(AnyArgumentBufferKind::Postgres(
            Default::default(),
            std::marker::PhantomData,
        ));

        for value in args.values {
            let _ = value.encode_by_ref(&mut buf);
        }

        if let AnyArgumentBufferKind::Postgres(args, _) = buf.0 {
            args
        } else {
            unreachable!()
        }
    }
}