Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_decimal() #177

Merged
merged 7 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ uuid = "1.0"
error-chain = "0.12"
parking_lot = "0.12"
libc = "0.2"
bigdecimal = "0.4.2"

[dev-dependencies]
tokio = { version = "1.0", features = ["rt", "rt-multi-thread", "macros", "test-util"] }
Expand Down
17 changes: 17 additions & 0 deletions src/cassandra/row.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bigdecimal::BigDecimal;

use crate::cassandra::error::*;

use crate::cassandra::inet::Inet;
Expand Down Expand Up @@ -314,6 +316,21 @@ impl AsRustType<Vec<u8>> for Row<'_> {
}
}

impl AsRustType<BigDecimal> for Row<'_> {
fn get(&self, index: usize) -> Result<BigDecimal> {
let col = self.get_column(index)?;
col.get_decimal().map(|x| x.into())
}

fn get_by_name<S>(&self, name: S) -> Result<BigDecimal>
where
S: Into<String>,
{
let col = self.get_column_by_name(name)?;
col.get_decimal().map(|x| x.into())
}
}

impl<'a> Row<'a> {
/// Get a particular column by index
pub fn get_column(&self, index: usize) -> Result<Value> {
Expand Down
94 changes: 57 additions & 37 deletions src/cassandra/statement.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::cassandra_sys::CASS_ERROR_LIB_INVALID_DATA;
use bigdecimal::num_bigint::BigInt;
use bigdecimal::BigDecimal;

use crate::cassandra::collection::List;
use crate::cassandra::collection::Map;
use crate::cassandra::custom_payload::CustomPayload;
// use decimal::d128;
use crate::cassandra::collection::Set;
use crate::cassandra::consistency::Consistency;
use crate::cassandra::custom_payload::CustomPayload;
use crate::cassandra::error::*;
use crate::cassandra::future::CassFuture;
use crate::cassandra::inet::Inet;
Expand Down Expand Up @@ -69,6 +72,7 @@ use crate::cassandra_sys::cass_true;
use crate::cassandra_sys::CassStatement as _Statement;
use crate::cassandra_sys::CASS_UINT64_MAX;

use std::convert::TryInto;
use std::os::raw::c_char;
use std::time::Duration;

Expand Down Expand Up @@ -340,6 +344,16 @@ impl BindRustType<Vec<u8>> for Statement {
}
}

impl BindRustType<&BigDecimal> for Statement {
fn bind(&mut self, index: usize, value: &BigDecimal) -> Result<&mut Self> {
self.bind_decimal(index, value)
}

fn bind_by_name(&mut self, col: &str, value: &BigDecimal) -> Result<&mut Self> {
self.bind_decimal_by_name(col, value)
}
}

impl Statement {
/// Creates a new query statement.
pub(crate) fn new(session: Session, query: &str, parameter_count: usize) -> Self {
Expand Down Expand Up @@ -727,41 +741,47 @@ impl Statement {
}
}

// ///Bind a "decimal" to a query or bound statement at the specified index.
// pub fn bind_decimal(&self,
// index: i32,
// value: d128)
// -> Result<&mut Self, CassError> {
// unsafe {
// CassError::build(
// cass_statement_bind_decimal(
// self.inner(),
// index,
// value
// )
// ).wrap(&mut self)
// }
// }

// Binds a "decimal" to all the values with the specified name.
//
// This can only be used with statements created by
// cass_prepared_bind().
// pub fn bind_decimal_by_name<'a>(&'a self,
// name: &str,
// value: String)
// -> Result<&'a Self, CassError> {
// unsafe {
// let name = CString::new(name).unwrap();
// CassError::build(
// cass_statement_bind_decimal_by_name(
// self.inner(),
// name.as_ptr(),
// value
// )
// ).wrap(&self)
// }
// }
/// Binds a "BigDecimal" to a query or bound statement at the specified index.
pub fn bind_decimal(&mut self, index: usize, value: &BigDecimal) -> Result<&mut Self> {
let dec_parts = value.as_bigint_and_exponent();
let varint = dec_parts.0.to_signed_bytes_be();
let scale: i32 = match dec_parts.1.try_into() {
Ok(s) => s,
Err(_) => {
return Err(CASS_ERROR_LIB_INVALID_DATA.to_error());
}
};

unsafe {
cass_statement_bind_decimal(self.inner(), index, varint.as_ptr(), varint.len(), scale)
.to_result(self)
}
}

/// Binds an "BigDecimal" to all the values with the specified name.
pub fn bind_decimal_by_name(&mut self, name: &str, value: &BigDecimal) -> Result<&mut Self> {
let dec_parts = value.as_bigint_and_exponent();
let varint = dec_parts.0.to_signed_bytes_be();
let scale: i32 = match dec_parts.1.try_into() {
Ok(s) => s,
Err(_) => {
return Err(CASS_ERROR_LIB_INVALID_DATA.to_error());
}
};

unsafe {
let name_ptr = name.as_ptr() as *const c_char;
cass_statement_bind_decimal_by_name_n(
self.inner(),
name_ptr,
name.len(),
varint.as_ptr(),
varint.len(),
scale,
)
.to_result(self)
}
}

/// Bind a "map" to a query or bound statement at the specified index.
pub fn bind_map(&mut self, index: usize, map: Map) -> Result<&mut Self> {
Expand Down
31 changes: 24 additions & 7 deletions src/cassandra/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::cassandra_sys::cass_true;
use crate::cassandra_sys::cass_value_data_type;
use crate::cassandra_sys::cass_value_get_bool;
use crate::cassandra_sys::cass_value_get_bytes;
use crate::cassandra_sys::cass_value_get_decimal;
use crate::cassandra_sys::cass_value_get_double;
use crate::cassandra_sys::cass_value_get_float;
use crate::cassandra_sys::cass_value_get_inet;
Expand Down Expand Up @@ -68,6 +69,8 @@ use crate::cassandra_sys::CASS_VALUE_TYPE_UUID;
use crate::cassandra_sys::CASS_VALUE_TYPE_VARCHAR;
use crate::cassandra_sys::CASS_VALUE_TYPE_VARINT;

use bigdecimal::num_bigint::BigInt;
use bigdecimal::BigDecimal;
use std::ffi::CString;
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
Expand Down Expand Up @@ -221,7 +224,9 @@ impl Debug for Value {
ValueType::ASCII | ValueType::TEXT | ValueType::VARCHAR => {
write_value(f, self.get_string(), |f, v| write!(f, "{:?}", v))
}
ValueType::DECIMAL => write_value(f, self.get_bytes(), |f, v| write!(f, "{:?}", v)),
ValueType::DECIMAL => {
write_value(f, self.get_decimal(), |f, v| write!(f, "{:?}", v))
}
ValueType::COUNTER => write_value(f, self.get_i64(), |f, v| write!(f, "{:?}", v)),
ValueType::BIGINT => write_value(f, self.get_i64(), |f, v| write!(f, "{:?}", v)),
ValueType::DATE => write_value(f, self.get_u32(), |f, v| write!(f, "{:?}", v)),
Expand Down Expand Up @@ -266,7 +271,7 @@ impl Display for Value {
write_value(f, self.get_string(), |f, v| write!(f, "{}", v))
}
ValueType::DECIMAL => {
write_value(f, self.get_bytes(), |f, v| write!(f, "DECIMAL:{:?}", v))
write_value(f, self.get_decimal(), |f, v| write!(f, "DECIMAL:{:?}", v))
}
ValueType::COUNTER => write_value(f, self.get_i64(), |f, v| write!(f, "{}", v)),
ValueType::BIGINT => write_value(f, self.get_i64(), |f, v| write!(f, "{}", v)),
Expand Down Expand Up @@ -317,11 +322,6 @@ impl Value {
.map(|(output, output_size)| slice::from_raw_parts(output, output_size))
}
}
// pub fn get_decimal<'a>(&'a self, mut output: String) ->
// Result<String,CassError> {unsafe{
// CassError::build(cass_value_get_decimal(self.0,&mut
// output)).wrap(output)
// }}

/// Get the type of this Cassandra value
pub fn get_type(&self) -> ValueType {
Expand Down Expand Up @@ -496,4 +496,21 @@ impl Value {
};
unsafe { cass_value_get_uuid(self.0, &mut output).to_result(Uuid::build(output)) }
}

/// Get this value as a BigDecimal
pub fn get_decimal(&self) -> Result<BigDecimal> {
let mut varint = std::ptr::null();
let mut varint_size = 0;
let mut scale = 0;

unsafe {
cass_value_get_decimal(self.0, &mut varint, &mut varint_size, &mut scale)
.to_result((varint, varint_size, scale))
.map(|(varint, varint_size, scale)| {
let slice = slice::from_raw_parts(varint, varint_size);
let bigint = BigInt::from_signed_bytes_be(slice);
BigDecimal::new(bigint, scale as i64)
})
}
}
}