use crate::{
CursorImpl, Error, Nullable, TruncationInfo,
buffers::{FetchRow, FetchRowMember as _},
handles::{AsStatementRef, SqlText, Statement, StatementRef},
parameter::VarCharArray,
};
#[derive(Clone, Copy, Default)]
pub struct ColumnsRow {
pub catalog: VarCharArray<128>,
pub schema: VarCharArray<128>,
pub table: VarCharArray<255>,
pub column_name: VarCharArray<255>,
pub data_type: i16,
pub type_name: VarCharArray<256>,
pub column_size: Nullable<i32>,
pub buffer_length: Nullable<i32>,
pub decimal_digits: Nullable<i16>,
pub num_prec_radix: Nullable<i16>,
pub nullable: i16,
pub remarks: VarCharArray<1024>,
pub column_default: VarCharArray<4000>,
pub sql_data_type: i16,
pub sql_datetime_sub: Nullable<i16>,
pub char_octet_length: Nullable<i32>,
pub ordinal_position: i32,
pub is_nullable: VarCharArray<4>,
}
unsafe impl FetchRow for ColumnsRow {
unsafe fn bind_columns_to_cursor(&mut self, mut cursor: StatementRef<'_>) -> Result<(), Error> {
unsafe {
self.catalog.bind_to_col(1, &mut cursor)?;
self.schema.bind_to_col(2, &mut cursor)?;
self.table.bind_to_col(3, &mut cursor)?;
self.column_name.bind_to_col(4, &mut cursor)?;
self.data_type.bind_to_col(5, &mut cursor)?;
self.type_name.bind_to_col(6, &mut cursor)?;
self.column_size.bind_to_col(7, &mut cursor)?;
self.buffer_length.bind_to_col(8, &mut cursor)?;
self.decimal_digits.bind_to_col(9, &mut cursor)?;
self.num_prec_radix.bind_to_col(10, &mut cursor)?;
self.nullable.bind_to_col(11, &mut cursor)?;
self.remarks.bind_to_col(12, &mut cursor)?;
self.column_default.bind_to_col(13, &mut cursor)?;
self.sql_data_type.bind_to_col(14, &mut cursor)?;
self.sql_datetime_sub.bind_to_col(15, &mut cursor)?;
self.char_octet_length.bind_to_col(16, &mut cursor)?;
self.ordinal_position.bind_to_col(17, &mut cursor)?;
self.is_nullable.bind_to_col(18, &mut cursor)?;
Ok(())
}
}
fn find_truncation(&self) -> Option<TruncationInfo> {
if let Some(t) = self.catalog.find_truncation(0) {
return Some(t);
}
if let Some(t) = self.schema.find_truncation(1) {
return Some(t);
}
if let Some(t) = self.table.find_truncation(2) {
return Some(t);
}
if let Some(t) = self.column_name.find_truncation(3) {
return Some(t);
}
if let Some(t) = self.data_type.find_truncation(4) {
return Some(t);
}
if let Some(t) = self.type_name.find_truncation(5) {
return Some(t);
}
if let Some(t) = self.column_size.find_truncation(6) {
return Some(t);
}
if let Some(t) = self.buffer_length.find_truncation(7) {
return Some(t);
}
if let Some(t) = self.decimal_digits.find_truncation(8) {
return Some(t);
}
if let Some(t) = self.num_prec_radix.find_truncation(9) {
return Some(t);
}
if let Some(t) = self.nullable.find_truncation(10) {
return Some(t);
}
if let Some(t) = self.remarks.find_truncation(11) {
return Some(t);
}
if let Some(t) = self.column_default.find_truncation(12) {
return Some(t);
}
if let Some(t) = self.sql_data_type.find_truncation(13) {
return Some(t);
}
if let Some(t) = self.sql_datetime_sub.find_truncation(14) {
return Some(t);
}
if let Some(t) = self.char_octet_length.find_truncation(15) {
return Some(t);
}
if let Some(t) = self.ordinal_position.find_truncation(16) {
return Some(t);
}
if let Some(t) = self.is_nullable.find_truncation(17) {
return Some(t);
}
None
}
}
pub fn execute_columns<S>(
mut statement: S,
catalog_name: &SqlText,
schema_name: &SqlText,
table_name: &SqlText,
column_name: &SqlText,
) -> Result<CursorImpl<S>, Error>
where
S: AsStatementRef,
{
let mut stmt = statement.as_stmt_ref();
stmt.columns(catalog_name, schema_name, table_name, column_name)
.into_result(&stmt)?;
debug_assert_ne!(stmt.num_result_cols().unwrap(), 0);
let cursor = unsafe { CursorImpl::new(statement) };
Ok(cursor)
}