Skip to main content

quaint/ast/
function.rs

1mod aggregate_to_string;
2mod count;
3mod row_number;
4
5pub use aggregate_to_string::*;
6pub use count::*;
7pub use row_number::*;
8
9use super::DatabaseValue;
10use std::borrow::Cow;
11
12/// A database function definition
13#[derive(Debug, Clone, PartialEq)]
14pub struct Function<'a> {
15    pub(crate) typ_: FunctionType<'a>,
16    pub(crate) alias: Option<Cow<'a, str>>,
17}
18
19/// A database function type
20#[derive(Debug, Clone, PartialEq)]
21pub(crate) enum FunctionType<'a> {
22    RowNumber(RowNumber<'a>),
23    Count(Count<'a>),
24    AggregateToString(AggregateToString<'a>),
25}
26
27impl<'a> Function<'a> {
28    /// Give the function an alias in the query.
29    pub fn alias<S>(mut self, alias: S) -> Self
30    where
31        S: Into<Cow<'a, str>>,
32    {
33        self.alias = Some(alias.into());
34        self
35    }
36}
37
38macro_rules! function {
39    ($($kind:ident),*) => (
40        $(
41            impl<'a> From<$kind<'a>> for Function<'a> {
42                #[inline]
43                fn from(f: $kind<'a>) -> Self {
44                    Function {
45                        typ_: FunctionType::$kind(f),
46                        alias: None,
47                    }
48                }
49            }
50
51            impl<'a> From<$kind<'a>> for DatabaseValue<'a> {
52                #[inline]
53                fn from(f: $kind<'a>) -> Self {
54                    Function::from(f).into()
55                }
56            }
57        )*
58    );
59}
60
61function!(RowNumber, Count, AggregateToString);