sqlint/ast/
function.rs

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