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
use crate::ast::{Delete, Insert, Merge, Select, Union, Update};
use std::borrow::Cow;

use super::IntoCommonTableExpression;

/// A database query
#[derive(Debug, PartialEq)]
pub enum Query<'a> {
    Select(Box<Select<'a>>),
    Insert(Box<Insert<'a>>),
    Update(Box<Update<'a>>),
    Delete(Box<Delete<'a>>),
    Union(Box<Union<'a>>),
    Merge(Box<Merge<'a>>),
    Raw(Cow<'a, str>),
}

impl<'a, T> From<T> for Query<'a>
where
    T: Into<Cow<'a, str>>,
{
    fn from(t: T) -> Self {
        Query::Raw(t.into())
    }
}

impl<'a> Query<'a> {
    pub fn is_select(&self) -> bool {
        matches!(self, Query::Select(_))
    }

    pub fn is_insert(&self) -> bool {
        matches!(self, Query::Insert(_))
    }

    pub fn is_update(&self) -> bool {
        matches!(self, Query::Update(_))
    }

    pub fn is_delete(&self) -> bool {
        matches!(self, Query::Delete(_))
    }

    pub fn is_union(&self) -> bool {
        matches!(self, Query::Union(_))
    }
}

/// A database query that only returns data without modifying anything.
#[derive(Debug, Clone, PartialEq)]
pub enum SelectQuery<'a> {
    Select(Box<Select<'a>>),
    Union(Box<Union<'a>>),
}

impl<'a> SelectQuery<'a> {
    /// Finds all named values or columns from the selection.
    pub fn named_selection(&self) -> Vec<String> {
        match self {
            Self::Select(s) => s.named_selection(),
            Self::Union(u) => u.named_selection(),
        }
    }

    #[cfg(feature = "mssql")]
    pub(crate) fn convert_tuple_selects_to_ctes(
        self,
        level: &mut usize,
    ) -> (Self, Vec<super::CommonTableExpression<'a>>) {
        match self {
            Self::Select(select) => match select.convert_tuple_selects_to_ctes(false, level) {
                either::Either::Left(select) => (Self::Select(Box::new(select)), Vec::new()),
                either::Either::Right((select, ctes)) => {
                    let select = Self::Select(Box::new(select));
                    (select, ctes)
                }
            },
            Self::Union(union) => match union.convert_tuple_selects_into_ctes(false, level) {
                either::Either::Left(union) => (Self::Union(Box::new(union)), Vec::new()),
                either::Either::Right((union, ctes)) => {
                    let union = Self::Union(Box::new(union));
                    (union, ctes)
                }
            },
        }
    }
}

impl<'a> From<Select<'a>> for SelectQuery<'a> {
    fn from(s: Select<'a>) -> Self {
        Self::Select(Box::new(s))
    }
}

impl<'a> From<Union<'a>> for SelectQuery<'a> {
    fn from(u: Union<'a>) -> Self {
        Self::Union(Box::new(u))
    }
}

impl<'a> From<SelectQuery<'a>> for Query<'a> {
    fn from(sq: SelectQuery<'a>) -> Self {
        match sq {
            SelectQuery::Select(s) => Query::Select(s),
            SelectQuery::Union(u) => Query::Union(u),
        }
    }
}

impl<'a> IntoCommonTableExpression<'a> for SelectQuery<'a> {}