use std::{collections::HashMap, fs::File, io::Write};
#[cfg(feature= "polars")]
use polars::prelude::*;
pub enum QueryResult {
Result { rows:Vec<Vec<String>>, col_names:Vec<String>},
None
}
impl QueryResult {
pub fn to_hashmap(&self) -> Option<HashMap<String, Vec<String>>> {
let mut hashmap = HashMap::<String, Vec<String>>::new();
match self {
QueryResult::Result { rows, col_names } => {
let cols = match self.rows_to_columns() {
Some(cols)
=> cols,
None => return None
};
for i in 0..cols.len() {
let col_name = col_names.get(i)?.clone();
let column = rows.get(i)?.clone();
hashmap.insert(col_name, column);
}
return Some(hashmap);
},
QueryResult::None => return None,
}
}
fn rows_to_columns(&self) -> Option<Vec<Vec<String>>> {
let mut col_table: Vec<Vec<String>> = Vec::new();
match self {
QueryResult::Result { rows, col_names } => {
for i in 0..col_names.len() {
let col = rows
.iter()
.map(|r| {
return r[i].clone();
})
.collect();
col_table.push(col);
}
return Some(col_table);
},
QueryResult::None => {
return None
}
}
}
pub(crate) fn new( rows: Vec<Vec<String>>, col_names: Vec<String>) -> Self {
if col_names.is_empty() || rows.is_empty(){
return QueryResult::None;
}
QueryResult::Result{ rows: rows, col_names: col_names }
}
pub fn to_csv(&self,file: &str) -> std::io::Result<()>{
match self {
QueryResult::Result { rows, col_names } => {
let mut file = File::create(file)?;
let column_heading = col_names.join(", ");
file.write_all(column_heading.as_bytes())?;
for row in rows {
file.write_all("\n".as_bytes())?;
file.write_all(row.join(", ").as_bytes())?;
}
},
&QueryResult::None => return Ok(())
}
Ok(())
}
#[cfg(feature = "polars")]
#[cfg_attr(docsrs,doc(cfg(feature = "polars")))]
pub fn to_polars(&self) -> Option<PolarsResult<DataFrame>> {
match self {
QueryResult::Result { col_names, .. } => {
let mut series_vec: Vec<polars::prelude::Column> = Vec::new();
let cols = match self.rows_to_columns() {
Some(cols)=> cols,
None=> return None,
};
for i in 0..cols.len() {
let col_name = col_names.get(i)?.clone();
let col = cols.get(i)?;
let s = polars::series::Series::new(col_name.into(), col.as_slice());
series_vec.push(s.into_column());
}
let df = polars::frame::DataFrame::new(series_vec);
return Some(df);
},
QueryResult::None => None,
}
}
}