Skip to content

Commit 10bd785

Browse files
committed
docs: add info about CqlDecimal
1 parent 145e3bb commit 10bd785

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

docs/source/data-types/data-types.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Database types and their Rust equivalents:
2626
* `Time` <----> `value::CqlTime`, `chrono::NaiveTime`, `time::Time`
2727
* `Timestamp` <----> `value::CqlTimestamp`, `chrono::DateTime<Utc>`, `time::OffsetDateTime`
2828
* `Duration` <----> `value::CqlDuration`
29-
* `Decimal` <----> `bigdecimal::Decimal`
29+
* `Decimal` <----> `value::CqlDecimal`, `bigdecimal::Decimal`
3030
* `Varint` <----> `value::CqlVarint`, `num_bigint::BigInt` (v0.3 and v0.4)
3131
* `List` <----> `Vec<T>`
3232
* `Set` <----> `Vec<T>`

docs/source/data-types/decimal.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
# Decimal
2-
`Decimal` is represented as [`bigdecimal::BigDecimal`](https://docs.rs/bigdecimal/0.2.0/bigdecimal/struct.BigDecimal.html)
2+
`Decimal` is represented as `value::CqlDecimal` or [`bigdecimal::BigDecimal`](https://docs.rs/bigdecimal/latest/bigdecimal/struct.BigDecimal.html)
3+
4+
## value::CqlDecimal
5+
6+
Without any feature flags, the user can interact with `decimal` type by making use of `value::CqlDecimal` which is a very simple wrapper representing the value as signed binary number in big-endian order with a 32-bit scale.
7+
8+
```rust
9+
# extern crate scylla;
10+
# use scylla::Session;
11+
# use std::error::Error;
12+
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
13+
use scylla::IntoTypedRows;
14+
use scylla::frame::value::CqlDecimal;
15+
use std::str::FromStr;
16+
17+
// Insert a decimal (123.456) into the table
18+
let to_insert: CqlDecimal =
19+
CqlDecimal::from_signed_be_bytes_and_exponent(vec![0x01, 0xE2, 0x40], 3);
20+
session
21+
.query("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
22+
.await?;
23+
24+
// Read a decimal from the table
25+
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
26+
for row in rows.into_typed::<(CqlDecimal,)>() {
27+
let (decimal_value,): (CqlDecimal,) = row?;
28+
}
29+
}
30+
# Ok(())
31+
# }
32+
```
33+
34+
## bigdecimal::BigDecimal
35+
36+
To make use of `bigdecimal::Bigdecimal` type, user should enable `bigdecimal-04` crate feature.
337

438
```rust
539
# extern crate scylla;

0 commit comments

Comments
 (0)