use crate::soch_ql::SochValue;
pub type Row = Vec<SochValue>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColumnMeta {
pub name: String,
pub table: Option<String>,
}
impl ColumnMeta {
pub fn new(name: impl Into<String>) -> Self {
Self { name: name.into(), table: None }
}
pub fn qualified(table: impl Into<String>, name: impl Into<String>) -> Self {
Self { name: name.into(), table: Some(table.into()) }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Schema {
pub columns: Vec<ColumnMeta>,
}
impl Schema {
pub fn new(columns: Vec<ColumnMeta>) -> Self {
Self { columns }
}
pub fn empty() -> Self {
Self { columns: vec![] }
}
pub fn len(&self) -> usize {
self.columns.len()
}
pub fn is_empty(&self) -> bool {
self.columns.is_empty()
}
pub fn index_of(&self, name: &str) -> Option<usize> {
self.columns.iter().position(|c| c.name == name)
}
pub fn index_of_qualified(&self, table: Option<&str>, name: &str) -> Option<usize> {
match table {
Some(t) => self.columns.iter().position(|c| {
c.name == name && c.table.as_deref() == Some(t)
}),
None => self.index_of(name),
}
}
pub fn column_names(&self) -> Vec<String> {
self.columns.iter().map(|c| c.name.clone()).collect()
}
pub fn merge(&self, other: &Schema) -> Schema {
let mut cols = self.columns.clone();
cols.extend(other.columns.iter().cloned());
Schema::new(cols)
}
}