Skip to main content

qcraft_core/ast/
common.rs

1/// Reference to a table (or schema object).
2#[derive(Debug, Clone, PartialEq)]
3pub struct SchemaRef {
4    pub name: String,
5    pub alias: Option<String>,
6    pub namespace: Option<String>,
7}
8
9impl SchemaRef {
10    pub fn new(name: impl Into<String>) -> Self {
11        Self {
12            name: name.into(),
13            alias: None,
14            namespace: None,
15        }
16    }
17
18    pub fn with_namespace(mut self, ns: impl Into<String>) -> Self {
19        self.namespace = Some(ns.into());
20        self
21    }
22
23    pub fn with_alias(mut self, alias: impl Into<String>) -> Self {
24        self.alias = Some(alias.into());
25        self
26    }
27}
28
29/// A field name, possibly with nested access (e.g. JSON path).
30#[derive(Debug, Clone, PartialEq)]
31pub struct FieldDef {
32    pub name: String,
33    pub child: Option<Box<FieldDef>>,
34}
35
36impl FieldDef {
37    pub fn new(name: impl Into<String>) -> Self {
38        Self {
39            name: name.into(),
40            child: None,
41        }
42    }
43}
44
45/// A field reference: field + table context.
46#[derive(Debug, Clone, PartialEq)]
47pub struct FieldRef {
48    pub field: FieldDef,
49    pub table_name: String,
50    pub namespace: Option<String>,
51}
52
53impl FieldRef {
54    pub fn new(table: impl Into<String>, field: impl Into<String>) -> Self {
55        Self {
56            field: FieldDef::new(field),
57            table_name: table.into(),
58            namespace: None,
59        }
60    }
61}
62
63/// Sort direction.
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum OrderDir {
66    Asc,
67    Desc,
68}
69
70/// NULLS placement in ORDER BY.
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72pub enum NullsOrder {
73    First,
74    Last,
75}
76
77/// ORDER BY element.
78#[derive(Debug, Clone)]
79pub struct OrderByDef {
80    pub expr: super::expr::Expr,
81    pub direction: OrderDir,
82    pub nulls: Option<NullsOrder>,
83}
84
85impl OrderByDef {
86    pub fn asc(expr: super::expr::Expr) -> Self {
87        Self {
88            expr,
89            direction: OrderDir::Asc,
90            nulls: None,
91        }
92    }
93
94    pub fn desc(expr: super::expr::Expr) -> Self {
95        Self {
96            expr,
97            direction: OrderDir::Desc,
98            nulls: None,
99        }
100    }
101
102    pub fn nulls_first(mut self) -> Self {
103        self.nulls = Some(NullsOrder::First);
104        self
105    }
106
107    pub fn nulls_last(mut self) -> Self {
108        self.nulls = Some(NullsOrder::Last);
109        self
110    }
111}