pub struct VarBinary<B> { /* private fields */ }Expand description
Binds a byte array as Variadic sized binary data. It can not be used for columnar bulk fetches, but if the buffer type is stack allocated it can be utilized in row wise bulk fetches.
Meaningful instantiations of this type are:
self::VarBinarySlice- immutable borrowed parameter.self::VarBinarySliceMut- mutable borrowed input / output parameterself::VarBinaryArray- stack allocated owned input / output parameterself::VarBinaryBox- heap allocated owned input /output parameter
Implementations§
source§impl<B> VarBinary<B>where
B: Borrow<[u8]>,
impl<B> VarBinary<B>where
B: Borrow<[u8]>,
sourcepub fn from_buffer(buffer: B, indicator: Indicator) -> Self
pub fn from_buffer(buffer: B, indicator: Indicator) -> Self
Creates a new instance from an existing buffer.
Examples found in repository?
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
pub fn null() -> Self {
// Insert 0 in buffer to avoid binding as VARBINARY(0)
Self::from_buffer(Box::new([0]), Indicator::Null)
}
/// Create an instance from a `Vec`.
pub fn from_vec(val: Vec<u8>) -> Self {
let indicator = Indicator::Length(val.len());
let buffer = val.into_boxed_slice();
Self::from_buffer(buffer, indicator)
}
}
impl<B> VarBinary<B>
where
B: Borrow<[u8]>,
{
/// Creates a new instance from an existing buffer.
pub fn from_buffer(buffer: B, indicator: Indicator) -> Self {
Self {
buffer,
indicator: indicator.to_isize(),
}
}
/// Valid payload of the buffer returned as slice or `None` in case the indicator is
/// `NULL_DATA`.
pub fn as_bytes(&self) -> Option<&[u8]> {
let slice = self.buffer.borrow();
match self.indicator() {
Indicator::Null => None,
Indicator::NoTotal => Some(slice),
Indicator::Length(len) => {
if self.is_complete() {
Some(&slice[..len])
} else {
Some(slice)
}
}
}
}
/// Call this method to ensure that the entire field content did fit into the buffer. If you
/// retrieve a field using [`crate::CursorRow::get_data`], you can repeat the call until this
/// method is false to read all the data.
///
/// ```
/// use odbc_api::{CursorRow, parameter::VarBinaryArray, Error, handles::Statement};
///
/// fn process_large_binary(
/// col_index: u16,
/// row: &mut CursorRow<'_>
/// ) -> Result<(), Error>{
/// let mut buf = VarBinaryArray::<512>::NULL;
/// row.get_data(col_index, &mut buf)?;
/// while !buf.is_complete() {
/// // Process bytes in stream without allocation. We can assume repeated calls to
/// // get_data do not return `None` since it would have done so on the first call.
/// process_slice(buf.as_bytes().unwrap());
/// }
/// Ok(())
/// }
///
/// fn process_slice(text: &[u8]) { /*...*/}
///
/// ```
pub fn is_complete(&self) -> bool {
let slice = self.buffer.borrow();
match self.indicator() {
Indicator::Null => true,
Indicator::NoTotal => false,
Indicator::Length(len) => len <= slice.len(),
}
}
/// Read access to the underlying ODBC indicator. After data has been fetched the indicator
/// value is set to the length the buffer should have had to hold the entire value. It may also
/// be [`Indicator::Null`] to indicate `NULL` or [`Indicator::NoTotal`] which tells us the data
/// source does not know how big the buffer must be to hold the complete value.
/// [`Indicator::NoTotal`] implies that the content of the current buffer is valid up to its
/// maximum capacity.
pub fn indicator(&self) -> Indicator {
Indicator::from_isize(self.indicator)
}
}
impl<B> VarBinary<B>
where
B: Borrow<[u8]>,
{
/// Call this method to reset the indicator to a value which matches the length returned by the
/// [`Self::as_bytes`] method. This is useful if you want to insert values into the database
/// despite the fact, that they might have been truncated.
pub fn hide_truncation(&mut self) {
if !self.is_complete() {
self.indicator = self.buffer.borrow().len().try_into().unwrap();
}
}
}
unsafe impl<B> CData for VarBinary<B>
where
B: Borrow<[u8]>,
{
fn cdata_type(&self) -> CDataType {
CDataType::Binary
}
fn indicator_ptr(&self) -> *const isize {
&self.indicator as *const isize
}
fn value_ptr(&self) -> *const c_void {
self.buffer.borrow().as_ptr() as *const c_void
}
fn buffer_length(&self) -> isize {
// This is the maximum buffer length, but it is NOT the length of an instance of Self due to
// the missing size of the indicator value. As such the buffer length can not be used to
// correctly index a columnar buffer of Self.
self.buffer.borrow().len().try_into().unwrap()
}
}
impl<B> HasDataType for VarBinary<B>
where
B: Borrow<[u8]>,
{
fn data_type(&self) -> DataType {
DataType::Varbinary {
length: self.buffer.borrow().len(),
}
}
}
unsafe impl<B> CDataMut for VarBinary<B>
where
B: BorrowMut<[u8]>,
{
fn mut_indicator_ptr(&mut self) -> *mut isize {
&mut self.indicator as *mut isize
}
fn mut_value_ptr(&mut self) -> *mut c_void {
self.buffer.borrow_mut().as_mut_ptr() as *mut c_void
}
}
/// Binds a byte array as a variadic binary input parameter.
///
/// While a byte array can provide us with a pointer to the start of the array and the length of the
/// array itself, it can not provide us with a pointer to the length of the buffer. So to bind
/// byte slices (`&[u8]`) we need to store the length in a separate value.
///
/// This type is created if `into_parameter` of the `IntoParameter` trait is called on a `&[u8]`.
pub type VarBinarySlice<'a> = VarBinary<&'a [u8]>;
impl<'a> VarBinarySlice<'a> {
/// Indicates missing data
pub const NULL: Self = Self {
// Insert 0 in buffer to avoid binding as VARBINARY(0)
buffer: &[0],
indicator: NULL_DATA,
};
/// Constructs a new instance containing the bytes in the specified buffer.
pub fn new(value: &'a [u8]) -> Self {
Self::from_buffer(value, Indicator::Length(value.len()))
}More examples
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
pub fn get_binary(&mut self, col_or_param_num: u16, buf: &mut Vec<u8>) -> Result<bool, Error> {
// Utilize all of the allocated buffer. Make sure buffer can at least hold one element.
buf.resize(max(1, buf.capacity()), 0);
// We repeatedly fetch data and add it to the buffer. The buffer length is therefore the
// accumulated value size. This variable keeps track of the number of bytes we added with
// the current call to get_data.
let mut fetch_size = buf.len();
let mut target = VarBinarySliceMut::from_buffer(buf.as_mut_slice(), Indicator::Null);
// Fetch binary data into buffer.
self.get_data(col_or_param_num, &mut target)?;
let not_null = loop {
match target.indicator() {
// Value is `NULL`. We are done here.
Indicator::Null => {
buf.clear();
break false;
}
// We do not know how large the value is. Let's fetch the data with repeated calls
// to get_data.
Indicator::NoTotal => {
let old_len = buf.len();
// Use an exponential strategy for increasing buffer size.
buf.resize(old_len * 2, 0);
let buf_extend = &mut buf[old_len..];
fetch_size = buf_extend.len();
target = VarBinarySliceMut::from_buffer(buf_extend, Indicator::Null);
self.get_data(col_or_param_num, &mut target)?;
}
// We did get the complete value, including the terminating zero. Let's resize the
// buffer to match the retrieved value exactly (excluding terminating zero).
Indicator::Length(len) if len <= fetch_size => {
let shrink_by = fetch_size - len;
buf.resize(buf.len() - shrink_by, 0);
break true;
}
// We did not get all of the value in one go, but the data source has been friendly
// enough to tell us how much is missing.
Indicator::Length(len) => {
let still_missing = len - fetch_size;
let old_len = buf.len();
buf.resize(old_len + still_missing, 0);
let buf_extend = &mut buf[old_len..];
fetch_size = buf_extend.len();
target = VarBinarySliceMut::from_buffer(buf_extend, Indicator::Null);
self.get_data(col_or_param_num, &mut target)?;
}
}
};
Ok(not_null)
}sourcepub fn as_bytes(&self) -> Option<&[u8]>
pub fn as_bytes(&self) -> Option<&[u8]>
Valid payload of the buffer returned as slice or None in case the indicator is
NULL_DATA.
sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Call this method to ensure that the entire field content did fit into the buffer. If you
retrieve a field using crate::CursorRow::get_data, you can repeat the call until this
method is false to read all the data.
use odbc_api::{CursorRow, parameter::VarBinaryArray, Error, handles::Statement};
fn process_large_binary(
col_index: u16,
row: &mut CursorRow<'_>
) -> Result<(), Error>{
let mut buf = VarBinaryArray::<512>::NULL;
row.get_data(col_index, &mut buf)?;
while !buf.is_complete() {
// Process bytes in stream without allocation. We can assume repeated calls to
// get_data do not return `None` since it would have done so on the first call.
process_slice(buf.as_bytes().unwrap());
}
Ok(())
}
fn process_slice(text: &[u8]) { /*...*/}
Examples found in repository?
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
pub fn as_bytes(&self) -> Option<&[u8]> {
let slice = self.buffer.borrow();
match self.indicator() {
Indicator::Null => None,
Indicator::NoTotal => Some(slice),
Indicator::Length(len) => {
if self.is_complete() {
Some(&slice[..len])
} else {
Some(slice)
}
}
}
}
/// Call this method to ensure that the entire field content did fit into the buffer. If you
/// retrieve a field using [`crate::CursorRow::get_data`], you can repeat the call until this
/// method is false to read all the data.
///
/// ```
/// use odbc_api::{CursorRow, parameter::VarBinaryArray, Error, handles::Statement};
///
/// fn process_large_binary(
/// col_index: u16,
/// row: &mut CursorRow<'_>
/// ) -> Result<(), Error>{
/// let mut buf = VarBinaryArray::<512>::NULL;
/// row.get_data(col_index, &mut buf)?;
/// while !buf.is_complete() {
/// // Process bytes in stream without allocation. We can assume repeated calls to
/// // get_data do not return `None` since it would have done so on the first call.
/// process_slice(buf.as_bytes().unwrap());
/// }
/// Ok(())
/// }
///
/// fn process_slice(text: &[u8]) { /*...*/}
///
/// ```
pub fn is_complete(&self) -> bool {
let slice = self.buffer.borrow();
match self.indicator() {
Indicator::Null => true,
Indicator::NoTotal => false,
Indicator::Length(len) => len <= slice.len(),
}
}
/// Read access to the underlying ODBC indicator. After data has been fetched the indicator
/// value is set to the length the buffer should have had to hold the entire value. It may also
/// be [`Indicator::Null`] to indicate `NULL` or [`Indicator::NoTotal`] which tells us the data
/// source does not know how big the buffer must be to hold the complete value.
/// [`Indicator::NoTotal`] implies that the content of the current buffer is valid up to its
/// maximum capacity.
pub fn indicator(&self) -> Indicator {
Indicator::from_isize(self.indicator)
}
}
impl<B> VarBinary<B>
where
B: Borrow<[u8]>,
{
/// Call this method to reset the indicator to a value which matches the length returned by the
/// [`Self::as_bytes`] method. This is useful if you want to insert values into the database
/// despite the fact, that they might have been truncated.
pub fn hide_truncation(&mut self) {
if !self.is_complete() {
self.indicator = self.buffer.borrow().len().try_into().unwrap();
}
}sourcepub fn indicator(&self) -> Indicator
pub fn indicator(&self) -> Indicator
Read access to the underlying ODBC indicator. After data has been fetched the indicator
value is set to the length the buffer should have had to hold the entire value. It may also
be Indicator::Null to indicate NULL or Indicator::NoTotal which tells us the data
source does not know how big the buffer must be to hold the complete value.
Indicator::NoTotal implies that the content of the current buffer is valid up to its
maximum capacity.
Examples found in repository?
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
pub fn as_bytes(&self) -> Option<&[u8]> {
let slice = self.buffer.borrow();
match self.indicator() {
Indicator::Null => None,
Indicator::NoTotal => Some(slice),
Indicator::Length(len) => {
if self.is_complete() {
Some(&slice[..len])
} else {
Some(slice)
}
}
}
}
/// Call this method to ensure that the entire field content did fit into the buffer. If you
/// retrieve a field using [`crate::CursorRow::get_data`], you can repeat the call until this
/// method is false to read all the data.
///
/// ```
/// use odbc_api::{CursorRow, parameter::VarBinaryArray, Error, handles::Statement};
///
/// fn process_large_binary(
/// col_index: u16,
/// row: &mut CursorRow<'_>
/// ) -> Result<(), Error>{
/// let mut buf = VarBinaryArray::<512>::NULL;
/// row.get_data(col_index, &mut buf)?;
/// while !buf.is_complete() {
/// // Process bytes in stream without allocation. We can assume repeated calls to
/// // get_data do not return `None` since it would have done so on the first call.
/// process_slice(buf.as_bytes().unwrap());
/// }
/// Ok(())
/// }
///
/// fn process_slice(text: &[u8]) { /*...*/}
///
/// ```
pub fn is_complete(&self) -> bool {
let slice = self.buffer.borrow();
match self.indicator() {
Indicator::Null => true,
Indicator::NoTotal => false,
Indicator::Length(len) => len <= slice.len(),
}
}More examples
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
pub fn get_binary(&mut self, col_or_param_num: u16, buf: &mut Vec<u8>) -> Result<bool, Error> {
// Utilize all of the allocated buffer. Make sure buffer can at least hold one element.
buf.resize(max(1, buf.capacity()), 0);
// We repeatedly fetch data and add it to the buffer. The buffer length is therefore the
// accumulated value size. This variable keeps track of the number of bytes we added with
// the current call to get_data.
let mut fetch_size = buf.len();
let mut target = VarBinarySliceMut::from_buffer(buf.as_mut_slice(), Indicator::Null);
// Fetch binary data into buffer.
self.get_data(col_or_param_num, &mut target)?;
let not_null = loop {
match target.indicator() {
// Value is `NULL`. We are done here.
Indicator::Null => {
buf.clear();
break false;
}
// We do not know how large the value is. Let's fetch the data with repeated calls
// to get_data.
Indicator::NoTotal => {
let old_len = buf.len();
// Use an exponential strategy for increasing buffer size.
buf.resize(old_len * 2, 0);
let buf_extend = &mut buf[old_len..];
fetch_size = buf_extend.len();
target = VarBinarySliceMut::from_buffer(buf_extend, Indicator::Null);
self.get_data(col_or_param_num, &mut target)?;
}
// We did get the complete value, including the terminating zero. Let's resize the
// buffer to match the retrieved value exactly (excluding terminating zero).
Indicator::Length(len) if len <= fetch_size => {
let shrink_by = fetch_size - len;
buf.resize(buf.len() - shrink_by, 0);
break true;
}
// We did not get all of the value in one go, but the data source has been friendly
// enough to tell us how much is missing.
Indicator::Length(len) => {
let still_missing = len - fetch_size;
let old_len = buf.len();
buf.resize(old_len + still_missing, 0);
let buf_extend = &mut buf[old_len..];
fetch_size = buf_extend.len();
target = VarBinarySliceMut::from_buffer(buf_extend, Indicator::Null);
self.get_data(col_or_param_num, &mut target)?;
}
}
};
Ok(not_null)
}source§impl<B> VarBinary<B>where
B: Borrow<[u8]>,
impl<B> VarBinary<B>where
B: Borrow<[u8]>,
sourcepub fn hide_truncation(&mut self)
pub fn hide_truncation(&mut self)
Call this method to reset the indicator to a value which matches the length returned by the
Self::as_bytes method. This is useful if you want to insert values into the database
despite the fact, that they might have been truncated.
Trait Implementations§
source§impl<B> CData for VarBinary<B>where
B: Borrow<[u8]>,
impl<B> CData for VarBinary<B>where
B: Borrow<[u8]>,
source§fn cdata_type(&self) -> CDataType
fn cdata_type(&self) -> CDataType
fetch, the driver converts the data to this type. When it sends data to
the source, the driver converts the data from this type.source§fn indicator_ptr(&self) -> *const isize
fn indicator_ptr(&self) -> *const isize
source§fn value_ptr(&self) -> *const c_void
fn value_ptr(&self) -> *const c_void
cdata_type.source§fn buffer_length(&self) -> isize
fn buffer_length(&self) -> isize
CStr.source§impl<B> CDataMut for VarBinary<B>where
B: BorrowMut<[u8]>,
impl<B> CDataMut for VarBinary<B>where
B: BorrowMut<[u8]>,
source§fn mut_indicator_ptr(&mut self) -> *mut isize
fn mut_indicator_ptr(&mut self) -> *mut isize
source§fn mut_value_ptr(&mut self) -> *mut c_void
fn mut_value_ptr(&mut self) -> *mut c_void
cdata_type.