Skip to main content

uqa_sql/ast/
interval.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7use serde::{Deserialize, Serialize};
8
9/// The stored-field restriction in a `PostgreSQL` interval declaration.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11#[repr(u16)]
12pub enum IntervalFields {
13    All = 32767,
14    Year = 4,
15    Month = 2,
16    Day = 8,
17    Hour = 1024,
18    Minute = 2048,
19    Second = 4096,
20    YearToMonth = 6,
21    DayToHour = 1032,
22    DayToMinute = 3080,
23    DayToSecond = 7176,
24    HourToMinute = 3072,
25    HourToSecond = 7168,
26    MinuteToSecond = 6144,
27}
28
29impl IntervalFields {
30    #[must_use]
31    pub const fn modifier_mask(self) -> u16 {
32        self as u16
33    }
34
35    #[must_use]
36    pub const fn from_modifier_mask(mask: i64) -> Option<Self> {
37        Some(match mask {
38            32767 => Self::All,
39            4 => Self::Year,
40            2 => Self::Month,
41            8 => Self::Day,
42            1024 => Self::Hour,
43            2048 => Self::Minute,
44            4096 => Self::Second,
45            6 => Self::YearToMonth,
46            1032 => Self::DayToHour,
47            3080 => Self::DayToMinute,
48            7176 => Self::DayToSecond,
49            3072 => Self::HourToMinute,
50            7168 => Self::HourToSecond,
51            6144 => Self::MinuteToSecond,
52            _ => return None,
53        })
54    }
55
56    #[must_use]
57    pub const fn sql_suffix(self) -> &'static str {
58        match self {
59            Self::All => "",
60            Self::Year => " year",
61            Self::Month => " month",
62            Self::Day => " day",
63            Self::Hour => " hour",
64            Self::Minute => " minute",
65            Self::Second => " second",
66            Self::YearToMonth => " year to month",
67            Self::DayToHour => " day to hour",
68            Self::DayToMinute => " day to minute",
69            Self::DayToSecond => " day to second",
70            Self::HourToMinute => " hour to minute",
71            Self::HourToSecond => " hour to second",
72            Self::MinuteToSecond => " minute to second",
73        }
74    }
75
76    #[must_use]
77    pub fn from_sql_suffix(suffix: &str) -> Option<Self> {
78        Some(match suffix.trim() {
79            "" => Self::All,
80            "year" => Self::Year,
81            "month" => Self::Month,
82            "day" => Self::Day,
83            "hour" => Self::Hour,
84            "minute" => Self::Minute,
85            "second" => Self::Second,
86            "year to month" => Self::YearToMonth,
87            "day to hour" => Self::DayToHour,
88            "day to minute" => Self::DayToMinute,
89            "day to second" => Self::DayToSecond,
90            "hour to minute" => Self::HourToMinute,
91            "hour to second" => Self::HourToSecond,
92            "minute to second" => Self::MinuteToSecond,
93            _ => return None,
94        })
95    }
96}