Skip to main content

vantage_sql/primitives/
interval.rs

1use vantage_expressions::{Expression, Expressive};
2
3/// Vendor-aware time interval literal.
4///
5/// Renders as:
6/// - **PostgreSQL:** `INTERVAL '1 year'`
7/// - **MySQL:**      `INTERVAL 1 YEAR`
8/// - **SQLite:**     `1` (numeric days — caller must convert units)
9///
10/// # Examples
11///
12/// ```ignore
13/// use vantage_sql::primitives::interval::Interval;
14///
15/// Interval::new(1, "year")
16/// Interval::new(6, "month")
17/// Interval::new(30, "day")
18/// ```
19#[derive(Debug, Clone)]
20pub struct Interval {
21    amount: i64,
22    unit: String,
23}
24
25impl Interval {
26    pub fn new(amount: i64, unit: impl Into<String>) -> Self {
27        Self {
28            amount,
29            unit: unit.into().to_lowercase(),
30        }
31    }
32
33    /// Convert to approximate days for SQLite (which has no INTERVAL type).
34    fn to_days(&self) -> i64 {
35        match self.unit.as_str() {
36            "year" | "years" => self.amount * 365,
37            "month" | "months" => self.amount * 30,
38            "week" | "weeks" => self.amount * 7,
39            "day" | "days" => self.amount,
40            "hour" | "hours" => (self.amount as f64 / 24.0).round() as i64,
41            _ => self.amount,
42        }
43    }
44}
45
46// -- SQLite: numeric days (approximate) --------------------------------------
47
48#[cfg(feature = "sqlite")]
49impl Expressive<crate::sqlite::types::AnySqliteType> for Interval {
50    fn expr(&self) -> Expression<crate::sqlite::types::AnySqliteType> {
51        Expression::new(format!("{}", self.to_days()), vec![])
52    }
53}
54
55// -- MySQL: INTERVAL 1 YEAR -------------------------------------------------
56
57#[cfg(feature = "mysql")]
58impl Expressive<crate::mysql::types::AnyMysqlType> for Interval {
59    fn expr(&self) -> Expression<crate::mysql::types::AnyMysqlType> {
60        Expression::new(
61            format!("INTERVAL {} {}", self.amount, self.unit.to_uppercase()),
62            vec![],
63        )
64    }
65}
66
67// -- PostgreSQL: INTERVAL '1 year' ------------------------------------------
68
69#[cfg(feature = "postgres")]
70impl Expressive<crate::postgres::types::AnyPostgresType> for Interval {
71    fn expr(&self) -> Expression<crate::postgres::types::AnyPostgresType> {
72        Expression::new(format!("INTERVAL '{} {}'", self.amount, self.unit), vec![])
73    }
74}