1use std::fmt;
2
3use powdb_storage::types::Value;
4
5#[derive(Debug)]
7pub enum QueryResult {
8 Rows {
9 columns: Vec<String>,
10 rows: Vec<Vec<Value>>,
11 },
12 Scalar(Value), Modified(u64), Created(String), Executed {
16 message: String,
17 }, }
19
20impl QueryResult {
21 pub fn row_count(&self) -> usize {
22 match self {
23 QueryResult::Rows { rows, .. } => rows.len(),
24 QueryResult::Scalar(_) => 1,
25 QueryResult::Modified(n) => *n as usize,
26 QueryResult::Created(_) => 0,
27 QueryResult::Executed { .. } => 0,
28 }
29 }
30}
31
32#[derive(Debug, Clone, PartialEq)]
39pub enum QueryError {
40 TableNotFound(String),
42 ColumnNotFound { table: String, column: String },
44 TypeError(String),
46 JoinLimitExceeded,
48 SortLimitExceeded,
50 Parse(String),
52 IndexError(String),
54 ViewError(String),
56 StorageError(String),
58 ReadonlyNeedsWrite,
60 Execution(String),
62}
63
64impl fmt::Display for QueryError {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 match self {
67 QueryError::TableNotFound(t) => write!(f, "table '{t}' not found"),
68 QueryError::ColumnNotFound { table, column } => {
69 if table.is_empty() {
70 write!(f, "column '{column}' not found")
71 } else {
72 write!(f, "column '{column}' not found in table '{table}'")
73 }
74 }
75 QueryError::TypeError(msg) => write!(f, "type mismatch: {msg}"),
76 QueryError::JoinLimitExceeded => write!(f, "join result exceeds row limit"),
77 QueryError::SortLimitExceeded => {
78 write!(f, "sort input exceeds row limit — add a LIMIT clause")
79 }
80 QueryError::Parse(msg) => write!(f, "{msg}"),
81 QueryError::IndexError(msg) => write!(f, "{msg}"),
82 QueryError::ViewError(msg) => write!(f, "{msg}"),
83 QueryError::StorageError(msg) => write!(f, "{msg}"),
84 QueryError::ReadonlyNeedsWrite => {
85 write!(f, "__POWDB_READONLY_NEEDS_WRITE__")
86 }
87 QueryError::Execution(msg) => write!(f, "{msg}"),
88 }
89 }
90}
91
92impl std::error::Error for QueryError {}
93
94impl From<String> for QueryError {
95 fn from(s: String) -> Self {
96 QueryError::Execution(s)
97 }
98}
99
100impl From<&str> for QueryError {
101 fn from(s: &str) -> Self {
102 QueryError::Execution(s.to_string())
103 }
104}