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
150
use alloc::vec;
use alloc::vec::Vec;
use crate::{
expression::{parse_expression, Expression},
keywords::Keyword,
lexer::Token,
parser::{ParseError, Parser},
select::{parse_table_reference, TableReference},
span::OptSpanned,
Identifier, Span, Spanned,
};
#[derive(Clone, Debug)]
pub enum UpdateFlag {
LowPriority(Span),
Ignore(Span),
}
impl Spanned for UpdateFlag {
fn span(&self) -> Span {
match &self {
UpdateFlag::LowPriority(v) => v.span(),
UpdateFlag::Ignore(v) => v.span(),
}
}
}
#[derive(Clone, Debug)]
pub struct Update<'a> {
pub update_span: Span,
pub flags: Vec<UpdateFlag>,
pub tables: Vec<TableReference<'a>>,
pub set_span: Span,
pub set: Vec<(Vec<Identifier<'a>>, Expression<'a>)>,
pub where_: Option<(Expression<'a>, Span)>,
}
impl<'a> Spanned for Update<'a> {
fn span(&self) -> Span {
let mut set_span = None;
for (a, b) in &self.set {
set_span = set_span.opt_join_span(a).opt_join_span(b)
}
self.update_span
.join_span(&self.flags)
.join_span(&self.tables)
.join_span(&self.set_span)
.join_span(&set_span)
.join_span(&self.where_)
}
}
pub(crate) fn parse_update<'a, 'b>(parser: &mut Parser<'a, 'b>) -> Result<Update<'a>, ParseError> {
let update_span = parser.consume_keyword(Keyword::UPDATE)?;
let mut flags = Vec::new();
loop {
match &parser.token {
Token::Ident(_, Keyword::LOW_PRIORITY) => flags.push(UpdateFlag::LowPriority(
parser.consume_keyword(Keyword::LOW_PRIORITY)?,
)),
Token::Ident(_, Keyword::IGNORE) => {
flags.push(UpdateFlag::Ignore(parser.consume_keyword(Keyword::IGNORE)?))
}
_ => break,
}
}
let mut tables = Vec::new();
loop {
tables.push(parse_table_reference(parser)?);
if parser.skip_token(Token::Comma).is_none() {
break;
}
}
let set_span = parser.consume_keyword(Keyword::SET)?;
let mut set = Vec::new();
loop {
let mut col = vec![parser.consume_plain_identifier()?];
while parser.skip_token(Token::Period).is_some() {
col.push(parser.consume_plain_identifier()?);
}
parser.consume_token(Token::Eq)?;
let val = parse_expression(parser, false)?;
set.push((col, val));
if parser.skip_token(Token::Comma).is_none() {
break;
}
}
let where_ = if let Some(span) = parser.skip_keyword(Keyword::WHERE) {
Some((parse_expression(parser, false)?, span))
} else {
None
};
Ok(Update {
flags,
update_span,
tables,
set_span,
set,
where_,
})
}