Skip to main content

Table

Trait Table 

Source
pub trait Table: View {
    type Pk: Send + 'static;
    type Setter: Default + Send + 'static;
    type Insert: Query + Send + Sync + 'static;
    type Update: Query + Send + Sync + 'static;
    type Delete: Query + Send + Sync + 'static;

    // Required methods
    fn insert_query(setter: Self::Setter) -> Self::Insert;
    fn update_query() -> Self::Update;
    fn apply_setter(setter: Self::Setter, q: &mut Self::Update);
    fn delete_query() -> Self::Delete;
    fn pk(row: &Self::Row) -> Self::Pk;

    // Provided methods
    fn before_insert<'a>(
        db: &'a dyn Executor,
        setter: &'a mut Self::Setter,
    ) -> ExecFuture<'a, Result<(), ExecError>> { ... }
    fn after_insert<'a>(
        db: &'a dyn Executor,
        rows: &'a [Self::Row],
    ) -> ExecFuture<'a, Result<(), ExecError>> { ... }
    fn before_update<'a>(
        db: &'a dyn Executor,
        setter: &'a mut Self::Setter,
    ) -> ExecFuture<'a, Result<(), ExecError>> { ... }
    fn after_update<'a>(
        db: &'a dyn Executor,
        affected: u64,
    ) -> ExecFuture<'a, Result<(), ExecError>> { ... }
    fn before_delete(db: &dyn Executor) -> ExecFuture<'_, Result<(), ExecError>> { ... }
    fn after_delete<'a>(
        db: &'a dyn Executor,
        affected: u64,
    ) -> ExecFuture<'a, Result<(), ExecError>> { ... }
}
Expand description

A writable model: a View with a primary key and the three mutations.

The View/Table split is the surface contract: SELECT-only models implement View alone, and insert/update/delete simply do not exist on them — misuse-resistance by trait bound, not by runtime error.

§What the generator emits per method

The *_query methods are the codegen seam: each returns (or completes) a plain Layer 1 statement of this model’s dialect, so everything the machinery runs is an ordinary Query that raw mods can keep modifying. The hand-written model in keelson-models/tests/ is the byte-for-byte specification of what the generator will write.

§Hooks

Same design as View::after_select: trait default methods, statically dispatched, &dyn Executor in. The before-mutation hooks additionally receive the Setter mutably — stamping a timestamp or normalising a value before it is written is the canonical before-hook, and giving the hook the same three-state Setter the caller used means it can also tell “not mentioned” from “set to NULL”.

Required Associated Types§

Source

type Pk: Send + 'static

The primary key’s Rust type. A composite key is a tuple.

Source

type Setter: Default + Send + 'static

The generated three-state setter struct.

Source

type Insert: Query + Send + Sync + 'static

The dialect’s INSERT statement type.

Source

type Update: Query + Send + Sync + 'static

The dialect’s UPDATE statement type.

Source

type Delete: Query + Send + Sync + 'static

The dialect’s DELETE statement type.

Required Methods§

Source

fn insert_query(setter: Self::Setter) -> Self::Insert

An INSERT of exactly the set fields, RETURNING this model’s columns (on dialects that have RETURNING; see the per-dialect notes in the crate docs). An all-unset setter inserts the row the schema’s defaults describe.

Source

fn update_query() -> Self::Update

The bare UPDATE of this model’s table, with no assignments yet: filters and mods apply to this, and the assignments arrive at run time via apply_setterafter before_update has had its chance to touch the setter.

Source

fn apply_setter(setter: Self::Setter, q: &mut Self::Update)

Turn the set fields into SET assignments on q. Unset fields do not appear.

Source

fn delete_query() -> Self::Delete

The bare DELETE FROM this model’s table.

Source

fn pk(row: &Self::Row) -> Self::Pk

This row’s primary key — what a keyed loader groups by.

Provided Methods§

Source

fn before_insert<'a>( db: &'a dyn Executor, setter: &'a mut Self::Setter, ) -> ExecFuture<'a, Result<(), ExecError>>

Runs before the INSERT is built; may rewrite the setter.

Source

fn after_insert<'a>( db: &'a dyn Executor, rows: &'a [Self::Row], ) -> ExecFuture<'a, Result<(), ExecError>>

Runs after the INSERT, with the returned rows (empty when the insert ran for its side effect only), on the caller’s executor — inside the caller’s transaction when there is one.

Source

fn before_update<'a>( db: &'a dyn Executor, setter: &'a mut Self::Setter, ) -> ExecFuture<'a, Result<(), ExecError>>

Runs before the assignments are built; may rewrite the setter.

Source

fn after_update<'a>( db: &'a dyn Executor, affected: u64, ) -> ExecFuture<'a, Result<(), ExecError>>

Runs after the UPDATE, with how many rows it touched (or returned).

Source

fn before_delete(db: &dyn Executor) -> ExecFuture<'_, Result<(), ExecError>>

Runs before the DELETE.

Source

fn after_delete<'a>( db: &'a dyn Executor, affected: u64, ) -> ExecFuture<'a, Result<(), ExecError>>

Runs after the DELETE, with how many rows it removed (or returned).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§