pub struct ColumnarBulkInserter<S, C> { /* private fields */ }
Expand description

Can be used to execute a statement with bulk array paramters. Contrary to its name any statement with parameters can be executed, not only INSERT however inserting large amounts of data in batches is the primary intended usecase.

Binding new buffers is quite expensive in ODBC, so the parameter buffers are reused for each batch (so the pointers bound to the statment stay valid). So we copy each batch of data into the buffers already bound first rather than binding user defined buffer. Often the data might need to be transformed anyway, so the copy is no actual overhead. Once the buffers are filled with a batch, we send the data.

Implementations§

source§

impl<S, C> ColumnarBulkInserter<S, C>where S: AsStatementRef,

source

pub unsafe fn new(statement: S, parameters: Vec<C>) -> Result<Self, Error>where C: ColumnBuffer + HasDataType,

Users are not encouraged to call this directly.

Safety
  • Statement is expected to be a perpared statement.
  • Parameters must all be valid for insertion. An example for an invalid parameter would be a text buffer with a cell those indiactor value exceeds the maximum element length. This can happen after when truncation occurs then writing into a buffer.
source

pub fn execute(&mut self) -> Result<Option<CursorImpl<StatementRef<'_>>>, Error>

Execute the prepared statement, with the parameters bound

source

pub fn clear(&mut self)

Sets the number of rows in the buffer to zero.

source

pub fn num_rows(&self) -> usize

Number of valid rows in the buffer

source

pub fn set_num_rows(&mut self, num_rows: usize)

Set number of valid rows in the buffer. Must not be larger than the batch size. If the specified number than the number of valid rows currently held by the buffer additional they will just hold the value previously assigned to them. Therfore if extending the number of valid rows users should take care to assign values to these rows. However, even if not assigend it is always guaranteed that every cell is valid for insertion and will not cause out of bounds access down in the ODBC driver. Therfore this method is safe. You can set the number of valid rows before or after filling values into the buffer, but you must do so before executing the query.

source

pub fn column_mut<'a>(&'a mut self, buffer_index: usize) -> C::SliceMutwhere C: BoundInputSlice<'a>,

Use this method to gain write access to the actual column data.

Parameters
  • buffer_index: Please note that the buffer index is not identical to the ODBC column index. For once it is zero based. It also indexes the buffer bound, and not the columns of the output result set. This is important, because not every column needs to be bound. Some columns may simply be ignored. That being said, if every column of the output is bound in the buffer, in the same order in which they are enumerated in the result set, the relationship between column index and buffer index is buffer_index = column_index - 1.
Example

This method is intend to be called if using ColumnarBulkInserter for column wise bulk inserts.

use odbc_api::{Connection, Error, buffers::BufferDesc};

fn insert_birth_years(conn: &Connection, names: &[&str], years: &[i16])
    -> Result<(), Error>
{

    // All columns must have equal length.
    assert_eq!(names.len(), years.len());
    // Prepare the insert statement
    let prepared = conn.prepare("INSERT INTO Birthdays (name, year) VALUES (?, ?)")?;
    // Create a columnar buffer which fits the input parameters.
    let buffer_description = [
        BufferDesc::Text { max_str_len: 255 },
        BufferDesc::I16 { nullable: false },
    ];
    // Here we do everything in one batch. So the capacity is the number of input
    // parameters.
    let capacity = names.len();
    let mut prebound = prepared.into_column_inserter(capacity, buffer_description)?;
    // Set number of input rows in the current batch.
    prebound.set_num_rows(names.len());
    // Fill the buffer with values column by column

    // Fill names
    let mut col = prebound
        .column_mut(0)
        .as_text_view()
        .expect("We know the name column to hold text.");
    for (index, name) in names.iter().map(|s| Some(s.as_bytes())).enumerate() {
        col.set_cell(index, name);
    }

    // Fill birth years
    let mut col = prebound
        .column_mut(1)
        .as_slice::<i16>()
        .expect("We know the year column to hold i16.");
    col.copy_from_slice(years);

    // Execute the prepared statment with the bound array parameters. Sending the values to
    // the database.
    prebound.execute()?;
    Ok(())
}
source

pub fn capacity(&self) -> usize

Maximum number of rows the buffer can hold at once.

source§

impl<S> ColumnarBulkInserter<S, TextColumn<u8>>

source

pub fn append<'b>( &mut self, row: impl Iterator<Item = Option<&'b [u8]>> ) -> Result<(), Error>where S: AsStatementRef,

Takes one element from the iterator for each internal column buffer and appends it to the end of the buffer. Should a cell of the row be too large for the associated column buffer, the column buffer will be reallocated with 1.2 times its size, and rebound to the statement.

This method panics if it is tried to insert elements beyond batch size. It will also panic if row does not contain at least one item for each internal column buffer.

Auto Trait Implementations§

§

impl<S, C> RefUnwindSafe for ColumnarBulkInserter<S, C>where C: RefUnwindSafe, S: RefUnwindSafe,

§

impl<S, C> Send for ColumnarBulkInserter<S, C>where C: Send, S: Send,

§

impl<S, C> Sync for ColumnarBulkInserter<S, C>where C: Sync, S: Sync,

§

impl<S, C> Unpin for ColumnarBulkInserter<S, C>where C: Unpin, S: Unpin,

§

impl<S, C> UnwindSafe for ColumnarBulkInserter<S, C>where C: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.