use crate::types::RowValues;
#[derive(Debug, Clone)]
pub struct CustomDbRow {
pub column_names: std::sync::Arc<Vec<String>>,
pub rows: Vec<RowValues>,
#[doc(hidden)]
pub(crate) column_index_cache: std::sync::Arc<std::collections::HashMap<String, usize>>,
}
impl CustomDbRow {
#[must_use]
pub fn new(column_names: std::sync::Arc<Vec<String>>, rows: Vec<RowValues>) -> Self {
let cache = std::sync::Arc::new(
column_names
.iter()
.enumerate()
.map(|(i, name)| (name.clone(), i))
.collect::<std::collections::HashMap<_, _>>(),
);
Self {
column_names,
rows,
column_index_cache: cache,
}
}
#[must_use]
pub fn get_column_index(&self, column_name: &str) -> Option<usize> {
if let Some(&idx) = self.column_index_cache.get(column_name) {
return Some(idx);
}
self.column_names.iter().position(|col| col == column_name)
}
#[must_use]
pub fn get(&self, column_name: &str) -> Option<&RowValues> {
let index_opt = self.get_column_index(column_name);
if let Some(idx) = index_opt {
self.rows.get(idx)
} else {
None
}
}
#[must_use]
pub fn get_by_index(&self, index: usize) -> Option<&RowValues> {
self.rows.get(index)
}
}