use surrealdb_types::{SqlFormat, ToSql};
use crate::expr::{Expr, Idiom, Model, Script};
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum Function {
Normal(String),
Custom(String),
Script(Script),
Model(Model),
Module(String, Option<String>),
Silo {
org: String,
pkg: String,
major: u32,
minor: u32,
patch: u32,
sub: Option<String>,
},
}
impl Function {
pub fn to_idiom(&self) -> Idiom {
match self {
Self::Script(_) => Idiom::field("function".to_owned()),
Self::Normal(f) => Idiom::field(f.to_owned()),
Self::Custom(f) => Idiom::field(format!("fn::{f}")),
Self::Model(m) => Idiom::field(m.to_sql()),
Self::Module(m, s) => match s {
Some(s) => Idiom::field(format!("mod::{m}::{s}")),
None => Idiom::field(format!("mod::{m}")),
},
Self::Silo {
org,
pkg,
major,
minor,
patch,
sub,
} => match sub {
Some(s) => {
Idiom::field(format!("silo::{org}::{pkg}::<{major}.{minor}.{patch}>::{s}"))
}
None => Idiom::field(format!("silo::{org}::{pkg}::<{major}.{minor}.{patch}>")),
},
}
}
pub fn read_only(&self) -> bool {
match self {
Self::Custom(_)
| Self::Script(_)
| Self::Module(_, _)
| Self::Silo {
..
} => false,
Self::Normal(f) => !crate::sql::method::is_writer_builtin(f),
Self::Model(_) => true,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct FunctionCall {
pub receiver: Function,
pub arguments: Vec<Expr>,
}
impl FunctionCall {
pub fn read_only(&self) -> bool {
self.receiver.read_only() && self.arguments.iter().all(|x| x.read_only())
}
}
impl ToSql for FunctionCall {
fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
let fnc: crate::sql::FunctionCall = self.clone().into();
fnc.fmt_sql(f, fmt);
}
}