vantage_sql/primitives/
interval.rs1use vantage_expressions::{Expression, Expressive};
2
3#[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 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#[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#[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#[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}