| Crates.io | rust_decimal_cql |
| lib.rs | rust_decimal_cql |
| version | 2.0.1 |
| created_at | 2025-01-24 07:06:09.601556+00 |
| updated_at | 2025-07-26 22:07:56.387302+00 |
| description | A library that wraps rust_decimal and implements (de)serialization support for ScyllaDB's native DECIMAL type. |
| homepage | https://github.com/erik404/ |
| repository | https://github.com/erik404/rust_decimal_cql |
| max_upload_size | |
| id | 1529108 |
| size | 63,483 |
rust_decimal_cql::DecimalCql is a wrapper
around rust_decimal::Decimal that implements
the DeserializeValue
and SerializeValue traits from
the ScyllaDB Rust Driver. This enables seamless integration with ScyllaDB's
native DECIMAL column type.
DecimalCql is fully compatible with the DeserializeRow
and SerializeRow traits, making it safe and effortless to use
within structs for storing and retrieving data directly from ScyllaDB.
Decimal to and from ScyllaDB's native DECIMAL.Decimal values retain precision and accuracy during database operations.
// Demonstrates the usage of DecimalCql for storing and retrieving Decimal values in ScyllaDB.
use rust_decimal::Decimal;
use rust_decimal_cql::DecimalCql;
use scylla::Session;
use scylla::query::Query;
use scylla::{DeserializeRow, SerializeRow, QueryRowsResult};
#[derive(SerializeRow, DeserializeRow, Debug)]
struct Data {
id: i64,
decimal_1: DecimalCql,
decimal_2: DecimalCql,
}
async fn example(session: Session) {
// Create Decimals
let decimal_1 = Decimal::from_str("3.1234567890987654321").unwrap();
let decimal_2 = Decimal::from_str("100.0987654321234567890").unwrap();
// Create the data struct, wrapping Decimal
let data: Data = Data {
id: 1,
decimal_1: decimal_1.into(),
decimal_2: decimal_2.into(),
};
// Access the Decimal by dereferencing
let value: Decimal = *data.decimal_1 / *data.decimal_2;
// Store the struct, no boilerplate code needed, just pass the struct
session.query_unpaged(
"INSERT INTO example_keyspace.example_table (id, decimal_1, decimal_2) VALUES (?, ?, ?)",
data, )
.await
.expect("Failed to insert data into ScyllaDB");
// Retrieve the row
let rows = session
.query_unpaged(
"SELECT id, decimal_1, decimal_2 FROM example_keyspace.example_table WHERE id = ?",
(1i64,),
)
.await
.expect("Failed to retrieve data from ScyllaDB")
.into_rows_result()
.expect("Failed to fetch rows");
// No boilerplate code needed, just use type annotation
let data: Data = rows.first_row().expect("No rows found");
// Access the retrieved Decimal by dereferencing
let value: Decimal = *data.decimal_1 * *data.decimal_2;
// Validate that the retrieved data matches the inserted data
assert_eq!(
*data.decimal_1,
Decimal::from_str("3.1234567890987654321").unwrap()
);
assert_eq!(
*data.decimal_2,
Decimal::from_str("100.0987654321234567890").unwrap()
);
}
Contributions are welcome! Feel free to open an issue or submit a pull request with improvements or new features.
This library is licensed under the MIT License.