Skip to content

Commit 88e55aa

Browse files
committed
Fix: PostgreSQL money values show as 0.0
The regression was introduced in v0.36.0. fixes #983
1 parent 168d781 commit 88e55aa

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG.md
22

3+
## v0.36.1
4+
- Fix regression introduced in v0.36.0: PostgreSQL money values showed as 0.0
5+
36
## v0.36.0
47
- added support for the MONEY and SMALLMONEY types in MSSQL.
58
- include [math functions](https://sqlite.org/lang_mathfunc.html) in the builtin sqlite3 database.

Cargo.lock

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ panic = "abort"
2323
codegen-units = 2
2424

2525
[dependencies]
26-
sqlx = { package = "sqlx-oldapi", version = "0.6.45", default-features = false, features = [
26+
sqlx = { package = "sqlx-oldapi", version = "0.6.47", default-features = false, features = [
2727
"any",
2828
"runtime-tokio-rustls",
2929
"migrate",

src/webserver/database/sql_to_json.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ fn decode_raw<'a, T: Decode<'a, sqlx::any::Any> + Default>(
5353
}
5454

5555
pub fn sql_nonnull_to_json<'r>(mut get_ref: impl FnMut() -> sqlx::any::AnyValueRef<'r>) -> Value {
56+
use AnyTypeInfoKind::{Mssql, MySql};
5657
let raw_value = get_ref();
5758
let type_info = raw_value.type_info();
5859
let type_name = type_info.name();
5960
log::trace!("Decoding a value of type {type_name:?} (type info: {type_info:?})");
61+
let AnyTypeInfo(ref db_type) = *type_info;
6062
match type_name {
61-
"REAL" | "FLOAT" | "FLOAT4" | "FLOAT8" | "DOUBLE" | "NUMERIC" | "DECIMAL" | "MONEY"
62-
| "SMALLMONEY" => decode_raw::<f64>(raw_value).into(),
63+
"REAL" | "FLOAT" | "FLOAT4" | "FLOAT8" | "DOUBLE" | "NUMERIC" | "DECIMAL" => {
64+
decode_raw::<f64>(raw_value).into()
65+
}
6366
"INT8" | "BIGINT" | "SERIAL8" | "BIGSERIAL" | "IDENTITY" | "INT64" | "INTEGER8"
6467
| "BIGINT SIGNED" => decode_raw::<i64>(raw_value).into(),
6568
"INT" | "INT4" | "INTEGER" | "MEDIUMINT" | "YEAR" => decode_raw::<i32>(raw_value).into(),
@@ -69,15 +72,11 @@ pub fn sql_nonnull_to_json<'r>(mut get_ref: impl FnMut() -> sqlx::any::AnyValueR
6972
decode_raw::<u32>(raw_value).into()
7073
}
7174
"BOOL" | "BOOLEAN" => decode_raw::<bool>(raw_value).into(),
72-
"BIT" if matches!(*type_info, AnyTypeInfo(AnyTypeInfoKind::Mssql(_))) => {
73-
decode_raw::<bool>(raw_value).into()
74-
}
75-
"BIT" if matches!(*type_info, AnyTypeInfo(AnyTypeInfoKind::MySql(ref mysql_type)) if mysql_type.max_size() == Some(1)) => {
75+
"BIT" if matches!(db_type, Mssql(_)) => decode_raw::<bool>(raw_value).into(),
76+
"BIT" if matches!(db_type, MySql(mysql_type) if mysql_type.max_size() == Some(1)) => {
7677
decode_raw::<bool>(raw_value).into()
7778
}
78-
"BIT" if matches!(*type_info, AnyTypeInfo(AnyTypeInfoKind::MySql(_))) => {
79-
decode_raw::<u64>(raw_value).into()
80-
}
79+
"BIT" if matches!(db_type, MySql(_)) => decode_raw::<u64>(raw_value).into(),
8180
"DATE" => decode_raw::<chrono::NaiveDate>(raw_value)
8281
.to_string()
8382
.into(),
@@ -93,6 +92,9 @@ pub fn sql_nonnull_to_json<'r>(mut get_ref: impl FnMut() -> sqlx::any::AnyValueR
9392
.format("%FT%T%.f")
9493
.to_string()
9594
.into(),
95+
"MONEY" | "SMALLMONEY" if matches!(db_type, Mssql(_)) => {
96+
decode_raw::<f64>(raw_value).into()
97+
}
9698
"JSON" | "JSON[]" | "JSONB" | "JSONB[]" => decode_raw::<Value>(raw_value),
9799
// Deserialize as a string by default
98100
_ => decode_raw::<String>(raw_value).into(),
@@ -212,7 +214,7 @@ mod tests {
212214
"jsonb": {"key": "value"},
213215
"age_interval": "2 mons 13 days",
214216
"justified_interval": "1 year 2 mons 3 days",
215-
"money_val": 0.0 // TODO: fix this. This should be 1234.56
217+
"money_val": "$1,234.56"
216218
}),
217219
);
218220
Ok(())

0 commit comments

Comments
 (0)