odbc_api::parameter

Struct BlobRead

Source
pub struct BlobRead<R> { /* private fields */ }
Expand description

Wraps an std::io::BufRead and implements self::Blob. Use this to stream contents from an std::io::BufRead to the database. The blob implementation is going to directly utilize the Buffer of the std::io::BufRead implementation, so the batch size is likely equal to that capacity.

Implementations§

Source§

impl<R> BlobRead<R>

Source

pub fn with_upper_bound(buf_read: R, upper_bound: usize) -> Self

Construct a blob read from any std::io::BufRead. The upper bound is used in the type description then binding the blob as a parameter.

§Examples

This is more flexible than Self::from_path. Note however that files provide metadata about the length of the data, which io::BufRead does not. This is not an issue for most drivers, but some can perform optimization if they know the size in advance. In the tests SQLite has shown a bug to only insert empty data if no size hint has been provided.

use std::io::BufRead;
use odbc_api::{Connection, parameter::{Blob, BlobRead}, IntoParameter, Error};

fn insert_image_to_db(
    conn: &Connection<'_>,
    id: &str,
    image_data: impl BufRead) -> Result<(), Error>
{
    const MAX_IMAGE_SIZE: usize = 4 * 1024 * 1024;
    let mut blob = BlobRead::with_upper_bound(image_data, MAX_IMAGE_SIZE);

    let sql = "INSERT INTO Images (id, image_data) VALUES (?, ?)";
    let parameters = (&id.into_parameter(), &mut blob.as_blob_param());
    conn.execute(sql, parameters)?;
    Ok(())
}
Source

pub unsafe fn with_exact_size(buf_read: R, exact_size: usize) -> Self

Construct a blob read from any std::io::BufRead. The upper bound is used in the type description then binding the blob as a parameter and is also passed to indicate the size of the actual value to the ODBC driver.

§Safety

The ODBC driver may use the exact size hint to allocate buffers internally. Too short may lead to invalid writes and too long may lead to invalid reads, so to be save the hint must be exact.

Source§

impl BlobRead<BufReader<File>>

Source

pub fn from_path(path: &Path) -> Result<Self>

Construct a blob from a Path. The metadata of the file is used to give the ODBC driver a size hint.

§Example

BlobRead::from_path is the most convenient way to turn a file path into a Blob parameter. The following example also demonstrates that the streamed blob parameter can be combined with reqular input parmeters like id.

use std::{error::Error, path::Path};
use odbc_api::{Connection, parameter::{Blob, BlobRead}, IntoParameter};

fn insert_image_to_db(
    conn: &Connection<'_>,
    id: &str,
    image_path: &Path) -> Result<(), Box<dyn Error>>
{
    let mut blob = BlobRead::from_path(&image_path)?;

    let sql = "INSERT INTO Images (id, image_data) VALUES (?, ?)";
    let parameters = (&id.into_parameter(), &mut blob.as_blob_param());
    conn.execute(sql, parameters)?;
    Ok(())
}

Trait Implementations§

Source§

impl<R> Blob for BlobRead<R>
where R: BufRead,

Source§

fn c_data_type(&self) -> CDataType

CData type of the binary data returned in the batches. Likely to be either crate::sys::CDataType::Binary, crate::sys::CDataType::Char or crate::sys::CDataType::WChar.
Source§

fn size_hint(&self) -> Option<usize>

Hint passed on to the driver regarding the combined size of all the batches. This hint is passed then the parameter is bound to the statement, so its meaning is only defined before the first call to next_batch. If None no hint about the total length of the batches is passed to the driver and the indicator will be set to crate::sys::DATA_AT_EXEC.
Source§

fn next_batch(&mut self) -> Result<Option<&[u8]>>

Retrieve the next batch of data from the source. Batches may not be empty. None indicates the last batch has been reached.
Source§

fn as_blob_param(&mut self) -> BlobParam<'_>
where Self: Sized,

Convinience function. Same as calling self::BlobParam::new.
Source§

impl<R> HasDataType for BlobRead<R>
where R: BufRead,

Source§

fn data_type(&self) -> DataType

The SQL data as which the parameter is bound to ODBC.

Auto Trait Implementations§

§

impl<R> Freeze for BlobRead<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for BlobRead<R>
where R: RefUnwindSafe,

§

impl<R> Send for BlobRead<R>
where R: Send,

§

impl<R> Sync for BlobRead<R>
where R: Sync,

§

impl<R> Unpin for BlobRead<R>
where R: Unpin,

§

impl<R> UnwindSafe for BlobRead<R>
where R: 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, 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.