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(FieldDef),
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    Transaction {
230        operations: Vec<MutationOperation>,
231    },
232}
233
234/// Job priority levels
235#[derive(Debug, Clone, Copy, PartialEq, Eq)]
236pub enum JobPriority {
237    Low,
238    Normal,
239    High,
240    Critical,
241}
242
243/// Filter expressions
244#[derive(Debug, Clone)]
245pub enum Filter {
246    Eq(String, Value),
247    Ne(String, Value),
248    Gt(String, Value),
249    Gte(String, Value),
250    Lt(String, Value),
251    Lte(String, Value),
252    In(String, Value),
253    NotIn(String, Value),
254    Contains(String, Value),
255    StartsWith(String, Value),
256    EndsWith(String, Value),
257    Matches(String, Value),
258    IsNull(String),
259    IsNotNull(String),
260    And(Vec<Filter>),
261    Or(Vec<Filter>),
262    Not(Box<Filter>),
263}
264
265/// Value types
266#[derive(Debug, Clone)]
267pub enum Value {
268    Null,
269    Boolean(bool),
270    Int(i64),
271    Float(f64),
272    String(String),
273    Array(Vec<Value>),
274    Object(HashMap<String, Value>),
275    Variable(String),
276    Enum(String),
277}
278
279/// Fragment definition (top-level)
280#[derive(Debug, Clone)]
281pub struct FragmentDef {
282    pub name: String,
283    pub type_condition: String,
284    pub selection_set: Vec<Selection>,
285}
286
287/// Introspection query (__schema)
288#[derive(Debug, Clone)]
289pub struct IntrospectionQuery {
290    pub arguments: Vec<Argument>,
291    pub fields: Vec<String>,
292}
293
294/// Selection within a selection set
295#[derive(Debug, Clone)]
296
297pub enum Selection {
298    Field(Field),
299    FragmentSpread(String),
300    InlineFragment(InlineFragment),
301}
302
303/// Inline fragment (... on Type { ... })
304#[derive(Debug, Clone)]
305pub struct InlineFragment {
306    pub type_condition: String,
307    pub selection_set: Vec<Selection>,
308}
309
310/// Computed field with expression
311#[derive(Debug, Clone)]
312pub struct ComputedField {
313    pub alias: String,
314    pub expression: ComputedExpression,
315}
316
317/// Computed expression types
318#[derive(Debug, Clone)]
319pub enum ComputedExpression {
320    TemplateString(String),
321    FunctionCall {
322        name: String,
323        args: Vec<Expression>,
324    },
325    PipeExpression {
326        base: Box<Expression>,
327        operations: Vec<PipeOp>,
328    },
329    SqlExpression(String),
330    AggregateFunction {
331        name: String,
332        field: String,
333    },
334}
335
336/// Pipe operation (for pipe expressions)
337#[derive(Debug, Clone)]
338pub struct PipeOp {
339    pub function: String,
340    pub args: Vec<Expression>,
341}
342
343/// Expression (for computed fields and filters)
344#[derive(Debug, Clone)]
345pub enum Expression {
346    Literal(Value),
347    FieldAccess(Vec<String>),
348    Variable(String),
349    FunctionCall {
350        name: String,
351        args: Vec<Expression>,
352    },
353    Binary {
354        op: BinaryOp,
355        left: Box<Expression>,
356        right: Box<Expression>,
357    },
358    Unary {
359        op: UnaryOp,
360        expr: Box<Expression>,
361    },
362    Ternary {
363        condition: Box<Expression>,
364        then_expr: Box<Expression>,
365        else_expr: Box<Expression>,
366    },
367}
368
369/// Binary operators
370#[derive(Debug, Clone, Copy, PartialEq, Eq)]
371pub enum BinaryOp {
372    Add,
373    Sub,
374    Mul,
375    Div,
376    Mod,
377    Eq,
378    Ne,
379    Gt,
380    Gte,
381    Lt,
382    Lte,
383    And,
384    Or,
385}
386
387/// Unary operators
388#[derive(Debug, Clone, Copy, PartialEq, Eq)]
389pub enum UnaryOp {
390    Not,
391    Neg,
392}
393
394/// Special selection types
395#[derive(Debug, Clone)]
396pub enum SpecialSelection {
397    Aggregate(AggregateSelection),
398    GroupBy(GroupBySelection),
399    Lookup(LookupSelection),
400    PageInfo(PageInfoSelection),
401    Edges(EdgesSelection),
402    Downsample(DownsampleSelection),
403    WindowFunction(WindowFunctionSelection),
404}
405
406/// Aggregate selection
407#[derive(Debug, Clone)]
408pub struct AggregateSelection {
409    pub fields: Vec<AggregateField>,
410}
411
412/// Aggregate field (count, sum, avg, etc.)
413#[derive(Debug, Clone)]
414pub struct AggregateField {
415    pub function: String,
416    pub field: Option<String>,
417}
418
419/// Group by selection
420#[derive(Debug, Clone)]
421pub struct GroupBySelection {
422    pub field: Option<String>,
423    pub fields: Option<Vec<String>>,
424    pub interval: Option<String>,
425    pub result_fields: Vec<String>,
426}
427
428/// Lookup selection (manual join)
429#[derive(Debug, Clone)]
430pub struct LookupSelection {
431    pub collection: String,
432    pub local_field: String,
433    pub foreign_field: String,
434    pub filter: Option<Filter>,
435    pub selection_set: Vec<Selection>,
436}
437
438/// Page info selection
439#[derive(Debug, Clone)]
440pub struct PageInfoSelection {
441    pub fields: Vec<String>,
442}
443
444/// Edges selection (cursor pagination)
445#[derive(Debug, Clone)]
446pub struct EdgesSelection {
447    pub fields: Vec<EdgeField>,
448}
449
450/// Edge field types
451#[derive(Debug, Clone)]
452pub enum EdgeField {
453    Cursor,
454    Node(Vec<Selection>),
455}
456
457/// Downsample selection (time-series)
458#[derive(Debug, Clone)]
459pub struct DownsampleSelection {
460    pub interval: String,
461    pub aggregation: String,
462    pub selection_set: Vec<Selection>,
463}
464
465/// Window function selection (time-series)
466#[derive(Debug, Clone)]
467pub struct WindowFunctionSelection {
468    pub alias: String,
469    pub field: String,
470    pub function: String,
471    pub window_size: i64,
472}
473
474// ============================================================================
475// SPECIAL ARGUMENT TYPES
476// ============================================================================
477
478/// Where clause
479#[derive(Debug, Clone)]
480pub struct WhereClause {
481    pub filter: Filter,
482}
483
484/// Order by clause
485#[derive(Debug, Clone)]
486pub struct OrderByClause {
487    pub orderings: Vec<Ordering>,
488}
489
490/// Single ordering
491#[derive(Debug, Clone)]
492pub struct Ordering {
493    pub field: String,
494    pub direction: SortDirection,
495}
496
497/// Sort direction
498#[derive(Debug, Clone, Copy, PartialEq, Eq)]
499pub enum SortDirection {
500    Asc,
501    Desc,
502}
503
504/// Search arguments
505#[derive(Debug, Clone)]
506pub struct SearchArgs {
507    pub query: String,
508    pub fields: Vec<String>,
509    pub fuzzy: bool,
510    pub min_score: Option<f64>,
511}
512
513/// Validation arguments
514#[derive(Debug, Clone)]
515pub struct ValidateArgs {
516    pub rules: Vec<ValidationRule>,
517}
518
519/// Validation rule for a field
520#[derive(Debug, Clone)]
521pub struct ValidationRule {
522    pub field: String,
523    pub constraints: Vec<ValidationConstraint>,
524}
525
526/// Validation constraint types
527#[derive(Debug, Clone)]
528pub enum ValidationConstraint {
529    Format(String),
530    Min(f64),
531    Max(f64),
532    MinLength(i64),
533    MaxLength(i64),
534    Pattern(String),
535}