DecimalCql 
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.
Why Use DecimalCql?
- Reliable Serialization and Deserialization: Converts
Decimal to and from ScyllaDB's native DECIMAL.
- Type Safety: Ensures that your
Decimal values retain precision and accuracy during database operations.
- Struct Compatibility: Designed for use in Rust structs, making it easy to work with ScyllaDB rows.
Example
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) {
let decimal_1 = Decimal::from_str("3.1234567890987654321").unwrap();
let decimal_2 = Decimal::from_str("100.0987654321234567890").unwrap();
let data: Data = Data {
id: 1,
decimal_1: decimal_1.into(),
decimal_2: decimal_2.into(),
};
let value: Decimal = *data.decimal_1 / *data.decimal_2;
session.query_unpaged(
"INSERT INTO example_keyspace.example_table (id, decimal_1, decimal_2) VALUES (?, ?, ?)",
data, )
.await
.expect("Failed to insert data into ScyllaDB");
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");
let data: Data = rows.first_row().expect("No rows found");
let value: Decimal = *data.decimal_1 * *data.decimal_2;
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.
License
This library is licensed under the MIT License.