odbc_api

Struct ColumnarBulkInserter

Source
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 use case.

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>

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. Therefore 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::SliceMut
where 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 one 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 intended 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> Freeze for ColumnarBulkInserter<S, C>
where S: Freeze,

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

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

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more