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
impl Database
Sourcepub async fn connect(url: &str) -> Result<Database>
pub async fn connect(url: &str) -> Result<Database>
Connect using a URL: postgres://user:password@host:port/database.
Sourcepub async fn with_config(config: DatabaseConfig) -> Result<Database>
pub async fn with_config(config: DatabaseConfig) -> Result<Database>
Connect using explicit settings, verifying the connection works.
Sourcepub fn lazy(config: DatabaseConfig) -> Result<Database>
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.
Sourcepub fn with_driver(driver: Arc<dyn Driver>) -> Database
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.
Sourcepub fn dialect(&self) -> &dyn Dialect
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.
pub fn pool(&self) -> &Pool
Sourcepub fn table(&self, name: &str) -> QueryBuilder
pub fn table(&self, name: &str) -> QueryBuilder
Start a query: db.table("users").filter("active", true).get(&db).await.
Sourcepub async fn select(&self, sql: &str, params: &[Value]) -> Result<Vec<Row>>
pub async fn select(&self, sql: &str, params: &[Value]) -> Result<Vec<Row>>
Run a query and return every row.
Sourcepub async fn select_one(
&self,
sql: &str,
params: &[Value],
) -> Result<Option<Row>>
pub async fn select_one( &self, sql: &str, params: &[Value], ) -> Result<Option<Row>>
Run a query expecting at most one row.
Sourcepub async fn execute(&self, sql: &str, params: &[Value]) -> Result<u64>
pub async fn execute(&self, sql: &str, params: &[Value]) -> Result<u64>
Run a statement and return the number of rows it affected.
Sourcepub async fn run(&self, sql: &str) -> Result<u64>
pub async fn run(&self, sql: &str) -> Result<u64>
Run one or more statements with no parameters — DDL, mostly.
Sourcepub async fn insert_returning_key(
&self,
sql: &str,
params: &[Value],
column: &str,
) -> Result<Option<Value>>
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.
Sourcepub async fn scalar<T: FromValue>(
&self,
sql: &str,
params: &[Value],
) -> Result<Option<T>>
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.
Sourcepub async fn begin(&self) -> Result<Transaction>
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.