Skip to content

Commit

Permalink
prepare trigger actions
Browse files Browse the repository at this point in the history
  • Loading branch information
gronke committed Oct 26, 2024
1 parent 73536a6 commit 7b5136b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
17 changes: 15 additions & 2 deletions src/backend/trigger_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::*;

pub trait TriggerBuilder: TableRefBuilder {
pub trait TriggerBuilder: TableRefBuilder + QueryBuilder {
/// Translate [`TriggerCreateStatement`] into SQL statement.
fn prepare_trigger_create_statement(
&self,
Expand All @@ -21,7 +21,16 @@ pub trait TriggerBuilder: TableRefBuilder {
self.prepare_table_ref_iden(&create.trigger.table, sql);
write!(sql, " FOR EACH ROW\nBEGIN\n").unwrap();

write!(sql, "\nEND").unwrap();
self.prepare_trigger_actions(&create.trigger.actions, sql);

write!(sql, "END").unwrap();
}

fn prepare_trigger_actions(&self, actions: &TriggerActions, sql: &mut dyn SqlWriter) {
for action in actions {
self.prepare_simple_expr_common(&action, sql);
write!(sql, ";\n").unwrap();
}
}

/// Translate IF NOT EXISTS expression in [`TriggerCreateStatement`].
Expand All @@ -45,4 +54,8 @@ pub trait TriggerBuilder: TableRefBuilder {
write!(sql, "DROP TRIGGER ").unwrap();
self.prepare_table_ref_iden(&drop.name.clone().into(), sql);
}

fn prepare_simple_expr_yeah(&self, simple_expr: &SimpleExpr, sql: &mut dyn SqlWriter) {
self.prepare_simple_expr_common(simple_expr, sql);
}
}
27 changes: 15 additions & 12 deletions src/trigger/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Iden, IntoTableRef, SchemaBuilder, SeaRc, TableRef};
use crate::{Iden, IntoTableRef, SchemaBuilder, SeaRc, SimpleExpr, TableRef};
use std::fmt;

mod create;
Expand Down Expand Up @@ -72,15 +72,20 @@ pub trait Configurable {
}
}

pub type TriggerAction = SimpleExpr;
pub type TriggerActions = Vec<TriggerAction>;

#[derive(Default, Debug, Clone)]
pub struct NamedTrigger {
pub(crate) name: TriggerRef
pub(crate) name: TriggerRef,
pub(crate) actions: TriggerActions,
}

impl NamedTrigger {
pub fn new<T: Into<TriggerRef>>(name: T) -> NamedTrigger {
Self {
name: name.into()
name: name.into(),
actions: vec![],
}
}
}
Expand All @@ -107,29 +112,25 @@ impl Configurable for NamedTrigger {
table: table,
event: event,
time: time,
actions: self.actions.clone(),
}
}
}

#[derive(Default, Debug, Clone)]
pub struct UnnamedTrigger {
pub(crate) table: Option<TableRef>,
pub(crate) event: Option<TriggerEvent>,
pub(crate) time: Option<TriggerActionTime>,
pub actions: TriggerActions,
}

impl UnnamedTrigger {
pub fn new() -> UnnamedTrigger {
Self {
table: None,
event: None,
time: None,
}
Self { actions: vec![] }
}
// an unnamed trigger can become a named one
pub fn name<T: Into<TriggerRef>>(&self, name: T) -> NamedTrigger {
NamedTrigger {
name: name.into()
name: name.into(),
actions: self.actions.clone(),
}
}
}
Expand All @@ -146,6 +147,7 @@ impl Configurable for UnnamedTrigger {
table: table,
event: event,
time: time,
actions: self.actions.clone(),
}
}
}
Expand All @@ -156,6 +158,7 @@ pub struct DefinedTrigger {
pub(crate) table: TableRef,
pub(crate) event: TriggerEvent,
pub(crate) time: TriggerActionTime,
pub(crate) actions: TriggerActions,
}

impl Referencable for DefinedTrigger {
Expand Down
24 changes: 22 additions & 2 deletions tests/mysql/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn create_unnamed_trigger() {
[
"CREATE TRIGGER `t_glyph_before_insert`",
"BEFORE INSERT ON `glyph`",
"FOR EACH ROW\nBEGIN\n\nEND",
"FOR EACH ROW\nBEGIN\nEND",
]
.join(" ")
);
Expand All @@ -35,7 +35,7 @@ fn create_named_trigger() {
[
"CREATE TRIGGER `my_trigger`",
"BEFORE INSERT ON `glyph`",
"FOR EACH ROW\nBEGIN\n\nEND",
"FOR EACH ROW\nBEGIN\nEND",
]
.join(" ")
);
Expand All @@ -58,3 +58,23 @@ fn drop_unnamed_trigger() {
"DROP TRIGGER `t_glyph_before_delete`"
);
}

#[test]
fn trigger_actions() {
let mut trigger = UnnamedTrigger::new();
trigger.actions.push(Expr::col(Glyph::Id).eq(1));

assert_eq!(
trigger
.before_insert(Glyph::Table)
.create()
.to_string(MysqlQueryBuilder),
[
"CREATE TRIGGER `t_glyph_before_insert` BEFORE INSERT ON `glyph` FOR EACH ROW",
"BEGIN",
"`id` = 1;",
"END"
]
.join("\n")
);
}

0 comments on commit 7b5136b

Please sign in to comment.