pub struct Transaction<'a> { /* private fields */ }Expand description
An open transaction. Statements run through it with the same methods as
on Client (it dereferences to the client).
Commit it with commit. Dropping it otherwise rolls it
back: the ROLLBACK is queued at once, ahead of anything the connection
runs afterwards.
A statement given up before its answer came (its future dropped, by a
timeout or a select say) may still run on the server, so the
transaction can no longer be committed: from then on every statement
sent through it, commit included, fails with an
ErrorKind::Db error that says so, and nothing more is sent. Drop the
transaction or rollback it, and run it again from the
start. A statement whose answer had arrived before its future was
dropped does not count: its outcome is known.
Implementations§
Source§impl Transaction<'_>
impl Transaction<'_>
Sourcepub async fn commit(self) -> Result<(), Error>
pub async fn commit(self) -> Result<(), Error>
Commits the transaction.
When a statement in the transaction failed earlier, PostgreSQL rolls
the transaction back instead; this is reported as an error, never as
a successful commit. When a statement in it was given up before its
answer came, COMMIT is not sent: the transaction is rolled back and
the error says why (see Transaction).
Not cancel safe: if the future is dropped, the transaction may have been committed or rolled back, and the caller does not learn which.
Methods from Deref<Target = Client>§
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.
Not cancel safe: if the future is dropped, the statement may still run
on the server, and what it changes stays; the connection stays usable.
Inside a Transaction, a later commit
commits it.
Sourcepub async fn query_in(
&self,
sql: &str,
params: &[&dyn ToSql],
arena: &Arena,
) -> Result<Vec<Row, Arena>, Error>
pub async fn query_in( &self, sql: &str, params: &[&dyn ToSql], arena: &Arena, ) -> Result<Vec<Row, Arena>, Error>
query, with the result in arena: the rows’ values
and the list of rows are allocated there instead of by the global
allocator, three allocations fewer per statement (see
giong::arena). Built with the allocator-api feature (Rust
1.100).
let arena = giong::arena::Arena::new();
let rows = client
.query_in("SELECT id, email FROM users WHERE team = $1", &[&7i64], &arena)
.await?;
for row in &rows {
let email: &str = row.get("email")?;
}The rows keep the arena’s memory alive however long they are kept; it goes back when the arena and every row list in it are dropped.
Not cancel safe, like query.
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.
Not cancel safe, like query.
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.
Not cancel safe, like query.
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).
Not cancel safe, like query.
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.
Not cancel safe: if the future is dropped, the statements may still run on the server, and what they change stays.
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.
A token taken from a PooledClient stops
working when the connection goes back to the pool, so that it cannot
cancel the statements of the tasks that use the connection next; and
a connection that goes back while a request from such a token is on
its way is closed rather than lent to another task.
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.