Skip to content

Commit 54d9eec

Browse files
committed
Add support for extra non-standard "DATETIME UTC" type for sqlite
This allows us to explicitly opt into using `DateTime<Utc>` instead of `NaiveDateTime` without doing conversions at query time (either via `col as "col: DateTime<Utc>" or mapping the results manually). Ref: launchbadge#598 (comment)
1 parent d25ab07 commit 54d9eec

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

sqlx-core/src/sqlite/type_info.rs

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub(crate) enum DataType {
2626
Date,
2727
Time,
2828
Datetime,
29+
DatetimeUtc,
2930
}
3031

3132
/// Type information for a SQLite type.
@@ -58,6 +59,7 @@ impl TypeInfo for SqliteTypeInfo {
5859
DataType::Date => "DATE",
5960
DataType::Time => "TIME",
6061
DataType::Datetime => "DATETIME",
62+
DataType::DatetimeUtc => "DATETIME UTC",
6163
}
6264
}
6365
}
@@ -93,6 +95,7 @@ impl FromStr for DataType {
9395
"date" => DataType::Date,
9496
"time" => DataType::Time,
9597
"datetime" | "timestamp" => DataType::Datetime,
98+
"datetime utc" | "timestamp utc" => DataType::DatetimeUtc,
9699

97100
_ if s.contains("int") => DataType::Int64,
98101

@@ -148,5 +151,7 @@ fn test_data_type_from_str() -> Result<(), BoxDynError> {
148151
assert_eq!(DataType::Time, "TIME".parse()?);
149152
assert_eq!(DataType::Date, "DATE".parse()?);
150153

154+
assert_eq!(DataType::DatetimeUtc, "DATETIME UTC".parse()?);
155+
151156
Ok(())
152157
}

sqlx-core/src/sqlite/types/chrono.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use chrono::{DateTime, Local, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeZ
1212

1313
impl<Tz: TimeZone> Type<Sqlite> for DateTime<Tz> {
1414
fn type_info() -> SqliteTypeInfo {
15-
SqliteTypeInfo(DataType::Datetime)
15+
SqliteTypeInfo(DataType::DatetimeUtc)
1616
}
1717

1818
fn compatible(ty: &SqliteTypeInfo) -> bool {
19-
<NaiveDateTime as Type<Sqlite>>::compatible(ty)
19+
matches!(ty.0, DataType::DatetimeUtc) || <NaiveDateTime as Type<Sqlite>>::compatible(ty)
2020
}
2121
}
2222

0 commit comments

Comments
 (0)