Table

Trait Table 

Source
pub trait Table: TableInternal {
    type Row: SpacetimeType + Serialize + DeserializeOwned + Sized + 'static;
    type UniqueConstraintViolation: MaybeError<UniqueConstraintViolation>;
    type AutoIncOverflow: MaybeError<AutoIncOverflow>;

    // Provided methods
    fn count(&self) -> u64 { ... }
    fn iter(&self) -> impl Iterator<Item = Self::Row> { ... }
    fn insert(&self, row: Self::Row) -> Self::Row { ... }
    fn try_insert(
        &self,
        row: Self::Row,
    ) -> Result<Self::Row, TryInsertError<Self>> { ... }
    fn delete(&self, row: Self::Row) -> bool { ... }
}
Expand description

Implemented for every TableHandle struct generated by the table macro. Contains methods that are present for every table, regardless of what unique constraints and indexes are present.

To get a TableHandle

Required Associated Types§

Source

type Row: SpacetimeType + Serialize + DeserializeOwned + Sized + 'static

The type of rows stored in this table.

Source

type UniqueConstraintViolation: MaybeError<UniqueConstraintViolation>

The error type for this table for unique constraint violations. Will either be UniqueConstraintViolation if the table has any unique constraints, or Infallible otherwise.

Source

type AutoIncOverflow: MaybeError<AutoIncOverflow>

The error type for this table for auto-increment overflows. Will either be AutoIncOverflow if the table has any auto-incrementing columns, or Infallible otherwise.

Provided Methods§

Source

fn count(&self) -> u64

Returns the number of rows of this table.

This takes into account modifications by the current transaction, even though those modifications have not yet been committed or broadcast to clients. This applies generally to insertions, deletions, updates, and iteration as well.

Source

fn iter(&self) -> impl Iterator<Item = Self::Row>

Iterate over all rows of the table.

For large tables, this can be a slow operation! Prefer filtering a RangedIndex or finding a UniqueColumn if possible.

(This keeps track of changes made to the table since the start of this reducer invocation. For example, if rows have been deleted since the start of this reducer invocation, those rows will not be returned by iter. Similarly, inserted rows WILL be returned.)

Source

fn insert(&self, row: Self::Row) -> Self::Row

Inserts row into the table.

The return value is the inserted row, with any auto-incrementing columns replaced with computed values. The insert method always returns the inserted row, even when the table contains no auto-incrementing columns.

(The returned row is a copy of the row in the database. Modifying this copy does not directly modify the database. See UniqueColumn::update if you want to update the row.)

May panic if inserting the row violates any constraints. Callers which intend to handle constraint violation errors should instead use Self::try_insert.

Inserting an exact duplicate of a row already present in the table is a no-op, as SpacetimeDB is a set-semantic database. This is true even for tables with unique constraints; inserting an exact duplicate of an already-present row will not panic.

Source

fn try_insert(&self, row: Self::Row) -> Result<Self::Row, TryInsertError<Self>>

Counterpart to Self::insert which allows handling failed insertions.

For tables with constraints, this method returns an Err when the insertion fails rather than panicking. For tables without any constraints, Self::UniqueConstraintViolation and Self::AutoIncOverflow will be std::convert::Infallible, and this will be a more-verbose Self::insert.

Inserting an exact duplicate of a row already present in the table is a no-op and returns Ok, as SpacetimeDB is a set-semantic database. This is true even for tables with unique constraints; inserting an exact duplicate of an already-present row will return Ok.

Source

fn delete(&self, row: Self::Row) -> bool

Deletes a row equal to row from the table.

Returns true if the row was present and has been deleted, or false if the row was not present and therefore the tables have not changed.

Unlike Self::insert, there is no need to return the deleted row, as it must necessarily have been exactly equal to the row argument. No analogue to auto-increment placeholders exists for deletions.

May panic if deleting the row violates any constraints.

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§