Entity

Trait Entity 

Source
pub trait Entity {
    type PrimaryKey<'a>;

Show 20 methods // Required methods fn table() -> &'static TableRef; fn columns() -> &'static [ColumnDef]; fn primary_key_def() -> &'static [&'static ColumnDef]; fn primary_key(&self) -> Self::PrimaryKey<'_>; fn unique_defs( ) -> impl ExactSizeIterator<Item = impl ExactSizeIterator<Item = &'static ColumnDef>>; fn row_filtered(&self) -> Box<[(&'static str, Value)]>; fn row_full(&self) -> Row; fn from_row(row: RowLabeled) -> Result<Self> where Self: Sized; fn find_pk( executor: &mut impl Executor, primary_key: &Self::PrimaryKey<'_>, ) -> impl Future<Output = Result<Option<Self>>> + Send where Self: Sized; fn delete_one( executor: &mut impl Executor, primary_key: Self::PrimaryKey<'_>, ) -> impl Future<Output = Result<RowsAffected>> + Send where Self: Sized; // Provided methods fn create_table( executor: &mut impl Executor, if_not_exists: bool, create_schema: bool, ) -> impl Future<Output = Result<()>> + Send where Self: Sized { ... } fn drop_table( executor: &mut impl Executor, if_exists: bool, drop_schema: bool, ) -> impl Future<Output = Result<()>> + Send where Self: Sized { ... } fn insert_one( executor: &mut impl Executor, entity: &impl Entity, ) -> impl Future<Output = Result<RowsAffected>> + Send { ... } fn insert_many<'a, It>( executor: &mut impl Executor, items: It, ) -> impl Future<Output = Result<RowsAffected>> + Send where Self: 'a + Sized, It: IntoIterator<Item = &'a Self> + Send, <It as IntoIterator>::IntoIter: Send { ... } fn prepare_find<Exec: Executor>( executor: &mut Exec, condition: &impl Expression, limit: Option<u32>, ) -> impl Future<Output = Result<Query<Exec::Driver>>> { ... } fn find_one( executor: &mut impl Executor, condition: &impl Expression, ) -> impl Future<Output = Result<Option<Self>>> + Send where Self: Sized { ... } fn find_many( executor: &mut impl Executor, condition: impl Expression, limit: Option<u32>, ) -> impl Stream<Item = Result<Self>> + Send where Self: Sized { ... } fn delete_many( executor: &mut impl Executor, condition: &impl Expression, ) -> impl Future<Output = Result<RowsAffected>> + Send where Self: Sized { ... } fn save( &self, executor: &mut impl Executor, ) -> impl Future<Output = Result<()>> + Send where Self: Sized { ... } fn delete( &self, executor: &mut impl Executor, ) -> impl Future<Output = Result<()>> + Send where Self: Sized { ... }
}
Expand description

A table-mapped record with schema and CRUD helpers.

Required Associated Types§

Source

type PrimaryKey<'a>

Primary key type. Tuple of the types of the fields forming the primary key.

Required Methods§

Source

fn table() -> &'static TableRef

Returns the table reference backing this entity.

Source

fn columns() -> &'static [ColumnDef]

Returns all declared column definitions in declaration order.

Source

fn primary_key_def() -> &'static [&'static ColumnDef]

Iterator over columns forming the primary key. Empty iterator means no PK.

Source

fn primary_key(&self) -> Self::PrimaryKey<'_>

Extracts the primary key value(s) from self.

Source

fn unique_defs() -> impl ExactSizeIterator<Item = impl ExactSizeIterator<Item = &'static ColumnDef>>

Returns an iterator over unique constraint definitions.

Source

fn row_filtered(&self) -> Box<[(&'static str, Value)]>

Returns a filtered mapping of column name to value, typically excluding auto-generated or default-only columns.

Source

fn row_full(&self) -> Row

Returns a full Row representation including all persisted columns.

Source

fn from_row(row: RowLabeled) -> Result<Self>
where Self: Sized,

Constructs Self from a labeled database row.

Error if mandatory columns are missing or type conversion fails.

Source

fn find_pk( executor: &mut impl Executor, primary_key: &Self::PrimaryKey<'_>, ) -> impl Future<Output = Result<Option<Self>>> + Send
where Self: Sized,

Finds an entity by primary key.

Returns Ok(None) if no row matches.

Source

fn delete_one( executor: &mut impl Executor, primary_key: Self::PrimaryKey<'_>, ) -> impl Future<Output = Result<RowsAffected>> + Send
where Self: Sized,

Deletes exactly one entity by primary key.

Returns rows affected (0 if not found).

Provided Methods§

Source

fn create_table( executor: &mut impl Executor, if_not_exists: bool, create_schema: bool, ) -> impl Future<Output = Result<()>> + Send
where Self: Sized,

Creates the underlying table (and optionally schema) if requested.

Parameters:

  • if_not_exists: guards against existing table (if drivers support it, otherwise just create table).
  • create_schema: attempt to create schema prior to table creation (if drivers support it).
Source

fn drop_table( executor: &mut impl Executor, if_exists: bool, drop_schema: bool, ) -> impl Future<Output = Result<()>> + Send
where Self: Sized,

Drops the underlying table (and optionally schema) if requested.

Parameters:

  • if_exists: guards against missing table (if drivers support it, otherwise just drop table).
  • drop_schema: attempt to drop schema after table removal (if drivers support it).
Source

fn insert_one( executor: &mut impl Executor, entity: &impl Entity, ) -> impl Future<Output = Result<RowsAffected>> + Send

Inserts a single entity row.

Returns rows affected (expected: 1 on success).

Source

fn insert_many<'a, It>( executor: &mut impl Executor, items: It, ) -> impl Future<Output = Result<RowsAffected>> + Send
where Self: 'a + Sized, It: IntoIterator<Item = &'a Self> + Send, <It as IntoIterator>::IntoIter: Send,

Multiple insert for a homogeneous iterator of entities.

Returns the number of rows inserted.

Source

fn prepare_find<Exec: Executor>( executor: &mut Exec, condition: &impl Expression, limit: Option<u32>, ) -> impl Future<Output = Result<Query<Exec::Driver>>>

Prepare (but do not yet run) a SQL select query.

Returns the prepared statement.

Source

fn find_one( executor: &mut impl Executor, condition: &impl Expression, ) -> impl Future<Output = Result<Option<Self>>> + Send
where Self: Sized,

Finds the first entity matching a condition expression.

Returns Ok(None) if no row matches.

Source

fn find_many( executor: &mut impl Executor, condition: impl Expression, limit: Option<u32>, ) -> impl Stream<Item = Result<Self>> + Send
where Self: Sized,

Streams entities matching a condition.

limit restricts the maximum number of rows returned at a database level if Some (if supported by the driver, unlimited otherwise).

Source

fn delete_many( executor: &mut impl Executor, condition: &impl Expression, ) -> impl Future<Output = Result<RowsAffected>> + Send
where Self: Sized,

Deletes all entities matching a condition.

Returns the number of rows deleted.

Source

fn save( &self, executor: &mut impl Executor, ) -> impl Future<Output = Result<()>> + Send
where Self: Sized,

Saves the entity (insert or update if available) based on primary key presence.

Errors:

  • Missing PK in the table.
  • Execution failures from underlying driver.
Source

fn delete( &self, executor: &mut impl Executor, ) -> impl Future<Output = Result<()>> + Send
where Self: Sized,

Deletes this entity instance via its primary key.

Errors:

  • Missing PK in the table.
  • If not exactly one row was deleted.
  • Execution failures from underlying driver.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§