use super::*;
#[derive(Debug, Clone, PartialEq)]
pub struct ConditionalClause {
pub branches: Vec<WhenBranch>,
pub else_body: Option<Statement>,
}
impl ConditionalClause {
pub fn representative_body(&self) -> Option<&Statement> {
self.branches
.first()
.map(|b| &b.body)
.or(self.else_body.as_ref())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct WhenBranch {
pub predicate: Expr,
pub body: Statement,
}
#[derive(Debug, Clone, PartialEq)]
pub struct MatchClause {
pub optional: bool,
pub match_mode: MatchMode,
pub pattern: Vec<PatternPath>,
pub using_scan: Vec<ScanHint>,
pub using_index: Vec<IndexHint>,
pub using_join: Vec<JoinHint>,
pub where_expr: Option<Expr>,
pub search: Option<SearchClause>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScanHint {
pub variable: SmallName,
pub label: SmallName,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexHint {
pub variable: SmallName,
pub label: SmallName,
pub properties: Vec<SmallName>,
}
impl IndexHint {
pub fn properties_text(&self) -> String {
self.properties
.iter()
.map(|property| property.to_string())
.collect::<Vec<_>>()
.join(", ")
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JoinHint {
pub variables: Vec<SmallName>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MatchMode {
#[default]
DifferentRelationships,
RepeatableElements,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PathMode {
Walk,
Trail,
Acyclic,
Simple,
}
#[derive(Debug, Clone, PartialEq)]
pub enum PathCount {
Literal(u32),
Parameter(String),
}
#[derive(Debug, Clone, PartialEq)]
pub enum PathSelector {
All,
Any(Option<PathCount>),
AnyShortest,
AllShortest,
Shortest {
count: Option<PathCount>,
group: bool,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct CreateClause {
pub pattern: Vec<PatternPath>,
pub is_insert: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct MergeClause {
pub pattern: PatternPath,
pub on_create: Vec<SetItem>,
pub on_match: Vec<SetItem>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DeleteClause {
pub detach: bool,
pub expressions: Vec<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SetClause {
pub items: Vec<SetItem>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SetItem {
pub property: Option<PropertyAccess>,
pub variable: Option<SmallName>,
pub labels: Vec<LabelExpr>,
pub value: Option<Expr>,
pub merge: bool,
pub is_keyword: bool,
pub labels_function: Option<(Expr, LabelsFunctionOp)>,
}
impl SetItem {
pub fn property_assignment(property: PropertyAccess, value: Expr) -> Self {
Self {
property: Some(property),
variable: None,
labels: Vec::new(),
value: Some(value),
merge: false,
is_keyword: false,
labels_function: None,
}
}
pub fn variable_assignment(variable: SmallName, value: Expr, merge: bool) -> Self {
Self {
property: None,
variable: Some(variable),
labels: Vec::new(),
value: Some(value),
merge,
is_keyword: false,
labels_function: None,
}
}
pub fn label_set(variable: SmallName, labels: Vec<LabelExpr>, is_keyword: bool) -> Self {
Self {
property: None,
variable: Some(variable),
labels,
value: None,
merge: false,
is_keyword,
labels_function: None,
}
}
pub fn labels_function(target: Expr, op: LabelsFunctionOp, value: Expr) -> Self {
Self {
property: None,
variable: None,
labels: Vec::new(),
value: Some(value),
merge: false,
is_keyword: false,
labels_function: Some((target, op)),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LabelsFunctionOp {
Overwrite,
Extend,
Remove,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RemoveClause {
pub items: Vec<RemoveItem>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RemoveItem {
pub property: Option<PropertyAccess>,
pub variable: Option<SmallName>,
pub labels: Vec<LabelExpr>,
pub is_keyword: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ReturnClause {
pub group_by: Option<GroupBySpec>,
pub distinct: bool,
pub items: Vec<ReturnItem>,
pub order_by: Vec<OrderByItem>,
pub skip: Option<Expr>,
pub limit: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ReturnItem {
pub expr: Expr,
pub alias: Option<SmallName>,
pub all: bool,
pub source_text: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct OrderByItem {
pub expr: Expr,
pub descending: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WithOrigin {
#[default]
Explicit,
Let,
Filter,
}
#[derive(Debug, Clone, PartialEq)]
pub enum GroupBySpec {
Keys(Vec<Expr>),
Empty,
All,
}
#[derive(Debug, Clone, PartialEq)]
pub struct WithClause {
pub group_by: Option<GroupBySpec>,
pub distinct: bool,
pub items: Vec<ReturnItem>,
pub order_by: Vec<OrderByItem>,
pub skip: Option<Expr>,
pub limit: Option<Expr>,
pub where_expr: Option<Expr>,
pub origin: WithOrigin,
}
#[derive(Debug, Clone, PartialEq)]
pub enum PageStage {
OrderBy(Vec<OrderByItem>),
Skip(Expr),
Limit(Expr),
}
#[derive(Debug, Clone, PartialEq)]
pub struct StandaloneOrderByClause {
pub stages: Vec<PageStage>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UnwindClause {
pub expr: Expr,
pub alias: SmallName,
pub for_syntax: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ForeachClause {
pub variable: SmallName,
pub list_expr: Expr,
pub clauses: Vec<Clause>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct LoadCSVClause {
pub with_headers: bool,
pub source: Expr,
pub alias: SmallName,
pub field_terminator: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CallSubqueryClause {
pub statement: Statement,
pub scope: CallScope,
pub optional: bool,
pub in_transactions: Option<InTransactionsClause>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct InTransactionsClause {
pub concurrency: Concurrency,
pub batch_size: Option<Expr>,
pub on_error: OnErrorMode,
pub report_status: Option<SmallName>,
pub disjoint: Option<DisjointBy>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DisjointBy {
Expressions(Vec<Expr>),
Auto,
None,
}
impl InTransactionsClause {
pub fn exprs(&self) -> Vec<&Expr> {
let mut out = Vec::new();
match &self.concurrency {
Concurrency::Value(expr) => out.push(expr),
Concurrency::Sequential | Concurrency::DefaultUnspecified => {}
}
out.extend(self.batch_size.as_ref());
match &self.on_error {
OnErrorMode::Retry { duration, .. } => out.extend(duration.as_ref()),
OnErrorMode::Fail | OnErrorMode::Continue | OnErrorMode::Break => {}
}
match &self.disjoint {
Some(DisjointBy::Expressions(keys)) => out.extend(keys),
Some(DisjointBy::Auto | DisjointBy::None) | None => {}
}
out
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Concurrency {
Sequential,
DefaultUnspecified,
Value(Expr),
}
#[derive(Debug, Clone, PartialEq)]
pub enum OnErrorMode {
Fail,
Continue,
Break,
Retry {
duration: Option<Expr>,
fallback: RetryFallback,
},
}
#[derive(Debug, Clone, PartialEq)]
pub enum RetryFallback {
Fail,
Continue,
Break,
}
#[derive(Debug, Clone, PartialEq)]
pub enum CallScope {
None,
Wildcard,
Vars(Vec<SmallName>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct UseClause {
pub graph_expr: Expr,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CallProcedureClause {
pub procedure: SmallName,
pub args: Vec<Expr>,
pub explicit_args: bool,
pub yields: YieldList,
pub optional: bool,
pub where_expr: Option<Expr>,
}