use crate::{
CursorImpl, Error, TruncationInfo,
buffers::{FetchRow, FetchRowMember as _},
handles::{SqlText, Statement, StatementRef},
parameter::VarCharArray,
};
#[derive(Clone, Copy, Default)]
pub struct PrimaryKeysRow {
pub catalog: VarCharArray<128>,
pub schema: VarCharArray<128>,
pub table: VarCharArray<255>,
pub column: VarCharArray<255>,
pub key_seq: i16,
pub pk_name: VarCharArray<128>,
}
unsafe impl FetchRow for PrimaryKeysRow {
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.bind_to_col(4, &mut cursor)?;
self.key_seq.bind_to_col(5, &mut cursor)?;
self.pk_name.bind_to_col(6, &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.find_truncation(3) {
return Some(t);
}
if let Some(t) = self.key_seq.find_truncation(4) {
return Some(t);
}
if let Some(t) = self.pk_name.find_truncation(5) {
return Some(t);
}
None
}
}
pub fn execute_primary_keys<S>(
mut statement: S,
catalog_name: Option<&str>,
schema_name: Option<&str>,
table_name: &str,
) -> Result<CursorImpl<S>, Error>
where
S: Statement,
{
statement
.primary_keys(
catalog_name.map(SqlText::new).as_ref(),
schema_name.map(SqlText::new).as_ref(),
&SqlText::new(table_name),
)
.into_result(&statement)?;
let cursor = unsafe { CursorImpl::new(statement) };
Ok(cursor)
}