Skip to content

Commit 296c027

Browse files
committed
add Money support to mssql, extract shared code to decimal_tools and write test code
1 parent a02401e commit 296c027

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ repository = "https://github.com/lovasoa/sqlx"
2727
documentation = "https://docs.rs/sqlx"
2828
description = "🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite."
2929
edition = "2021"
30-
keywords = ["database", "async", "postgres", "mysql", "sqlite"]
30+
keywords = ["database", "async", "postgres", "mysql", "sqlite", "mssql"]
3131
categories = ["database", "asynchronous"]
3232
authors = [
3333
"Ryan Leckey <[email protected]>",
@@ -126,7 +126,7 @@ bstr = ["sqlx-core/bstr"]
126126
git2 = ["sqlx-core/git2"]
127127

128128
[dependencies]
129-
sqlx-core = { package = "sqlx-core-oldapi", version = "0.6.42", path = "sqlx-core", default-features = false }
129+
sqlx-core = { package = "sqlx-core-oldapi", version = "0.6.42", path = "sqlx-core", default-features = false }
130130
sqlx-macros = { package = "sqlx-macros-oldapi", version = "0.6.42", path = "sqlx-macros", default-features = false, optional = true }
131131

132132
[dev-dependencies]
@@ -149,7 +149,9 @@ rand_xoshiro = "0.7.0"
149149
hex = "0.4.3"
150150
tempdir = "0.3.7"
151151
# Needed to test SQLCipher
152-
libsqlite3-sys = { version = "0", features = ["bundled-sqlcipher-vendored-openssl"] }
152+
libsqlite3-sys = { version = "0", features = [
153+
"bundled-sqlcipher-vendored-openssl",
154+
] }
153155

154156

155157
[lints.rust]

sqlx-core/src/mssql/options/parse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@ impl std::error::Error for MssqlInvalidOption {}
143143

144144
#[test]
145145
fn it_parses_username_with_at_sign_correctly() {
146-
let url = "mysql://user@hostname:password@hostname:5432/database";
146+
let url = "mssql://user@hostname:password@hostname:5432/database";
147147
let opts = MssqlConnectOptions::from_str(url).unwrap();
148148

149149
assert_eq!("user@hostname", &opts.username);
150150
}
151151

152152
#[test]
153153
fn it_parses_password_with_non_ascii_chars_correctly() {
154-
let url = "mysql://username:p@ssw0rd@hostname:5432/database";
154+
let url = "mssql://username:p@ssw0rd@hostname:5432/database";
155155
let opts = MssqlConnectOptions::from_str(url).unwrap();
156156

157157
assert_eq!(Some("p@ssw0rd".into()), opts.password);

sqlx-core/src/mssql/types/decimal_tools.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ mod tests {
4141
use super::*;
4242
use std::error::Error;
4343

44-
fn err_of(msg: &str) -> String {
45-
format!("encountered unexpected or invalid data: {}", msg)
46-
}
47-
48-
// ========== decode_money_bytes ==========
44+
// ========== test decode_money_bytes ==========
4945

5046
#[test]
5147
fn test_decode_money_bytes_empty() {
@@ -54,7 +50,7 @@ mod tests {
5450
assert!(result.is_err());
5551
assert_eq!(
5652
result.unwrap_err().to_string(),
57-
err_of("expected 8/4 bytes for Money, got 0")
53+
err_protocol!("expected 8/4 bytes for Money, got {}", bytes.len()).to_string()
5854
);
5955
}
6056

@@ -65,7 +61,7 @@ mod tests {
6561
assert!(result.is_err());
6662
assert_eq!(
6763
result.unwrap_err().to_string(),
68-
err_of("expected 8/4 bytes for Money, got 3")
64+
err_protocol!("expected 8/4 bytes for Money, got {}", bytes.len()).to_string()
6965
);
7066
}
7167

@@ -118,7 +114,7 @@ mod tests {
118114
assert_eq!(amount, 0);
119115
}
120116

121-
// ========== decode_numeric_bytes ==========
117+
// ========== test decode_numeric_bytes ==========
122118

123119
#[test]
124120
fn test_decode_numeric_bytes_empty() {
@@ -127,7 +123,7 @@ mod tests {
127123
assert!(result.is_err());
128124
assert_eq!(
129125
result.unwrap_err().to_string(),
130-
err_of("numeric bytes cannot be empty")
126+
err_protocol!("numeric bytes cannot be empty").to_string()
131127
);
132128
}
133129

@@ -138,7 +134,7 @@ mod tests {
138134
assert!(result.is_err());
139135
assert_eq!(
140136
result.unwrap_err().to_string(),
141-
err_of("invalid sign byte: 0x02")
137+
err_protocol!("invalid sign byte: 0x02").to_string()
142138
);
143139
}
144140

@@ -149,7 +145,7 @@ mod tests {
149145
assert!(result.is_err());
150146
assert_eq!(
151147
result.unwrap_err().to_string(),
152-
err_of("numeric value exceeds 16 bytes")
148+
err_protocol!("numeric value exceeds 16 bytes").to_string()
153149
);
154150
}
155151

sqlx-macros/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#![allow(clippy::large_enum_variant)]
22
#![cfg_attr(
3-
not(any(feature = "postgres", feature = "mysql", feature = "offline")),
3+
not(any(
4+
feature = "postgres",
5+
feature = "mysql",
6+
feature = "mssql",
7+
feature = "offline"
8+
)),
49
allow(dead_code, unused_macros, unused_imports)
510
)]
611
#![cfg_attr(

tests/mssql/types.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ test_type!(numeric<f64>(Mssql,
6767
"CAST(0.0000000000000001 AS NUMERIC(38,16))" == 0.0000000000000001_f64,
6868
"CAST(939399419.1225182 AS NUMERIC(15,2))" == 939399419.12_f64,
6969
"CAST(939399419.1225182 AS DECIMAL(15,2))" == 939399419.12_f64,
70-
"CAST(123456789.0123456789 AS NUMERIC(38,10))" == 123_456_789.012_345_67_f64,
71-
"CAST(123456789.0123456789012 AS NUMERIC(38,13))" == 123_456_789.012_345_67_f64,
72-
"CAST(123456789.012345678901234 AS NUMERIC(38,15))" == 123_456_789.012_345_67_f64,
73-
70+
"CAST(123456789.0123456789 AS NUMERIC(38,10))" == 123_456_789.012_345_678_9_f64,
71+
"CAST(123456789.0123456789012 AS NUMERIC(38,13))" == 123_456_789.012_345_678_901_2_f64,
72+
"CAST(123456789.012345678901234 AS NUMERIC(38,15))" == 123_456_789.012_345_678_901_234_f64,
7473
"CAST(1.0000000000000001 AS NUMERIC(18,16))" == 1.0000000000000001_f64,
7574
"CAST(0.99999999999999 AS NUMERIC(18,14))" == 0.99999999999999_f64,
7675
"CAST(2.00000000000001 AS NUMERIC(18,14))" == 2.00000000000001_f64,
@@ -184,6 +183,8 @@ mod decimal {
184183
== Decimal::from_str_exact("0.01234567890123456789").unwrap(),
185184
"CAST('-12345678901234' AS DECIMAL(28,5))"
186185
== Decimal::from_str_exact("-12345678901234").unwrap(),
186+
"CAST('-1234567890.1234' AS MONEY)" == Decimal::from_str_exact("-1234567890.1234").unwrap(),
187+
"CAST('-123456.1234' AS SMALLMONEY)" == Decimal::from_str_exact("-123456.1234").unwrap(),
187188
));
188189
}
189190

@@ -204,6 +205,8 @@ mod bigdecimal {
204205
== BigDecimal::from_str("-12345678901234567890").unwrap(),
205206
"CAST('-12345678901234567890.012345678901234' AS DECIMAL(38,15))"
206207
== BigDecimal::from_str("-12345678901234567890.012345678901234").unwrap(),
208+
"CAST('-1234567890.1234' AS MONEY)" == BigDecimal::from_str("-1234567890.1234").unwrap(),
209+
"CAST('-123456.1234' AS SMALLMONEY)" == BigDecimal::from_str("-123456.1234").unwrap(),
207210
));
208211
}
209212

0 commit comments

Comments
 (0)