1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
mod aggregate_to_string;
mod average;
mod coalesce;
mod concat;
mod count;
mod json_array_agg;
mod json_build_obj;
#[cfg(any(feature = "postgresql", feature = "mysql"))]
mod json_extract;
#[cfg(any(feature = "postgresql", feature = "mysql"))]
mod json_extract_array;
#[cfg(any(feature = "postgresql", feature = "mysql"))]
mod json_unquote;
mod lower;
mod maximum;
mod minimum;
mod row_number;
#[cfg(feature = "postgresql")]
mod row_to_json;
#[cfg(any(feature = "postgresql", feature = "mysql"))]
mod search;
mod sum;
mod upper;

#[cfg(feature = "mysql")]
mod uuid;

pub use aggregate_to_string::*;
pub use average::*;
pub use coalesce::*;
pub use concat::*;
pub use count::*;
pub use json_array_agg::*;
pub use json_build_obj::*;
#[cfg(any(feature = "postgresql", feature = "mysql"))]
pub use json_extract::*;
#[cfg(any(feature = "postgresql", feature = "mysql"))]
pub(crate) use json_extract_array::*;
#[cfg(any(feature = "postgresql", feature = "mysql"))]
pub use json_unquote::*;
pub use lower::*;
pub use maximum::*;
pub use minimum::*;
pub use row_number::*;
#[cfg(feature = "postgresql")]
pub use row_to_json::*;
#[cfg(any(feature = "mysql", feature = "postgresql"))]
pub use search::*;
pub use sum::*;
pub use upper::*;

#[cfg(feature = "mysql")]
pub use self::uuid::*;

use super::{Aliasable, Expression};
use std::borrow::Cow;

/// A database function definition
#[derive(Debug, Clone, PartialEq)]
pub struct Function<'a> {
    pub(crate) typ_: FunctionType<'a>,
    pub(crate) alias: Option<Cow<'a, str>>,
}

impl<'a> Function<'a> {
    pub fn returns_json(&self) -> bool {
        match self.typ_ {
            #[cfg(feature = "postgresql")]
            FunctionType::RowToJson(_) => true,
            #[cfg(feature = "mysql")]
            FunctionType::JsonExtract(_) => true,
            #[cfg(any(feature = "postgresql", feature = "mysql"))]
            FunctionType::JsonExtractLastArrayElem(_) => true,
            #[cfg(any(feature = "postgresql", feature = "mysql"))]
            FunctionType::JsonExtractFirstArrayElem(_) => true,
            _ => false,
        }
    }
}

/// A database function type
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum FunctionType<'a> {
    #[cfg(feature = "postgresql")]
    RowToJson(RowToJson<'a>),
    RowNumber(RowNumber<'a>),
    Count(Count<'a>),
    AggregateToString(AggregateToString<'a>),
    Average(Average<'a>),
    Sum(Sum<'a>),
    Lower(Lower<'a>),
    Upper(Upper<'a>),
    Minimum(Minimum<'a>),
    Maximum(Maximum<'a>),
    Coalesce(Coalesce<'a>),
    Concat(Concat<'a>),
    #[cfg(any(feature = "postgresql", feature = "mysql"))]
    JsonExtract(JsonExtract<'a>),
    #[cfg(any(feature = "postgresql", feature = "mysql"))]
    JsonExtractLastArrayElem(JsonExtractLastArrayElem<'a>),
    #[cfg(any(feature = "postgresql", feature = "mysql"))]
    JsonExtractFirstArrayElem(JsonExtractFirstArrayElem<'a>),
    #[cfg(any(feature = "postgresql", feature = "mysql"))]
    JsonUnquote(JsonUnquote<'a>),
    #[cfg(feature = "postgresql")]
    JsonArrayAgg(JsonArrayAgg<'a>),
    #[cfg(feature = "postgresql")]
    JsonBuildObject(JsonBuildObject<'a>),
    #[cfg(any(feature = "postgresql", feature = "mysql"))]
    TextSearch(TextSearch<'a>),
    #[cfg(any(feature = "postgresql", feature = "mysql"))]
    TextSearchRelevance(TextSearchRelevance<'a>),
    #[cfg(feature = "mysql")]
    UuidToBin,
    #[cfg(feature = "mysql")]
    UuidToBinSwapped,
    #[cfg(feature = "mysql")]
    Uuid,
}

impl<'a> Aliasable<'a> for Function<'a> {
    type Target = Function<'a>;

    fn alias<T>(mut self, alias: T) -> Self::Target
    where
        T: Into<Cow<'a, str>>,
    {
        self.alias = Some(alias.into());
        self
    }
}

#[cfg(feature = "postgresql")]
function!(RowToJson);

#[cfg(any(feature = "postgresql", feature = "mysql"))]
function!(JsonExtract);

#[cfg(any(feature = "postgresql", feature = "mysql"))]
function!(JsonExtractLastArrayElem);

#[cfg(any(feature = "postgresql", feature = "mysql"))]
function!(JsonExtractFirstArrayElem);

#[cfg(any(feature = "postgresql", feature = "mysql"))]
function!(JsonUnquote);

#[cfg(any(feature = "postgresql", feature = "mysql"))]
function!(TextSearch);

#[cfg(any(feature = "postgresql", feature = "mysql"))]
function!(TextSearchRelevance);

function!(
    RowNumber,
    Count,
    AggregateToString,
    Average,
    Sum,
    Lower,
    Upper,
    Minimum,
    Maximum,
    Coalesce,
    Concat,
    JsonArrayAgg,
    JsonBuildObject
);