Skip to content

Commit a0376a1

Browse files
committed
sql_parser: replace statement parser
1 parent dd638d6 commit a0376a1

File tree

5 files changed

+35
-25
lines changed

5 files changed

+35
-25
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ The items were taken from the official SQLite documentation
8080
1. [update-stmt](https://www.sqlite.org/syntax/update-stmt.html) ✅
8181
1. [update-stmt-limited](https://www.sqlite.org/syntax/update-stmt-limited.html) ✅
8282

83-
#### INSERT Statement ![progress](https://progress-bar.xyz/0/?scale=7&suffix=%%%20(0%20of%207)&width=140)
84-
1. [insert-with-values](https://www.sqlite.org/lang_insert.html)
85-
1. [insert-as-select](https://www.sqlite.org/lang_insert.html)
86-
1. [insert-with-upsert](https://www.sqlite.org/lang_insert.html)
87-
1. [insert-as-default](https://www.sqlite.org/lang_insert.html)
88-
1. [insert-with-returning](https://www.sqlite.org/lang_insert.html)
89-
1. [insert-with-cte](https://www.sqlite.org/lang_insert.html)
90-
1. [replace-stmt](https://www.sqlite.org/lang_replace.html)
83+
#### INSERT Statement ![progress](https://progress-bar.xyz/7/?scale=7&suffix=%%%20(7%20of%207)&width=140)
84+
1. [insert-with-values](https://www.sqlite.org/lang_insert.html) ✅
85+
1. [insert-as-select](https://www.sqlite.org/lang_insert.html) ✅
86+
1. [insert-with-upsert](https://www.sqlite.org/lang_insert.html) ✅
87+
1. [insert-as-default](https://www.sqlite.org/lang_insert.html) ✅
88+
1. [insert-with-returning](https://www.sqlite.org/lang_insert.html) ✅
89+
1. [insert-with-cte](https://www.sqlite.org/lang_insert.html) ✅
90+
1. [replace-stmt](https://www.sqlite.org/lang_replace.html) ✅
9191

9292
#### CREATE Statements ![progress](https://progress-bar.xyz/0/?scale=45&suffix=%%%20(0%20of%205)&width=140)
9393
1. create-virtual-table-stmt

src/parser/insert/mod.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
2-
parser::select::SelectStatementParser, Identifier, InsertStatement, InsertValues, Keyword,
3-
QualifiedTableName, TokenType, UpsertAction, UpsertClause, UpsertConflictTarget, UpsertUpdate,
2+
parser::select::SelectStatementParser, ConflictClause, Identifier, InsertStatement,
3+
InsertValues, Keyword, QualifiedTableName, TokenType, UpsertAction, UpsertClause,
4+
UpsertConflictTarget, UpsertUpdate,
45
};
56

67
use super::{
@@ -32,6 +33,17 @@ pub trait InsertStatementParser {
3233

3334
impl<'a> InsertStatementParser for Parser<'a> {
3435
fn parse_insert_statement(&mut self) -> Result<InsertStatement, ParsingError> {
36+
if self.consume_as_keyword(Keyword::Replace).is_ok() {
37+
return Ok(InsertStatement {
38+
conflict_clause: ConflictClause::None,
39+
table_name: self.parse_table_name()?,
40+
columns: self.parse_insert_columns()?,
41+
values: self.parse_insert_values()?,
42+
upsert_clause: self.parse_upsert_clauses()?,
43+
returning_clause: self.parse_returning_clause()?,
44+
});
45+
}
46+
3547
self.consume_as_keyword(Keyword::Insert)?;
3648

3749
Ok(InsertStatement {
@@ -45,13 +57,11 @@ impl<'a> InsertStatementParser for Parser<'a> {
4557
}
4658

4759
fn parse_table_name(&mut self) -> Result<QualifiedTableName, ParsingError> {
48-
dbg!("parse_table_name");
4960
self.consume_as_keyword(Keyword::Into)?;
5061
self.parse_qualified_table_name()
5162
}
5263

5364
fn parse_insert_columns(&mut self) -> Result<Vec<Identifier>, ParsingError> {
54-
dbg!("parse_insert_columns");
5565
let mut columns = vec![];
5666

5767
if self.consume_as(TokenType::LeftParen).is_ok() {
@@ -69,7 +79,6 @@ impl<'a> InsertStatementParser for Parser<'a> {
6979
}
7080

7181
fn parse_insert_values(&mut self) -> Result<InsertValues, ParsingError> {
72-
dbg!("parse_insert_values");
7382
if self.consume_as_keyword(Keyword::Values).is_ok() {
7483
let mut values = vec![];
7584

@@ -131,12 +140,7 @@ impl<'a> InsertStatementParser for Parser<'a> {
131140
self.consume_as_keyword(Keyword::Conflict)?;
132141

133142
let conflict_target = self.parse_upsert_conflict_target()?;
134-
dbg!("conflict_target", &conflict_target);
135-
136-
dbg!("parse_upsert_action");
137-
dbg!("self.peek_token()", self.peek_token()?);
138143
let action = self.parse_upsert_action()?;
139-
dbg!("action", &action);
140144

141145
Ok(UpsertClause {
142146
conflict_target,
@@ -176,10 +180,6 @@ impl<'a> InsertStatementParser for Parser<'a> {
176180
}
177181

178182
if self.consume_as_keyword(Keyword::Update).is_ok() {
179-
dbg!("parse_upsert_action update");
180-
dbg!("self.peek_token()", self.peek_token()?);
181-
// self.consume_as_keyword(Keyword::Set)?;
182-
183183
let set_clauses = self.parse_set_clauses()?;
184184

185185
let where_clause = self.parse_where_clause()?;
@@ -509,4 +509,14 @@ mod tests_insert_statement {
509509
Statement::WithCte(expected_statement),
510510
);
511511
}
512+
513+
#[test]
514+
fn test_parse_insert_statement_with_replace() {
515+
let mut expected_statement = insert_statement();
516+
expected_statement.conflict_clause = ConflictClause::None;
517+
run_sunny_day_test(
518+
"REPLACE INTO table1 DEFAULT VALUES",
519+
Statement::Insert(expected_statement),
520+
);
521+
}
512522
}

src/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl<'a> Parser<'a> {
323323
Keyword::Update => {
324324
UpdateStatementParser::parse_update_statement(self).map(Statement::Update)
325325
}
326-
Keyword::Insert => {
326+
Keyword::Insert | Keyword::Replace => {
327327
InsertStatementParser::parse_insert_statement(self).map(Statement::Insert)
328328
}
329329
Keyword::Alter => AlterTableStatementParser::parse_alter_table_statement(self)

tests/bart-riers-sqlite-dataset/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The test dataset is taken from the [Bart Kiers' SQLite ANTLR parser](https://github.com/bkiers/sqlite-parser/tree/master).
44

5-
In the current implementation, there is 70.95% of the tests that are passing.
5+
In the current implementation, there is 74.94% of the tests that are passing.
66
By passing tests, I mean that the parser successfully parses the SQL query and returns a valid `Expression` AST.
77
The structure of the parsed AST is not checked, as there are no expected ASTs list
88

tests/codeschool-sqlite-parser-test-files/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
The queries under `sql/` folder are taken from the [Codeschool SQLite Parser](https://github.com/codeschool/sqlite-parser/tree/master) repository.
44

55

6-
Current test pass rate is 40.95%
6+
Current test pass rate is 42.41%

0 commit comments

Comments
 (0)