Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Triggers #824

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Triggers #824

wants to merge 2 commits into from

Conversation

gronke
Copy link

@gronke gronke commented Oct 5, 2024

Issue #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

  • Create and drop TRIGGER
  • Named and unnamed triggers (name derived from table, action and time)
  • Concatenate list of statements

TBD

  • Missing statement implementations
    • IF-ELSE statements
    • LOOP
    • custom SQL errors
  • Use wrappers around triggers to polyfill CKECK constraints for MySQL and Sqlite
  • Buil unit-tests for PostgreSQL and SQlite3
  • Address IF NOT EXISTS differences between database backends + write unit-tests
  • Integration testing against live backends before engaging in fancy feature wrappers

@gronke gronke marked this pull request as draft October 5, 2024 11:57
@gronke gronke changed the title [WIP] Triggers Triggers Oct 5, 2024
self.prepare_table_ref_iden(&trigger_ref, sql);
write!(sql, " {} {} ON ", create.trigger.time, create.trigger.event).unwrap();
self.prepare_table_ref_iden(&create.trigger.table, sql);
write!(sql, " FOR EACH ROW\nBEGIN\n").unwrap();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have statement level triggers although they aren't available in mysql.

@gronke gronke mentioned this pull request Oct 13, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants