use std::fmt;
use powdb_storage::types::Value;
#[derive(Debug)]
pub enum QueryResult {
Rows {
columns: Vec<String>,
rows: Vec<Vec<Value>>,
},
Scalar(Value), Modified(u64), Created(String), Executed {
message: String,
}, }
impl QueryResult {
pub fn row_count(&self) -> usize {
match self {
QueryResult::Rows { rows, .. } => rows.len(),
QueryResult::Scalar(_) => 1,
QueryResult::Modified(n) => *n as usize,
QueryResult::Created(_) => 0,
QueryResult::Executed { .. } => 0,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum QueryError {
TableNotFound(String),
ColumnNotFound { table: String, column: String },
TypeError(String),
JoinLimitExceeded,
SortLimitExceeded,
Parse(String),
IndexError(String),
ViewError(String),
StorageError(String),
ReadonlyNeedsWrite,
Execution(String),
}
impl fmt::Display for QueryError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
QueryError::TableNotFound(t) => write!(f, "table '{t}' not found"),
QueryError::ColumnNotFound { table, column } => {
if table.is_empty() {
write!(f, "column '{column}' not found")
} else {
write!(f, "column '{column}' not found in table '{table}'")
}
}
QueryError::TypeError(msg) => write!(f, "type mismatch: {msg}"),
QueryError::JoinLimitExceeded => write!(f, "join result exceeds row limit"),
QueryError::SortLimitExceeded => {
write!(f, "sort input exceeds row limit — add a LIMIT clause")
}
QueryError::Parse(msg) => write!(f, "{msg}"),
QueryError::IndexError(msg) => write!(f, "{msg}"),
QueryError::ViewError(msg) => write!(f, "{msg}"),
QueryError::StorageError(msg) => write!(f, "{msg}"),
QueryError::ReadonlyNeedsWrite => {
write!(f, "__POWDB_READONLY_NEEDS_WRITE__")
}
QueryError::Execution(msg) => write!(f, "{msg}"),
}
}
}
impl std::error::Error for QueryError {}
impl From<String> for QueryError {
fn from(s: String) -> Self {
QueryError::Execution(s)
}
}
impl From<&str> for QueryError {
fn from(s: &str) -> Self {
QueryError::Execution(s.to_string())
}
}