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