qail_core/ast/
values.rs

1use crate::ast::QailCmd;
2use serde::{Deserialize, Serialize};
3use uuid::Uuid;
4
5/// Time interval unit for duration expressions
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7pub enum IntervalUnit {
8    Second,
9    Minute,
10    Hour,
11    Day,
12    Week,
13    Month,
14    Year,
15}
16
17impl std::fmt::Display for IntervalUnit {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        match self {
20            IntervalUnit::Second => write!(f, "seconds"),
21            IntervalUnit::Minute => write!(f, "minutes"),
22            IntervalUnit::Hour => write!(f, "hours"),
23            IntervalUnit::Day => write!(f, "days"),
24            IntervalUnit::Week => write!(f, "weeks"),
25            IntervalUnit::Month => write!(f, "months"),
26            IntervalUnit::Year => write!(f, "years"),
27        }
28    }
29}
30
31/// A value in a condition.
32#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
33pub enum Value {
34    /// NULL value
35    Null,
36    /// Boolean
37    Bool(bool),
38    /// Integer
39    Int(i64),
40    /// Float
41    Float(f64),
42    /// String
43    String(String),
44    /// Parameter reference ($1, $2, etc.)
45    Param(usize),
46    /// Named parameter reference (:name, :id, etc.)
47    NamedParam(String),
48    /// SQL function call (e.g., now())
49    Function(String),
50    /// Array of values
51    Array(Vec<Value>),
52    /// Subquery for IN/EXISTS expressions (e.g., WHERE id IN (SELECT ...))
53    Subquery(Box<QailCmd>),
54    /// Column reference (e.g. JOIN ... ON a.id = b.id)
55    Column(String),
56    /// UUID value
57    Uuid(Uuid),
58    /// Null UUID value (for typed NULL in UUID columns)
59    NullUuid,
60    /// Time interval (e.g., 24 hours, 7 days)
61    Interval { amount: i64, unit: IntervalUnit },
62    /// Timestamp value (for DateTime binding)
63    Timestamp(String),
64    /// Binary data (bytea)
65    Bytes(Vec<u8>),
66}
67
68impl std::fmt::Display for Value {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        match self {
71            Value::Null => write!(f, "NULL"),
72            Value::Bool(b) => write!(f, "{}", b),
73            Value::Int(n) => write!(f, "{}", n),
74            Value::Float(n) => write!(f, "{}", n),
75            Value::String(s) => write!(f, "'{}'", s),
76            Value::Param(n) => write!(f, "${}", n),
77            Value::NamedParam(name) => write!(f, ":{}", name),
78            Value::Function(s) => write!(f, "{}", s),
79            Value::Array(arr) => {
80                write!(f, "(")?;
81                for (i, v) in arr.iter().enumerate() {
82                    if i > 0 {
83                        write!(f, ", ")?;
84                    }
85                    write!(f, "{}", v)?;
86                }
87                write!(f, ")")
88            }
89            Value::Subquery(_) => write!(f, "(SUBQUERY)"),
90            Value::Column(s) => write!(f, "{}", s),
91            Value::Uuid(u) => write!(f, "'{}'", u),
92            Value::NullUuid => write!(f, "NULL"),
93            Value::Interval { amount, unit } => write!(f, "INTERVAL '{} {}'", amount, unit),
94            Value::Timestamp(ts) => write!(f, "'{}'", ts),
95            Value::Bytes(bytes) => {
96                write!(f, "'\\x")?;
97                for byte in bytes {
98                    write!(f, "{:02x}", byte)?;
99                }
100                write!(f, "'")
101            }
102        }
103    }
104}
105
106impl From<bool> for Value {
107    fn from(b: bool) -> Self {
108        Value::Bool(b)
109    }
110}
111
112impl From<i32> for Value {
113    fn from(n: i32) -> Self {
114        Value::Int(n as i64)
115    }
116}
117
118impl From<i64> for Value {
119    fn from(n: i64) -> Self {
120        Value::Int(n)
121    }
122}
123
124impl From<f64> for Value {
125    fn from(n: f64) -> Self {
126        Value::Float(n)
127    }
128}
129
130impl From<&str> for Value {
131    fn from(s: &str) -> Self {
132        Value::String(s.to_string())
133    }
134}
135
136impl From<String> for Value {
137    fn from(s: String) -> Self {
138        Value::String(s)
139    }
140}
141
142impl From<Uuid> for Value {
143    fn from(u: Uuid) -> Self {
144        Value::Uuid(u)
145    }
146}
147
148impl From<Option<Uuid>> for Value {
149    fn from(opt: Option<Uuid>) -> Self {
150        match opt {
151            Some(u) => Value::Uuid(u),
152            None => Value::NullUuid,
153        }
154    }
155}