sea-query
sea-query copied to clipboard
Triggers
Issue https://github.com/SeaQL/sea-query/issues/403 discusses ideas and interfaces to implement SQL triggers. Inspired by this conversation, this pull-request suggests a way to implement triggers.
This PR is in an early stage and meant to provide surface for discussion and tinkering.
// the most simple way to create a trigger
let unnamed_trigger: UnnamedTrigger = UnnamedTrigger::new();
// it can be named later, and become a NamedTrigger
let named_trigger: NamedTrigger = unnamed_trigger.name("my_trigger");
// this is useful because it allows dropping directly
let drop_sql: String = named_trigger.drop().to_string(MysqlQueryBuilder);
// with a table, action and action time we dare do derive a trigger name
let trigger: DefinedTrigger = unnamed_trigger.before_insert(Glyph::Table);
assert_eq!(trigger.trigger_name(), "t_glyph_before_insert");
// demo expression, blocked by missing features (e.g. IF-ELSE statements)
let action: SimpleExpr = Expr::col(Glyph::Id).eq(1);
// multiple actions will be executed in order of insertion
trigger.actions.push(action);
println!(trigger.create().to_string(MysqlQueryBuilder));
CREATE TRIGGER `t_glyph_before_insert` BEFORE INSERT ON `glyph` FOR EACH ROW
BEGIN
`id` = 1;
END
The action expression (between BEGIN and END) does not make sense yet and exists to outline the idea. With new features to express logic, variable declaration, loops and custom errors this could be achieved though.
New Features
- [x] Create and drop TRIGGER
- [x] Named and unnamed triggers (name derived from table, action and time)
- [x] Concatenate list of statements
TBD
- Related statement implementations
- [x] IF-ELSE statements #828
- [ ] LOOP
- [x] custom SQL errors #829
- [ ] Use wrappers around triggers to polyfill
CKECKconstraints for MySQL and Sqlite - [ ] Buil unit-tests for PostgreSQL and SQlite3
- [ ] Address
IF NOT EXISTSdifferences between database backends + write unit-tests - [ ] Integration testing against live backends before engaging in fancy feature wrappers