#![allow(dead_code)]
use std::collections::BTreeMap;
use crate::value::Value;
#[derive(Clone, Debug, PartialEq)]
pub enum Statement {
Single(Query),
Create(CreateStatement),
CreateRelationship(CreateRelationshipStatement),
Set(SetStatement),
Foreach(ForeachStatement),
Delete(DeleteStatement),
Remove(RemoveStatement),
Merge(MergeStatement),
Union { first: Query, parts: Vec<UnionPart> },
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateStatement {
pub node: NodePattern,
pub returns: ReturnProjection,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateRelationshipStatement {
pub matches: Vec<MatchClause>,
pub pattern: Pattern,
pub returns: ReturnProjection,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SetStatement {
pub matches: Vec<MatchClause>,
pub items: Vec<SetItem>,
pub returns: ReturnProjection,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ForeachStatement {
pub matches: Vec<MatchClause>,
pub variable: String,
pub expression: Expression,
pub items: Vec<SetItem>,
pub returns: ReturnProjection,
}
#[derive(Clone, Debug, PartialEq)]
pub enum SetItem {
Property {
variable: String,
key: String,
expression: Expression,
},
PropertiesMerge {
variable: String,
expression: Expression,
},
PropertiesReplace {
variable: String,
expression: Expression,
},
Label {
variable: String,
labels: Vec<String>,
},
}
#[derive(Clone, Debug, PartialEq)]
pub struct DeleteStatement {
pub matches: Vec<MatchClause>,
pub variables: Vec<String>,
pub detach: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RemoveStatement {
pub matches: Vec<MatchClause>,
pub items: Vec<RemoveItem>,
pub returns: ReturnProjection,
}
#[derive(Clone, Debug, PartialEq)]
pub enum RemoveItem {
Property {
variable: String,
key: String,
},
Label {
variable: String,
labels: Vec<String>,
},
}
#[derive(Clone, Debug, PartialEq)]
pub enum MergeStatement {
Node {
node: NodePattern,
actions: Vec<MergeAction>,
returns: ReturnProjection,
},
Relationship {
matches: Vec<MatchClause>,
pattern: Pattern,
actions: Vec<MergeAction>,
returns: ReturnProjection,
},
}
#[derive(Clone, Debug, PartialEq)]
pub struct MergeAction {
pub branch: MergeBranch,
pub items: Vec<SetItem>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MergeBranch {
OnCreate,
OnMatch,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UnionPart {
pub all: bool,
pub query: Query,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Query {
pub has_initial_match: bool,
pub optional_match: bool,
pub pattern: Pattern,
pub patterns: Vec<Pattern>,
pub predicate: Option<Predicate>,
pub extra_matches: Vec<MatchClause>,
pub unwinds: Vec<UnwindClause>,
pub post_unwind_matches: Vec<MatchClause>,
pub procedure_calls: Vec<ProcedureCall>,
pub post_procedure_unwinds: Vec<UnwindClause>,
pub post_procedure_matches: Vec<MatchClause>,
pub with: Option<WithProjection>,
pub with_predicate: Option<Predicate>,
pub with_order_by: Vec<OrderItem>,
pub with_skip: Option<RowCount>,
pub with_limit: Option<RowCount>,
pub post_with_unwinds: Vec<UnwindClause>,
pub post_with_matches: Vec<MatchClause>,
pub post_with_procedure_calls: Vec<ProcedureCall>,
pub post_with_procedure_unwinds: Vec<UnwindClause>,
pub post_with_procedure_matches: Vec<MatchClause>,
pub post_with_with: Option<WithProjection>,
pub post_with_with_predicate: Option<Predicate>,
pub post_with_with_order_by: Vec<OrderItem>,
pub post_with_with_skip: Option<RowCount>,
pub post_with_with_limit: Option<RowCount>,
pub post_with_with_unwinds: Vec<UnwindClause>,
pub post_with_with_matches: Vec<MatchClause>,
pub post_with_with_procedure_calls: Vec<ProcedureCall>,
pub post_with_with_procedure_unwinds: Vec<UnwindClause>,
pub post_with_with_procedure_matches: Vec<MatchClause>,
pub post_with_with_calls: Vec<CallSubquery>,
pub calls: Vec<CallSubquery>,
pub post_call_unwinds: Vec<UnwindClause>,
pub post_call_matches: Vec<MatchClause>,
pub post_call_with: Option<WithProjection>,
pub post_call_with_predicate: Option<Predicate>,
pub post_call_with_order_by: Vec<OrderItem>,
pub post_call_with_skip: Option<RowCount>,
pub post_call_with_limit: Option<RowCount>,
pub post_call_with_unwinds: Vec<UnwindClause>,
pub post_call_with_matches: Vec<MatchClause>,
pub distinct: bool,
pub returns: ReturnProjection,
pub implicit_return: bool,
pub order_by: Vec<OrderItem>,
pub skip: Option<RowCount>,
pub limit: Option<RowCount>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ProcedureCall {
pub name: String,
pub arguments: Vec<Expression>,
pub yields: Vec<ProcedureYield>,
pub predicate: Option<Predicate>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProcedureYield {
pub field: String,
pub alias: Option<String>,
}
impl ProcedureYield {
pub fn binding(&self) -> &str {
self.alias.as_deref().unwrap_or(&self.field)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct MatchClause {
pub optional_match: bool,
pub pattern: Pattern,
pub patterns: Vec<Pattern>,
pub predicate: Option<Predicate>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct WithProjection {
pub distinct: bool,
pub projection: ReturnProjection,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UnwindClause {
pub expression: Expression,
pub variable: String,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CallSubquery {
pub imports: Vec<String>,
pub predicate: Option<Predicate>,
pub unwinds: Vec<UnwindClause>,
pub matches: Vec<MatchClause>,
pub post_match_unwinds: Vec<UnwindClause>,
pub with: Option<WithProjection>,
pub with_predicate: Option<Predicate>,
pub with_order_by: Vec<OrderItem>,
pub with_skip: Option<RowCount>,
pub with_limit: Option<RowCount>,
pub post_with_unwinds: Vec<UnwindClause>,
pub post_with_matches: Vec<MatchClause>,
pub post_with_with: Option<WithProjection>,
pub post_with_with_predicate: Option<Predicate>,
pub post_with_with_order_by: Vec<OrderItem>,
pub post_with_with_skip: Option<RowCount>,
pub post_with_with_limit: Option<RowCount>,
pub post_with_with_unwinds: Vec<UnwindClause>,
pub post_with_with_matches: Vec<MatchClause>,
pub returns: ReturnProjection,
pub order_by: Vec<OrderItem>,
pub skip: Option<RowCount>,
pub limit: Option<RowCount>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Pattern {
pub variable: Option<String>,
pub shortest_path: Option<ShortestPathMode>,
pub start: NodePattern,
pub chains: Vec<PatternChain>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShortestPathMode {
Single,
All,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PatternChain {
pub left_variable: Option<String>,
pub relationship: RelationshipPattern,
pub node: NodePattern,
}
#[derive(Clone, Debug, PartialEq)]
pub struct NodePattern {
pub variable: Option<String>,
pub labels: Vec<String>,
pub properties: Vec<PropertyPredicate>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RelationshipPattern {
pub variable: Option<String>,
pub types: Vec<String>,
pub direction: Direction,
pub variable_length: bool,
pub min_hops: usize,
pub max_hops: Option<usize>,
pub properties: Vec<PropertyPredicate>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Direction {
Outgoing,
Incoming,
Undirected,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PropertyPredicate {
pub key: String,
pub expression: Expression,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Expression {
Literal(Value),
Parameter(String),
Operand(Operand),
Add(Box<Expression>, Box<Expression>),
Subtract(Box<Expression>, Box<Expression>),
Multiply(Box<Expression>, Box<Expression>),
Divide(Box<Expression>, Box<Expression>),
Modulo(Box<Expression>, Box<Expression>),
List(Vec<Expression>),
Map(BTreeMap<String, Expression>),
MapProjection {
variable: String,
items: Vec<MapProjectionItem>,
},
Coalesce(Vec<Expression>),
ToLower(Box<Expression>),
ToUpper(Box<Expression>),
ToString(Box<Expression>),
ToInteger(Box<Expression>),
ToFloat(Box<Expression>),
ToBoolean(Box<Expression>),
ToStringOrNull(Box<Expression>),
ToIntegerOrNull(Box<Expression>),
ToFloatOrNull(Box<Expression>),
ToBooleanOrNull(Box<Expression>),
IsEmpty(Box<Expression>),
Head(Box<Expression>),
Last(Box<Expression>),
Tail(Box<Expression>),
Range {
start: Box<Expression>,
end: Box<Expression>,
step: Option<Box<Expression>>,
},
ListIndex {
value: Box<Expression>,
index: Box<Expression>,
},
ListSlice {
value: Box<Expression>,
start: Option<Box<Expression>>,
end: Option<Box<Expression>>,
},
Size(Box<Expression>),
Substring {
value: Box<Expression>,
start: Box<Expression>,
length: Option<Box<Expression>>,
},
Replace {
value: Box<Expression>,
search: Box<Expression>,
replacement: Box<Expression>,
},
Trim {
mode: TrimMode,
expression: Box<Expression>,
},
Split {
value: Box<Expression>,
delimiter: Box<Expression>,
},
StringSlice {
mode: StringSliceMode,
value: Box<Expression>,
length: Box<Expression>,
},
Reverse(Box<Expression>),
Abs(Box<Expression>),
Rounding {
mode: RoundingMode,
expression: Box<Expression>,
},
Sign(Box<Expression>),
Sqrt(Box<Expression>),
Round(Box<Expression>),
Log(Box<Expression>),
Log10(Box<Expression>),
Exp(Box<Expression>),
Pi,
E,
AngleConversion {
mode: AngleConversionMode,
expression: Box<Expression>,
},
Trigonometric {
mode: TrigonometricMode,
expression: Box<Expression>,
},
InverseTrigonometric {
mode: InverseTrigonometricMode,
expression: Box<Expression>,
},
Atan2 {
y: Box<Expression>,
x: Box<Expression>,
},
Haversin(Box<Expression>),
ListPredicate {
mode: ListPredicateMode,
variable: String,
list: Box<Expression>,
predicate: Box<Predicate>,
},
ListComprehension {
variable: String,
list: Box<Expression>,
predicate: Option<Box<Predicate>>,
projection: Box<Expression>,
},
PatternComprehension {
pattern: Pattern,
predicate: Option<Box<Predicate>>,
projection: Box<Expression>,
},
Reduce {
accumulator: String,
initial: Box<Expression>,
variable: String,
list: Box<Expression>,
expression: Box<Expression>,
},
Case {
alternatives: Vec<CaseAlternative>,
else_expression: Box<Expression>,
},
SimpleCase {
operand: Box<Expression>,
alternatives: Vec<SimpleCaseAlternative>,
else_expression: Box<Expression>,
},
Predicate(Box<Predicate>),
}
#[derive(Clone, Debug, PartialEq)]
pub enum MapProjectionItem {
AllProperties,
Property(String),
Variable(String),
Expression { key: String, expression: Expression },
}
#[derive(Clone, Debug, PartialEq)]
pub struct CaseAlternative {
pub predicate: Predicate,
pub expression: Expression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SimpleCaseAlternative {
pub value: Expression,
pub expression: Expression,
}
#[derive(Clone, Debug, PartialEq)]
pub enum TrimMode {
Both,
Left,
Right,
}
#[derive(Clone, Debug, PartialEq)]
pub enum StringSliceMode {
Left,
Right,
}
#[derive(Clone, Debug, PartialEq)]
pub enum RoundingMode {
Ceil,
Floor,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ListPredicateMode {
All,
Any,
None,
Single,
}
#[derive(Clone, Debug, PartialEq)]
pub enum AngleConversionMode {
Degrees,
Radians,
}
#[derive(Clone, Debug, PartialEq)]
pub enum TrigonometricMode {
Sin,
Cos,
Tan,
Cot,
}
#[derive(Clone, Debug, PartialEq)]
pub enum InverseTrigonometricMode {
Acos,
Asin,
Atan,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Predicate {
Comparison {
left: Expression,
operator: ComparisonOperator,
right: Expression,
},
In {
item: Expression,
list: Expression,
},
StringMatch {
left: Expression,
operator: StringOperator,
right: Expression,
},
And(Box<Predicate>, Box<Predicate>),
Or(Box<Predicate>, Box<Predicate>),
Xor(Box<Predicate>, Box<Predicate>),
Not(Box<Predicate>),
Pattern(Pattern),
IsNull(Expression),
IsNotNull(Expression),
Exists(Operand),
ExistsSubquery(Vec<MatchClause>),
Boolean(Expression),
}
#[derive(Clone, Debug, PartialEq)]
pub enum Operand {
Literal(Value),
Parameter(String),
Property {
variable: String,
key: String,
},
DynamicProperty {
variable: String,
key: Box<Expression>,
},
Variable(String),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ComparisonOperator {
Equal,
NotEqual,
LessThan,
LessThanOrEqual,
GreaterThan,
GreaterThanOrEqual,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StringOperator {
StartsWith,
EndsWith,
Contains,
Regex,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ReturnItem {
pub expression: ReturnExpression,
pub alias: Option<String>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ReturnExpression {
Star,
Variable(String),
Property { variable: String, key: String },
PropertyWildcard(String),
Scalar(Expression),
Labels(String),
Type(String),
Properties(String),
Keys(String),
ElementId(String),
Id(String),
StartNode(String),
EndNode(String),
Nodes(String),
Relationships(String),
Length(String),
CountStar,
CountDistinctStar,
CountVariable(String),
CountDistinctVariable(String),
CountProperty { variable: String, key: String },
CountDistinctProperty { variable: String, key: String },
CountExpression(Expression),
CountDistinctExpression(Expression),
SumProperty { variable: String, key: String },
AvgProperty { variable: String, key: String },
MinProperty { variable: String, key: String },
MaxProperty { variable: String, key: String },
SumExpression(Expression),
AvgExpression(Expression),
SumDistinctExpression(Expression),
AvgDistinctExpression(Expression),
MinExpression(Expression),
MaxExpression(Expression),
MinDistinctExpression(Expression),
MaxDistinctExpression(Expression),
CollectVariable(String),
CollectProperty { variable: String, key: String },
CollectDistinctVariable(String),
CollectDistinctProperty { variable: String, key: String },
CollectExpression(Expression),
CollectDistinctExpression(Expression),
}
#[derive(Clone, Debug, PartialEq)]
pub enum ReturnProjection {
Items(Vec<ReturnItem>),
Star,
}
#[derive(Clone, Debug, PartialEq)]
pub struct OrderItem {
pub item: ReturnItem,
pub direction: OrderDirection,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OrderDirection {
Ascending,
Descending,
}
#[derive(Clone, Debug, PartialEq)]
pub enum RowCount {
Literal(i64),
Parameter(String),
Expression(Expression),
InvalidLiteral(String),
}