Skip to content

Commit

Permalink
use BufferDesc to construct AnyBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Nov 18, 2022
1 parent 3a9463c commit 7f5b8ec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 67 deletions.
85 changes: 20 additions & 65 deletions odbc-api/src/buffers/any_buffer.rs
Expand Up @@ -17,7 +17,7 @@ use super::{
},
columnar::ColumnBuffer,
text_column::TextColumnSliceMut,
BinColumn, BinColumnView, BufferDesc, BufferDescription, BufferKind, CharColumn,
BinColumn, BinColumnView, BufferDesc, BufferDescription, CharColumn,
ColumnarBuffer, Item, NullableSlice, NullableSliceMut, TextColumn, TextColumnView, WCharColumn,
};

Expand Down Expand Up @@ -66,80 +66,35 @@ pub enum AnyBuffer {

impl AnyBuffer {
/// Map buffer description to actual buffer.
#[deprecated = "Use try_from_desc instead"]
pub fn try_from_description(
max_rows: usize,
desc: BufferDescription,
) -> Result<Self, TooLargeBufferSize> {
let fallible_allocations = true;
Self::impl_from_description(max_rows, desc, fallible_allocations)
Self::impl_from_desc(max_rows, desc.into(), fallible_allocations)
}

/// Map buffer description to actual buffer.
pub fn try_from_desc(
max_rows: usize,
desc: BufferDesc,
) -> Result<Self, TooLargeBufferSize> {
let fallible_allocations = true;
Self::impl_from_desc(max_rows, desc, fallible_allocations)
}

/// Map buffer description to actual buffer.
#[deprecated = "Use form_desc instead"]
pub fn from_description(max_rows: usize, desc: BufferDescription) -> Self {
let fallible_allocations = false;
Self::impl_from_description(max_rows, desc, fallible_allocations).unwrap()
Self::impl_from_desc(max_rows, desc.into(), fallible_allocations).unwrap()
}

/// Map buffer description to actual buffer.
fn impl_from_description(
max_rows: usize,
desc: BufferDescription,
fallible_allocations: bool,
) -> Result<Self, TooLargeBufferSize> {
let buffer = match (desc.kind, desc.nullable) {
(BufferKind::Binary { length }, _) => {
if fallible_allocations {
AnyBuffer::Binary(BinColumn::try_new(max_rows as usize, length)?)
} else {
AnyBuffer::Binary(BinColumn::new(max_rows as usize, length))
}
}
(BufferKind::Text { max_str_len }, _) => {
if fallible_allocations {
AnyBuffer::Text(TextColumn::try_new(max_rows as usize, max_str_len)?)
} else {
AnyBuffer::Text(TextColumn::new(max_rows as usize, max_str_len))
}
}
(BufferKind::WText { max_str_len }, _) => {
if fallible_allocations {
AnyBuffer::WText(TextColumn::try_new(max_rows as usize, max_str_len)?)
} else {
AnyBuffer::WText(TextColumn::new(max_rows as usize, max_str_len))
}
}
(BufferKind::Date, false) => AnyBuffer::Date(vec![Date::default(); max_rows as usize]),
(BufferKind::Time, false) => AnyBuffer::Time(vec![Time::default(); max_rows as usize]),
(BufferKind::Timestamp, false) => {
AnyBuffer::Timestamp(vec![Timestamp::default(); max_rows as usize])
}
(BufferKind::F64, false) => AnyBuffer::F64(vec![f64::default(); max_rows as usize]),
(BufferKind::F32, false) => AnyBuffer::F32(vec![f32::default(); max_rows as usize]),
(BufferKind::I8, false) => AnyBuffer::I8(vec![i8::default(); max_rows as usize]),
(BufferKind::I16, false) => AnyBuffer::I16(vec![i16::default(); max_rows as usize]),
(BufferKind::I32, false) => AnyBuffer::I32(vec![i32::default(); max_rows as usize]),
(BufferKind::I64, false) => AnyBuffer::I64(vec![i64::default(); max_rows as usize]),
(BufferKind::U8, false) => AnyBuffer::U8(vec![u8::default(); max_rows as usize]),
(BufferKind::Bit, false) => AnyBuffer::Bit(vec![Bit::default(); max_rows as usize]),
(BufferKind::Date, true) => {
AnyBuffer::NullableDate(OptDateColumn::new(max_rows as usize))
}
(BufferKind::Time, true) => {
AnyBuffer::NullableTime(OptTimeColumn::new(max_rows as usize))
}
(BufferKind::Timestamp, true) => {
AnyBuffer::NullableTimestamp(OptTimestampColumn::new(max_rows as usize))
}
(BufferKind::F64, true) => AnyBuffer::NullableF64(OptF64Column::new(max_rows as usize)),
(BufferKind::F32, true) => AnyBuffer::NullableF32(OptF32Column::new(max_rows as usize)),
(BufferKind::I8, true) => AnyBuffer::NullableI8(OptI8Column::new(max_rows as usize)),
(BufferKind::I16, true) => AnyBuffer::NullableI16(OptI16Column::new(max_rows as usize)),
(BufferKind::I32, true) => AnyBuffer::NullableI32(OptI32Column::new(max_rows as usize)),
(BufferKind::I64, true) => AnyBuffer::NullableI64(OptI64Column::new(max_rows as usize)),
(BufferKind::U8, true) => AnyBuffer::NullableU8(OptU8Column::new(max_rows as usize)),
(BufferKind::Bit, true) => AnyBuffer::NullableBit(OptBitColumn::new(max_rows as usize)),
};
Ok(buffer)
pub fn from_desc(max_rows: usize, desc: BufferDesc) -> Self {
let fallible_allocations = false;
Self::impl_from_desc(max_rows, desc, fallible_allocations).unwrap()
}

/// Map buffer description to actual buffer.
Expand Down Expand Up @@ -378,7 +333,7 @@ impl ColumnarAnyBuffer {
let columns = descs
.into_iter()
.map(move |desc| {
let buffer = AnyBuffer::from_description(capacity, desc);
let buffer = AnyBuffer::from_desc(capacity, desc.into());
column_index += 1;
(column_index, buffer)
})
Expand All @@ -397,7 +352,7 @@ impl ColumnarAnyBuffer {
let mut column_index = 0;
let columns = descs
.map(move |desc| {
let buffer = AnyBuffer::try_from_description(capacity, desc)
let buffer = AnyBuffer::try_from_desc(capacity, desc.into())
.map_err(|source| source.add_context(column_index))?;
column_index += 1;
Ok((column_index, buffer))
Expand All @@ -418,7 +373,7 @@ impl ColumnarAnyBuffer {
.map(|(col_index, buffer_desc)| {
(
col_index,
AnyBuffer::from_description(max_rows, buffer_desc),
AnyBuffer::from_desc(max_rows, buffer_desc.into()),
)
})
.collect();
Expand Down
22 changes: 22 additions & 0 deletions odbc-api/src/buffers/description.rs
Expand Up @@ -152,6 +152,28 @@ impl BufferDescription {
}
}

impl From<BufferDescription> for BufferDesc {
fn from(source: BufferDescription) -> Self {
let nullable = source.nullable;
match source.kind {
BufferKind::Binary { length } => BufferDesc::Binary { length },
BufferKind::Text { max_str_len } => BufferDesc::Text { max_str_len },
BufferKind::WText { max_str_len } => BufferDesc::WText { max_str_len },
BufferKind::F64 => BufferDesc::F64 { nullable },
BufferKind::F32 => BufferDesc::F32 { nullable },
BufferKind::Date => BufferDesc::Date { nullable },
BufferKind::Time => BufferDesc::Time { nullable },
BufferKind::Timestamp => BufferDesc::Timestamp { nullable },
BufferKind::I8 => BufferDesc::I8 { nullable },
BufferKind::I16 => BufferDesc::I16 { nullable },
BufferKind::I32 => BufferDesc::I32 { nullable },
BufferKind::I64 => BufferDesc::I64 { nullable },
BufferKind::U8 => BufferDesc::U8 { nullable },
BufferKind::Bit => BufferDesc::Bit { nullable },
}
}
}

/// This class is used together with [`BufferDescription`] to specify the layout of buffers bound to
/// ODBC cursors and statements.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down
4 changes: 2 additions & 2 deletions odbc-api/src/prepared.rs
Expand Up @@ -194,7 +194,7 @@ where
) -> Result<ColumnarBulkInserter<S, AnyBuffer>, Error> {
let parameter_buffers = descriptions
.into_iter()
.map(|desc| AnyBuffer::from_description(capacity, desc))
.map(|desc| AnyBuffer::from_desc(capacity, desc.into()))
.collect();
unsafe { self.unchecked_bind_columnar_array_parameters(parameter_buffers) }
}
Expand All @@ -214,7 +214,7 @@ where

let parameter_buffers = descriptions
.into_iter()
.map(|desc| AnyBuffer::from_description(capacity, desc))
.map(|desc| AnyBuffer::from_desc(capacity, desc.into()))
.collect();
unsafe { ColumnarBulkInserter::new(stmt, parameter_buffers) }
}
Expand Down

0 comments on commit 7f5b8ec

Please sign in to comment.