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>
impl<R> BlobRead<R>
Sourcepub fn with_upper_bound(buf_read: R, upper_bound: usize) -> Self
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(())
}
Sourcepub unsafe fn with_exact_size(buf_read: R, exact_size: usize) -> Self
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>>
impl BlobRead<BufReader<File>>
Sourcepub fn from_path(path: &Path) -> Result<Self>
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,
impl<R> Blob for BlobRead<R>where
R: BufRead,
Source§fn c_data_type(&self) -> CDataType
fn c_data_type(&self) -> CDataType
crate::sys::CDataType::Binary
, crate::sys::CDataType::Char
or
crate::sys::CDataType::WChar
.Source§fn size_hint(&self) -> Option<usize>
fn size_hint(&self) -> Option<usize>
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]>>
fn next_batch(&mut self) -> Result<Option<&[u8]>>
None
indicates
the last batch has been reached.Source§fn as_blob_param(&mut self) -> BlobParam<'_>where
Self: Sized,
fn as_blob_param(&mut self) -> BlobParam<'_>where
Self: Sized,
self::BlobParam::new
.