use std::marker::PhantomData;
use odbc_sys::{
CDataType, Desc, HDesc, HStmt, Handle, HandleType, IS_POINTER, IS_SMALLINT, Pointer,
};
use super::{AnyHandle, SqlResult, sql_result::ExtSqlReturn};
#[cfg(not(any(feature = "wide", all(not(feature = "narrow"), target_os = "windows"))))]
use odbc_sys::SQLSetDescField as sql_set_desc_field;
#[cfg(any(feature = "wide", all(not(feature = "narrow"), target_os = "windows")))]
use odbc_sys::SQLSetDescFieldW as sql_set_desc_field;
pub struct Descriptor<'stmt> {
handle: HDesc,
parent: PhantomData<&'stmt HStmt>,
}
impl Descriptor<'_> {
pub unsafe fn new(handle: HDesc) -> Self {
Self {
handle,
parent: PhantomData,
}
}
pub fn as_sys(&self) -> HDesc {
self.handle
}
pub fn set_precision(&mut self, rec_number: i16, precision: i16) -> SqlResult<()> {
unsafe {
sql_set_desc_field(
self.as_sys(),
rec_number,
Desc::Precision,
precision as Pointer,
IS_SMALLINT,
)
.into_sql_result("SQLSetDescField")
}
}
pub fn set_scale(&mut self, rec_number: i16, scale: i16) -> SqlResult<()> {
unsafe {
sql_set_desc_field(
self.as_sys(),
rec_number,
Desc::Scale,
scale as Pointer,
IS_SMALLINT,
)
.into_sql_result("SQLSetDescField")
}
}
pub unsafe fn set_type(&mut self, rec_number: i16, c_type: CDataType) -> SqlResult<()> {
unsafe {
sql_set_desc_field(
self.as_sys(),
rec_number,
Desc::Type,
c_type as i16 as Pointer,
IS_SMALLINT,
)
}
.into_sql_result("SQLSetDescField")
}
pub unsafe fn set_data_ptr(&mut self, rec_number: i16, data_ptr: Pointer) -> SqlResult<()> {
unsafe {
sql_set_desc_field(
self.as_sys(),
rec_number,
Desc::DataPtr,
data_ptr,
IS_POINTER,
)
}
.into_sql_result("SQLSetDescField")
}
}
unsafe impl AnyHandle for Descriptor<'_> {
fn as_handle(&self) -> Handle {
self.handle.as_handle()
}
fn handle_type(&self) -> HandleType {
HandleType::Desc
}
}