Skip to main content

aurora_db/parser/
ast.rs

1//! AST types for Aurora Query Language (AQL)
2
3use std::collections::HashMap;
4
5/// Root document containing all operations
6#[derive(Debug, Clone)]
7pub struct Document {
8    pub operations: Vec<Operation>,
9}
10
11/// Top-level operation types
12#[derive(Debug, Clone)]
13pub enum Operation {
14    Query(Query),
15    Mutation(Mutation),
16    Subscription(Subscription),
17    Schema(Schema),
18    Migration(Migration),
19    FragmentDefinition(FragmentDef),
20    Introspection(IntrospectionQuery),
21    Handler(HandlerDef),
22}
23
24/// Query operation
25#[derive(Debug, Clone)]
26pub struct Query {
27    pub name: Option<String>,
28    pub variable_definitions: Vec<VariableDefinition>,
29    pub directives: Vec<Directive>,
30    pub selection_set: Vec<Selection>,
31    pub variables_values: HashMap<String, Value>,
32}
33
34/// Mutation operation
35#[derive(Debug, Clone)]
36pub struct Mutation {
37    pub name: Option<String>,
38    pub variable_definitions: Vec<VariableDefinition>,
39    pub directives: Vec<Directive>,
40    pub operations: Vec<MutationOperation>,
41    pub variables_values: HashMap<String, Value>,
42}
43
44/// Subscription operation
45#[derive(Debug, Clone)]
46pub struct Subscription {
47    pub name: Option<String>,
48    pub variable_definitions: Vec<VariableDefinition>,
49    pub directives: Vec<Directive>,
50    pub selection_set: Vec<Selection>,
51    pub variables_values: HashMap<String, Value>,
52}
53
54#[derive(Debug, Clone)]
55pub struct Schema {
56    pub operations: Vec<SchemaOp>,
57}
58
59#[derive(Debug, Clone)]
60pub enum SchemaOp {
61    DefineCollection {
62        name: String,
63        if_not_exists: bool,
64        fields: Vec<FieldDef>,
65        directives: Vec<Directive>,
66    },
67    AlterCollection {
68        name: String,
69        actions: Vec<AlterAction>,
70    },
71    DropCollection {
72        name: String,
73        if_exists: bool,
74    },
75}
76
77#[derive(Debug, Clone)]
78pub enum AlterAction {
79    AddField { field: FieldDef, default: Option<Value> },
80    DropField(String),
81    RenameField { from: String, to: String },
82    ModifyField(FieldDef),
83}
84
85#[derive(Debug, Clone)]
86pub struct Migration {
87    pub steps: Vec<MigrationStep>,
88}
89
90#[derive(Debug, Clone)]
91pub struct MigrationStep {
92    pub version: String,
93    pub actions: Vec<MigrationAction>,
94}
95
96#[derive(Debug, Clone)]
97pub enum MigrationAction {
98    Schema(SchemaOp),
99    DataMigration(DataMigration),
100}
101
102#[derive(Debug, Clone)]
103pub struct DataMigration {
104    pub collection: String,
105    pub transforms: Vec<DataTransform>,
106}
107
108#[derive(Debug, Clone)]
109pub struct DataTransform {
110    pub field: String,
111    pub expression: String, // Rhai expression
112    pub filter: Option<Filter>,
113}
114
115/// Handler definition for event-driven automation
116#[derive(Debug, Clone)]
117pub struct HandlerDef {
118    pub name: String,
119    pub trigger: HandlerTrigger,
120    pub action: MutationOperation,
121}
122
123/// Event trigger for handlers
124#[derive(Debug, Clone, PartialEq)]
125pub enum HandlerTrigger {
126    /// Fires when a background job completes
127    JobCompleted,
128    /// Fires when a background job fails
129    JobFailed,
130    /// Fires when a document is inserted (optionally into specific collection)
131    Insert { collection: Option<String> },
132    /// Fires when a document is updated (optionally in specific collection)
133    Update { collection: Option<String> },
134    /// Fires when a document is deleted (optionally from specific collection)
135    Delete { collection: Option<String> },
136    /// Custom event name
137    Custom(String),
138}
139
140/// Field definition in schema
141#[derive(Debug, Clone)]
142pub struct FieldDef {
143    pub name: String,
144    pub field_type: TypeAnnotation,
145    pub directives: Vec<Directive>,
146}
147
148/// Variable definition
149#[derive(Debug, Clone)]
150pub struct VariableDefinition {
151    pub name: String,
152    pub var_type: TypeAnnotation,
153    pub default_value: Option<Value>,
154}
155
156/// Type annotation
157#[derive(Debug, Clone)]
158pub struct TypeAnnotation {
159    pub name: String,
160    pub is_array: bool,
161    pub is_required: bool,
162}
163
164/// Directive (e.g., @include, @skip)
165#[derive(Debug, Clone)]
166pub struct Directive {
167    pub name: String,
168    pub arguments: Vec<Argument>,
169}
170
171/// Field selection
172#[derive(Debug, Clone)]
173pub struct Field {
174    pub alias: Option<String>,
175    pub name: String,
176    pub arguments: Vec<Argument>,
177    pub directives: Vec<Directive>,
178    pub selection_set: Vec<Selection>,
179}
180
181/// Argument (key-value pair)
182#[derive(Debug, Clone)]
183pub struct Argument {
184    pub name: String,
185    pub value: Value,
186}
187
188/// Mutation operation wrapper
189#[derive(Debug, Clone)]
190pub struct MutationOperation {
191    pub alias: Option<String>,
192    pub operation: MutationOp,
193    pub directives: Vec<Directive>,
194    pub selection_set: Vec<Selection>,
195}
196
197/// Mutation operation types
198#[derive(Debug, Clone)]
199pub enum MutationOp {
200    Insert {
201        collection: String,
202        data: Value,
203    },
204    InsertMany {
205        collection: String,
206        data: Vec<Value>,
207    },
208    Update {
209        collection: String,
210        filter: Option<Filter>,
211        data: Value,
212    },
213    Upsert {
214        collection: String,
215        filter: Option<Filter>,
216        data: Value,
217    },
218    Delete {
219        collection: String,
220        filter: Option<Filter>,
221    },
222    EnqueueJob {
223        job_type: String,
224        payload: Value,
225        priority: JobPriority,
226        scheduled_at: Option<String>,
227        max_retries: Option<u32>,
228    },
229    EnqueueJobs {
230        job_type: String,
231        payloads: Vec<Value>,
232        priority: JobPriority,
233        max_retries: Option<u32>,
234    },
235    Import {
236        collection: String,
237        data: Vec<Value>,
238    },
239    Export {
240        collection: String,
241        format: String,
242    },
243    Transaction {
244        operations: Vec<MutationOperation>,
245    },
246}
247
248/// Job priority levels
249#[derive(Debug, Clone, Copy, PartialEq, Eq)]
250pub enum JobPriority {
251    Low,
252    Normal,
253    High,
254    Critical,
255}
256
257/// Filter expressions
258#[derive(Debug, Clone)]
259pub enum Filter {
260    Eq(String, Value),
261    Ne(String, Value),
262    Gt(String, Value),
263    Gte(String, Value),
264    Lt(String, Value),
265    Lte(String, Value),
266    In(String, Value),
267    NotIn(String, Value),
268    Contains(String, Value),
269    ContainsAny(String, Value),
270    ContainsAll(String, Value),
271    StartsWith(String, Value),
272    EndsWith(String, Value),
273    Matches(String, Value),
274    IsNull(String),
275    IsNotNull(String),
276    And(Vec<Filter>),
277    Or(Vec<Filter>),
278    Not(Box<Filter>),
279}
280
281/// Value types
282#[derive(Debug, Clone)]
283pub enum Value {
284    Null,
285    Boolean(bool),
286    Int(i64),
287    Float(f64),
288    String(String),
289    Array(Vec<Value>),
290    Object(HashMap<String, Value>),
291    Variable(String),
292    Enum(String),
293}
294
295/// Fragment definition (top-level)
296#[derive(Debug, Clone)]
297pub struct FragmentDef {
298    pub name: String,
299    pub type_condition: String,
300    pub selection_set: Vec<Selection>,
301}
302
303/// Introspection query (__schema)
304#[derive(Debug, Clone)]
305pub struct IntrospectionQuery {
306    pub arguments: Vec<Argument>,
307    pub fields: Vec<String>,
308}
309
310/// Selection within a selection set
311#[derive(Debug, Clone)]
312pub enum Selection {
313    Field(Field),
314    FragmentSpread(String),
315    InlineFragment(InlineFragment),
316}
317
318/// Inline fragment (... on Type { ... })
319#[derive(Debug, Clone)]
320pub struct InlineFragment {
321    pub type_condition: String,
322    pub selection_set: Vec<Selection>,
323}
324
325/// Computed field with expression
326#[derive(Debug, Clone)]
327pub struct ComputedField {
328    pub alias: String,
329    pub expression: ComputedExpression,
330}
331
332/// Computed expression types
333#[derive(Debug, Clone)]
334pub enum ComputedExpression {
335    TemplateString(String),
336    FunctionCall {
337        name: String,
338        args: Vec<Expression>,
339    },
340    PipeExpression {
341        base: Box<Expression>,
342        operations: Vec<PipeOp>,
343    },
344    SqlExpression(String),
345    AggregateFunction {
346        name: String,
347        field: String,
348    },
349}
350
351/// Pipe operation (for pipe expressions)
352#[derive(Debug, Clone)]
353pub struct PipeOp {
354    pub function: String,
355    pub args: Vec<Expression>,
356}
357
358/// Expression (for computed fields and filters)
359#[derive(Debug, Clone)]
360pub enum Expression {
361    Literal(Value),
362    FieldAccess(Vec<String>),
363    Variable(String),
364    FunctionCall {
365        name: String,
366        args: Vec<Expression>,
367    },
368    Binary {
369        op: BinaryOp,
370        left: Box<Expression>,
371        right: Box<Expression>,
372    },
373    Unary {
374        op: UnaryOp,
375        expr: Box<Expression>,
376    },
377    Ternary {
378        condition: Box<Expression>,
379        then_expr: Box<Expression>,
380        else_expr: Box<Expression>,
381    },
382}
383
384/// Binary operators
385#[derive(Debug, Clone, Copy, PartialEq, Eq)]
386pub enum BinaryOp {
387    Add,
388    Sub,
389    Mul,
390    Div,
391    Mod,
392    Eq,
393    Ne,
394    Gt,
395    Gte,
396    Lt,
397    Lte,
398    And,
399    Or,
400}
401
402/// Unary operators
403#[derive(Debug, Clone, Copy, PartialEq, Eq)]
404pub enum UnaryOp {
405    Not,
406    Neg,
407}
408
409/// Special selection types
410#[derive(Debug, Clone)]
411pub enum SpecialSelection {
412    Aggregate(AggregateSelection),
413    GroupBy(GroupBySelection),
414    Lookup(LookupSelection),
415    PageInfo(PageInfoSelection),
416    Edges(EdgesSelection),
417    Downsample(DownsampleSelection),
418    WindowFunction(WindowFunctionSelection),
419}
420
421/// Aggregate selection
422#[derive(Debug, Clone)]
423pub struct AggregateSelection {
424    pub fields: Vec<AggregateField>,
425}
426
427/// Aggregate field (count, sum, avg, etc.)
428#[derive(Debug, Clone)]
429pub struct AggregateField {
430    pub function: String,
431    pub field: Option<String>,
432}
433
434/// Group by selection
435#[derive(Debug, Clone)]
436pub struct GroupBySelection {
437    pub field: Option<String>,
438    pub fields: Option<Vec<String>>,
439    pub interval: Option<String>,
440    pub result_fields: Vec<String>,
441}
442
443/// Lookup selection (manual join)
444#[derive(Debug, Clone)]
445pub struct LookupSelection {
446    pub collection: String,
447    pub local_field: String,
448    pub foreign_field: String,
449    pub filter: Option<Filter>,
450    pub selection_set: Vec<Selection>,
451}
452
453/// Page info selection
454#[derive(Debug, Clone)]
455pub struct PageInfoSelection {
456    pub fields: Vec<String>,
457}
458
459/// Edges selection (cursor pagination)
460#[derive(Debug, Clone)]
461pub struct EdgesSelection {
462    pub fields: Vec<EdgeField>,
463}
464
465/// Edge field types
466#[derive(Debug, Clone)]
467pub enum EdgeField {
468    Cursor,
469    Node(Vec<Selection>),
470}
471
472/// Downsample selection (time-series)
473#[derive(Debug, Clone)]
474pub struct DownsampleSelection {
475    pub interval: String,
476    pub aggregation: String,
477    pub selection_set: Vec<Selection>,
478}
479
480/// Window function selection (time-series)
481#[derive(Debug, Clone)]
482pub struct WindowFunctionSelection {
483    pub alias: String,
484    pub field: String,
485    pub function: String,
486    pub window_size: i64,
487}
488
489// ============================================================================
490// SPECIAL ARGUMENT TYPES
491// ============================================================================
492
493/// Where clause
494#[derive(Debug, Clone)]
495pub struct WhereClause {
496    pub filter: Filter,
497}
498
499/// Order by clause
500#[derive(Debug, Clone)]
501pub struct OrderByClause {
502    pub orderings: Vec<Ordering>,
503}
504
505/// Single ordering
506#[derive(Debug, Clone)]
507pub struct Ordering {
508    pub field: String,
509    pub direction: SortDirection,
510}
511
512/// Sort direction
513#[derive(Debug, Clone, Copy, PartialEq, Eq)]
514pub enum SortDirection {
515    Asc,
516    Desc,
517}
518
519/// Search arguments
520#[derive(Debug, Clone)]
521pub struct SearchArgs {
522    pub query: String,
523    pub fields: Vec<String>,
524    pub fuzzy: bool,
525    pub min_score: Option<f64>,
526}
527
528/// Validation arguments
529#[derive(Debug, Clone)]
530pub struct ValidateArgs {
531    pub rules: Vec<ValidationRule>,
532}
533
534/// Validation rule for a field
535#[derive(Debug, Clone)]
536pub struct ValidationRule {
537    pub field: String,
538    pub constraints: Vec<ValidationConstraint>,
539}
540
541/// Validation constraint types
542#[derive(Debug, Clone)]
543pub enum ValidationConstraint {
544    Format(String),
545    Min(f64),
546    Max(f64),
547    MinLength(i64),
548    MaxLength(i64),
549    Pattern(String),
550}