Skip to main content

CreateTriggerStatement

Struct CreateTriggerStatement 

Source
pub struct CreateTriggerStatement { /* private fields */ }
Expand description

CREATE TRIGGER statement builder

This struct provides a fluent API for constructing CREATE TRIGGER queries.

§Backend Support

  • PostgreSQL: Full support (BEFORE/AFTER/INSTEAD OF, FOR EACH ROW/STATEMENT, WHEN clause)
  • MySQL: Basic support (BEFORE/AFTER, FOR EACH ROW only, FOLLOWS/PRECEDES)
  • SQLite: Basic support (BEFORE/AFTER/INSTEAD OF, FOR EACH ROW, WHEN clause)

§Examples

use reinhardt_query::prelude::*;
use reinhardt_query::types::{TriggerEvent, TriggerTiming, TriggerScope, TriggerBody};

// PostgreSQL: Call a function when a row is inserted
let query = Query::create_trigger()
    .name("audit_insert")
    .timing(TriggerTiming::After)
    .event(TriggerEvent::Insert)
    .on_table("users")
    .for_each(TriggerScope::Row)
    .execute_function("audit_log_insert");

// MySQL: Multiple statements on update
let query = Query::create_trigger()
    .name("update_timestamp")
    .timing(TriggerTiming::Before)
    .event(TriggerEvent::Update { columns: None })
    .on_table("users")
    .for_each(TriggerScope::Row)
    .body(TriggerBody::multiple(vec![
        "SET NEW.updated_at = NOW()",
    ]));

Implementations§

Source§

impl CreateTriggerStatement

Source

pub fn new() -> Self

Create a new CREATE TRIGGER statement

Source

pub fn take(&mut self) -> Self

Take the ownership of data in the current CreateTriggerStatement

Source

pub fn name<N>(&mut self, name: N) -> &mut Self
where N: IntoIden,

Set the trigger name

§Examples
use reinhardt_query::prelude::*;

let query = Query::create_trigger()
    .name("audit_insert");
Source

pub fn timing(&mut self, timing: TriggerTiming) -> &mut Self

Set the trigger timing (BEFORE, AFTER, INSTEAD OF)

§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::TriggerTiming;

let query = Query::create_trigger()
    .timing(TriggerTiming::Before);
Source

pub fn event(&mut self, event: TriggerEvent) -> &mut Self

Add a trigger event (INSERT, UPDATE, DELETE)

Can be called multiple times to add multiple events (PostgreSQL only).

§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::TriggerEvent;

let query = Query::create_trigger()
    .event(TriggerEvent::Insert)
    .event(TriggerEvent::Update { columns: None });
Source

pub fn on_table<T>(&mut self, table: T) -> &mut Self
where T: IntoTableRef,

Set the table on which the trigger operates

§Examples
use reinhardt_query::prelude::*;

let query = Query::create_trigger()
    .on_table("users");
Source

pub fn for_each(&mut self, scope: TriggerScope) -> &mut Self

Set the trigger scope (FOR EACH ROW or FOR EACH STATEMENT)

§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::TriggerScope;

let query = Query::create_trigger()
    .for_each(TriggerScope::Row);
Source

pub fn when_condition(&mut self, condition: SimpleExpr) -> &mut Self

Set the WHEN condition (optional filter for when trigger fires)

§Examples
use reinhardt_query::prelude::*;

let query = Query::create_trigger()
    .when_condition(Expr::col("status").eq("active"));
Source

pub fn body(&mut self, body: TriggerBody) -> &mut Self

Set the trigger body (SQL statements to execute)

§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::TriggerBody;

let query = Query::create_trigger()
    .body(TriggerBody::single("UPDATE counters SET count = count + 1"));
Source

pub fn execute_function<S: Into<String>>( &mut self, function_name: S, ) -> &mut Self

Set the PostgreSQL function to execute (EXECUTE FUNCTION function_name())

This is a convenience method for PostgreSQL triggers.

§Examples
use reinhardt_query::prelude::*;

let query = Query::create_trigger()
    .execute_function("audit_log_insert");
Source

pub fn order(&mut self, order: TriggerOrder) -> &mut Self

Set the MySQL trigger order (FOLLOWS or PRECEDES)

MySQL-specific feature to control trigger execution order.

§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::TriggerOrder;

let query = Query::create_trigger()
    .order(TriggerOrder::Follows("other_trigger".to_string()));

Trait Implementations§

Source§

impl Clone for CreateTriggerStatement

Source§

fn clone(&self) -> CreateTriggerStatement

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CreateTriggerStatement

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CreateTriggerStatement

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl QueryStatementBuilder for CreateTriggerStatement

Source§

fn build_any(&self, query_builder: &dyn QueryBuilderTrait) -> (String, Values)

Build SQL statement for a database backend and collect query parameters Read more
Source§

fn to_string<T: QueryBuilderTrait>(&self, query_builder: T) -> String

Build SQL statement for a database backend and return SQL string with values inlined as SQL literals. Read more
Source§

fn build<T: QueryBuilderTrait>(&self, query_builder: T) -> (String, Values)

Build SQL statement with parameter collection Read more
Source§

impl QueryStatementWriter for CreateTriggerStatement

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.