1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* -------------------------------------------------------------------------------------------------- */
/* Port of the Intel Decimal Floating-Point Math Library decimal128 type to Rust. */
/* decmathlib-rs - Copyright (C) 2023-2024 Carlos Guzmán Álvarez */
/* -------------------------------------------------------------------------------------------------- */
/* Licensed under the MIT license. See LICENSE file in the project root for full license information. */
/* -------------------------------------------------------------------------------------------------- */
/* Intel® Decimal Floating-Point Math Library - Copyright (c) 2018 Intel Corp. */
/* -------------------------------------------------------------------------------------------------- */
#[macro_export]
macro_rules! sqlx_test {
($name:ident, sqlx_decimal, $precision:expr, $scale:expr, $input1:expr) => {
#[cfg(feature = "sqlx-postgres")]
#[sqlx::test]
#[allow(dead_code)]
async fn $name() -> sqlx::Result<()> {
use std::str::FromStr;
let table_name = stringify!($name);
// Create a connection pool to your PostgreSQL database
let pool = sqlx::PgPool::connect("postgres://postgres:postgres@localhost/postgres").await?;
// Drop the test table
let drop_query = format!("DROP TABLE IF EXISTS {}", table_name);
sqlx::query(&drop_query)
.execute(&pool)
.await?;
// Create the test table
let create_query = format!("CREATE TABLE {} (id SERIAL PRIMARY KEY, value DECIMAL({}, {}))", table_name, $precision, $scale);
sqlx::query(&create_query)
.execute(&pool)
.await?;
// Insert sample data into the table using a parameterized query
let dec1: decmathlib_rs::d128::d128 = decmathlib_rs::d128::d128::from_str($input1).unwrap();
let insert_query = format!("INSERT INTO {} (value) VALUES ($1)", table_name);
sqlx::query(&insert_query)
.bind(dec1)
.execute(&pool)
.await?;
#[derive(sqlx::FromRow)]
struct TestData { id: i32, value: decmathlib_rs::d128::d128 }
// Fetch data from the table
let select_query = format!("SELECT * FROM {}", table_name);
let result = sqlx::query_as::<_, TestData>(&select_query)
.fetch_one(&pool)
.await?;
assert_eq!(dec1, result.value);
Ok(())
}
};
}
// sqlx_test!(sqlx_decimal_001, sqlx_decimal, 18, 6, "100000.122334");
// sqlx_test!(sqlx_decimal_002, sqlx_decimal, 18, 6, "+9878987.5679766895E0");
// sqlx_test!(sqlx_decimal_005, sqlx_decimal, 18, 6, "9.999999999999999999999999999999999E6144");
// sqlx_test!(sqlx_decimal_006, sqlx_decimal, 34, 6, "1");
// sqlx_test!(sqlx_decimal_007, sqlx_decimal, 34, 6, "1E+4");
// sqlx_test!(sqlx_decimal_008, sqlx_decimal, 34, 6, "1E+8");
// sqlx_test!(sqlx_decimal_009, sqlx_decimal, 34, 6, "1E+12");
// sqlx_test!(sqlx_decimal_010, sqlx_decimal, 34, 6, "1E+16");
// sqlx_test!(sqlx_decimal_011, sqlx_decimal, 34, 6, "1E+20");
// sqlx_test!(sqlx_decimal_012, sqlx_decimal, 34, 6, "1E+24");
// sqlx_test!(sqlx_decimal_013, sqlx_decimal, 34, 6, "1E+28");