postgres: fix decimal conversions

This commit is contained in:
Chris Sosnin 2020-12-10 22:22:33 +03:00
parent 82b66b29a3
commit 9747218ad3
2 changed files with 51 additions and 18 deletions

37
Cargo.lock generated
View File

@ -40,6 +40,12 @@ dependencies = [
"threadpool",
]
[[package]]
name = "ahash"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6789e291be47ace86a60303502173d84af8327e3627ecf334356ee0f87a164c"
[[package]]
name = "ahash"
version = "0.5.8"
@ -1018,6 +1024,18 @@ name = "hashbrown"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
dependencies = [
"ahash 0.4.6",
]
[[package]]
name = "hashlink"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d99cf782f0dc4372d26846bec3de7804ceb5df083c2d4462c0b8d2330e894fa8"
dependencies = [
"hashbrown",
]
[[package]]
name = "heck"
@ -1197,12 +1215,6 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "linked-hash-map"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
[[package]]
name = "lock_api"
version = "0.4.1"
@ -1221,15 +1233,6 @@ dependencies = [
"cfg-if 0.1.10",
]
[[package]]
name = "lru-cache"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c"
dependencies = [
"linked-hash-map",
]
[[package]]
name = "maplit"
version = "1.0.2"
@ -2200,7 +2203,7 @@ dependencies = [
name = "sqlx-core"
version = "0.4.0"
dependencies = [
"ahash",
"ahash 0.5.8",
"atoi",
"base64 0.13.0",
"bigdecimal",
@ -2220,6 +2223,7 @@ dependencies = [
"futures-core",
"futures-util",
"generic-array",
"hashlink",
"hex",
"hmac",
"ipnetwork",
@ -2227,7 +2231,6 @@ dependencies = [
"libc",
"libsqlite3-sys",
"log",
"lru-cache",
"md-5",
"memchr",
"num-bigint 0.3.1",

View File

@ -115,9 +115,11 @@ impl TryFrom<&'_ BigDecimal> for PgNumeric {
let mut digits = Vec::with_capacity(digits_len);
if let Some(first) = base_10.get(..offset) {
if offset != 0 {
if !first.is_empty() {
digits.push(base_10_to_10000(first));
}
} else if offset != 0 {
digits.push(base_10_to_10000(&base_10) * 10i16.pow(3 - base_10.len() as u32));
}
if let Some(rest) = base_10.get(offset..) {
@ -275,6 +277,34 @@ mod bigdecimal_to_pgnumeric {
);
}
#[test]
fn one_hundredth() {
let one_hundredth: BigDecimal = "0.01".parse().unwrap();
assert_eq!(
PgNumeric::try_from(&one_hundredth).unwrap(),
PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 2,
weight: -1,
digits: vec![100]
}
);
}
#[test]
fn twelve_thousandths() {
let twelve_thousandths: BigDecimal = "0.012".parse().unwrap();
assert_eq!(
PgNumeric::try_from(&twelve_thousandths).unwrap(),
PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 3,
weight: -1,
digits: vec![120]
}
);
}
#[test]
fn decimal_1() {
let decimal: BigDecimal = "1.2345".parse().unwrap();