use candid::CandidType;
use serde::{Deserialize, Serialize};
use wasm_dbms_api::prelude::{JoinColumnDef, JoinResultSet, JoinRow, Value};
#[derive(Debug, Clone, PartialEq, Eq, CandidType, Serialize, Deserialize)]
pub enum SelectOutput {
Raw(Vec<Vec<(JoinColumnDef, Value)>>),
Joined(JoinResultSet),
}
impl SelectOutput {
pub fn as_raw(&self) -> Option<&[Vec<(JoinColumnDef, Value)>]> {
match self {
Self::Raw(rows) => Some(rows),
Self::Joined(_) => None,
}
}
pub fn as_joined(&self) -> Option<&JoinResultSet> {
match self {
Self::Raw(_) => None,
Self::Joined(rows) => Some(rows),
}
}
pub fn len(&self) -> usize {
match self {
Self::Raw(rows) => rows.len(),
Self::Joined(rows) => rows.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn iter(&self) -> impl Iterator<Item = SelectRow<'_>> {
self.as_raw()
.into_iter()
.flatten()
.map(|row| SelectRow::Raw(row))
.chain(
self.as_joined()
.into_iter()
.flat_map(JoinResultSet::iter)
.map(SelectRow::Joined),
)
}
}
#[derive(Debug, Clone, Copy)]
pub enum SelectRow<'a> {
Raw(&'a [(JoinColumnDef, Value)]),
Joined(JoinRow<'a>),
}
impl<'a> SelectRow<'a> {
pub fn iter(&self) -> impl Iterator<Item = (&'a JoinColumnDef, &'a Value)> + use<'a> {
let (raw, joined) = match self {
Self::Raw(row) => (Some(*row), None),
Self::Joined(row) => (None, Some(*row)),
};
raw.into_iter()
.flatten()
.map(|(column, value)| (column, value))
.chain(joined.into_iter().flat_map(|row| row.iter()))
}
}