Skip to main content

activecube_rs/compiler/
ir.rs

1/// SQL binding value — database-agnostic representation.
2#[derive(Debug, Clone)]
3pub enum SqlValue {
4    String(String),
5    Int(i64),
6    Float(f64),
7    Bool(bool),
8}
9
10/// Intermediate representation of a compiled GraphQL cube query.
11#[derive(Debug, Clone)]
12pub struct QueryIR {
13    pub cube: String,
14    pub schema: String,
15    pub table: String,
16    pub selects: Vec<SelectExpr>,
17    pub filters: FilterNode,
18    pub having: FilterNode,
19    pub group_by: Vec<String>,
20    pub order_by: Vec<OrderExpr>,
21    pub limit: u32,
22    pub offset: u32,
23}
24
25#[derive(Debug, Clone)]
26pub enum SelectExpr {
27    Column {
28        column: String,
29        alias: Option<String>,
30    },
31    Aggregate {
32        function: String,
33        column: String,
34        alias: String,
35    },
36}
37
38#[derive(Debug, Clone)]
39pub enum FilterNode {
40    And(Vec<FilterNode>),
41    Or(Vec<FilterNode>),
42    Condition {
43        column: String,
44        op: CompareOp,
45        value: SqlValue,
46    },
47    Empty,
48}
49
50#[derive(Debug, Clone)]
51pub enum CompareOp {
52    Eq,
53    Ne,
54    Gt,
55    Ge,
56    Lt,
57    Le,
58    Like,
59    In,
60    NotIn,
61    Includes,
62    IsNull,
63    IsNotNull,
64}
65
66impl CompareOp {
67    pub fn sql_op(&self) -> &'static str {
68        match self {
69            CompareOp::Eq => "=",
70            CompareOp::Ne => "!=",
71            CompareOp::Gt => ">",
72            CompareOp::Ge => ">=",
73            CompareOp::Lt => "<",
74            CompareOp::Le => "<=",
75            CompareOp::Like => "LIKE",
76            CompareOp::In => "IN",
77            CompareOp::NotIn => "NOT IN",
78            CompareOp::Includes => "LIKE",
79            CompareOp::IsNull => "IS NULL",
80            CompareOp::IsNotNull => "IS NOT NULL",
81        }
82    }
83
84    pub fn is_unary(&self) -> bool {
85        matches!(self, CompareOp::IsNull | CompareOp::IsNotNull)
86    }
87}
88
89#[derive(Debug, Clone)]
90pub struct OrderExpr {
91    pub column: String,
92    pub descending: bool,
93}
94
95impl FilterNode {
96    pub fn is_empty(&self) -> bool {
97        matches!(self, FilterNode::Empty)
98    }
99}