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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! Traits for passing arguments to SQL queries.

use crate::database::Database;
use crate::encode::Encode;
use crate::types::HasSqlType;

/// A tuple of arguments to be sent to the database.
pub trait Arguments: Send + Sized + Default + 'static {
    type Database: Database + ?Sized;

    /// Returns `true` if there are no values.
    #[inline]
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the number of values.
    fn len(&self) -> usize;

    /// Returns the size of the arguments, in bytes.
    fn size(&self) -> usize;

    /// Reserves the capacity for at least `len` more values (of `size` bytes) to
    /// be added to the arguments without a reallocation.  
    fn reserve(&mut self, len: usize, size: usize);

    /// Add the value to the end of the arguments.
    fn add<T>(&mut self, value: T)
    where
        Self::Database: HasSqlType<T>,
        T: Encode<Self::Database>;
}

pub trait IntoArguments<DB>
where
    DB: Database,
{
    fn into_arguments(self) -> DB::Arguments;
}

impl<DB> IntoArguments<DB> for DB::Arguments
where
    DB: Database,
{
    #[inline]
    fn into_arguments(self) -> DB::Arguments {
        self
    }
}

#[allow(unused)]
macro_rules! impl_into_arguments {
    ($B:ident: $( ($idx:tt) -> $T:ident );+;) => {
        impl<$($T,)+> crate::arguments::IntoArguments<$B> for ($($T,)+)
        where
            $($B: crate::types::HasSqlType<$T>,)+
            $($T: crate::encode::Encode<$B>,)+
        {
            fn into_arguments(self) -> <$B as crate::database::Database>::Arguments {
                use crate::arguments::Arguments;

                let mut arguments = <$B as crate::database::Database>::Arguments::default();

                let binds = 0 $(+ { $idx; 1 } )+;
                let bytes = 0 $(+ crate::encode::Encode::size_hint(&self.$idx))+;

                arguments.reserve(binds, bytes);

                $(crate::arguments::Arguments::add(&mut arguments, self.$idx);)+

                arguments
            }
        }
    };
}

#[allow(unused)]
macro_rules! impl_into_arguments_for_database {
    ($B:ident) => {
        impl crate::arguments::IntoArguments<$B> for ()
        {
            #[inline]
            fn into_arguments(self) -> <$B as crate::database::Database>::Arguments {
                Default::default()
            }
        }

        impl_into_arguments!($B:
            (0) -> T1;
        );

        impl_into_arguments!($B:
            (0) -> T1;
            (1) -> T2;
        );

        impl_into_arguments!($B:
            (0) -> T1;
            (1) -> T2;
            (2) -> T3;
        );

        impl_into_arguments!($B:
            (0) -> T1;
            (1) -> T2;
            (2) -> T3;
            (3) -> T4;
        );

        impl_into_arguments!($B:
            (0) -> T1;
            (1) -> T2;
            (2) -> T3;
            (3) -> T4;
            (4) -> T5;
        );

        impl_into_arguments!($B:
            (0) -> T1;
            (1) -> T2;
            (2) -> T3;
            (3) -> T4;
            (4) -> T5;
            (5) -> T6;
        );

        impl_into_arguments!($B:
            (0) -> T1;
            (1) -> T2;
            (2) -> T3;
            (3) -> T4;
            (4) -> T5;
            (5) -> T6;
            (6) -> T7;
        );

        impl_into_arguments!($B:
            (0) -> T1;
            (1) -> T2;
            (2) -> T3;
            (3) -> T4;
            (4) -> T5;
            (5) -> T6;
            (6) -> T7;
            (7) -> T8;
        );

        impl_into_arguments!($B:
            (0) -> T1;
            (1) -> T2;
            (2) -> T3;
            (3) -> T4;
            (4) -> T5;
            (5) -> T6;
            (6) -> T7;
            (7) -> T8;
            (8) -> T9;
        );
    }
}