Skip to content

Commit

Permalink
fix and test handling of 0 for BigDecimal in Postgres/MySQL
Browse files Browse the repository at this point in the history
closes #283
  • Loading branch information
abonander committed Apr 28, 2020
1 parent a7d0399 commit c285e28
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sqlx-core/src/postgres/types/bigdecimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,12 @@ impl TryFrom<PgNumeric> for BigDecimal {
}
};

if digits.is_empty() {
// Postgres returns an empty digit array for 0 but BigInt expects at least one zero
return Ok(0u64.into());
}

let sign = match sign {
_ if digits.is_empty() => Sign::NoSign,
PgNumericSign::Positive => Sign::Plus,
PgNumericSign::Negative => Sign::Minus,
};
Expand Down
1 change: 1 addition & 0 deletions tests/mysql-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ mod time_tests {
test_type!(decimal(
MySql,
sqlx::types::BigDecimal,
"CAST(0 as DECIMAL(0, 0))" == "0".parse::<sqlx::types::BigDecimal>().unwrap(),
"CAST(1 AS DECIMAL(1, 0))" == "1".parse::<sqlx::types::BigDecimal>().unwrap(),
"CAST(10000 AS DECIMAL(5, 0))" == "10000".parse::<sqlx::types::BigDecimal>().unwrap(),
"CAST(0.1 AS DECIMAL(2, 1))" == "0.1".parse::<sqlx::types::BigDecimal>().unwrap(),
Expand Down
2 changes: 2 additions & 0 deletions tests/postgres-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ test_prepared_type!(numeric(
test_type!(decimal(
Postgres,
sqlx::types::BigDecimal,
// https://github.com/launchbadge/sqlx/issues/283
"0::numeric" == "0".parse::<sqlx::types::BigDecimal>().unwrap(),
"1::numeric" == "1".parse::<sqlx::types::BigDecimal>().unwrap(),
"10000::numeric" == "10000".parse::<sqlx::types::BigDecimal>().unwrap(),
"0.1::numeric" == "0.1".parse::<sqlx::types::BigDecimal>().unwrap(),
Expand Down

0 comments on commit c285e28

Please sign in to comment.