Skip to main content

Entity

Trait Entity 

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

Show 19 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 primary_key_expr(&self) -> impl Expression; fn unique_defs( ) -> impl ExactSizeIterator<Item = impl ExactSizeIterator<Item = &'static ColumnDef>>; fn row_values(&self) -> RowValues; fn from_row(row: Row) -> Result<Self> where Self: Sized; // Provided methods fn row(&self) -> Row { ... } 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: Sized + 'a, 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>>> + Send { ... } 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

Database entity mapping.

Use #[derive(Entity)] to implement this trait.

Required Associated Types§

Source

type PrimaryKey<'a> where Self: 'a

Primary key type. A tuple of field types (or single type) forming the PK.

Required Methods§

Source

fn table() -> &'static TableRef

Table reference matching the #[tank(...)] attributes.

Source

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

All column definitions in declaration order.

Source

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

Primary key column definitions. Empty if no PK defined.

Source

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

Extract PK value(s) from self.

Source

fn primary_key_expr(&self) -> impl Expression

Build an expression matching the PK of self.

Source

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

Unique constraint definitions.

Source

fn row_values(&self) -> RowValues

Full row representation including all persisted columns.

Source

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

Reconstruct Self from a labeled row.

Fails if columns are missing or type conversion fails.

Provided Methods§

Source

fn row(&self) -> Row

Full row representation with column labels.

Source

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

Create table (and optional schema).

  • if_not_exists: Emits IF NOT EXISTS if supported.
  • create_schema: Attempts schema creation first.
Source

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

Drop the table (and optional schema).

  • if_exists: Emits IF EXISTS if supported.
  • drop_schema: Drops schema after table removal (if empty).
Source

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

Insert a single entity.

Source

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

Bulk insert entities.

Source

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

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 deleted rows.

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".

Implementors§