Skip to content

Commit 385c53a

Browse files
committed
Merge pull request #4102 from VicVerevita/bigdecimal-to-isnumeric
Add Decimal in NUMERIC_TYPES
1 parent 585a052 commit 385c53a

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

diesel_derives/src/table.rs

+1
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ fn is_numeric(ty: &syn::TypePath) -> bool {
586586
"Int8",
587587
"Bigint",
588588
"BigSerial",
589+
"Decimal",
589590
"Float",
590591
"Float4",
591592
"Float8",

diesel_tests/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ mod limit_offset;
3737
mod macros;
3838
#[cfg(feature = "postgres")]
3939
mod only;
40+
#[cfg(not(feature = "sqlite"))]
41+
mod operations;
4042
mod order;
4143
mod perf_details;
4244
#[cfg(feature = "postgres")]

diesel_tests/tests/operations.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::str::FromStr;
2+
3+
use crate::schema::*;
4+
use bigdecimal::BigDecimal;
5+
use diesel::{insert_into, prelude::*, update};
6+
7+
diesel::table! {
8+
bigdecimal_table (id) {
9+
id -> Integer,
10+
big_decimal -> Decimal,
11+
}
12+
}
13+
14+
#[derive(Debug, Insertable, Queryable, Selectable)]
15+
#[diesel(table_name = bigdecimal_table)]
16+
struct CustomBigDecimal {
17+
pub id: i32,
18+
pub big_decimal: BigDecimal,
19+
}
20+
21+
#[test]
22+
fn big_decimal_add() {
23+
let connection = &mut connection();
24+
25+
let custom_value = CustomBigDecimal {
26+
id: 1,
27+
big_decimal: BigDecimal::from_str("0.80").unwrap(),
28+
};
29+
30+
diesel::sql_query(
31+
"
32+
CREATE TEMPORARY TABLE bigdecimal_table (
33+
id SERIAL PRIMARY KEY,
34+
big_decimal DECIMAL(8,2) NOT NULL
35+
);
36+
",
37+
)
38+
.execute(connection)
39+
.unwrap();
40+
41+
insert_into(bigdecimal_table::table)
42+
.values(&custom_value)
43+
.execute(connection)
44+
.unwrap();
45+
46+
let val = BigDecimal::from_str("0.1").unwrap();
47+
48+
update(bigdecimal_table::table)
49+
.set(bigdecimal_table::big_decimal.eq(bigdecimal_table::big_decimal + val))
50+
.execute(connection)
51+
.unwrap();
52+
53+
let updated: CustomBigDecimal = bigdecimal_table::table.first(connection).unwrap();
54+
55+
assert_eq!(BigDecimal::from_str("0.90").unwrap(), updated.big_decimal);
56+
}

0 commit comments

Comments
 (0)