Skip to main content

no_mistakes_postgres_sql/function_calls/
clause.rs

1/// The innermost SQL clause containing a syntactic function call.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
3#[non_exhaustive]
4pub enum SqlFunctionClause {
5    Where,
6    JoinOn,
7    Having,
8    SelectList,
9    OrderBy,
10    Values,
11    Set,
12    Default,
13    Returning,
14    Cte,
15}
16
17impl SqlFunctionClause {
18    pub fn parse(value: &str) -> Option<Self> {
19        Some(match value {
20            "where" => Self::Where,
21            "join-on" => Self::JoinOn,
22            "having" => Self::Having,
23            "select-list" => Self::SelectList,
24            "order-by" => Self::OrderBy,
25            "values" => Self::Values,
26            "set" => Self::Set,
27            "default" => Self::Default,
28            "returning" => Self::Returning,
29            "cte" => Self::Cte,
30            _ => return None,
31        })
32    }
33
34    pub fn as_str(self) -> &'static str {
35        match self {
36            Self::Where => "where",
37            Self::JoinOn => "join-on",
38            Self::Having => "having",
39            Self::SelectList => "select-list",
40            Self::OrderBy => "order-by",
41            Self::Values => "values",
42            Self::Set => "set",
43            Self::Default => "default",
44            Self::Returning => "returning",
45            Self::Cte => "cte",
46        }
47    }
48
49    pub fn label(self) -> &'static str {
50        match self {
51            Self::Where => "WHERE",
52            Self::JoinOn => "JOIN ON",
53            Self::Having => "HAVING",
54            Self::SelectList => "SELECT list",
55            Self::OrderBy => "ORDER BY",
56            Self::Values => "VALUES",
57            Self::Set => "SET",
58            Self::Default => "DEFAULT",
59            Self::Returning => "RETURNING",
60            Self::Cte => "a CTE",
61        }
62    }
63}