ic_dbms_api/select.rs
1//! Untyped select results and borrowed row views for the canister API.
2
3use candid::CandidType;
4use serde::{Deserialize, Serialize};
5use wasm_dbms_api::prelude::{JoinColumnDef, JoinResultSet, JoinRow, Value};
6
7/// Result of an untyped select, retaining the query's native row representation.
8///
9/// Queries without joins return [`Self::Raw`]; queries with joins return
10/// [`Self::Joined`]. Joined results store column metadata once, including
11/// when no rows match. Use [`Self::iter`] to borrow either representation.
12///
13/// # Examples
14///
15/// ```
16/// use ic_dbms_api::prelude::SelectOutput;
17///
18/// let result = SelectOutput::Raw(vec![]);
19/// assert!(result.is_empty());
20/// for row in result.iter() {
21/// for (column, value) in row.iter() {
22/// println!("{name}: {value:?}", name = column.name);
23/// }
24/// }
25/// ```
26#[derive(Debug, Clone, PartialEq, Eq, CandidType, Serialize, Deserialize)]
27pub enum SelectOutput {
28 /// Single-table rows containing serializable column-value pairs.
29 Raw(Vec<Vec<(JoinColumnDef, Value)>>),
30 /// Joined rows sharing column metadata with source table names.
31 Joined(JoinResultSet),
32}
33
34impl SelectOutput {
35 /// Borrows raw rows, or returns `None` for joined results.
36 pub fn as_raw(&self) -> Option<&[Vec<(JoinColumnDef, Value)>]> {
37 match self {
38 Self::Raw(rows) => Some(rows),
39 Self::Joined(_) => None,
40 }
41 }
42
43 /// Borrows the joined result set, or returns `None` for raw results.
44 pub fn as_joined(&self) -> Option<&JoinResultSet> {
45 match self {
46 Self::Raw(_) => None,
47 Self::Joined(rows) => Some(rows),
48 }
49 }
50
51 /// Returns the number of rows.
52 pub fn len(&self) -> usize {
53 match self {
54 Self::Raw(rows) => rows.len(),
55 Self::Joined(rows) => rows.len(),
56 }
57 }
58
59 /// Returns `true` when no rows matched the query.
60 pub fn is_empty(&self) -> bool {
61 self.len() == 0
62 }
63
64 /// Iterates over borrowed row views without cloning columns or values.
65 pub fn iter(&self) -> impl Iterator<Item = SelectRow<'_>> {
66 self.as_raw()
67 .into_iter()
68 .flatten()
69 .map(|row| SelectRow::Raw(row))
70 .chain(
71 self.as_joined()
72 .into_iter()
73 .flat_map(JoinResultSet::iter)
74 .map(SelectRow::Joined),
75 )
76 }
77}
78
79/// Borrowed row from either representation of a [`SelectOutput`].
80#[derive(Debug, Clone, Copy)]
81pub enum SelectRow<'a> {
82 /// Column-value pairs from a single-table query.
83 Raw(&'a [(JoinColumnDef, Value)]),
84 /// Values paired with the joined result set's shared columns.
85 Joined(JoinRow<'a>),
86}
87
88impl<'a> SelectRow<'a> {
89 /// Iterates over borrowed column-value pairs in output order.
90 pub fn iter(&self) -> impl Iterator<Item = (&'a JoinColumnDef, &'a Value)> + use<'a> {
91 let (raw, joined) = match self {
92 Self::Raw(row) => (Some(*row), None),
93 Self::Joined(row) => (None, Some(*row)),
94 };
95 raw.into_iter()
96 .flatten()
97 .map(|(column, value)| (column, value))
98 .chain(joined.into_iter().flat_map(|row| row.iter()))
99 }
100}