Skip to content

Commit 8785955

Browse files
committed
fix #16
1 parent 8088579 commit 8785955

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

sqlx-macros/src/database/mssql.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ impl_database_ext! {
1010
f32,
1111
f64,
1212
String,
13+
14+
#[cfg(feature = "chrono")]
15+
sqlx::types::chrono::NaiveTime,
16+
17+
#[cfg(feature = "chrono")]
18+
sqlx::types::chrono::NaiveDate,
19+
20+
#[cfg(feature = "chrono")]
21+
sqlx::types::chrono::NaiveDateTime,
22+
23+
#[cfg(feature = "chrono")]
24+
sqlx::types::chrono::DateTime<sqlx::types::chrono::Utc>,
25+
26+
#[cfg(feature = "chrono")]
27+
sqlx::types::chrono::DateTime<sqlx::types::chrono::FixedOffset>,
1328
},
1429
ParamChecking::Weak,
1530
feature-types: _info => None,

tests/mssql/macros.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,70 @@ async fn test_query_simple() -> anyhow::Result<()> {
1919

2020
Ok(())
2121
}
22+
23+
#[sqlx_macros::test]
24+
async fn test_query_datetime() -> anyhow::Result<()> {
25+
let mut conn = new::<Mssql>().await?;
26+
27+
// Define the expected NaiveDateTime value
28+
let expected_naive_dt = sqlx_oldapi::types::chrono::NaiveDate::from_ymd_opt(2024, 7, 15)
29+
.expect("Invalid date")
30+
.and_hms_milli_opt(10, 30, 0, 123)
31+
.expect("Invalid time");
32+
33+
// Use DATETIME2(3) for precise millisecond storage in MSSQL.
34+
// The query! macro requires a string literal.
35+
let record =
36+
sqlx_oldapi::query!("SELECT CAST('2024-07-15 10:30:00.123' AS DATETIME2(3)) as dt")
37+
.fetch_one(&mut conn)
38+
.await?;
39+
40+
assert_eq!(record.dt, Some(expected_naive_dt));
41+
42+
Ok(())
43+
}
44+
45+
#[derive(sqlx_oldapi::FromRow, Debug, Clone, PartialEq)]
46+
pub struct LogNotificationConfig {
47+
pub id: i32,
48+
pub config_key: String,
49+
pub config_value: String,
50+
pub created_on: Option<sqlx_oldapi::types::chrono::NaiveDateTime>,
51+
pub last_updated: Option<sqlx_oldapi::types::chrono::NaiveDateTime>,
52+
}
53+
54+
#[sqlx_macros::test]
55+
async fn test_query_as_from_issue() -> anyhow::Result<()> {
56+
let mut conn = new::<Mssql>().await?;
57+
58+
let expected_created_on = sqlx_oldapi::types::chrono::NaiveDate::from_ymd_opt(2023, 1, 1)
59+
.unwrap()
60+
.and_hms_milli_opt(10, 0, 0, 0)
61+
.unwrap();
62+
let expected_last_updated = sqlx_oldapi::types::chrono::NaiveDate::from_ymd_opt(2023, 1, 2)
63+
.unwrap()
64+
.and_hms_milli_opt(11, 30, 0, 500)
65+
.unwrap();
66+
67+
let result = sqlx_oldapi::query_as!(
68+
LogNotificationConfig,
69+
r#"
70+
SELECT
71+
1 AS id,
72+
'test_key' AS config_key,
73+
'test_value' AS config_value,
74+
CAST('2023-01-01 10:00:00.000' AS DATETIME2(3)) AS created_on,
75+
CAST('2023-01-02 11:30:00.500' AS DATETIME2(3)) AS last_updated
76+
"#
77+
)
78+
.fetch_one(&mut conn)
79+
.await?;
80+
81+
assert_eq!(result.id, 1);
82+
assert_eq!(result.config_key, "test_key");
83+
assert_eq!(result.config_value, "test_value");
84+
assert_eq!(result.created_on, Some(expected_created_on));
85+
assert_eq!(result.last_updated, Some(expected_last_updated));
86+
87+
Ok(())
88+
}

0 commit comments

Comments
 (0)