Skip to content

Commit 2a7f966

Browse files
committed
sql_parser: insert statement AST
1 parent 36dddb9 commit 2a7f966

File tree

2 files changed

+73
-10
lines changed

2 files changed

+73
-10
lines changed

README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -80,15 +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=5&suffix=%%%20(0%20of%208)&width=140)
84-
1. insert-with-values
85-
1. insert-with-select
86-
1. insert-with-upsert
87-
1. insert-with-returning
88-
1. insert-with-default
89-
1. insert-with-cte
90-
1. replace-stmt
91-
1. common-table-expressions
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)
9291

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

src/ast/insert.rs

+65-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,67 @@
1+
use super::{
2+
ConflictClause, Expression, Identifier, QualifiedTableName, ReturningClause, SelectStatement,
3+
SetClause,
4+
};
5+
16
/// An AST for [INSERT](https://www.sqlite.org/lang_insert.html) SQL statement.
27
#[derive(Debug, PartialEq)]
3-
pub struct InsertStatement {}
8+
pub struct InsertStatement {
9+
pub conflict_clause: ConflictClause,
10+
11+
/// TODO: Change the type, as QualifiedTableName contains indexing type
12+
/// which is not applicable for insert statements
13+
pub table_name: QualifiedTableName,
14+
15+
pub columns: Vec<Identifier>,
16+
17+
pub values: InsertValues,
18+
19+
pub upsert_clause: Option<Vec<UpsertClause>>,
20+
21+
pub returning_clause: Vec<ReturningClause>,
22+
}
23+
24+
/// The values to insert into the table.
25+
#[derive(Debug, PartialEq)]
26+
pub enum InsertValues {
27+
/// The values to insert into the table.
28+
Values(Vec<Expression>),
29+
30+
/// The values to insert into the table from a select statement.
31+
Select(SelectStatement),
32+
33+
/// The values to insert into the table from a default value.
34+
DefaultValues,
35+
}
36+
37+
/// The upsert clause to update the table with the new values.
38+
#[derive(Debug, PartialEq)]
39+
pub struct UpsertClause {
40+
pub conflict_target: Option<UpsertConflictTarget>,
41+
pub action: UpsertAction,
42+
}
43+
44+
/// The [conflict target](https://www.sqlite.org/lang_upsert.html#upsert_conflict_target)
45+
/// to update the table with the new values.
46+
#[derive(Debug, PartialEq)]
47+
pub struct UpsertConflictTarget {
48+
pub columns: Vec<Identifier>,
49+
pub where_clause: Option<Expression>,
50+
}
51+
52+
/// The [action](https://www.sqlite.org/lang_upsert.html#upsert_action) to take when a conflict occurs.
53+
#[derive(Debug, PartialEq)]
54+
pub enum UpsertAction {
55+
/// The action to take when a conflict occurs.
56+
Nothing,
57+
58+
/// The action to take when a conflict occurs.
59+
Update(UpsertUpdate),
60+
}
61+
62+
/// The [update set](https://www.sqlite.org/lang_upsert.html) action to take when a conflict occurs.
63+
#[derive(Debug, PartialEq)]
64+
pub struct UpsertUpdate {
65+
pub set_clauses: Vec<SetClause>,
66+
pub where_clause: Option<Expression>,
67+
}

0 commit comments

Comments
 (0)