Skip to main content

Transaction

Struct Transaction 

Source
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.

Implementations§

Source§

impl Transaction<'_>

Source

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.

Source

pub async fn rollback(self) -> Result<(), Error>

Rolls the transaction back and waits for the server to confirm.

Methods from Deref<Target = Client>§

Source

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.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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.

Source

pub fn parameter(&self, name: &str) -> Option<String>

A setting the server reported, such as server_version, TimeZone or integer_datetimes.

Source

pub fn is_closed(&self) -> bool

Whether the connection has stopped working. A closed client fails every statement with ErrorKind::Closed.

Source

pub fn in_flight(&self) -> usize

How many requests have been sent and not answered yet.

Trait Implementations§

Source§

impl Debug for Transaction<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for Transaction<'_>

Source§

type Target = Client

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Client

Dereferences the value.
Source§

impl Drop for Transaction<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Transaction<'a>

§

impl<'a> !Send for Transaction<'a>

§

impl<'a> !Sync for Transaction<'a>

§

impl<'a> !UnwindSafe for Transaction<'a>

§

impl<'a> Forget for Transaction<'a>

§

impl<'a> Freeze for Transaction<'a>

§

impl<'a> Move for Transaction<'a>

§

impl<'a> Unpin for Transaction<'a>

§

impl<'a> UnsafeUnpin for Transaction<'a>

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> 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.