Skip to content

Commit b9c449f

Browse files
committed
sql_parser: update statement parser implemented
1 parent 0d804df commit b9c449f

File tree

6 files changed

+1053
-36
lines changed

6 files changed

+1053
-36
lines changed

src/ast/alter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub struct PrimaryKeyConstraint {
6969
/// Do not get confused with the conflict clause in the
7070
/// [RaiseFunction](https://www.sqlite.org/lang_raise.html) as they look similar
7171
/// but they are different.
72-
#[derive(Debug, PartialEq)]
72+
#[derive(Debug, PartialEq, Clone)]
7373
pub enum ConflictClause {
7474
/// No conflict clause
7575
None,

src/ast/delete.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{Expression, LimitClause, OrderingTerm, QualifiedTableName};
22

33
/// An AST for [DELETE](https://www.sqlite.org/lang_delete.html) SQL statement.
4-
#[derive(Debug, PartialEq)]
4+
#[derive(Debug, PartialEq, Clone)]
55
pub struct DeleteStatement {
66
/// The table name to delete from
77
pub table_name: QualifiedTableName,
@@ -20,7 +20,7 @@ pub struct DeleteStatement {
2020
}
2121

2222
/// A [RETURNING](https://www.sqlite.org/lang_returning.html) clause types
23-
#[derive(Debug, PartialEq)]
23+
#[derive(Debug, PartialEq, Clone)]
2424
pub enum ReturningClause {
2525
/// A single expression
2626
Expr(Expression),

src/ast/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub use trx::{
3535
BeginTransactionStatement, CommitTransactionStatement, ReleaseStatement,
3636
RollbackTransactionStatement, SavepointStatement, TransactionType,
3737
};
38-
pub use update::UpdateStatement;
38+
pub use update::*;
3939

4040
/// Top-level AST node representing any SQLite statement
4141
#[derive(Debug, PartialEq)]

src/ast/update.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use super::{
33
};
44

55
/// An AST for [UPDATE SQL](https://www.sqlite.org/lang_update.html) statement.
6-
#[derive(Debug, PartialEq)]
6+
#[derive(Debug, PartialEq, Clone)]
77
pub struct UpdateStatement {
88
// The conflict clause defined after the OR keyword is used to specify the
99
// behavior of the UPDATE statement when a constraint violation occurs.
10-
pub conflict_clause: Option<ConflictClause>,
10+
pub conflict_clause: ConflictClause,
1111

1212
// The table name to update.
1313
pub table_name: QualifiedTableName,
@@ -19,18 +19,18 @@ pub struct UpdateStatement {
1919
pub from_clause: Option<FromClause>,
2020

2121
// The where clause to filter the rows to update.
22-
pub where_clause: Option<Expression>,
22+
pub where_clause: Option<Box<Expression>>,
2323

2424
// The returning clause to return the updated rows.
25-
pub returning_clause: Option<ReturningClause>,
25+
pub returning_clause: Vec<ReturningClause>,
2626
}
2727

2828
/// A [SetClause](https://www.sqlite.org/syntax/set-clause.html)
29-
#[derive(Debug, PartialEq)]
29+
#[derive(Debug, PartialEq, Clone)]
3030
pub enum SetClause {
3131
/// A single column assignment, e.g. `column = expression`.
3232
ColumnAssignment(Identifier, Expression),
3333

3434
/// A multiple column assignment, e.g. `(column1, column2) = expression`.
35-
MultipleColumnAssignment((Vec<Identifier>, Expression)),
35+
MultipleColumnAssignment(Vec<Identifier>, Expression),
3636
}

src/parser/select/mod.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,15 @@ pub trait SelectStatementParser {
2727

2828
fn parse_select_column(&mut self) -> Result<SelectItem, ParsingError>;
2929

30-
fn parse_select_from_clause(&mut self) -> Result<Option<FromClause>, ParsingError>;
30+
fn parse_from_clause(&mut self) -> Result<Option<FromClause>, ParsingError>;
3131

32-
fn parse_select_from_clause_subquery(&mut self) -> Result<FromClause, ParsingError>;
32+
fn parse_from_clause_subquery(&mut self) -> Result<FromClause, ParsingError>;
3333

34-
fn parse_select_from_join_clause(
35-
&mut self,
36-
lhs: FromClause,
37-
) -> Result<FromClause, ParsingError>;
34+
fn parse_from_join_clause(&mut self, lhs: FromClause) -> Result<FromClause, ParsingError>;
3835

39-
fn parse_select_from_clause_join_type(&mut self) -> Result<JoinType, ParsingError>;
36+
fn parse_from_clause_join_type(&mut self) -> Result<JoinType, ParsingError>;
4037

41-
fn parse_select_from_clause_join_constraints(
38+
fn parse_from_clause_join_constraints(
4239
&mut self,
4340
) -> Result<Option<JoinConstraint>, ParsingError>;
4441

@@ -92,7 +89,7 @@ impl<'a> SelectStatementParser for Parser<'a> {
9289
Ok(SelectStatement::Select(Select {
9390
distinct_type: self.parse_distinct_type()?,
9491
columns: self.parse_select_columns()?,
95-
from: self.parse_select_from_clause()?,
92+
from: self.parse_from_clause()?,
9693
where_clause: self.parse_where_clause()?,
9794
group_by: self.parse_group_by_clause()?,
9895
having: self.parse_having_clause()?,
@@ -157,15 +154,15 @@ impl<'a> SelectStatementParser for Parser<'a> {
157154
))
158155
}
159156

160-
fn parse_select_from_clause(&mut self) -> Result<Option<FromClause>, ParsingError> {
157+
fn parse_from_clause(&mut self) -> Result<Option<FromClause>, ParsingError> {
161158
if let Ok(Keyword::From) = self.peek_as_keyword() {
162159
self.consume_as_keyword(Keyword::From)?;
163-
return Ok(Some(self.parse_select_from_clause_subquery()?));
160+
return Ok(Some(self.parse_from_clause_subquery()?));
164161
}
165162
Ok(None)
166163
}
167164

168-
fn parse_select_from_clause_subquery(&mut self) -> Result<FromClause, ParsingError> {
165+
fn parse_from_clause_subquery(&mut self) -> Result<FromClause, ParsingError> {
169166
dbg!("parse_select_from_clause");
170167

171168
dbg!(&self.peek_token());
@@ -186,7 +183,7 @@ impl<'a> SelectStatementParser for Parser<'a> {
186183
let mut froms = Vec::new();
187184
loop {
188185
dbg!("parse_select_from_clause");
189-
let table_or_subquery = self.parse_select_from_clause_subquery()?;
186+
let table_or_subquery = self.parse_from_clause_subquery()?;
190187
dbg!(&table_or_subquery);
191188
froms.push(table_or_subquery);
192189

@@ -224,7 +221,7 @@ impl<'a> SelectStatementParser for Parser<'a> {
224221
indexed_type,
225222
});
226223

227-
return self.parse_select_from_join_clause(lhs);
224+
return self.parse_from_join_clause(lhs);
228225
}
229226
}
230227
// TODO: improve this error message
@@ -233,19 +230,16 @@ impl<'a> SelectStatementParser for Parser<'a> {
233230
))
234231
}
235232

236-
fn parse_select_from_join_clause(
237-
&mut self,
238-
lhs: FromClause,
239-
) -> Result<FromClause, ParsingError> {
233+
fn parse_from_join_clause(&mut self, lhs: FromClause) -> Result<FromClause, ParsingError> {
240234
dbg!("parse_select_from_join_clause");
241235
let mut join_tables = Vec::new();
242236

243-
while let Ok(join_type) = self.parse_select_from_clause_join_type() {
244-
let rhs = self.parse_select_from_clause_subquery()?;
237+
while let Ok(join_type) = self.parse_from_clause_join_type() {
238+
let rhs = self.parse_from_clause_subquery()?;
245239
join_tables.push(JoinTable {
246240
join_type,
247241
table: Box::new(rhs),
248-
constraints: self.parse_select_from_clause_join_constraints()?,
242+
constraints: self.parse_from_clause_join_constraints()?,
249243
});
250244
}
251245

@@ -259,7 +253,7 @@ impl<'a> SelectStatementParser for Parser<'a> {
259253
}))
260254
}
261255

262-
fn parse_select_from_clause_join_type(&mut self) -> Result<JoinType, ParsingError> {
256+
fn parse_from_clause_join_type(&mut self) -> Result<JoinType, ParsingError> {
263257
let natural_join = self.consume_as_keyword(Keyword::Natural).is_ok();
264258

265259
if self.consume_as(TokenType::Comma).is_ok() {
@@ -316,7 +310,7 @@ impl<'a> SelectStatementParser for Parser<'a> {
316310
}
317311
}
318312

319-
fn parse_select_from_clause_join_constraints(
313+
fn parse_from_clause_join_constraints(
320314
&mut self,
321315
) -> Result<Option<JoinConstraint>, ParsingError> {
322316
if self.consume_as_keyword(Keyword::On).is_ok() {

0 commit comments

Comments
 (0)