[−][src]Struct odbc_api::buffers::TextRowSet
This row set binds a string buffer to each column, which is large enough to hold the maximum length string representation for each element in the row set at once.
Example
//! A program executing a query and printing the result as csv to standard out. Requires //! `anyhow` and `csv` crate. use anyhow::Error; use odbc_api::{buffers::TextRowSet, Cursor, Environment}; use std::{ ffi::CStr, io::{stdout, Write}, path::PathBuf, }; /// Maximum number of rows fetched with one row set. Fetching batches of rows is usually much /// faster than fetching individual rows. const BATCH_SIZE: u32 = 100000; fn main() -> Result<(), Error> { // Write csv to standard out let out = stdout(); let mut writer = csv::Writer::from_writer(out); // We know this is going to be the only ODBC environment in the entire process, so this is // safe. let environment = unsafe { Environment::new() }?; // Connect using a DSN. Alternatively we could have used a connection string let mut connection = environment.connect( "DataSourceName", "Username", "Password", )?; // Execute a one of query without any parameters. match connection.execute("SELECT * FROM TableName", ())? { Some(cursor) => { // Write the column names to stdout let mut headline : Vec<String> = cursor.column_names()?.collect::<Result<_,_>>()?; writer.write_record(headline)?; // Use schema in cursor to initialize a text buffer large enough to hold the largest // possible strings for each column. let mut buffers = TextRowSet::for_cursor(BATCH_SIZE, &cursor)?; // Bind the buffer to the cursor. It is now being filled with every call to fetch. let mut row_set_cursor = cursor.bind_buffer(&mut buffers)?; // Iterate over batches while let Some(batch) = row_set_cursor.fetch()? { // Within a batch, iterate over every row for row_index in 0..batch.num_rows() { // Within a row iterate over every column let record = (0..batch.num_cols()).map(|col_index| { batch .at(col_index, row_index) .map(CStr::to_bytes) .unwrap_or(&[]) }); // Writes row as csv writer.write_record(record)?; } } } None => { eprintln!( "Query came back empty. No output has been created." ); } } Ok(()) }
Implementations
impl TextRowSet
[src]
pub fn for_cursor(
batch_size: u32,
cursor: &impl Cursor
) -> Result<TextRowSet, Error>
[src]
batch_size: u32,
cursor: &impl Cursor
) -> Result<TextRowSet, Error>
Use cursor
to query the display size for each column of the row set and allocates the
buffers accordingly.
pub fn new(
batch_size: u32,
max_str_lengths: impl Iterator<Item = usize>
) -> Self
[src]
batch_size: u32,
max_str_lengths: impl Iterator<Item = usize>
) -> Self
Creates a text buffer large enough to hold batch_size
rows with one column for each item
max_str_lengths
of respective size.
pub fn at(&self, col_index: usize, row_index: usize) -> Option<&CStr>
[src]
Access the element at the specified position in the row set.
pub fn at_as_str(
&self,
col_index: usize,
row_index: usize
) -> Result<Option<&str>, Utf8Error>
[src]
&self,
col_index: usize,
row_index: usize
) -> Result<Option<&str>, Utf8Error>
Access the element at the specified position in the row set.
pub fn num_cols(&self) -> usize
[src]
Return the number of columns in the row set.
pub fn num_rows(&self) -> usize
[src]
Return the number of rows in the row set.
pub fn append<'a>(&mut self, row: impl Iterator<Item = Option<&'a [u8]>>)
[src]
Takes one element from the iterater for each internal column buffer and appends it to the
end of the buffer. Should the buffer be not large enough to hold the element, it will be
reallocated with 1.2
times its size.
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.
pub fn clear(&mut self)
[src]
Sets the number of rows in the buffer to zero.
Trait Implementations
impl<'_> ParameterCollection for &'_ TextRowSet
[src]
pub fn parameter_set_size(&self) -> u32
[src]
pub unsafe fn bind_parameters_to(
&self,
stmt: &mut Statement<'_>
) -> Result<(), Error>
[src]
&self,
stmt: &mut Statement<'_>
) -> Result<(), Error>
impl RowSetBuffer for TextRowSet
[src]
pub fn row_array_size(&self) -> u32
[src]
pub fn mut_num_fetch_rows(&mut self) -> &mut usize
[src]
pub fn bind_type(&self) -> u32
[src]
pub unsafe fn bind_to_cursor(
&mut self,
cursor: &mut impl Cursor
) -> Result<(), Error>
[src]
&mut self,
cursor: &mut impl Cursor
) -> Result<(), Error>
Auto Trait Implementations
impl RefUnwindSafe for TextRowSet
impl Send for TextRowSet
impl Sync for TextRowSet
impl Unpin for TextRowSet
impl UnwindSafe for TextRowSet
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,