sqlx-odbc 0.0.2

ODBC driver implementation for SQLx.
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
use crate::value::OdbcValueRef;
use crate::{DataTypeExt, Odbc, OdbcArgumentValue, OdbcTypeInfo, OdbcValueKind};
use ::chrono::{
    DateTime, Datelike, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, Timelike, Utc,
};
use odbc_api::DataType;
use sqlx_core::decode::Decode;
use sqlx_core::encode::{Encode, IsNull};
use sqlx_core::error::BoxDynError;
use sqlx_core::types::Type;
use sqlx_core::value::ValueRef;

impl Type<Odbc> for NaiveDate {
    fn type_info() -> OdbcTypeInfo {
        OdbcTypeInfo::DATE
    }

    fn compatible(ty: &OdbcTypeInfo) -> bool {
        matches!(ty.data_type(), DataType::Date)
            || ty.data_type().accepts_character_data()
            || ty.data_type().accepts_numeric_data()
            || matches!(ty.data_type(), DataType::Other { .. } | DataType::Unknown)
    }
}

impl Type<Odbc> for NaiveTime {
    fn type_info() -> OdbcTypeInfo {
        OdbcTypeInfo::TIME
    }

    fn compatible(ty: &OdbcTypeInfo) -> bool {
        matches!(ty.data_type(), DataType::Time { .. })
            || ty.data_type().accepts_character_data()
            || ty.data_type().accepts_numeric_data()
            || matches!(ty.data_type(), DataType::Other { .. } | DataType::Unknown)
    }
}

impl Type<Odbc> for NaiveDateTime {
    fn type_info() -> OdbcTypeInfo {
        OdbcTypeInfo::TIMESTAMP
    }

    fn compatible(ty: &OdbcTypeInfo) -> bool {
        matches!(ty.data_type(), DataType::Timestamp { .. })
            || ty.data_type().accepts_character_data()
            || ty.data_type().accepts_numeric_data()
            || matches!(ty.data_type(), DataType::Other { .. } | DataType::Unknown)
    }
}

impl Type<Odbc> for DateTime<Utc> {
    fn type_info() -> OdbcTypeInfo {
        OdbcTypeInfo::TIMESTAMP
    }

    fn compatible(ty: &OdbcTypeInfo) -> bool {
        <NaiveDateTime as Type<Odbc>>::compatible(ty)
    }
}

impl Type<Odbc> for DateTime<FixedOffset> {
    fn type_info() -> OdbcTypeInfo {
        OdbcTypeInfo::TIMESTAMP
    }

    fn compatible(ty: &OdbcTypeInfo) -> bool {
        <NaiveDateTime as Type<Odbc>>::compatible(ty)
    }
}

impl Type<Odbc> for DateTime<Local> {
    fn type_info() -> OdbcTypeInfo {
        OdbcTypeInfo::TIMESTAMP
    }

    fn compatible(ty: &OdbcTypeInfo) -> bool {
        matches!(ty.data_type(), DataType::Timestamp { .. })
            || ty.data_type().accepts_character_data()
            || matches!(ty.data_type(), DataType::Other { .. } | DataType::Unknown)
    }
}

impl<'q> Encode<'q, Odbc> for NaiveDate {
    fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue>) -> Result<IsNull, BoxDynError> {
        buf.push(OdbcArgumentValue::Date(odbc_api::sys::Date {
            year: self.year() as i16,
            month: self.month() as u16,
            day: self.day() as u16,
        }));
        Ok(IsNull::No)
    }
}

impl<'q> Encode<'q, Odbc> for NaiveTime {
    fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue>) -> Result<IsNull, BoxDynError> {
        buf.push(OdbcArgumentValue::Time(odbc_api::sys::Time {
            hour: self.hour() as u16,
            minute: self.minute() as u16,
            second: self.second() as u16,
        }));
        Ok(IsNull::No)
    }
}

impl<'q> Encode<'q, Odbc> for NaiveDateTime {
    fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue>) -> Result<IsNull, BoxDynError> {
        buf.push(OdbcArgumentValue::Timestamp(timestamp_from_naive(*self)));
        Ok(IsNull::No)
    }
}

impl<'q> Encode<'q, Odbc> for DateTime<Utc> {
    fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue>) -> Result<IsNull, BoxDynError> {
        buf.push(OdbcArgumentValue::Text(self.to_rfc3339()));
        Ok(IsNull::No)
    }
}

impl<'q> Encode<'q, Odbc> for DateTime<FixedOffset> {
    fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue>) -> Result<IsNull, BoxDynError> {
        buf.push(OdbcArgumentValue::Text(self.to_rfc3339()));
        Ok(IsNull::No)
    }
}

impl<'q> Encode<'q, Odbc> for DateTime<Local> {
    fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue>) -> Result<IsNull, BoxDynError> {
        buf.push(OdbcArgumentValue::Timestamp(timestamp_from_naive(
            self.naive_local(),
        )));
        Ok(IsNull::No)
    }
}

fn timestamp_from_naive(value: NaiveDateTime) -> odbc_api::sys::Timestamp {
    odbc_api::sys::Timestamp {
        year: value.year() as i16,
        month: value.month() as u16,
        day: value.day() as u16,
        hour: value.hour() as u16,
        minute: value.minute() as u16,
        second: value.second() as u16,
        fraction: value.nanosecond(),
    }
}

fn parse_yyyymmdd_as_naive_date(value: i64) -> Option<NaiveDate> {
    if !(19000101..=30001231).contains(&value) {
        return None;
    }

    let year = (value / 10000) as i32;
    let month = ((value % 10000) / 100) as u32;
    let day = (value % 100) as u32;
    NaiveDate::from_ymd_opt(year, month, day)
}

fn parse_yyyymmdd_text_as_naive_date(value: &str) -> Option<NaiveDate> {
    if value.len() != 8 || !value.chars().all(|ch| ch.is_ascii_digit()) {
        return None;
    }

    let year = value[0..4].parse().ok()?;
    let month = value[4..6].parse().ok()?;
    let day = value[6..8].parse().ok()?;
    NaiveDate::from_ymd_opt(year, month, day)
}

fn raw_date(value: OdbcValueRef<'_>) -> Option<odbc_api::sys::Date> {
    match ValueRef::to_owned(&value).kind() {
        OdbcValueKind::Date(date) => Some(*date),
        _ => None,
    }
}

fn raw_time(value: OdbcValueRef<'_>) -> Option<odbc_api::sys::Time> {
    match ValueRef::to_owned(&value).kind() {
        OdbcValueKind::Time(time) => Some(*time),
        _ => None,
    }
}

fn raw_timestamp(value: OdbcValueRef<'_>) -> Option<odbc_api::sys::Timestamp> {
    match ValueRef::to_owned(&value).kind() {
        OdbcValueKind::Timestamp(timestamp) => Some(*timestamp),
        _ => None,
    }
}

fn trimmed_text(value: OdbcValueRef<'_>) -> Option<String> {
    Some(value.as_str()?.trim_end_matches('\0').trim().to_owned())
}

fn naive_from_timestamp(value: odbc_api::sys::Timestamp) -> Result<NaiveDateTime, BoxDynError> {
    let date = NaiveDate::from_ymd_opt(value.year as i32, value.month as u32, value.day as u32)
        .ok_or_else(|| "ODBC: invalid date values in timestamp".to_string())?;
    let time = NaiveTime::from_hms_nano_opt(
        value.hour as u32,
        value.minute as u32,
        value.second as u32,
        value.fraction,
    )
    .ok_or_else(|| "ODBC: invalid time values in timestamp".to_string())?;
    Ok(NaiveDateTime::new(date, time))
}

impl<'r> Decode<'r, Odbc> for NaiveDate {
    fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> {
        if let Some(date) = raw_date(value) {
            return NaiveDate::from_ymd_opt(date.year as i32, date.month as u32, date.day as u32)
                .ok_or_else(|| "ODBC: invalid date values".into());
        }

        if let Some(text) = trimmed_text(value) {
            if let Some(date) = parse_yyyymmdd_text_as_naive_date(&text) {
                return Ok(date);
            }

            if let Ok(date) = text.parse() {
                return Ok(date);
            }
        }

        if let Some(integer) = value.as_i64() {
            if let Some(date) = parse_yyyymmdd_as_naive_date(integer) {
                return Ok(date);
            }

            return Err(format!(
                "ODBC: cannot decode NaiveDate from integer '{integer}': not in YYYYMMDD range"
            )
            .into());
        }

        if let Some(float) = value.as_f64() {
            if let Some(date) = parse_yyyymmdd_as_naive_date(float as i64) {
                return Ok(date);
            }

            return Err(format!(
                "ODBC: cannot decode NaiveDate from float '{float}': not in YYYYMMDD range"
            )
            .into());
        }

        Err("ODBC: cannot decode NaiveDate".into())
    }
}

impl<'r> Decode<'r, Odbc> for NaiveTime {
    fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> {
        if let Some(time) = raw_time(value) {
            return NaiveTime::from_hms_opt(
                time.hour as u32,
                time.minute as u32,
                time.second as u32,
            )
            .ok_or_else(|| "ODBC: invalid time values".into());
        }

        let Some(text) = trimmed_text(value) else {
            return Err("ODBC: cannot decode NaiveTime".into());
        };

        text.parse()
            .map_err(|error| format!("ODBC: cannot decode NaiveTime from '{text}': {error}").into())
    }
}

impl<'r> Decode<'r, Odbc> for NaiveDateTime {
    fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> {
        if let Some(timestamp) = raw_timestamp(value) {
            return naive_from_timestamp(timestamp);
        }

        let Some(text) = trimmed_text(value) else {
            return Err("ODBC: cannot decode NaiveDateTime".into());
        };

        if let Ok(datetime) = NaiveDateTime::parse_from_str(&text, "%Y-%m-%d %H:%M:%S%.f") {
            return Ok(datetime);
        }

        if let Ok(datetime) = NaiveDateTime::parse_from_str(&text, "%Y-%m-%d %H:%M:%S") {
            return Ok(datetime);
        }

        text.parse().map_err(|error| {
            format!("ODBC: cannot decode NaiveDateTime from '{text}': {error}").into()
        })
    }
}

impl<'r> Decode<'r, Odbc> for DateTime<Utc> {
    fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> {
        if let Some(timestamp) = raw_timestamp(value) {
            return Ok(DateTime::<Utc>::from_naive_utc_and_offset(
                naive_from_timestamp(timestamp)?,
                Utc,
            ));
        }

        let Some(text) = trimmed_text(value) else {
            return Err("ODBC: cannot decode DateTime<Utc>".into());
        };

        if let Ok(datetime) = text.parse() {
            return Ok(datetime);
        }

        if let Ok(datetime) = <NaiveDateTime as Decode<Odbc>>::decode(value) {
            return Ok(DateTime::<Utc>::from_naive_utc_and_offset(datetime, Utc));
        }

        Err(format!("ODBC: cannot decode DateTime<Utc> from '{text}'").into())
    }
}

impl<'r> Decode<'r, Odbc> for DateTime<FixedOffset> {
    fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> {
        if let Some(timestamp) = raw_timestamp(value) {
            return Ok(DateTime::<Utc>::from_naive_utc_and_offset(
                naive_from_timestamp(timestamp)?,
                Utc,
            )
            .fixed_offset());
        }

        let Some(text) = trimmed_text(value) else {
            return Err("ODBC: cannot decode DateTime<FixedOffset>".into());
        };

        if let Ok(datetime) = text.parse() {
            return Ok(datetime);
        }

        if let Ok(datetime) = <NaiveDateTime as Decode<Odbc>>::decode(value) {
            return Ok(DateTime::<Utc>::from_naive_utc_and_offset(datetime, Utc).fixed_offset());
        }

        Err(format!("ODBC: cannot decode DateTime<FixedOffset> from '{text}'").into())
    }
}

impl<'r> Decode<'r, Odbc> for DateTime<Local> {
    fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> {
        if let Some(timestamp) = raw_timestamp(value) {
            return Ok(DateTime::<Utc>::from_naive_utc_and_offset(
                naive_from_timestamp(timestamp)?,
                Utc,
            )
            .with_timezone(&Local));
        }

        Ok(<DateTime<Utc> as Decode<Odbc>>::decode(value)?.with_timezone(&Local))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::OdbcValue;
    use sqlx_core::type_info::TypeInfo;
    use sqlx_core::value::Value;

    #[test]
    fn naive_date_type_compatibility_matches_old_odbc() {
        assert!(<NaiveDate as Type<Odbc>>::compatible(&OdbcTypeInfo::DATE));
        assert!(<NaiveDate as Type<Odbc>>::compatible(
            &OdbcTypeInfo::varchar(None)
        ));
        assert!(<NaiveDate as Type<Odbc>>::compatible(
            &OdbcTypeInfo::INTEGER
        ));
    }

    #[test]
    fn naive_date_decodes_old_text_and_numeric_forms() -> Result<(), BoxDynError> {
        for value in [
            OdbcValue::new(OdbcValueKind::Text("2020-01-02".to_owned())),
            OdbcValue::new(OdbcValueKind::Text("20200102".to_owned())),
            OdbcValue::new(OdbcValueKind::BigInt(20200102)),
            OdbcValue::new(OdbcValueKind::Double(20200102.0)),
        ] {
            assert_eq!(
                <NaiveDate as Decode<Odbc>>::decode(value.as_ref())?,
                NaiveDate::from_ymd_opt(2020, 1, 2).unwrap()
            );
        }

        Ok(())
    }

    #[test]
    fn chrono_values_decode_raw_odbc_temporal_kinds() -> Result<(), BoxDynError> {
        let date = OdbcValue::new(OdbcValueKind::Date(odbc_api::sys::Date {
            year: 2020,
            month: 1,
            day: 2,
        }));
        assert_eq!(
            <NaiveDate as Decode<Odbc>>::decode(date.as_ref())?,
            NaiveDate::from_ymd_opt(2020, 1, 2).unwrap()
        );

        let time = OdbcValue::new(OdbcValueKind::Time(odbc_api::sys::Time {
            hour: 15,
            minute: 30,
            second: 45,
        }));
        assert_eq!(
            <NaiveTime as Decode<Odbc>>::decode(time.as_ref())?,
            NaiveTime::from_hms_opt(15, 30, 45).unwrap()
        );

        let timestamp = OdbcValue::new(OdbcValueKind::Timestamp(odbc_api::sys::Timestamp {
            year: 2020,
            month: 1,
            day: 2,
            hour: 15,
            minute: 30,
            second: 45,
            fraction: 123_456_789,
        }));
        assert_eq!(
            <NaiveDateTime as Decode<Odbc>>::decode(timestamp.as_ref())?,
            NaiveDate::from_ymd_opt(2020, 1, 2)
                .unwrap()
                .and_hms_nano_opt(15, 30, 45, 123_456_789)
                .unwrap()
        );

        Ok(())
    }

    #[test]
    fn chrono_values_decode_text_datetime_forms() -> Result<(), BoxDynError> {
        let value = OdbcValue::new(OdbcValueKind::Text("2020-01-02 15:30:45".to_owned()));
        let expected = NaiveDate::from_ymd_opt(2020, 1, 2)
            .unwrap()
            .and_hms_opt(15, 30, 45)
            .unwrap();

        assert_eq!(
            <NaiveDateTime as Decode<Odbc>>::decode(value.as_ref())?,
            expected
        );
        assert_eq!(
            <DateTime<Utc> as Decode<Odbc>>::decode(value.as_ref())?,
            DateTime::<Utc>::from_naive_utc_and_offset(expected, Utc)
        );

        Ok(())
    }

    #[test]
    fn chrono_values_encode_to_old_odbc_argument_forms() -> Result<(), BoxDynError> {
        let mut buf = Vec::new();
        let date = NaiveDate::from_ymd_opt(2020, 1, 2).unwrap();
        let _ = <NaiveDate as Encode<Odbc>>::encode(date, &mut buf)?;
        assert_eq!(
            buf,
            vec![OdbcArgumentValue::Date(odbc_api::sys::Date {
                year: 2020,
                month: 1,
                day: 2
            })]
        );

        buf.clear();
        let datetime = date.and_hms_nano_opt(15, 30, 45, 123_456_789).unwrap();
        let _ = <NaiveDateTime as Encode<Odbc>>::encode(datetime, &mut buf)?;
        assert_eq!(
            buf,
            vec![OdbcArgumentValue::Timestamp(odbc_api::sys::Timestamp {
                year: 2020,
                month: 1,
                day: 2,
                hour: 15,
                minute: 30,
                second: 45,
                fraction: 123_456_789
            })]
        );

        buf.clear();
        let utc = DateTime::<Utc>::from_naive_utc_and_offset(datetime, Utc);
        let _ = <DateTime<Utc> as Encode<Odbc>>::encode(utc, &mut buf)?;
        assert_eq!(buf, vec![OdbcArgumentValue::Text(utc.to_rfc3339())]);

        Ok(())
    }

    #[test]
    fn chrono_type_info_names_match_old_odbc() {
        assert_eq!(<NaiveDate as Type<Odbc>>::type_info().name(), "DATE");
        assert_eq!(<NaiveTime as Type<Odbc>>::type_info().name(), "TIME");
        assert_eq!(
            <NaiveDateTime as Type<Odbc>>::type_info().name(),
            "TIMESTAMP"
        );
        assert_eq!(
            <DateTime<FixedOffset> as Type<Odbc>>::type_info().name(),
            "TIMESTAMP"
        );
    }
}