use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt;
use crate::executor;
use crate::parser;
pub use parser::ColumnType;
#[derive(Debug, Clone)]
pub struct Schema {
pub columns: Vec<String>,
pub types: Vec<ColumnType>,
}
impl Schema {
pub fn index_of(&self, name: &str) -> Option<usize> {
self.columns.iter().position(|c| c == name)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DbError {
UnknownColumn(String),
TypeMismatch,
ColumnTypeMismatch(String),
ArityMismatch,
NotAVectorColumn(String),
MissingParam,
RowNotFound(u64),
CorruptRow(u64),
UnknownTable(String),
TransactionError(String),
TableAlreadyExists(String),
ViewAlreadyExists(String),
UnknownView(String),
RecursiveView(String),
Unsupported(String),
ColumnCountMismatch,
SubqueryCardinality(String),
Exec(executor::ExecError),
}
impl From<executor::ExecError> for DbError {
fn from(e: executor::ExecError) -> Self {
match e {
executor::ExecError::UnknownColumn(c) => DbError::UnknownColumn(c),
executor::ExecError::TypeMismatch => DbError::TypeMismatch,
executor::ExecError::GroupByColumnNotFound(c) => DbError::UnknownColumn(c),
executor::ExecError::UnresolvedSubquery => DbError::Unsupported(
"internal: subquery reached the pure evaluator unresolved".to_string(),
),
}
}
}
impl fmt::Display for DbError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DbError::UnknownColumn(c) => write!(f, "unknown column: {}", c),
DbError::TypeMismatch => write!(f, "type mismatch"),
DbError::ColumnTypeMismatch(c) => write!(f, "column type mismatch: {}", c),
DbError::ArityMismatch => write!(f, "arity mismatch"),
DbError::NotAVectorColumn(c) => write!(f, "not a vector column: {}", c),
DbError::MissingParam => write!(f, "missing parameter"),
DbError::RowNotFound(id) => write!(f, "row not found: {}", id),
DbError::CorruptRow(id) => write!(f, "corrupt row: {}", id),
DbError::UnknownTable(t) => write!(f, "unknown table: {}", t),
DbError::TransactionError(msg) => write!(f, "transaction error: {}", msg),
DbError::TableAlreadyExists(t) => write!(f, "table already exists: {}", t),
DbError::ViewAlreadyExists(v) => write!(f, "view already exists: {}", v),
DbError::UnknownView(v) => write!(f, "unknown view: {}", v),
DbError::RecursiveView(v) => write!(f, "recursive view: {}", v),
DbError::Unsupported(msg) => write!(f, "unsupported: {}", msg),
DbError::ColumnCountMismatch => write!(f, "column count mismatch"),
DbError::SubqueryCardinality(msg) => write!(f, "subquery cardinality: {}", msg),
DbError::Exec(e) => write!(f, "execution error: {:?}", e),
}
}
}
impl core::error::Error for DbError {}