Skip to main content

tiberius/tds/codec/token/token_row/
into_row.rs

1use crate::{IntoSql, TokenRow};
2
3/// create a TokenRow from list of values
4pub trait IntoRow<'a> {
5    /// create a TokenRow from list of values which implements IntoSQL
6    fn into_row(self) -> TokenRow<'a>;
7}
8
9impl<'a, A> IntoRow<'a> for A
10where
11    A: IntoSql<'a>,
12{
13    fn into_row(self) -> TokenRow<'a> {
14        let mut row = TokenRow::with_capacity(1);
15        row.push(self.into_sql());
16        row
17    }
18}
19
20impl<'a, A, B> IntoRow<'a> for (A, B)
21where
22    A: IntoSql<'a>,
23    B: IntoSql<'a>,
24{
25    fn into_row(self) -> TokenRow<'a> {
26        let mut row = TokenRow::with_capacity(2);
27        row.push(self.0.into_sql());
28        row.push(self.1.into_sql());
29        row
30    }
31}
32
33impl<'a, A, B, C> IntoRow<'a> for (A, B, C)
34where
35    A: IntoSql<'a>,
36    B: IntoSql<'a>,
37    C: IntoSql<'a>,
38{
39    fn into_row(self) -> TokenRow<'a> {
40        let mut row = TokenRow::with_capacity(3);
41        row.push(self.0.into_sql());
42        row.push(self.1.into_sql());
43        row.push(self.2.into_sql());
44        row
45    }
46}
47
48impl<'a, A, B, C, D> IntoRow<'a> for (A, B, C, D)
49where
50    A: IntoSql<'a>,
51    B: IntoSql<'a>,
52    C: IntoSql<'a>,
53    D: IntoSql<'a>,
54{
55    fn into_row(self) -> TokenRow<'a> {
56        let mut row = TokenRow::with_capacity(4);
57        row.push(self.0.into_sql());
58        row.push(self.1.into_sql());
59        row.push(self.2.into_sql());
60        row.push(self.3.into_sql());
61        row
62    }
63}
64
65impl<'a, A, B, C, D, E> IntoRow<'a> for (A, B, C, D, E)
66where
67    A: IntoSql<'a>,
68    B: IntoSql<'a>,
69    C: IntoSql<'a>,
70    D: IntoSql<'a>,
71    E: IntoSql<'a>,
72{
73    fn into_row(self) -> TokenRow<'a> {
74        let mut row = TokenRow::with_capacity(5);
75        row.push(self.0.into_sql());
76        row.push(self.1.into_sql());
77        row.push(self.2.into_sql());
78        row.push(self.3.into_sql());
79        row.push(self.4.into_sql());
80        row
81    }
82}
83
84impl<'a, A, B, C, D, E, F> IntoRow<'a> for (A, B, C, D, E, F)
85where
86    A: IntoSql<'a>,
87    B: IntoSql<'a>,
88    C: IntoSql<'a>,
89    D: IntoSql<'a>,
90    E: IntoSql<'a>,
91    F: IntoSql<'a>,
92{
93    fn into_row(self) -> TokenRow<'a> {
94        let mut row = TokenRow::with_capacity(6);
95        row.push(self.0.into_sql());
96        row.push(self.1.into_sql());
97        row.push(self.2.into_sql());
98        row.push(self.3.into_sql());
99        row.push(self.4.into_sql());
100        row.push(self.5.into_sql());
101        row
102    }
103}
104
105impl<'a, A, B, C, D, E, F, G> IntoRow<'a> for (A, B, C, D, E, F, G)
106where
107    A: IntoSql<'a>,
108    B: IntoSql<'a>,
109    C: IntoSql<'a>,
110    D: IntoSql<'a>,
111    E: IntoSql<'a>,
112    F: IntoSql<'a>,
113    G: IntoSql<'a>,
114{
115    fn into_row(self) -> TokenRow<'a> {
116        let mut row = TokenRow::with_capacity(7);
117        row.push(self.0.into_sql());
118        row.push(self.1.into_sql());
119        row.push(self.2.into_sql());
120        row.push(self.3.into_sql());
121        row.push(self.4.into_sql());
122        row.push(self.5.into_sql());
123        row.push(self.6.into_sql());
124        row
125    }
126}
127
128impl<'a, A, B, C, D, E, F, G, H> IntoRow<'a> for (A, B, C, D, E, F, G, H)
129where
130    A: IntoSql<'a>,
131    B: IntoSql<'a>,
132    C: IntoSql<'a>,
133    D: IntoSql<'a>,
134    E: IntoSql<'a>,
135    F: IntoSql<'a>,
136    G: IntoSql<'a>,
137    H: IntoSql<'a>,
138{
139    fn into_row(self) -> TokenRow<'a> {
140        let mut row = TokenRow::with_capacity(8);
141        row.push(self.0.into_sql());
142        row.push(self.1.into_sql());
143        row.push(self.2.into_sql());
144        row.push(self.3.into_sql());
145        row.push(self.4.into_sql());
146        row.push(self.5.into_sql());
147        row.push(self.6.into_sql());
148        row.push(self.7.into_sql());
149        row
150    }
151}
152
153impl<'a, A, B, C, D, E, F, G, H, I> IntoRow<'a> for (A, B, C, D, E, F, G, H, I)
154where
155    A: IntoSql<'a>,
156    B: IntoSql<'a>,
157    C: IntoSql<'a>,
158    D: IntoSql<'a>,
159    E: IntoSql<'a>,
160    F: IntoSql<'a>,
161    G: IntoSql<'a>,
162    H: IntoSql<'a>,
163    I: IntoSql<'a>,
164{
165    fn into_row(self) -> TokenRow<'a> {
166        let mut row = TokenRow::with_capacity(9);
167        row.push(self.0.into_sql());
168        row.push(self.1.into_sql());
169        row.push(self.2.into_sql());
170        row.push(self.3.into_sql());
171        row.push(self.4.into_sql());
172        row.push(self.5.into_sql());
173        row.push(self.6.into_sql());
174        row.push(self.7.into_sql());
175        row.push(self.8.into_sql());
176        row
177    }
178}
179
180impl<'a, A, B, C, D, E, F, G, H, I, J> IntoRow<'a> for (A, B, C, D, E, F, G, H, I, J)
181where
182    A: IntoSql<'a>,
183    B: IntoSql<'a>,
184    C: IntoSql<'a>,
185    D: IntoSql<'a>,
186    E: IntoSql<'a>,
187    F: IntoSql<'a>,
188    G: IntoSql<'a>,
189    H: IntoSql<'a>,
190    I: IntoSql<'a>,
191    J: IntoSql<'a>,
192{
193    fn into_row(self) -> TokenRow<'a> {
194        let mut row = TokenRow::with_capacity(10);
195        row.push(self.0.into_sql());
196        row.push(self.1.into_sql());
197        row.push(self.2.into_sql());
198        row.push(self.3.into_sql());
199        row.push(self.4.into_sql());
200        row.push(self.5.into_sql());
201        row.push(self.6.into_sql());
202        row.push(self.7.into_sql());
203        row.push(self.8.into_sql());
204        row.push(self.9.into_sql());
205        row
206    }
207}
208
209#[cfg(test)]
210mod tests {
211    use super::*;
212    use crate::tds::codec::ColumnData;
213
214    #[test]
215    fn single_value_into_row() {
216        let row = 42i32.into_row();
217        assert_eq!(row.len(), 1);
218        assert_eq!(row.get(0), Some(&ColumnData::I32(Some(42))));
219    }
220
221    #[test]
222    fn tuple_arities_produce_expected_lengths_and_order() {
223        assert_eq!((1i32, 2i32).into_row().len(), 2);
224        assert_eq!((1i32, 2i32, 3i32).into_row().len(), 3);
225        assert_eq!((1i32, 2i32, 3i32, 4i32).into_row().len(), 4);
226        assert_eq!((1i32, 2i32, 3i32, 4i32, 5i32).into_row().len(), 5);
227        assert_eq!((1i32, 2i32, 3i32, 4i32, 5i32, 6i32).into_row().len(), 6);
228        assert_eq!(
229            (1i32, 2i32, 3i32, 4i32, 5i32, 6i32, 7i32).into_row().len(),
230            7
231        );
232        assert_eq!(
233            (1i32, 2i32, 3i32, 4i32, 5i32, 6i32, 7i32, 8i32)
234                .into_row()
235                .len(),
236            8
237        );
238        assert_eq!(
239            (1i32, 2i32, 3i32, 4i32, 5i32, 6i32, 7i32, 8i32, 9i32)
240                .into_row()
241                .len(),
242            9
243        );
244
245        let row = (1i32, 2i32, 3i32, 4i32, 5i32, 6i32, 7i32, 8i32, 9i32, 10i32).into_row();
246        assert_eq!(row.len(), 10);
247
248        // Values are pushed in tuple order.
249        for (i, value) in row.iter().enumerate() {
250            assert_eq!(value, &ColumnData::I32(Some(i as i32 + 1)));
251        }
252    }
253
254    #[test]
255    fn mixed_types_preserve_positions() {
256        let row = (true, 7u8, "hello", 3.5f64).into_row();
257        assert_eq!(row.len(), 4);
258        assert_eq!(row.get(0), Some(&ColumnData::Bit(Some(true))));
259        assert_eq!(row.get(1), Some(&ColumnData::U8(Some(7))));
260        assert_eq!(row.get(3), Some(&ColumnData::F64(Some(3.5))));
261    }
262}