Skip to main content

Database

Struct Database 

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

The application’s handle on the database.

Registered as application state, so a handler reaches it with req.state::<Database>().

Implementations§

Source§

impl Database

Source

pub async fn connect(url: &str) -> Result<Database>

Connect using a URL: postgres://user:password@host:port/database.

Source

pub async fn with_config(config: DatabaseConfig) -> Result<Database>

Connect using explicit settings, verifying the connection works.

Source

pub fn lazy(config: DatabaseConfig) -> Result<Database>

Build a handle without touching the network. Useful when the process should start even if the database is briefly down.

Source

pub fn with_driver(driver: Arc<dyn Driver>) -> Database

Use a driver directly — how a database this crate does not know about would be plugged in.

Source

pub fn dialect(&self) -> &dyn Dialect

What SQL this connection speaks.

The query and schema builders take it, which is how one builder produces correct SQL for three different databases.

Source

pub fn pool(&self) -> &Pool

Source

pub fn table(&self, name: &str) -> QueryBuilder

Start a query: db.table("users").filter("active", true).get(&db).await.

Source

pub async fn select(&self, sql: &str, params: &[Value]) -> Result<Vec<Row>>

Run a query and return every row.

Source

pub async fn select_one( &self, sql: &str, params: &[Value], ) -> Result<Option<Row>>

Run a query expecting at most one row.

Source

pub async fn execute(&self, sql: &str, params: &[Value]) -> Result<u64>

Run a statement and return the number of rows it affected.

Source

pub async fn run(&self, sql: &str) -> Result<u64>

Run one or more statements with no parameters — DDL, mostly.

Source

pub async fn insert_returning_key( &self, sql: &str, params: &[Value], column: &str, ) -> Result<Option<Value>>

Run an insert and hand back the key the database generated.

Three mechanisms, one method: PostgreSQL returns a row from RETURNING, SQL Server from OUTPUT, and MySQL reports the id in the packet that acknowledges the insert, with no row at all.

Source

pub async fn scalar<T: FromValue>( &self, sql: &str, params: &[Value], ) -> Result<Option<T>>

Read a single value from the first column of the first row.

Source

pub async fn begin(&self) -> Result<Transaction>

Begin a transaction.

Rust’s borrow rules make Laravel’s DB::transaction(closure) shape awkward — the closure’s future would have to borrow the connection it was handed — so the transaction is a value you hold instead:

let mut tx = db.begin().await?;
tx.execute("update accounts set balance = balance - $1", &[amount]).await?;
tx.commit().await?;

Dropping it without committing rolls back, so an early ? cannot leave a half-finished transaction behind.

Source

pub async fn close(&self)

Close every pooled connection.

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.