tiberius-ng 0.13.1

A TDS (Microsoft SQL Server) driver for Rust — actively-maintained community continuation of tiberius
Documentation
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use crate::{
    tds::{codec::ColumnData, Numeric},
    xml::XmlData,
};
use std::borrow::Cow;
use uuid::Uuid;

/// A conversion trait to a TDS type.
///
/// A `ToSql` implementation for a Rust type is needed for using it as a
/// parameter in the [`Client#query`] or [`Client#execute`] methods. The
/// following Rust types are already implemented to match the given server
/// types:
///
/// |Rust type|Server type|
/// |--------|--------|
/// |`u8`|`tinyint`|
/// |`i16`|`smallint`|
/// |`i32`|`int`|
/// |`i64`|`bigint`|
/// |`f32`|`float(24)`|
/// |`f64`|`float(53)`|
/// |`bool`|`bit`|
/// |`String`/`&str` (< 4000 characters)|`nvarchar(4000)`|
/// |`String`/`&str`|`nvarchar(max)`|
/// |`Vec<u8>`/`&[u8]` (< 8000 bytes)|`varbinary(8000)`|
/// |`Vec<u8>`/`&[u8]`|`varbinary(max)`|
/// |[`Uuid`]|`uniqueidentifier`|
/// |[`Numeric`]|`numeric`/`decimal`|
/// |[`Decimal`] (with feature flag `rust_decimal`)|`numeric`/`decimal`|
/// |[`BigDecimal`] (with feature flag `bigdecimal`)|`numeric`/`decimal`|
/// |[`XmlData`]|`xml`|
/// |[`NaiveDate`] (with `chrono` feature, TDS 7.3 >)|`date`|
/// |[`NaiveTime`] (with `chrono` feature, TDS 7.3 >)|`time`|
/// |[`DateTime`] (with `chrono` feature, TDS 7.3 >)|`datetimeoffset`|
/// |[`NaiveDateTime`] (with `chrono` feature, TDS 7.3 >)|`datetime2`|
/// |[`NaiveDateTime`] (with `chrono` feature, TDS 7.2)|`datetime`|
///
/// It is possible to use some of the types to write into columns that are not
/// of the same type. For example on systems following the TDS 7.3 standard (SQL
/// Server 2008 and later), the chrono type `NaiveDateTime` can also be used to
/// write to `datetime`, `datetime2` and `smalldatetime` columns. All string
/// types can also be used with `ntext`, `text`, `varchar`, `nchar` and `char`
/// columns. All binary types can also be used with `binary` and `image`
/// columns.
///
/// See the [`time`] module for more information about the date and time structs.
///
/// [`Client#query`]: struct.Client.html#method.query
/// [`Client#execute`]: struct.Client.html#method.execute
/// [`time`]: time/index.html
/// [`Uuid`]: struct.Uuid.html
/// [`Numeric`]: numeric/struct.Numeric.html
/// [`Decimal`]: numeric/struct.Decimal.html
/// [`BigDecimal`]: numeric/struct.BigDecimal.html
/// [`XmlData`]: xml/struct.XmlData.html
/// [`NaiveDateTime`]: time/chrono/struct.NaiveDateTime.html
/// [`NaiveDate`]: time/chrono/struct.NaiveDate.html
/// [`NaiveTime`]: time/chrono/struct.NaiveTime.html
/// [`DateTime`]: time/chrono/struct.DateTime.html
pub trait ToSql: Send + Sync {
    /// Convert to a value understood by the SQL Server. Conversion
    /// by-reference.
    fn to_sql(&self) -> ColumnData<'_>;
}

/// A by-value conversion trait to a TDS type.
pub trait IntoSql<'a>: Send + Sync {
    /// Convert to a value understood by the SQL Server. Conversion by-value.
    fn into_sql(self) -> ColumnData<'a>;
}

impl<'a> IntoSql<'a> for &'a str {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::String(Some(Cow::Borrowed(self)))
    }
}

impl<'a> IntoSql<'a> for Option<&'a str> {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::String(self.map(Cow::Borrowed))
    }
}

impl<'a> IntoSql<'a> for &'a String {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::String(Some(Cow::Borrowed(self)))
    }
}

impl<'a> IntoSql<'a> for Option<&'a String> {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::String(self.map(Cow::from))
    }
}

impl<'a> IntoSql<'a> for &'a [u8] {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::Binary(Some(Cow::Borrowed(self)))
    }
}

impl<'a> IntoSql<'a> for Option<&'a [u8]> {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::Binary(self.map(Cow::Borrowed))
    }
}

impl<'a> IntoSql<'a> for &'a Vec<u8> {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::Binary(Some(Cow::from(self)))
    }
}

impl<'a> IntoSql<'a> for Option<&'a Vec<u8>> {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::Binary(self.map(Cow::from))
    }
}

impl<'a> IntoSql<'a> for Cow<'a, str> {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::String(Some(self))
    }
}

impl<'a> IntoSql<'a> for Option<Cow<'a, str>> {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::String(self)
    }
}

impl<'a> IntoSql<'a> for Cow<'a, [u8]> {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::Binary(Some(self))
    }
}

impl<'a> IntoSql<'a> for Option<Cow<'a, [u8]>> {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::Binary(self)
    }
}

impl<'a> IntoSql<'a> for &'a XmlData {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::Xml(Some(Cow::Borrowed(self)))
    }
}

impl<'a> IntoSql<'a> for Option<&'a XmlData> {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::Xml(self.map(Cow::Borrowed))
    }
}

impl<'a> IntoSql<'a> for &'a Uuid {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::Guid(Some(*self))
    }
}

impl<'a> IntoSql<'a> for Option<&'a Uuid> {
    fn into_sql(self) -> ColumnData<'a> {
        ColumnData::Guid(self.copied())
    }
}

into_sql!(self_,
          String: (ColumnData::String, Cow::from(self_));
          Vec<u8>: (ColumnData::Binary, Cow::from(self_));
          Numeric: (ColumnData::Numeric, self_);
          XmlData: (ColumnData::Xml, Cow::Owned(self_));
          Uuid: (ColumnData::Guid, self_);
          bool: (ColumnData::Bit, self_);
          u8: (ColumnData::U8, self_);
          i16: (ColumnData::I16, self_);
          i32: (ColumnData::I32, self_);
          i64: (ColumnData::I64, self_);
          f32: (ColumnData::F32, self_);
          f64: (ColumnData::F64, self_);
);

to_sql!(self_,
        bool: (ColumnData::Bit, *self_);
        u8: (ColumnData::U8, *self_);
        i16: (ColumnData::I16, *self_);
        i32: (ColumnData::I32, *self_);
        i64: (ColumnData::I64, *self_);
        f32: (ColumnData::F32, *self_);
        f64: (ColumnData::F64, *self_);
        &str: (ColumnData::String, Cow::from(*self_));
        String: (ColumnData::String, Cow::from(self_));
        Cow<'_, str>: (ColumnData::String, self_.clone());
        &[u8]: (ColumnData::Binary, Cow::from(*self_));
        Cow<'_, [u8]>: (ColumnData::Binary, self_.clone());
        Vec<u8>: (ColumnData::Binary, Cow::from(self_));
        Numeric: (ColumnData::Numeric, *self_);
        XmlData: (ColumnData::Xml, Cow::Borrowed(self_));
        Uuid: (ColumnData::Guid, *self_);
);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tds::Numeric;
    use crate::{IntoSql, ToSql};

    #[test]
    fn to_sql_scalars() {
        assert_eq!(true.to_sql(), ColumnData::Bit(Some(true)));
        assert_eq!(8u8.to_sql(), ColumnData::U8(Some(8)));
        assert_eq!(16i16.to_sql(), ColumnData::I16(Some(16)));
        assert_eq!(32i32.to_sql(), ColumnData::I32(Some(32)));
        assert_eq!(64i64.to_sql(), ColumnData::I64(Some(64)));
        assert_eq!(1.5f32.to_sql(), ColumnData::F32(Some(1.5)));
        assert_eq!(2.5f64.to_sql(), ColumnData::F64(Some(2.5)));
    }

    #[test]
    // The `&Some(..)`/`&None` borrows are intentional: they exercise the
    // `ToSql for &T` impls, not the by-value ones, so the borrow is not needless.
    #[allow(clippy::needless_borrow)]
    fn to_sql_option_some_and_none() {
        assert_eq!(Some(1i32).to_sql(), ColumnData::I32(Some(1)));
        assert_eq!(None::<i32>.to_sql(), ColumnData::I32(None));
        assert_eq!((&Some(1i32)).to_sql(), ColumnData::I32(Some(1)));
        assert_eq!((&None::<i32>).to_sql(), ColumnData::I32(None));
    }

    #[test]
    fn to_sql_strings_and_binary() {
        assert_eq!("abc".to_sql(), ColumnData::String(Some(Cow::from("abc"))));
        assert_eq!(
            String::from("abc").to_sql(),
            ColumnData::String(Some(Cow::from("abc")))
        );
        let v = vec![1u8, 2, 3];
        assert_eq!(
            v.to_sql(),
            ColumnData::Binary(Some(Cow::from(vec![1, 2, 3])))
        );
        assert_eq!(
            [1u8, 2, 3].as_slice().to_sql(),
            ColumnData::Binary(Some(Cow::from(vec![1, 2, 3])))
        );
    }

    #[test]
    fn to_sql_numeric_and_uuid() {
        let n = Numeric::new_with_scale(5, 1);
        assert_eq!(n.to_sql(), ColumnData::Numeric(Some(n)));

        let uuid = Uuid::nil();
        assert_eq!(uuid.to_sql(), ColumnData::Guid(Some(uuid)));
    }

    #[test]
    fn into_sql_borrowed_and_owned() {
        assert_eq!(
            "abc".into_sql(),
            ColumnData::String(Some(Cow::Borrowed("abc")))
        );
        assert_eq!(
            Some("abc").into_sql(),
            ColumnData::String(Some(Cow::Borrowed("abc")))
        );
        assert_eq!(None::<&str>.into_sql(), ColumnData::String(None));

        let bytes = vec![9u8, 8, 7];
        assert_eq!(
            bytes.as_slice().into_sql(),
            ColumnData::Binary(Some(Cow::Borrowed(bytes.as_slice())))
        );
        assert_eq!(
            (&bytes).into_sql(),
            ColumnData::Binary(Some(Cow::from(&bytes)))
        );

        let uuid = Uuid::nil();
        assert_eq!((&uuid).into_sql(), ColumnData::Guid(Some(uuid)));
        assert_eq!(Some(&uuid).into_sql(), ColumnData::Guid(Some(uuid)));
        assert_eq!(None::<&Uuid>.into_sql(), ColumnData::Guid(None));
    }

    #[test]
    fn into_sql_scalars() {
        assert_eq!(true.into_sql(), ColumnData::Bit(Some(true)));
        assert_eq!(5i32.into_sql(), ColumnData::I32(Some(5)));
        assert_eq!(None::<i32>.into_sql(), ColumnData::I32(None));
        assert_eq!(
            String::from("x").into_sql(),
            ColumnData::String(Some(Cow::from("x")))
        );
    }

    #[test]
    fn into_sql_owned_string_and_ref() {
        let owned = String::from("abc");
        assert_eq!(
            (&owned).into_sql(),
            ColumnData::String(Some(Cow::from("abc")))
        );
        assert_eq!(
            Some(&owned).into_sql(),
            ColumnData::String(Some(Cow::from("abc")))
        );
        assert_eq!(None::<&String>.into_sql(), ColumnData::String(None));
    }

    #[test]
    fn into_sql_binary_option_variants() {
        assert_eq!(None::<&[u8]>.into_sql(), ColumnData::Binary(None));

        let bytes = vec![1u8, 2, 3];
        assert_eq!(
            Some(bytes.as_slice()).into_sql(),
            ColumnData::Binary(Some(Cow::from(bytes.as_slice())))
        );
        assert_eq!(None::<&Vec<u8>>.into_sql(), ColumnData::Binary(None));
        assert_eq!(
            bytes.into_sql(),
            ColumnData::Binary(Some(Cow::from(vec![1, 2, 3])))
        );
    }

    #[test]
    fn into_sql_cow_variants() {
        let cow_str: Cow<'_, str> = Cow::Borrowed("hi");
        assert_eq!(
            cow_str.into_sql(),
            ColumnData::String(Some(Cow::from("hi")))
        );
        assert_eq!(
            Some(Cow::Borrowed("hi")).into_sql(),
            ColumnData::String(Some(Cow::from("hi")))
        );
        assert_eq!(None::<Cow<'_, str>>.into_sql(), ColumnData::String(None));

        let cow_bin: Cow<'_, [u8]> = Cow::Borrowed(&[1u8, 2][..]);
        assert_eq!(
            cow_bin.into_sql(),
            ColumnData::Binary(Some(Cow::from(vec![1u8, 2])))
        );
        assert_eq!(
            Some(Cow::<[u8]>::Borrowed(&[1u8, 2][..])).into_sql(),
            ColumnData::Binary(Some(Cow::from(vec![1u8, 2])))
        );
        assert_eq!(None::<Cow<'_, [u8]>>.into_sql(), ColumnData::Binary(None));
    }

    #[test]
    fn into_sql_xml_and_numeric() {
        let xml = XmlData::new("<a/>".to_string());
        assert_eq!(
            (&xml).into_sql(),
            ColumnData::Xml(Some(Cow::Borrowed(&xml)))
        );
        assert_eq!(
            Some(&xml).into_sql(),
            ColumnData::Xml(Some(Cow::Borrowed(&xml)))
        );
        assert_eq!(None::<&XmlData>.into_sql(), ColumnData::Xml(None));

        let xml_owned = XmlData::new("<b/>".to_string());
        assert_eq!(
            xml_owned.clone().into_sql(),
            ColumnData::Xml(Some(Cow::Owned(xml_owned)))
        );

        let n = Numeric::new_with_scale(42, 0);
        assert_eq!(n.into_sql(), ColumnData::Numeric(Some(n)));
    }

    #[test]
    // The `&value` borrows are intentional: they exercise the `ToSql for &T`
    // impls for the base scalar types, so the borrow is not needless.
    #[allow(clippy::needless_borrow)]
    fn to_sql_by_reference_scalars() {
        // The macro-generated impls also cover `&T` for the base scalar types.
        assert_eq!((&true).to_sql(), ColumnData::Bit(Some(true)));
        assert_eq!((&8u8).to_sql(), ColumnData::U8(Some(8)));
        assert_eq!((&16i16).to_sql(), ColumnData::I16(Some(16)));
        assert_eq!((&64i64).to_sql(), ColumnData::I64(Some(64)));
        assert_eq!((&1.5f32).to_sql(), ColumnData::F32(Some(1.5)));
        assert_eq!((&2.5f64).to_sql(), ColumnData::F64(Some(2.5)));
    }

    #[test]
    fn to_sql_cow_variants() {
        let cow_str: Cow<'_, str> = Cow::Borrowed("hi");
        assert_eq!(cow_str.to_sql(), ColumnData::String(Some(Cow::from("hi"))));

        let cow_bin: Cow<'_, [u8]> = Cow::Borrowed(&[1u8, 2][..]);
        assert_eq!(
            cow_bin.to_sql(),
            ColumnData::Binary(Some(Cow::from(vec![1u8, 2])))
        );
    }

    #[test]
    fn to_sql_xml() {
        let xml = XmlData::new("<a/>".to_string());
        assert_eq!(xml.to_sql(), ColumnData::Xml(Some(Cow::Borrowed(&xml))));
    }
}