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
use std::fmt;
use std::str;

use nom::branch::alt;
use nom::bytes::complete::{tag, tag_no_case};
use nom::character::complete::{multispace0, multispace1};
use nom::combinator::{map, opt};
use nom::multi::many1;
use nom::sequence::{delimited, preceded, tuple};
use nom::IResult;

use base::error::ParseSQLError;
use base::{CommonParser, OrderClause};
use dms::select::{LimitClause, SelectStatement};

// TODO 用于 create 语句的 select
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
pub struct CompoundSelectStatement {
    pub selects: Vec<(Option<CompoundSelectOperator>, SelectStatement)>,
    pub order: Option<OrderClause>,
    pub limit: Option<LimitClause>,
}

impl CompoundSelectStatement {
    // Parse compound selection
    pub fn parse(i: &str) -> IResult<&str, CompoundSelectStatement, ParseSQLError<&str>> {
        let (remaining_input, (first_select, other_selects, _, order, limit, _)) = tuple((
            CommonParser::opt_delimited(tag("("), SelectStatement::nested_selection, tag(")")),
            many1(Self::other_selects),
            multispace0,
            opt(OrderClause::parse),
            opt(LimitClause::parse),
            CommonParser::statement_terminator,
        ))(i)?;

        let mut selects = vec![(None, first_select)];
        selects.extend(other_selects);

        Ok((
            remaining_input,
            CompoundSelectStatement {
                selects,
                order,
                limit,
            },
        ))
    }

    fn other_selects(
        i: &str,
    ) -> IResult<&str, (Option<CompoundSelectOperator>, SelectStatement), ParseSQLError<&str>> {
        let (remaining_input, (_, op, _, select)) = tuple((
            multispace0,
            CompoundSelectOperator::parse,
            multispace1,
            CommonParser::opt_delimited(
                tag("("),
                delimited(multispace0, SelectStatement::nested_selection, multispace0),
                tag(")"),
            ),
        ))(i)?;

        Ok((remaining_input, (Some(op), select)))
    }
}

impl fmt::Display for CompoundSelectStatement {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for (ref op, ref sel) in &self.selects {
            if op.is_some() {
                write!(f, " {}", op.as_ref().unwrap())?;
            }
            write!(f, " {}", sel)?;
        }
        if self.order.is_some() {
            write!(f, " {}", self.order.as_ref().unwrap())?;
        }
        if self.limit.is_some() {
            write!(f, " {}", self.order.as_ref().unwrap())?;
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
pub enum CompoundSelectOperator {
    Union,
    DistinctUnion,
    Intersect,
    Except,
}

impl CompoundSelectOperator {
    // Parse compound operator
    fn parse(i: &str) -> IResult<&str, CompoundSelectOperator, ParseSQLError<&str>> {
        alt((
            map(
                preceded(
                    tag_no_case("UNION"),
                    opt(preceded(
                        multispace1,
                        alt((
                            map(tag_no_case("ALL"), |_| false),
                            map(tag_no_case("DISTINCT"), |_| true),
                        )),
                    )),
                ),
                |distinct| match distinct {
                    // DISTINCT is the default in both MySQL and SQLite
                    None => CompoundSelectOperator::DistinctUnion,
                    Some(d) => {
                        if d {
                            CompoundSelectOperator::DistinctUnion
                        } else {
                            CompoundSelectOperator::Union
                        }
                    }
                },
            ),
            map(tag_no_case("INTERSECT"), |_| {
                CompoundSelectOperator::Intersect
            }),
            map(tag_no_case("EXCEPT"), |_| CompoundSelectOperator::Except),
        ))(i)
    }
}

impl fmt::Display for CompoundSelectOperator {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            CompoundSelectOperator::Union => write!(f, "UNION"),
            CompoundSelectOperator::DistinctUnion => write!(f, "UNION DISTINCT"),
            CompoundSelectOperator::Intersect => write!(f, "INTERSECT"),
            CompoundSelectOperator::Except => write!(f, "EXCEPT"),
        }
    }
}