use anyhow::Result;
use surrealdb_types::{SqlFormat, ToSql, write_sql};
use uuid::Uuid;
use crate::expr::visit::{Visit, Visitor};
use crate::expr::{Cond, CoverStmts, Expr, Fetchs, Fields, Idiom, Param};
const DOC_PARAMS: &[&str] = &["value", "before", "after", "event", "this"];
struct IsDocumentDependentChecker;
impl Visitor for IsDocumentDependentChecker {
type Error = ();
fn visit_idiom(&mut self, _idiom: &Idiom) -> Result<(), ()> {
Err(()) }
fn visit_param(&mut self, param: &Param) -> Result<(), ()> {
if DOC_PARAMS.contains(¶m.as_str()) {
Err(()) } else {
Ok(())
}
}
}
pub fn is_document_dependent(expr: &Expr) -> bool {
expr.visit(&mut IsDocumentDependentChecker).is_err()
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum LiveFields {
Diff,
Select(Fields),
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct LiveStatement {
pub id: Uuid,
pub node: Uuid,
pub fields: LiveFields,
pub what: Expr,
pub cond: Option<Cond>,
pub fetch: Option<Fetchs>,
}
impl ToSql for LiveStatement {
fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
f.push_str("LIVE SELECT");
match &self.fields {
LiveFields::Diff => write_sql!(f, fmt, " DIFF"),
LiveFields::Select(x) => write_sql!(f, fmt, " {}", x),
}
write_sql!(f, fmt, " FROM {}", CoverStmts(&self.what));
if let Some(ref v) = self.cond {
write_sql!(f, fmt, " {v}");
}
if let Some(ref v) = self.fetch {
write_sql!(f, fmt, " {v}");
}
}
}