use std::iter::repeat;
use lazy_static::lazy_static;
use odbc_api::{
buffers::{self, Indicator},
handles::{CDataMut, Statement, StatementRef},
Connection, ConnectionOptions, Cursor, Environment, Error, RowSetBuffer,
};
lazy_static! {
pub static ref ENV: Environment = {
let _ = env_logger::builder().is_test(true).try_init();
Environment::new().unwrap()
};
}
#[derive(Clone, Copy, Debug)]
pub struct Profile {
pub connection_string: &'static str,
pub index_type: &'static str,
pub blob_type: &'static str,
}
impl Profile {
pub fn connection(&self) -> Result<Connection<'static>, Error> {
ENV.connect_with_connection_string(self.connection_string, ConnectionOptions::default())
}
pub fn setup_empty_table(
&self,
table_name: &str,
column_types: &[&str],
) -> Result<Connection<'static>, odbc_api::Error> {
let conn = self.connection()?;
let table = Table::new(table_name, column_types);
conn.execute(&table.sql_drop_if_exists(), ())?;
conn.execute(&table.sql_create_table(self.index_type), ())?;
Ok(conn)
}
pub fn given<'a>(
&self,
table_name: &'a str,
column_types: &'a [&'a str],
) -> Result<(Connection<'static>, Table<'a>), odbc_api::Error> {
let conn = self.connection()?;
let table = Table::new(table_name, column_types);
conn.execute(&table.sql_drop_if_exists(), ())?;
conn.execute(&table.sql_create_table(self.index_type), ())?;
Ok((conn, table))
}
}
pub struct Table<'a> {
pub name: &'a str,
pub column_types: &'a [&'a str],
pub column_names: &'a [&'a str],
}
impl<'a> Table<'a> {
pub fn new(name: &'a str, column_types: &'a [&'a str]) -> Self {
let column_names = &["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"];
Table {
name,
column_types,
column_names: &column_names[..column_types.len()],
}
}
pub fn sql_drop_if_exists(&self) -> String {
format!("DROP TABLE IF EXISTS {};", self.name)
}
pub fn sql_create_table(&self, index_type: &str) -> String {
let cols = self
.column_types
.iter()
.zip(self.column_names)
.map(|(ty, name)| format!("{name} {ty}"))
.collect::<Vec<_>>()
.join(", ");
format!("CREATE TABLE {} (id {index_type},{cols});", self.name)
}
pub fn sql_all_ordered_by_id(&self) -> String {
let cols = self.column_names.join(",");
format!("SELECT {cols} FROM {} ORDER BY Id;", self.name)
}
pub fn sql_insert(&self) -> String {
let cols = self.column_names.join(",");
let placeholders = repeat("?")
.take(self.column_names.len())
.collect::<Vec<_>>()
.join(",");
format!(
"INSERT INTO {} ({cols}) VALUES ({placeholders});",
self.name
)
}
pub fn content_as_string(&self, conn: &Connection<'_>) -> String {
let cursor = conn
.execute(&self.sql_all_ordered_by_id(), ())
.unwrap()
.unwrap();
cursor_to_string(cursor)
}
}
pub fn cursor_to_string(mut cursor: impl Cursor) -> String {
let batch_size = 20;
let mut buffer = buffers::TextRowSet::for_cursor(batch_size, &mut cursor, Some(8192)).unwrap();
let mut row_set_cursor = cursor.bind_buffer(&mut buffer).unwrap();
let mut text = String::new();
let mut first_batch = true;
while let Some(row_set) = row_set_cursor.fetch().unwrap() {
if first_batch {
first_batch = false;
} else {
text.push('\n');
}
for row_index in 0..row_set.num_rows() {
if row_index != 0 {
text.push('\n');
}
for col_index in 0..row_set.num_cols() {
if col_index != 0 {
text.push(',');
}
text.push_str(
row_set
.at_as_str(col_index, row_index)
.unwrap()
.unwrap_or("NULL"),
);
}
}
}
text
}
pub struct SingleColumnRowSetBuffer<C> {
num_rows_fetched: Box<usize>,
batch_size: usize,
column: C,
}
impl<T> SingleColumnRowSetBuffer<Vec<T>>
where
T: Clone + Default,
{
pub fn new(batch_size: usize) -> Self {
SingleColumnRowSetBuffer {
num_rows_fetched: Box::new(0),
batch_size,
column: vec![T::default(); batch_size],
}
}
pub fn get(&self) -> &[T] {
&self.column[0..*self.num_rows_fetched]
}
}
unsafe impl<C> RowSetBuffer for SingleColumnRowSetBuffer<C>
where
C: CDataMut,
{
fn bind_type(&self) -> usize {
0 }
fn row_array_size(&self) -> usize {
self.batch_size
}
fn mut_num_fetch_rows(&mut self) -> &mut usize {
self.num_rows_fetched.as_mut()
}
unsafe fn bind_colmuns_to_cursor(
&mut self,
mut cursor: StatementRef<'_>,
) -> Result<(), odbc_api::Error> {
cursor.bind_col(1, &mut self.column).into_result(&cursor)?;
Ok(())
}
fn find_truncation(&self) -> Option<Indicator> {
unimplemented!()
}
}