sqlite3-parser: separate boxed TriggerCmd struct variants

This commit is contained in:
Jussi Saurio
2025-02-09 12:52:30 +02:00
parent af920a317c
commit f75aca67bb
3 changed files with 67 additions and 56 deletions

View File

@@ -1651,13 +1651,14 @@ impl ToTokens for TriggerEvent {
impl ToTokens for TriggerCmd {
fn to_tokens<S: TokenStream>(&self, s: &mut S) -> Result<(), S::Error> {
match self {
Self::Update {
or_conflict,
tbl_name,
sets,
from,
where_clause,
} => {
Self::Update(update) => {
let TriggerCmdUpdate {
or_conflict,
tbl_name,
sets,
from,
where_clause,
} = &**update;
s.append(TK_UPDATE, None)?;
if let Some(or_conflict) = or_conflict {
s.append(TK_OR, None)?;
@@ -1676,14 +1677,15 @@ impl ToTokens for TriggerCmd {
}
Ok(())
}
Self::Insert {
or_conflict,
tbl_name,
col_names,
select,
upsert,
returning,
} => {
Self::Insert(insert) => {
let TriggerCmdInsert {
or_conflict,
tbl_name,
col_names,
select,
upsert,
returning,
} = &**insert;
if let Some(ResolveType::Replace) = or_conflict {
s.append(TK_REPLACE, None)?;
} else {
@@ -1710,14 +1712,11 @@ impl ToTokens for TriggerCmd {
}
Ok(())
}
Self::Delete {
tbl_name,
where_clause,
} => {
Self::Delete(delete) => {
s.append(TK_DELETE, None)?;
s.append(TK_FROM, None)?;
tbl_name.to_tokens(s)?;
if let Some(where_clause) = where_clause {
delete.tbl_name.to_tokens(s)?;
if let Some(where_clause) = &delete.where_clause {
s.append(TK_WHERE, None)?;
where_clause.to_tokens(s)?;
}

View File

@@ -1640,44 +1640,56 @@ pub enum TriggerEvent {
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TriggerCmd {
/// `UPDATE`
Update {
/// `OR`
or_conflict: Option<ResolveType>,
/// table name
tbl_name: Name,
/// `SET` assignments
sets: Vec<Set>,
/// `FROM`
from: Option<FromClause>,
/// `WHERE` clause
where_clause: Option<Expr>,
},
Update(Box<TriggerCmdUpdate>),
/// `INSERT`
Insert {
/// `OR`
or_conflict: Option<ResolveType>,
/// table name
tbl_name: Name,
/// `COLUMNS`
col_names: Option<DistinctNames>,
/// `SELECT` or `VALUES`
select: Box<Select>,
/// `ON CONLICT` clause
upsert: Option<Upsert>,
/// `RETURNING`
returning: Option<Vec<ResultColumn>>,
},
Insert(Box<TriggerCmdInsert>),
/// `DELETE`
Delete {
/// table name
tbl_name: Name,
/// `WHERE` clause
where_clause: Option<Expr>,
},
Delete(Box<TriggerCmdDelete>),
/// `SELECT`
Select(Box<Select>),
}
/// `UPDATE` trigger command
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TriggerCmdUpdate {
/// `OR`
pub or_conflict: Option<ResolveType>,
/// table name
pub tbl_name: Name,
/// `SET` assignments
pub sets: Vec<Set>,
/// `FROM`
pub from: Option<FromClause>,
/// `WHERE` clause
pub where_clause: Option<Expr>,
}
/// `INSERT` trigger command
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TriggerCmdInsert {
/// `OR`
pub or_conflict: Option<ResolveType>,
/// table name
pub tbl_name: Name,
/// `COLUMNS`
pub col_names: Option<DistinctNames>,
/// `SELECT` or `VALUES`
pub select: Box<Select>,
/// `ON CONLICT` clause
pub upsert: Option<Upsert>,
/// `RETURNING`
pub returning: Option<Vec<ResultColumn>>,
}
/// `DELETE` trigger command
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TriggerCmdDelete {
/// table name
pub tbl_name: Name,
/// `WHERE` clause
pub where_clause: Option<Expr>,
}
/// Conflict resolution types
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ResolveType {