sea-query
sea-query copied to clipboard
Add support for Postgresql's function and trigger
Motivation
Give users the ablity to create functions and triggers on database by using sea-query
instead of raw SQL.
Proposed Solutions
N/A
Additional Information
https://www.postgresql.org/docs/current/plpgsql-trigger.html
Hey @HigherOrderLogic, thanks for the feature request!
I think creating, dropping and altering of function and trigger can be offered under sea_query::extension::postgres
module.
I'll open this for contributions! :)
@billy1624 @tyt2y3 do we really want to add support for this functionality?
If you are open to contribution for this issue, then I'd like to work on it
Hey @anshulxyz, this is still open for contribution! Please go ahead :D
@billy1624 I'm not sure whether @anshulxyz is still working on this issue or not but I'm going to start working to add support for the trigger
first
@stevenhansel I'm not, you can take over the issue.
Thanks!! @stevenhansel Let me know if you need any help :)
@billy1624 I'm not sure whether @anshulxyz is still working on this issue or not but I'm going to start working to add support for the
trigger
first
Do you have any idea about the API design in mind?
Hey @HigherOrderLogic, you can take this as a reference TypeCreateStatement. We need to create a struct let say TriggerCreateStatement
under extension/postgres
module since this is Postgres specific. Looking at the docs CREATE TRIGGER.
I imagine the API be like...
enum Event {
Insert,
Update(Vec<ColumnRef>),
Delete,
Truncate,
}
Trigger::create()
.or_replace()
.name(NameIden)
.table(TableIden)
// One of the event that fire the trigger
.before_event(Event::Insert)
.after_event(Event::Update(vec![ ... ]))
.instead_of_event(Event::Delete)
// How the trigger function should be fired
.for_each_row()
.for_each_statement()
// Function to execute
.execute_function(Func)
.to_string(PostgresQueryBuilder);
Do you have any idea about the API design in mind?
@HigherOrderLogic I'm mostly thinking the same thing with what @billy1624 wrote, for creating a trigger we can just follow the existing TypeCreateStatement
pattern which I believe fundamentally follows the Builder Pattern. For the other one, such as ALTER
and DROP
, well we also have the same counterparts e.g. TableAlterStatement
, TableDropStatement
(for the tables).
e.g. this is how you would drop a table using seaquery (from the docs)
let table = Table::alter()
.table(Font::Table)
.add_column(
ColumnDef::new(Alias::new("new_col"))
.integer()
.not_null()
.default(100),
)
.to_owned();
Well, this is probably a rough counterpart for the Trigger::drop
, let me know if you have any comments
Trigger::drop()
.if_exists()
.name(NameIden)
.table(TableIden);