#decimal #scylla #precision #serialization #finance

rust_decimal_cql

A library that wraps rust_decimal::Decimal and implements traits for integration with ScyllaDB's native DECIMAL column type

1 stable release

new 1.0.0 Jan 24, 2025

#597 in Math

Download history 103/week @ 2025-01-22

103 downloads per month

MIT license

17KB
245 lines

DecimalCql   Build Status

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


// 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.


License

This library is licensed under the MIT License.

Dependencies

~11–21MB
~289K SLoC