pub struct Client { /* private fields */ }Expand description
A connection to a PostgreSQL server.
Every method takes &self, so tasks can share a client (through an Rc)
and run statements at the same time: their messages go out together and
the server answers them in order, one round trip for all of them
(pipelining). A Transaction needs &mut self, so that no other task
can slip a statement into it.
Statements are prepared on the server the first time the connection runs
them and kept (see
Config::statement_cache_capacity),
so a statement that ran before takes a single round trip.
A client lives on the worker thread that opened it. Dropping it closes the connection once the statements already sent have been written.
Implementations§
Source§impl Client
impl Client
Sourcepub async fn connect(config: &Config) -> Result<Client, Error>
pub async fn connect(config: &Config) -> Result<Client, Error>
Opens a connection and logs in.
Fails with ErrorKind::Connect when the server cannot be reached
within Config::connect_timeout,
ErrorKind::Auth when it refuses the credentials, and
ErrorKind::Config when the configuration is incomplete.
§Panics
Panics when called outside of a giong runtime.
Sourcepub async fn query(
&self,
sql: &str,
params: &[&dyn ToSql],
) -> Result<Vec<Row>, Error>
pub async fn query( &self, sql: &str, params: &[&dyn ToSql], ) -> Result<Vec<Row>, Error>
Runs a statement and returns its rows.
Parameters are written $1, $2… in the SQL and passed in order:
let rows = client
.query("SELECT id, email FROM users WHERE created_at > now() - $1::interval", &[&"7 days"])
.await?;
for row in &rows {
let id: i64 = row.get("id")?;
let email: &str = row.get("email")?;
}The server decides each parameter’s type from the SQL; see the
types module for which Rust types fit which.
Cancel safe in the sense that matters: dropping the future leaves the connection usable. The statement may still run on the server.
Sourcepub async fn query_one(
&self,
sql: &str,
params: &[&dyn ToSql],
) -> Result<Row, Error>
pub async fn query_one( &self, sql: &str, params: &[&dyn ToSql], ) -> Result<Row, Error>
Runs a statement that must return exactly one row, and returns it.
No row or several rows fail with ErrorKind::RowCount.
Sourcepub async fn query_opt(
&self,
sql: &str,
params: &[&dyn ToSql],
) -> Result<Option<Row>, Error>
pub async fn query_opt( &self, sql: &str, params: &[&dyn ToSql], ) -> Result<Option<Row>, Error>
Runs a statement that returns at most one row.
Several rows fail with ErrorKind::RowCount.
Sourcepub async fn execute(
&self,
sql: &str,
params: &[&dyn ToSql],
) -> Result<u64, Error>
pub async fn execute( &self, sql: &str, params: &[&dyn ToSql], ) -> Result<u64, Error>
Runs a statement and returns the number of rows it inserted, updated
or deleted (or returned, for a SELECT).
Sourcepub async fn batch_execute(&self, sql: &str) -> Result<(), Error>
pub async fn batch_execute(&self, sql: &str) -> Result<(), Error>
Runs SQL that may hold several statements separated by ;, with no
parameters, such as a schema migration. Rows they return are thrown
away.
The statements run in one implicit transaction unless the SQL has its
own BEGIN and COMMIT: if one fails, the earlier ones are rolled
back.
Sourcepub async fn transaction(&mut self) -> Result<Transaction<'_>, Error>
pub async fn transaction(&mut self) -> Result<Transaction<'_>, Error>
Starts a transaction.
It is committed by Transaction::commit; dropping it without a
commit rolls it back. While it is open, it holds the client mutably,
so no other statement can run on the connection outside it.
let tx = client.transaction().await?;
tx.execute("UPDATE accounts SET balance = balance - $1 WHERE id = $2", &[&100i64, &1i64]).await?;
tx.execute("UPDATE accounts SET balance = balance + $1 WHERE id = $2", &[&100i64, &2i64]).await?;
tx.commit().await?;Sourcepub fn cancel_token(&self) -> CancelToken
pub fn cancel_token(&self) -> CancelToken
A handle that asks the server to cancel whatever this connection is running, from any thread.
Sourcepub fn parameter(&self, name: &str) -> Option<String>
pub fn parameter(&self, name: &str) -> Option<String>
A setting the server reported, such as server_version, TimeZone
or integer_datetimes.
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Whether the connection has stopped working. A closed client fails
every statement with ErrorKind::Closed.