Skip to main content

Database

Struct Database 

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

Async SQLite database backed by a deadpool-sqlite connection pool.

Implementations§

Source§

impl Database

Source

pub fn pool(&self) -> &Pool

Returns the underlying SQLite pool for advanced driver-specific use.

Source

pub fn connect(path: impl Into<String>) -> Result<Self, OrmError>

Opens a SQLite database with a pool size of four connections.

Source

pub fn connect_with_pool_size( path: impl Into<String>, pool_size: usize, ) -> Result<Self, OrmError>

Opens a SQLite database with an explicit maximum pool size.

Source

pub fn connect_in_memory() -> Result<Self, OrmError>

Opens an in-memory SQLite database with a single connection.

Source

pub async fn execute(&self, ast: QueryAst) -> Result<ExecuteResult, OrmError>

Executes a compiled AST query asynchronously.

Source

pub async fn create_table<M: Model>(&self) -> Result<ExecuteResult, OrmError>

Creates a model table and its indexes.

Source

pub async fn insert<M: Model>( &self, model: &M, ) -> Result<ExecuteResult, OrmError>

Inserts a model. Primary key and managed timestamp fields are omitted.

Source

pub async fn fetch_all<M: Model + Send + 'static>( &self, query: SelectQuery<M>, ) -> Result<Vec<M>, OrmError>

Fetches all rows matching a typed select query.

Source

pub async fn fetch_one<M: Model + Send + 'static>( &self, query: SelectQuery<M>, ) -> Result<Option<M>, OrmError>

Fetches at most one row matching a typed select query.

Source

pub async fn get<M: Model + Send + 'static>( &self, id: i64, ) -> Result<Option<M>, OrmError>

Fetches a model by its generated integer primary key.

Source

pub async fn all<M: Model + Send + 'static>(&self) -> Result<Vec<M>, OrmError>

Fetches every row in a model’s table.

Source

pub async fn count<M: Model + Send + 'static>(&self) -> Result<usize, OrmError>

Source

pub async fn count_query<M: Model + Send + 'static>( &self, query: SelectQuery<M>, ) -> Result<usize, OrmError>

Source

pub fn create<M: Model>(&self) -> CreateBuilder<'_, M>

Starts a high-level insert builder.

Source

pub fn update<M: Model>(&self, id: i64) -> UpdateBuilder<'_, M>

Starts a high-level update builder scoped to one primary key.

Source

pub async fn delete<M: Model + Send + 'static>( &self, id: i64, ) -> Result<bool, OrmError>

Deletes one row by its generated integer primary key.

Source

pub fn query<M: Model>(&self) -> DatabaseQuery<M>

Starts a high-level typed select builder bound to this database.

Source

pub async fn fetch_by<M, T, V>( &self, field: ModelField<M, T>, value: V, ) -> Result<Vec<M>, OrmError>
where M: Model + Send + 'static, V: QueryValue<T>,

Fetches related rows by a typed model field.

This is the low-level building block for has_many and belongs_to: pass the foreign-key field on the target model and the owner key.

Source

pub async fn fetch_by_many<M, T, I, V>( &self, field: ModelField<M, T>, values: I, ) -> Result<Vec<M>, OrmError>
where M: Model + Send + 'static, I: IntoIterator<Item = V>, V: QueryValue<T>,

Fetches rows whose typed field matches one of the supplied values.

Source

pub async fn transaction<T, F>(&self, action: F) -> Result<T, OrmError>
where T: Send + 'static, F: FnOnce(&Connection) -> Result<T> + Send + 'static,

Runs a blocking SQLite closure in a pool worker transaction.

Source

pub async fn typed_transaction<T, F>(&self, action: F) -> Result<T, OrmError>
where T: Send + 'static, F: FnOnce(&Transaction<'_>) -> Result<T, OrmError> + Send + 'static,

Runs typed ORM operations on one SQLite transaction.

Returning an error rolls back every mutation performed by action.

Trait Implementations§

Source§

impl Clone for Database

Source§

fn clone(&self) -> Database

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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.