grafbase_sql_ast/ast/
function.rs

1mod aggregate_to_string;
2mod average;
3mod coalesce;
4mod concat;
5mod count;
6#[cfg(feature = "postgresql")]
7mod encode;
8#[cfg(feature = "postgresql")]
9mod json_agg;
10#[cfg(feature = "postgresql")]
11mod json_build_object;
12#[cfg(any(feature = "postgresql", feature = "mysql"))]
13mod json_extract;
14#[cfg(any(feature = "postgresql", feature = "mysql"))]
15mod json_extract_array;
16#[cfg(any(feature = "postgresql", feature = "mysql"))]
17mod json_unquote;
18mod lower;
19mod maximum;
20mod minimum;
21mod row_number;
22#[cfg(feature = "postgresql")]
23mod row_to_json;
24mod sum;
25#[cfg(feature = "postgresql")]
26mod to_jsonb;
27mod upper;
28
29pub use aggregate_to_string::*;
30pub use average::*;
31pub use coalesce::*;
32pub use concat::*;
33pub use count::*;
34#[cfg(feature = "postgresql")]
35pub use encode::*;
36#[cfg(feature = "postgresql")]
37pub use json_agg::*;
38#[cfg(feature = "postgresql")]
39pub use json_build_object::*;
40#[cfg(any(feature = "postgresql", feature = "mysql"))]
41pub use json_extract::*;
42#[cfg(any(feature = "postgresql", feature = "mysql"))]
43pub(crate) use json_extract_array::*;
44#[cfg(any(feature = "postgresql", feature = "mysql"))]
45pub use json_unquote::*;
46pub use lower::*;
47pub use maximum::*;
48pub use minimum::*;
49pub use row_number::*;
50#[cfg(feature = "postgresql")]
51pub use row_to_json::*;
52pub use sum::*;
53#[cfg(feature = "postgresql")]
54pub use to_jsonb::*;
55pub use upper::*;
56
57use super::Aliasable;
58use std::borrow::Cow;
59
60/// A database function definition
61#[derive(Debug, Clone, PartialEq)]
62pub struct Function<'a> {
63    pub(crate) typ_: FunctionType<'a>,
64    pub(crate) alias: Option<Cow<'a, str>>,
65}
66
67impl<'a> Function<'a> {
68    pub fn returns_json(&self) -> bool {
69        match self.typ_ {
70            #[cfg(feature = "postgresql")]
71            FunctionType::RowToJson(_) => true,
72            #[cfg(any(feature = "postgresql", feature = "mysql"))]
73            FunctionType::JsonExtract(_) => true,
74            #[cfg(any(feature = "postgresql", feature = "mysql"))]
75            FunctionType::JsonExtractLastArrayElem(_) => true,
76            #[cfg(any(feature = "postgresql", feature = "mysql"))]
77            FunctionType::JsonExtractFirstArrayElem(_) => true,
78            #[cfg(feature = "postgresql")]
79            FunctionType::ToJsonb(_) => true,
80            _ => false,
81        }
82    }
83}
84
85/// A database function type
86#[derive(Debug, Clone, PartialEq)]
87pub(crate) enum FunctionType<'a> {
88    Count(Count<'a>),
89    AggregateToString(AggregateToString<'a>),
90    Average(Average<'a>),
91    Sum(Sum<'a>),
92    Lower(Lower<'a>),
93    Upper(Upper<'a>),
94    Minimum(Minimum<'a>),
95    Maximum(Maximum<'a>),
96    Coalesce(Coalesce<'a>),
97    Concat(Concat<'a>),
98    #[cfg(any(feature = "postgresql", feature = "mysql"))]
99    JsonExtract(JsonExtract<'a>),
100    #[cfg(any(feature = "postgresql", feature = "mysql"))]
101    JsonExtractLastArrayElem(JsonExtractLastArrayElem<'a>),
102    #[cfg(any(feature = "postgresql", feature = "mysql"))]
103    JsonExtractFirstArrayElem(JsonExtractFirstArrayElem<'a>),
104    #[cfg(any(feature = "postgresql", feature = "mysql"))]
105    JsonUnquote(JsonUnquote<'a>),
106    #[cfg(feature = "postgresql")]
107    RowToJson(RowToJson<'a>),
108    #[cfg(feature = "postgresql")]
109    ToJsonb(ToJsonb<'a>),
110    #[cfg(feature = "postgresql")]
111    JsonAgg(JsonAgg<'a>),
112    #[cfg(feature = "postgresql")]
113    Encode(Encode<'a>),
114    #[cfg(feature = "postgresql")]
115    JsonBuildObject(JsonBuildObject<'a>),
116}
117
118impl<'a> Aliasable<'a> for Function<'a> {
119    type Target = Function<'a>;
120
121    fn alias<T>(mut self, alias: T) -> Self::Target
122    where
123        T: Into<Cow<'a, str>>,
124    {
125        self.alias = Some(alias.into());
126        self
127    }
128}