Database

Struct Database 

Source
pub struct Database<S> { /* private fields */ }
Expand description

Database is a proof that the database has been configured.

Creating a Database requires going through the steps to migrate an existing database to the required schema, or creating a new database from scratch (See also crate::migration::Config). Please see Database::migrator to get started.

Having done the setup to create a compatible database is sadly not a guarantee that the database will stay compatible for the lifetime of the Database struct. That is why Database also stores the schema_version. This allows detecting non-malicious modifications to the schema and gives us the ability to panic when this is detected. Such non-malicious modification of the schema can happen for example if another Database instance is created with additional migrations (e.g. by another newer instance of your program).

Implementations§

Source§

impl<S: Schema> Database<S>

Source

pub fn migrator(config: Config) -> Option<Migrator<S>>

Create a Migrator to migrate a database.

Returns None if the database user_version on disk is older than S.

Source§

impl<S: Send + Sync + Schema> Database<S>

Source

pub fn transaction<R: Send>( &self, f: impl Send + FnOnce(&'static Transaction<S>) -> R, ) -> R

Create a Transaction. Creating the transaction will not block by default.

This function will panic if the schema was modified compared to when the Database value was created. This can happen for example by running another instance of your program with additional migrations.

Note that many systems have a limit on the number of file descriptors that can exist in a single process. On my machine the soft limit is (1024) by default. If this limit is reached, it may cause a panic in this method.

Source

pub fn transaction_mut<O: Send, E: Send>( &self, f: impl Send + FnOnce(&'static mut Transaction<S>) -> Result<O, E>, ) -> Result<O, E>

Create a mutable Transaction. This operation needs to wait for all other mutable Transactions for this database to be finished. There is currently no timeout on this operation, so it will wait indefinitly if required.

Whether the transaction is commited depends on the result of the closure. The transaction is only commited if the closure return Ok. In the case that it returns Err or when the closure panics, a rollback is performed.

This function will panic if the schema was modified compared to when the Database value was created. This can happen for example by running another instance of your program with additional migrations.

Note that many systems have a limit on the number of file descriptors that can exist in a single process. On my machine the soft limit is (1024) by default. If this limit is reached, it may cause a panic in this method.

Source

pub fn transaction_mut_ok<R: Send>( &self, f: impl Send + FnOnce(&'static mut Transaction<S>) -> R, ) -> R

Same as Self::transaction_mut, but always commits the transaction.

The only exception is that if the closure panics, a rollback is performed.

Source

pub fn rusqlite_connection(&self) -> Connection

Create a new rusqlite::Connection to the database.

You can do (almost) anything you want with this connection as it is almost completely isolated from all other rust_query connections. The only thing you should not do here is changing the schema. Schema changes are detected with the schema_version pragma and will result in a panic when creating a new rust_query transaction.

The foreign_keys pragma is always enabled here, even if [crate::migrate::ForeignKeys::SQLite] is not used.

Note that many systems have a limit on the number of file descriptors that can exist in a single process. On my machine the soft limit is (1024) by default. If this limit is reached, it may cause a panic in this method.

Auto Trait Implementations§

§

impl<S> !Freeze for Database<S>

§

impl<S> !RefUnwindSafe for Database<S>

§

impl<S> Send for Database<S>
where S: Send,

§

impl<S> Sync for Database<S>
where S: Sync,

§

impl<S> Unpin for Database<S>
where S: Unpin,

§

impl<S> !UnwindSafe for Database<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V