tdsql 0.4.1

Ergonomic async Microsoft SQL Server client for Rust, with typed rows and multi-result-set support
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! The dynamic value type, the SQL type tags, and the conversion traits.

use chrono::{DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
use rust_decimal::Decimal;
use uuid::Uuid;

use crate::error::{Error, Result};

/// A value read from, or bound to, SQL Server.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum DataValue {
    /// `tinyint`
    TinyInt(u8),
    /// `smallint`
    SmallInt(i16),
    /// `int`
    Int(i32),
    /// `bigint`
    BigInt(i64),
    /// `float` / `real`
    Float(f64),
    /// `decimal` / `numeric` / `money`
    Decimal(Decimal),
    /// `bit`
    Bool(bool),
    /// Any character or XML type.
    Text(String),
    /// Any binary type.
    Binary(Vec<u8>),
    /// `uniqueidentifier`
    Guid(Uuid),
    /// `date`
    Date(NaiveDate),
    /// `time`
    Time(NaiveTime),
    /// `datetime` / `datetime2` / `smalldatetime`
    DateTime(NaiveDateTime),
    /// `datetimeoffset`
    DateTimeOffset(DateTime<FixedOffset>),
    /// SQL `NULL`.
    #[default]
    Null,
}

impl DataValue {
    /// Whether this value is SQL `NULL`.
    pub fn is_null(&self) -> bool {
        matches!(self, DataValue::Null)
    }

    /// The name of the variant, used in conversion error messages.
    pub fn type_name(&self) -> &'static str {
        match self {
            DataValue::TinyInt(_) => "TinyInt",
            DataValue::SmallInt(_) => "SmallInt",
            DataValue::Int(_) => "Int",
            DataValue::BigInt(_) => "BigInt",
            DataValue::Float(_) => "Float",
            DataValue::Decimal(_) => "Decimal",
            DataValue::Bool(_) => "Bool",
            DataValue::Text(_) => "Text",
            DataValue::Binary(_) => "Binary",
            DataValue::Guid(_) => "Guid",
            DataValue::Date(_) => "Date",
            DataValue::Time(_) => "Time",
            DataValue::DateTime(_) => "DateTime",
            DataValue::DateTimeOffset(_) => "DateTimeOffset",
            DataValue::Null => "Null",
        }
    }
}

/// The SQL Server type of a column, as reported by the server.
///
/// This mirrors the TDS type tags. It replaces the previous stringly-typed
/// `sql_type: String`, which exposed the driver's `Debug` output as a public
/// contract.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum SqlType {
    Null,
    Bit,
    TinyInt,
    SmallInt,
    Int,
    BigInt,
    SmallDateTime,
    Real,
    Float,
    Money,
    DateTime,
    SmallMoney,
    Guid,
    IntN,
    BitN,
    Decimal,
    Numeric,
    FloatN,
    DateTimeN,
    Date,
    Time,
    DateTime2,
    DateTimeOffset,
    VarBinary,
    VarChar,
    Binary,
    Char,
    NVarChar,
    NChar,
    Xml,
    Udt,
    Text,
    Image,
    NText,
    Variant,
}

impl From<tiberius::ColumnType> for SqlType {
    fn from(t: tiberius::ColumnType) -> Self {
        use tiberius::ColumnType as C;
        match t {
            C::Null => SqlType::Null,
            C::Bit => SqlType::Bit,
            C::Int1 => SqlType::TinyInt,
            C::Int2 => SqlType::SmallInt,
            C::Int4 => SqlType::Int,
            C::Int8 => SqlType::BigInt,
            C::Datetime4 => SqlType::SmallDateTime,
            C::Float4 => SqlType::Real,
            C::Float8 => SqlType::Float,
            C::Money => SqlType::Money,
            C::Datetime => SqlType::DateTime,
            C::Money4 => SqlType::SmallMoney,
            C::Guid => SqlType::Guid,
            C::Intn => SqlType::IntN,
            C::Bitn => SqlType::BitN,
            C::Decimaln => SqlType::Decimal,
            C::Numericn => SqlType::Numeric,
            C::Floatn => SqlType::FloatN,
            C::Datetimen => SqlType::DateTimeN,
            C::Daten => SqlType::Date,
            C::Timen => SqlType::Time,
            C::Datetime2 => SqlType::DateTime2,
            C::DatetimeOffsetn => SqlType::DateTimeOffset,
            C::BigVarBin => SqlType::VarBinary,
            C::BigVarChar => SqlType::VarChar,
            C::BigBinary => SqlType::Binary,
            C::BigChar => SqlType::Char,
            C::NVarchar => SqlType::NVarChar,
            C::NChar => SqlType::NChar,
            C::Xml => SqlType::Xml,
            C::Udt => SqlType::Udt,
            C::Text => SqlType::Text,
            C::Image => SqlType::Image,
            C::NText => SqlType::NText,
            C::SSVariant => SqlType::Variant,
        }
    }
}

// ---------------------------------------------------------------------------
// Rust -> SQL
// ---------------------------------------------------------------------------

/// A Rust value that can be bound as a SQL parameter.
///
/// Implemented for the primitive types, `chrono` date/time types, [`Decimal`],
/// [`Uuid`], and `Option<T>` for any `T: ToSql` (binding SQL `NULL`).
///
/// `DateTime<Tz>` is implemented for *any* chrono time zone -- [`Utc`],
/// [`Local`], [`FixedOffset`], or a `chrono_tz::Tz` -- so an aware timestamp
/// can be bound directly. It is normalised to its UTC offset and sent as
/// `datetimeoffset`, which preserves the instant whatever zone it came from.
pub trait ToSql: Send + Sync {
    /// Convert to the dynamic value that gets sent to the server.
    fn to_value(&self) -> DataValue;
}

impl ToSql for DataValue {
    fn to_value(&self) -> DataValue {
        self.clone()
    }
}

impl<T: ToSql + ?Sized> ToSql for &T {
    fn to_value(&self) -> DataValue {
        (**self).to_value()
    }
}

impl<T: ToSql> ToSql for Option<T> {
    fn to_value(&self) -> DataValue {
        match self {
            Some(v) => v.to_value(),
            None => DataValue::Null,
        }
    }
}

macro_rules! impl_to_sql {
    ($($t:ty => $variant:ident),* $(,)?) => {
        $(impl ToSql for $t {
            fn to_value(&self) -> DataValue { DataValue::$variant(self.clone()) }
        })*
    };
}

impl_to_sql! {
    u8 => TinyInt,
    i16 => SmallInt,
    i32 => Int,
    i64 => BigInt,
    f64 => Float,
    bool => Bool,
    Decimal => Decimal,
    String => Text,
    Vec<u8> => Binary,
    Uuid => Guid,
    NaiveDate => Date,
    NaiveTime => Time,
    NaiveDateTime => DateTime,
}

/// Any time-zone-aware timestamp binds as `datetimeoffset`.
///
/// The zone is carried through as a fixed offset, so `Utc`, `Local` and a named
/// zone all round-trip to the same instant the server stores.
impl<Tz> ToSql for DateTime<Tz>
where
    Tz: TimeZone + Send + Sync,
    Tz::Offset: Send + Sync,
{
    fn to_value(&self) -> DataValue {
        DataValue::DateTimeOffset(self.fixed_offset())
    }
}

impl ToSql for str {
    fn to_value(&self) -> DataValue {
        DataValue::Text(self.to_string())
    }
}

impl ToSql for [u8] {
    fn to_value(&self) -> DataValue {
        DataValue::Binary(self.to_vec())
    }
}

impl ToSql for f32 {
    fn to_value(&self) -> DataValue {
        DataValue::Float(*self as f64)
    }
}

// `From` conversions, used by `Parameter::new` and by tests building values.
macro_rules! impl_from {
    ($($t:ty => $variant:ident),* $(,)?) => {
        $(impl From<$t> for DataValue {
            fn from(v: $t) -> Self { DataValue::$variant(v) }
        })*
    };
}

impl_from! {
    u8 => TinyInt,
    i16 => SmallInt,
    i32 => Int,
    i64 => BigInt,
    f64 => Float,
    bool => Bool,
    Decimal => Decimal,
    String => Text,
    Vec<u8> => Binary,
    Uuid => Guid,
    NaiveDate => Date,
    NaiveTime => Time,
    NaiveDateTime => DateTime,
}

impl<Tz: TimeZone> From<DateTime<Tz>> for DataValue {
    fn from(v: DateTime<Tz>) -> Self {
        DataValue::DateTimeOffset(v.fixed_offset())
    }
}

impl From<&str> for DataValue {
    fn from(v: &str) -> Self {
        DataValue::Text(v.to_string())
    }
}

impl From<&[u8]> for DataValue {
    fn from(v: &[u8]) -> Self {
        DataValue::Binary(v.to_vec())
    }
}

impl From<f32> for DataValue {
    fn from(v: f32) -> Self {
        DataValue::Float(v as f64)
    }
}

/// Binding an `Option` sends SQL `NULL` for `None`.
impl<T: Into<DataValue>> From<Option<T>> for DataValue {
    fn from(v: Option<T>) -> Self {
        match v {
            Some(v) => v.into(),
            None => DataValue::Null,
        }
    }
}

// ---------------------------------------------------------------------------
// SQL -> Rust
// ---------------------------------------------------------------------------

/// A Rust type that can be produced from a SQL value.
///
/// This is the counterpart to [`ToSql`], and is what makes
/// [`Row::get`](crate::Row::get) typed.
pub trait FromSql: Sized {
    /// Convert from the dynamic value, or fail with [`Error::Conversion`].
    ///
    /// `column` names the column being read, purely for the error message.
    fn from_sql(value: &DataValue, column: &str) -> Result<Self>;
}

fn mismatch<T>(value: &DataValue, column: &str, target: &'static str) -> Result<T> {
    Err(Error::Conversion {
        column: column.to_string(),
        actual: value.type_name(),
        target,
    })
}

/// `NULL` becomes `None`; anything else delegates to `T`.
impl<T: FromSql> FromSql for Option<T> {
    fn from_sql(value: &DataValue, column: &str) -> Result<Self> {
        match value {
            DataValue::Null => Ok(None),
            other => T::from_sql(other, column).map(Some),
        }
    }
}

macro_rules! impl_from_sql {
    ($t:ty, $target:literal, |$v:ident| $body:expr) => {
        impl FromSql for $t {
            fn from_sql(value: &DataValue, column: &str) -> Result<Self> {
                let $v = value;
                match $body {
                    Some(v) => Ok(v),
                    None => mismatch(value, column, $target),
                }
            }
        }
    };
}

// Integers widen upward but never narrow, so a value is never silently truncated.
impl_from_sql!(u8, "u8", |v| match v {
    DataValue::TinyInt(n) => Some(*n),
    _ => None,
});

impl_from_sql!(i16, "i16", |v| match v {
    DataValue::TinyInt(n) => Some(*n as i16),
    DataValue::SmallInt(n) => Some(*n),
    _ => None,
});

impl_from_sql!(i32, "i32", |v| match v {
    DataValue::TinyInt(n) => Some(*n as i32),
    DataValue::SmallInt(n) => Some(*n as i32),
    DataValue::Int(n) => Some(*n),
    _ => None,
});

impl_from_sql!(i64, "i64", |v| match v {
    DataValue::TinyInt(n) => Some(*n as i64),
    DataValue::SmallInt(n) => Some(*n as i64),
    DataValue::Int(n) => Some(*n as i64),
    DataValue::BigInt(n) => Some(*n),
    _ => None,
});

impl_from_sql!(f64, "f64", |v| match v {
    DataValue::Float(n) => Some(*n),
    _ => None,
});

impl_from_sql!(bool, "bool", |v| match v {
    DataValue::Bool(b) => Some(*b),
    _ => None,
});

impl_from_sql!(Decimal, "Decimal", |v| match v {
    DataValue::Decimal(d) => Some(*d),
    _ => None,
});

impl_from_sql!(String, "String", |v| match v {
    DataValue::Text(s) => Some(s.clone()),
    _ => None,
});

impl_from_sql!(Vec<u8>, "Vec<u8>", |v| match v {
    DataValue::Binary(b) => Some(b.clone()),
    _ => None,
});

impl_from_sql!(Uuid, "Uuid", |v| match v {
    DataValue::Guid(g) => Some(*g),
    _ => None,
});

impl_from_sql!(NaiveDate, "NaiveDate", |v| match v {
    DataValue::Date(d) => Some(*d),
    _ => None,
});

impl_from_sql!(NaiveTime, "NaiveTime", |v| match v {
    DataValue::Time(t) => Some(*t),
    _ => None,
});

impl_from_sql!(NaiveDateTime, "NaiveDateTime", |v| match v {
    DataValue::DateTime(d) => Some(*d),
    _ => None,
});

impl_from_sql!(
    DateTime<FixedOffset>,
    "DateTime<FixedOffset>",
    |v| match v {
        DataValue::DateTimeOffset(d) => Some(*d),
        _ => None,
    }
);

// Reading into `Utc` or `Local` re-projects the offset the server sent, which
// names the same instant. A bare `datetime2` is *not* accepted here: it carries
// no offset, so calling it UTC would be a guess rather than a conversion. Read
// it as `NaiveDateTime` and pick the zone yourself, e.g. `.and_utc()`.
impl_from_sql!(DateTime<Utc>, "DateTime<Utc>", |v| match v {
    DataValue::DateTimeOffset(d) => Some(d.with_timezone(&Utc)),
    _ => None,
});

impl_from_sql!(DateTime<Local>, "DateTime<Local>", |v| match v {
    DataValue::DateTimeOffset(d) => Some(d.with_timezone(&Local)),
    _ => None,
});

impl FromSql for DataValue {
    fn from_sql(value: &DataValue, _column: &str) -> Result<Self> {
        Ok(value.clone())
    }
}

// ---------------------------------------------------------------------------
// Comparisons against native types, so assertions stay terse.
// ---------------------------------------------------------------------------

macro_rules! impl_partial_eq {
    ($t:ty, |$s:ident, $o:ident| $body:expr) => {
        impl PartialEq<$t> for DataValue {
            fn eq(&self, other: &$t) -> bool {
                let $s = self;
                let $o = other;
                $body
            }
        }
    };
}

// Integer comparisons widen so `assert_eq!(row["c"], 1)` works whatever the
// server actually sent.
impl_partial_eq!(i64, |s, o| match s {
    DataValue::TinyInt(v) => *v as i64 == *o,
    DataValue::SmallInt(v) => *v as i64 == *o,
    DataValue::Int(v) => *v as i64 == *o,
    DataValue::BigInt(v) => *v == *o,
    _ => false,
});
impl_partial_eq!(i32, |s, o| *s == (*o as i64));
impl_partial_eq!(i16, |s, o| *s == (*o as i64));
impl_partial_eq!(u8, |s, o| *s == (*o as i64));
impl_partial_eq!(bool, |s, o| matches!(s, DataValue::Bool(v) if v == o));
impl_partial_eq!(f64, |s, o| matches!(s, DataValue::Float(v) if v == o));
impl_partial_eq!(Decimal, |s, o| matches!(s, DataValue::Decimal(v) if v == o));
impl_partial_eq!(
    str,
    |s, o| matches!(s, DataValue::Text(v) if v.as_str() == o)
);
impl_partial_eq!(
    &str,
    |s, o| matches!(s, DataValue::Text(v) if v.as_str() == *o)
);
impl_partial_eq!(String, |s, o| matches!(s, DataValue::Text(v) if v == o));
impl_partial_eq!(Uuid, |s, o| matches!(s, DataValue::Guid(v) if v == o));
impl_partial_eq!(NaiveDate, |s, o| matches!(s, DataValue::Date(v) if v == o));
impl_partial_eq!(NaiveTime, |s, o| matches!(s, DataValue::Time(v) if v == o));
impl_partial_eq!(
    NaiveDateTime,
    |s, o| matches!(s, DataValue::DateTime(v) if v == o)
);
// Generic over the zone, and chrono compares instants across zones, so the same
// moment written as `Utc` and as `FixedOffset` both match.
impl<Tz: TimeZone> PartialEq<DateTime<Tz>> for DataValue {
    fn eq(&self, other: &DateTime<Tz>) -> bool {
        matches!(self, DataValue::DateTimeOffset(v) if v == other)
    }
}
impl_partial_eq!(Vec<u8>, |s, o| matches!(s, DataValue::Binary(v) if v == o));
impl_partial_eq!(
    &[u8],
    |s, o| matches!(s, DataValue::Binary(v) if v.as_slice() == *o)
);

#[cfg(test)]
mod tests;