1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
use odbc_sys::{len_data_at_exec, CDataType, DATA_AT_EXEC};
use crate::{
handles::{DelayedInput, HasDataType, Statement},
DataType, Error, ParameterRef,
};
use std::{
ffi::c_void,
fs::File,
io::{self, BufRead, BufReader},
path::Path,
};
/// A `Blob` can stream its contents to the database batch by batch and may therefore be used to
/// transfer large amounts of data, exceeding the drivers capabilities for normal input parameters.
///
/// # Safety
///
/// If a hint is implemented for `blob_size` it must be accurate before the first call to
/// `next_batch`.
pub unsafe trait Blob: HasDataType {
/// 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`].
fn c_data_type(&self) -> CDataType;
/// 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`].
fn size_hint(&self) -> Option<usize>;
/// Retrieve the netxt batch of data from the source. Batches may not be empty. `None` indicates
/// the last batch has been reached.
fn next_batch(&mut self) -> io::Result<Option<&[u8]>>;
/// Convinience function. Same as calling [`self::BlobParam::new`].
fn as_blob_param(&mut self) -> BlobParam
where
Self: Sized,
{
BlobParam::new(self)
}
}
/// Parameter type which can be used to bind a [`self::Blob`] as parameter to a statement in order
/// for its contents to be streamed to the database at statement execution time.
pub struct BlobParam<'a> {
/// Should be [`crate::sys::DATA_AT_EXEC`] if no size hint is given, or the result of
/// [`crate::sys::len_data_at_exec`].
indicator: isize,
/// Trait object to be bound as a delayed parameter.
blob: &'a mut dyn Blob,
}
impl<'a> BlobParam<'a> {
pub fn new(blob: &'a mut impl Blob) -> Self {
let indicator = if let Some(size) = blob.size_hint() {
len_data_at_exec(size.try_into().unwrap())
} else {
DATA_AT_EXEC
};
Self { indicator, blob }
}
}
unsafe impl DelayedInput for BlobParam<'_> {
fn cdata_type(&self) -> CDataType {
self.blob.c_data_type()
}
fn indicator_ptr(&self) -> *const isize {
&self.indicator as *const isize
}
fn stream_ptr(&mut self) -> *mut c_void {
// Types must have the same size for the transmute to work in the reverse cast.
debug_assert_eq!(
std::mem::size_of::<*mut &mut dyn Blob>(),
std::mem::size_of::<*mut c_void>()
);
&mut self.blob as *mut &mut dyn Blob as *mut c_void
}
}
impl HasDataType for BlobParam<'_> {
fn data_type(&self) -> DataType {
self.blob.data_type()
}
}
unsafe impl ParameterRef for &mut BlobParam<'_> {
unsafe fn bind_to(
&mut self,
parameter_number: u16,
stmt: &mut impl Statement,
) -> Result<(), Error> {
stmt.bind_delayed_input_parameter(parameter_number, *self)
.into_result(stmt)
}
}
/// Wraps borrowed bytes with a batch_size and implements [`self::Blob`]. Use this type to send long
/// array of bytes to the database.
pub struct BlobSlice<'a> {
/// If `true` the blob is going to be bound as [`DataType::LongVarbinary`] and the bytes are
/// interpreted as [`CDataType::Binary`]. If false the blob is going to be bound as
/// [`DataType::LongVarchar`] and the bytes are interpreted as [`CDataType::Char`].
pub is_binary: bool,
/// Maximum number of bytes transferred to the database in one go. May be largere than the
/// remaining blob size.
pub batch_size: usize,
/// Remaining bytes to transfer to the database.
pub blob: &'a [u8],
}
impl<'a> BlobSlice<'a> {
/// Construct a Blob from a byte slice. The blob is going to be bound as a `LongVarbinary` and
/// will be transmitted in one batch.
///
/// # Example
///
/// ```
/// use odbc_api::{Connection, parameter::{Blob, BlobSlice}, IntoParameter, Error};
///
/// fn insert_image(
/// conn: &Connection<'_>,
/// id: &str,
/// image_data: &[u8]
/// ) -> Result<(), Error>
/// {
/// let mut blob = BlobSlice::from_byte_slice(image_data);
///
/// let insert = "INSERT INTO Images (id, image_data) VALUES (?,?)";
/// let parameters = (&id.into_parameter(), &mut blob.as_blob_param());
/// conn.execute(&insert, parameters)?;
/// Ok(())
/// }
/// ```
pub fn from_byte_slice(blob: &'a [u8]) -> Self {
Self {
is_binary: true,
batch_size: blob.len(),
blob,
}
}
/// Construct a Blob from a text slice. The blob is going to be bound as a `LongVarchar` and
/// will be transmitted in one batch.
///
/// # Example
///
/// This example insert `title` as a normal input parameter but streams the potentially much
/// longer `String` in `text` to the database as a large text blob. This allows to circumvent
/// the size restrictions for `String` arguments of many drivers (usually around 4 or 8 KiB).
///
/// ```
/// use odbc_api::{Connection, parameter::{Blob, BlobSlice}, IntoParameter, Error};
///
/// fn insert_book(
/// conn: &Connection<'_>,
/// title: &str,
/// text: &str
/// ) -> Result<(), Error>
/// {
/// let mut blob = BlobSlice::from_text(&text);
///
/// let insert = "INSERT INTO Books (title, text) VALUES (?,?)";
/// let parameters = (&title.into_parameter(), &mut blob.as_blob_param());
/// conn.execute(&insert, parameters)?;
/// Ok(())
/// }
/// ```
pub fn from_text(text: &'a str) -> Self {
Self {
is_binary: false,
batch_size: text.len(),
blob: text.as_bytes(),
}
}
}
impl HasDataType for BlobSlice<'_> {
fn data_type(&self) -> DataType {
if self.is_binary {
DataType::LongVarbinary {
length: self.blob.len(),
}
} else {
DataType::LongVarchar {
length: self.blob.len(),
}
}
}
}
unsafe impl Blob for BlobSlice<'_> {
fn c_data_type(&self) -> CDataType {
if self.is_binary {
CDataType::Binary
} else {
CDataType::Char
}
}
fn size_hint(&self) -> Option<usize> {
Some(self.blob.len())
}
fn next_batch(&mut self) -> io::Result<Option<&[u8]>> {
if self.blob.is_empty() {
return Ok(None);
}
if self.blob.len() >= self.batch_size {
let (head, tail) = self.blob.split_at(self.batch_size);
self.blob = tail;
Ok(Some(head))
} else {
let last_batch = self.blob;
self.blob = &[];
Ok(Some(last_batch))
}
}
}
/// 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.
pub struct BlobRead<R> {
/// `true` if `size` is to interpreted as the exact ammount of bytes contained in the reader, at
/// the time of binding it as a parameter. `false` if `size` is to be interpreted as an upper
/// bound.
exact: bool,
size: usize,
consume: usize,
buf_read: R,
}
impl<R> BlobRead<R> {
/// 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(())
/// }
/// ```
pub fn with_upper_bound(buf_read: R, upper_bound: usize) -> Self {
Self {
exact: false,
consume: 0,
size: upper_bound,
buf_read,
}
}
/// 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.
pub unsafe fn with_exact_size(buf_read: R, exact_size: usize) -> Self {
Self {
exact: true,
consume: 0,
size: exact_size,
buf_read,
}
}
}
impl BlobRead<BufReader<File>> {
/// 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 convinient 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(())
/// }
/// ```
pub fn from_path(path: &Path) -> io::Result<Self> {
let file = File::open(path)?;
let size = file.metadata()?.len().try_into().unwrap();
let buf_read = BufReader::new(file);
Ok(Self {
consume: 0,
exact: true,
size,
buf_read,
})
}
}
impl<R> HasDataType for BlobRead<R>
where
R: BufRead,
{
fn data_type(&self) -> DataType {
DataType::LongVarbinary { length: self.size }
}
}
unsafe impl<R> Blob for BlobRead<R>
where
R: BufRead,
{
fn c_data_type(&self) -> CDataType {
CDataType::Binary
}
fn size_hint(&self) -> Option<usize> {
if self.exact {
Some(self.size)
} else {
None
}
}
fn next_batch(&mut self) -> io::Result<Option<&[u8]>> {
if self.consume != 0 {
self.buf_read.consume(self.consume);
}
let batch = self.buf_read.fill_buf()?;
self.consume = batch.len();
if batch.is_empty() {
Ok(None)
} else {
Ok(Some(batch))
}
}
}