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 MemoryLimitExceeded {
54 limit_bytes: usize,
55 requested_bytes: usize,
56 },
57 Parse(String),
59 IndexError(String),
61 ViewError(String),
63 StorageError(String),
65 ReadonlyNeedsWrite,
67 Execution(String),
69}
70
71impl fmt::Display for QueryError {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 match self {
74 QueryError::TableNotFound(t) => write!(f, "table '{t}' not found"),
75 QueryError::ColumnNotFound { table, column } => {
76 if table.is_empty() {
77 write!(f, "column '{column}' not found")
78 } else {
79 write!(f, "column '{column}' not found in table '{table}'")
80 }
81 }
82 QueryError::TypeError(msg) => write!(f, "type mismatch: {msg}"),
83 QueryError::JoinLimitExceeded => write!(f, "join result exceeds row limit"),
84 QueryError::SortLimitExceeded => {
85 write!(f, "sort input exceeds row limit — add a LIMIT clause")
86 }
87 QueryError::MemoryLimitExceeded {
88 limit_bytes,
89 requested_bytes,
90 } => write!(
91 f,
92 "query exceeded memory budget: requested {requested_bytes} bytes, limit {limit_bytes} bytes"
93 ),
94 QueryError::Parse(msg) => write!(f, "{msg}"),
95 QueryError::IndexError(msg) => write!(f, "{msg}"),
96 QueryError::ViewError(msg) => write!(f, "{msg}"),
97 QueryError::StorageError(msg) => write!(f, "{msg}"),
98 QueryError::ReadonlyNeedsWrite => {
99 write!(f, "__POWDB_READONLY_NEEDS_WRITE__")
100 }
101 QueryError::Execution(msg) => write!(f, "{msg}"),
102 }
103 }
104}
105
106impl std::error::Error for QueryError {}
107
108impl From<String> for QueryError {
109 fn from(s: String) -> Self {
110 QueryError::Execution(s)
111 }
112}
113
114impl From<&str> for QueryError {
115 fn from(s: &str) -> Self {
116 QueryError::Execution(s.to_string())
117 }
118}