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§
Required Methods§
Sourcefn insert_query(setter: Self::Setter) -> Self::Insert
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.
Sourcefn update_query() -> Self::Update
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_setter — after
before_update has had its chance to touch
the setter.
Sourcefn apply_setter(setter: Self::Setter, q: &mut Self::Update)
fn apply_setter(setter: Self::Setter, q: &mut Self::Update)
Turn the set fields into SET assignments on q. Unset fields do not
appear.
Sourcefn delete_query() -> Self::Delete
fn delete_query() -> Self::Delete
The bare DELETE FROM this model’s table.
Provided Methods§
Sourcefn before_insert<'a>(
db: &'a dyn Executor,
setter: &'a mut Self::Setter,
) -> ExecFuture<'a, Result<(), ExecError>>
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.
Sourcefn after_insert<'a>(
db: &'a dyn Executor,
rows: &'a [Self::Row],
) -> ExecFuture<'a, Result<(), ExecError>>
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.
Sourcefn before_update<'a>(
db: &'a dyn Executor,
setter: &'a mut Self::Setter,
) -> ExecFuture<'a, Result<(), ExecError>>
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.
Sourcefn after_update<'a>(
db: &'a dyn Executor,
affected: u64,
) -> ExecFuture<'a, Result<(), ExecError>>
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).
Sourcefn before_delete(db: &dyn Executor) -> ExecFuture<'_, Result<(), ExecError>>
fn before_delete(db: &dyn Executor) -> ExecFuture<'_, Result<(), ExecError>>
Runs before the DELETE.
Sourcefn after_delete<'a>(
db: &'a dyn Executor,
affected: u64,
) -> ExecFuture<'a, Result<(), ExecError>>
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".