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
use crate::ast::{Expression, Query, Select};
use std::{collections::BTreeSet, fmt};

use super::CommonTableExpression;
use super::IntoCommonTableExpression;

#[derive(Debug, PartialEq, Clone, Copy)]
pub(crate) enum UnionType {
    All,
    Distinct,
}

impl fmt::Display for UnionType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            UnionType::All => write!(f, "UNION ALL"),
            UnionType::Distinct => write!(f, "UNION"),
        }
    }
}

/// A builder for a `UNION`s over multiple `SELECT` statements.
#[derive(Debug, PartialEq, Clone, Default)]
pub struct Union<'a> {
    pub(crate) selects: Vec<Select<'a>>,
    pub(crate) types: Vec<UnionType>,
    pub(crate) ctes: Vec<CommonTableExpression<'a>>,
}

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

impl<'a> From<Union<'a>> for Expression<'a> {
    fn from(uaua: Union<'a>) -> Self {
        Expression::union(uaua)
    }
}

impl<'a> Union<'a> {
    pub fn new(q: Select<'a>) -> Self {
        Self {
            selects: vec![q],
            types: Vec::new(),
            ctes: Vec::new(),
        }
    }

    /// Creates a union with previous selection and the given `SELECT`
    /// statement, allowing duplicates.
    ///
    /// ```rust
    /// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
    /// # fn main() -> Result<(), quaint::error::Error> {
    /// let s1 = Select::default().value(1);
    /// let s2 = Select::default().value(2);
    /// let (sql, params) = Sqlite::build(Union::new(s1).all(s2))?;
    ///
    /// assert_eq!("SELECT ? UNION ALL SELECT ?", sql);
    ///
    /// assert_eq!(vec![
    ///     Value::from(1),
    ///     Value::from(2)
    /// ], params);
    /// # Ok(())
    /// # }
    /// ```
    pub fn all(mut self, q: Select<'a>) -> Self {
        self.selects.push(q);
        self.types.push(UnionType::All);
        self
    }

    /// Creates a union with previous selection and the given `SELECT`
    /// statement, selecting only distinct values.
    ///
    /// ```rust
    /// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
    /// # fn main() -> Result<(), quaint::error::Error> {
    /// let s1 = Select::default().value(1);
    /// let s2 = Select::default().value(2);
    /// let (sql, params) = Sqlite::build(Union::new(s1).distinct(s2))?;
    ///
    /// assert_eq!("SELECT ? UNION SELECT ?", sql);
    ///
    /// assert_eq!(vec![
    ///     Value::from(1),
    ///     Value::from(2)
    /// ], params);
    /// # Ok(())
    /// # }
    /// ```
    pub fn distinct(mut self, q: Select<'a>) -> Self {
        self.selects.push(q);
        self.types.push(UnionType::Distinct);
        self
    }

    /// A list of item names in the queries, skipping the anonymous values or
    /// columns.
    pub(crate) fn named_selection(&self) -> Vec<String> {
        self.selects
            .iter()
            .fold(BTreeSet::new(), |mut acc, select| {
                for name in select.named_selection() {
                    acc.insert(name);
                }

                acc
            })
            .into_iter()
            .collect()
    }

    /// Finds all comparisons between tuples and selects in the queries and
    /// converts them to common table expressions for making the query
    /// compatible with databases not supporting tuples.
    #[cfg(feature = "mssql")]
    pub(crate) fn convert_tuple_selects_into_ctes(
        mut self,
        top_level: bool,
        level: &mut usize,
    ) -> either::Either<Self, (Self, Vec<CommonTableExpression<'a>>)> {
        let mut queries = Vec::with_capacity(self.selects.len());
        let mut combined_ctes = Vec::new();

        for select in self.selects.drain(0..) {
            let (select, ctes) = select
                .convert_tuple_selects_to_ctes(false, level)
                .expect_right("Nested select should always be right.");

            queries.push(select);
            combined_ctes.extend(ctes);
        }

        self.selects = queries;

        if top_level {
            self.ctes = combined_ctes;
            either::Either::Left(self)
        } else {
            either::Either::Right((self, combined_ctes))
        }
    }
}

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