use crate::cassandra::data_type::ConstDataType;
use crate::cassandra::error::*;
use crate::cassandra::statement::Statement;
use crate::cassandra::util::Protected;
use crate::cassandra_sys::cass_prepared_bind;
use crate::cassandra_sys::cass_prepared_free;
use crate::cassandra_sys::cass_prepared_parameter_data_type;
use crate::cassandra_sys::cass_prepared_parameter_data_type_by_name_n;
use crate::cassandra_sys::cass_prepared_parameter_name;
use crate::cassandra_sys::CassPrepared as _PreparedStatement;
use std::os::raw::c_char;
use std::{mem, slice, str};
#[derive(Debug)]
pub struct PreparedStatement(*const _PreparedStatement);
unsafe impl Send for PreparedStatement {}
unsafe impl Sync for PreparedStatement {}
impl Drop for PreparedStatement {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe { cass_prepared_free(self.0) }
}
}
}
impl Protected<*const _PreparedStatement> for PreparedStatement {
fn inner(&self) -> *const _PreparedStatement {
self.0
}
fn build(inner: *const _PreparedStatement) -> Self {
if inner.is_null() {
panic!("Unexpected null pointer")
};
PreparedStatement(inner)
}
}
impl PreparedStatement {
pub fn bind(&self) -> Statement {
unsafe { Statement::build(cass_prepared_bind(self.0)) }
}
pub fn parameter_name(&self, index: usize) -> Result<&str> {
unsafe {
let mut name = mem::zeroed();
let mut name_length = mem::zeroed();
cass_prepared_parameter_name(self.0, index, &mut name, &mut name_length)
.to_result(())
.and_then(|_| {
Ok(str::from_utf8(slice::from_raw_parts(
name as *const u8,
name_length as usize,
))?)
})
}
}
pub fn parameter_data_type(&self, index: usize) -> ConstDataType {
unsafe { ConstDataType::build(cass_prepared_parameter_data_type(self.0, index)) }
}
pub fn parameter_data_type_by_name(&self, name: &str) -> ConstDataType {
unsafe {
let name_ptr = name.as_ptr() as *const c_char;
ConstDataType::build(cass_prepared_parameter_data_type_by_name_n(
self.0,
name_ptr,
name.len(),
))
}
}
}