Struct Database

Source
pub struct Database(/* private fields */);
Expand description

Handle to a pool of database connections

Executing sql statements is done through the Executor trait which is implemented on &Database.

Common operations are implemented as functions in the database module.

Cloning is cheap i.e. two Arcs.

Implementations§

Source§

impl Database

Source

pub async fn connect( configuration: DatabaseConfiguration, ) -> Result<Self, Error>

Connects to the database using configuration

Source

pub async fn raw_sql<'a>( &self, query_string: &'a str, bind_params: Option<&[Value<'a>]>, transaction: Option<&mut Transaction>, ) -> Result<Vec<Row>, Error>

👎Deprecated: Use Executor::execute instead

Execute raw SQL statements on the database.

If possible, the statement is executed as prepared statement.

To bind parameter, use ? as placeholder in SQLite and MySQL and $1, $2, $n in Postgres.

Parameter:

  • query_string: Reference to a valid SQL query.
  • bind_params: Optional list of values to bind in the query.
  • transaction: Optional transaction to execute the query on.

Returns a list of rows. If there are no values to retrieve, an empty list is returned.

Source

pub async fn start_transaction(&self) -> Result<Transaction, Error>

Starts a new transaction

&mut Transaction implements Executor like &Database does but its database operations can be reverted using Transaction::rollback or simply dropping the transaction without calling Transaction::commit.

Source

pub async fn close(self)

Closes the database connection

While calling this method is not strictly necessary, terminating your program without it might result in some final queries not being flushed properly.

This method consumes the database handle, but actually all handles created using clone will become invalid after this call. This means any further operation would result in an Err

Trait Implementations§

Source§

impl Clone for Database

Source§

fn clone(&self) -> Database

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Drop for Database

Source§

fn drop(&mut self)

Checks whether Database::close has been called before the last instance is dropped

Source§

impl<'executor> Executor<'executor> for &'executor Database

Source§

type EnsureTransactionFuture = Pin<Box<dyn Future<Output = Result<TransactionGuard<'executor>, Error>> + Send + 'executor>>

A future producing a TransactionGuard returned by ensure_transaction
Source§

fn execute<'data, 'result, Q>( self, query: String, values: Vec<Value<'data>>, ) -> Q::Result<'result>
where Q: QueryStrategy, 'executor: 'result, 'data: 'result,

Executes a raw SQL query Read more
Source§

fn into_dyn(self) -> DynamicExecutor<'executor>

Convenience method to convert into a “dyn Executor
Source§

fn dialect(&self) -> DBImpl

Get the executor’s sql dialect.
Source§

fn ensure_transaction( self, ) -> Pin<Box<dyn Future<Output = Result<TransactionGuard<'executor>, Error>> + Send + 'executor>>

Ensure a piece of code is run inside a transaction using a TransactionGuard. 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = 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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,