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
mod aggregate_to_string;
mod average;
mod coalesce;
mod concat;
mod count;
#[cfg(feature = "postgresql")]
mod encode;
#[cfg(feature = "postgresql")]
mod json_agg;
#[cfg(feature = "postgresql")]
mod json_build_object;
#[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;
mod sum;
#[cfg(feature = "postgresql")]
mod to_jsonb;
mod upper;

pub use aggregate_to_string::*;
pub use average::*;
pub use coalesce::*;
pub use concat::*;
pub use count::*;
#[cfg(feature = "postgresql")]
pub use encode::*;
#[cfg(feature = "postgresql")]
pub use json_agg::*;
#[cfg(feature = "postgresql")]
pub use json_build_object::*;
#[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::*;
pub use sum::*;
#[cfg(feature = "postgresql")]
pub use to_jsonb::*;
pub use upper::*;

use super::Aliasable;
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(any(feature = "postgresql", feature = "mysql"))]
            FunctionType::JsonExtract(_) => true,
            #[cfg(any(feature = "postgresql", feature = "mysql"))]
            FunctionType::JsonExtractLastArrayElem(_) => true,
            #[cfg(any(feature = "postgresql", feature = "mysql"))]
            FunctionType::JsonExtractFirstArrayElem(_) => true,
            #[cfg(feature = "postgresql")]
            FunctionType::ToJsonb(_) => true,
            _ => false,
        }
    }
}

/// A database function type
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum FunctionType<'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")]
    RowToJson(RowToJson<'a>),
    #[cfg(feature = "postgresql")]
    ToJsonb(ToJsonb<'a>),
    #[cfg(feature = "postgresql")]
    JsonAgg(JsonAgg<'a>),
    #[cfg(feature = "postgresql")]
    Encode(Encode<'a>),
    #[cfg(feature = "postgresql")]
    JsonBuildObject(JsonBuildObject<'a>),
}

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
    }
}