pub struct DatabaseSession(/* private fields */);Expand description
A cloneable handle to one attached database and its existing storage lease.
The host attaches once using Self::new and hands each caller a clone.
Cloning takes no database borrow and performs no storage access.
Use a handle within one synchronous message execution. Drop explicit read
guards before writing. Reacquire a handle from the host after an await. A handle supplies neither a snapshot nor a transaction
spanning calls. Each batch uses the ordinary enclosing-message commit rules.
Each insert, replace or delete commits a separate batch in the
enclosing message. Submit dependent changes together:
let mut batch = Batch::new();
batch.replace(first)?;
batch.replace(second)?;
session.commit(batch)?;Engine work and output limits apply to each query. Cloning creates no budget. Hosts enforcing a cumulative request allowance must share the same allowance across helpers in that request and establish a fresh one at the next boundary.
Implementations§
Source§impl DatabaseSession
impl DatabaseSession
Sourcepub fn new(database: Database) -> Self
pub fn new(database: Database) -> Self
Wrap an already-created or reopened database. Its allocation remains leased until the last handle is dropped.
Sourcepub fn try_into_database(self) -> Result<Database, Self>
pub fn try_into_database(self) -> Result<Database, Self>
Recover the attached database for a lifecycle operation requiring ownership.
§Errors
Returns this same handle if another handle still owns the attachment.
Sourcepub fn read(&self) -> Result<Ref<'_, Database>, Error>
pub fn read(&self) -> Result<Ref<'_, Database>, Error>
Borrow the database for fluent queries or a sequence of synchronous reads.
A builder from read.query::<E>() borrows this guard. Commits can resume
after the guard is dropped, even if several session handles still exist.
§Errors
Returns Error::BorrowConflict if a commit has exclusive access.
Sourcepub fn query<'a, E: Entity>(&self) -> Query<'a, E>
pub fn query<'a, E: Entity>(&self) -> Query<'a, E>
Build a live query over this attachment. Execution sees writes committed
before the terminal call. An exclusive borrow at execution returns
Error::BorrowConflict. The builder owns a session handle and borrows
only its predicate and cursor inputs.
Sourcepub fn get<E: Entity>(&self, key: E::Key) -> Result<Option<E>, Error>
pub fn get<E: Entity>(&self, key: E::Key) -> Result<Option<E>, Error>
Read an owned entity by primary key, releasing the borrow before returning.
§Errors
Returns a borrow conflict or the original Database::get error.
Sourcepub fn get_many<E: Entity>(
&self,
keys: &[E::Key],
) -> Result<Vec<Option<E>>, Error>
pub fn get_many<E: Entity>( &self, keys: &[E::Key], ) -> Result<Vec<Option<E>>, Error>
Read up to 32 primary keys in input order, preserving duplicates and missing slots.
§Errors
Returns a borrow conflict or the original Database::get_many error.
Sourcepub fn get_unique<K: UniqueKey>(
&self,
key: &K,
) -> Result<Option<K::Entity>, Error>
pub fn get_unique<K: UniqueKey>( &self, key: &K, ) -> Result<Option<K::Entity>, Error>
Look up an owned row using a generated unique key, such as
UserByIssuerAndSubject { issuer, subject }.
§Errors
Propagates session borrow, schema, key admission and row integrity errors.
Sourcepub fn insert_input<I: InsertInput>(&self, input: I) -> Result<I::Entity, Error>
pub fn insert_input<I: InsertInput>(&self, input: I) -> Result<I::Entity, Error>
Sourcepub fn delete<E: Entity>(&self, key: E::Key) -> Result<(), Error>
pub fn delete<E: Entity>(&self, key: E::Key) -> Result<(), Error>
Delete one entity by primary key as one batch.
§Errors
Propagates batch preparation and commit errors.
Sourcepub fn commit(&self, batch: Batch) -> Result<(), Error>
pub fn commit(&self, batch: Batch) -> Result<(), Error>
Commit one batch through the attached database.
§Errors
Returns Error::BorrowConflict before mutation if a borrow is active,
or the original Database::commit error.