Skip to content

Commit d88f346

Browse files
committedDec 19, 2024
sql_parser: create trigger statement parser implemented
1 parent 6de1a99 commit d88f346

File tree

10 files changed

+502
-36
lines changed

10 files changed

+502
-36
lines changed
 

‎src/ast/create.rs

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::fmt::Display;
22

3+
use crate::ParsingError;
4+
35
use super::{
46
ColumnDefinition, ConflictClause, DeleteStatement, Expression, ForeignKeyClause, Identifier,
5-
IndexedColumn, InsertStatement, SelectStatement, UpdateStatement,
7+
IndexedColumn, InsertStatement, SelectStatement, Statement, UpdateStatement,
68
};
79

810
/// An AST for [CREATE VIRTUAL TABLE](https://www.sqlite.org/lang_createvtab.html) SQL statement.
@@ -110,7 +112,7 @@ impl Display for TableOption {
110112
}
111113

112114
/// An AST for [CREATE TRIGGER](https://www.sqlite.org/lang_createtrigger.html) SQL statement.
113-
#[derive(Debug, PartialEq)]
115+
#[derive(Debug, PartialEq, Clone)]
114116
pub struct CreateTriggerStatement {
115117
pub temporary: bool,
116118

@@ -129,7 +131,7 @@ pub struct CreateTriggerStatement {
129131
pub trigger_statements: Vec<TriggerStatement>,
130132
}
131133

132-
#[derive(Debug, PartialEq)]
134+
#[derive(Debug, PartialEq, Clone)]
133135
pub enum TriggerPreCondition {
134136
Before,
135137

@@ -138,21 +140,31 @@ pub enum TriggerPreCondition {
138140
InsteadOf,
139141
}
140142

141-
#[derive(Debug, PartialEq)]
143+
impl Display for TriggerPreCondition {
144+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
145+
match self {
146+
TriggerPreCondition::Before => write!(f, "BEFORE"),
147+
TriggerPreCondition::After => write!(f, "AFTER"),
148+
TriggerPreCondition::InsteadOf => write!(f, "INSTEAD OF"),
149+
}
150+
}
151+
}
152+
153+
#[derive(Debug, PartialEq, Clone)]
142154
pub struct TriggerEvent {
143155
pub event_type: TriggerEventType,
144156

145157
pub table_name: Identifier,
146158
}
147159

148-
#[derive(Debug, PartialEq)]
160+
#[derive(Debug, PartialEq, Clone)]
149161
pub enum TriggerEventType {
150162
Delete,
151163
Insert,
152164
Update(Option<Vec<Identifier>>),
153165
}
154166

155-
#[derive(Debug, PartialEq)]
167+
#[derive(Debug, PartialEq, Clone)]
156168
pub enum TriggerStatement {
157169
Update(UpdateStatement),
158170

@@ -162,3 +174,20 @@ pub enum TriggerStatement {
162174

163175
Select(SelectStatement),
164176
}
177+
178+
impl TryFrom<Statement> for TriggerStatement {
179+
type Error = ParsingError;
180+
181+
fn try_from(statement: Statement) -> Result<Self, Self::Error> {
182+
match statement {
183+
Statement::Update(update_statement) => Ok(TriggerStatement::Update(update_statement)),
184+
Statement::Insert(insert_statement) => Ok(TriggerStatement::Insert(insert_statement)),
185+
Statement::Delete(delete_statement) => Ok(TriggerStatement::Delete(delete_statement)),
186+
Statement::Select(select_statement) => Ok(TriggerStatement::Select(select_statement)),
187+
_ => Err(ParsingError::UnexpectedParsingState(format!(
188+
"Expected UPDATE, INSERT, DELETE or SELECT statement, got: {:?}",
189+
statement
190+
))),
191+
}
192+
}
193+
}

‎src/ast/insert.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{
44
};
55

66
/// An AST for [INSERT](https://www.sqlite.org/lang_insert.html) SQL statement.
7-
#[derive(Debug, PartialEq)]
7+
#[derive(Debug, PartialEq, Clone)]
88
pub struct InsertStatement {
99
pub conflict_clause: ConflictClause,
1010

@@ -22,7 +22,7 @@ pub struct InsertStatement {
2222
}
2323

2424
/// The values to insert into the table.
25-
#[derive(Debug, PartialEq)]
25+
#[derive(Debug, PartialEq, Clone)]
2626
pub enum InsertValues {
2727
/// The values to insert into the table.
2828
Values(Vec<Vec<Expression>>),
@@ -35,15 +35,15 @@ pub enum InsertValues {
3535
}
3636

3737
/// The upsert clause to update the table with the new values.
38-
#[derive(Debug, PartialEq)]
38+
#[derive(Debug, PartialEq, Clone)]
3939
pub struct UpsertClause {
4040
pub conflict_target: Option<UpsertConflictTarget>,
4141
pub action: UpsertAction,
4242
}
4343

4444
/// The [conflict target](https://www.sqlite.org/lang_upsert.html#upsert_conflict_target)
4545
/// to update the table with the new values.
46-
#[derive(Debug, PartialEq)]
46+
#[derive(Debug, PartialEq, Clone)]
4747
pub struct UpsertConflictTarget {
4848
pub columns: Vec<IndexedColumn>,
4949
pub where_clause: Option<Box<Expression>>,
@@ -58,7 +58,7 @@ pub struct IndexedColumn {
5858
}
5959

6060
/// The [action](https://www.sqlite.org/lang_upsert.html#upsert_action) to take when a conflict occurs.
61-
#[derive(Debug, PartialEq)]
61+
#[derive(Debug, PartialEq, Clone)]
6262
pub enum UpsertAction {
6363
/// The action to take when a conflict occurs.
6464
Nothing,
@@ -68,7 +68,7 @@ pub enum UpsertAction {
6868
}
6969

7070
/// The [update set](https://www.sqlite.org/lang_upsert.html) action to take when a conflict occurs.
71-
#[derive(Debug, PartialEq)]
71+
#[derive(Debug, PartialEq, Clone)]
7272
pub struct UpsertUpdate {
7373
pub set_clauses: Vec<SetClause>,
7474
pub where_clause: Option<Box<Expression>>,

‎src/parser/create/create_table.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<'a> CreateTableStatementParser for Parser<'a> {
152152
Keyword::Foreign => {
153153
self.consume_as_keyword(Keyword::Foreign)?;
154154
self.consume_as_keyword(Keyword::Key)?;
155-
let columns = self.parse_columns()?;
155+
let columns = self.parse_columns_names()?;
156156
let foreign_key_clause = self.parse_foreign_key_clause()?;
157157
Ok(TableConstraintType::ForeignKey(columns, foreign_key_clause))
158158
}

0 commit comments

Comments
 (0)
Please sign in to comment.