Skip to main content

Transaction

Struct Transaction 

Source
pub struct Transaction<'conn> { /* private fields */ }
Expand description

Represents a transaction on a database connection.

§Note

Transactions will roll back by default. Use commit method to explicitly commit the transaction, or use set_drop_behavior to change what happens on the next access to the connection after the transaction is dropped.

§Example

async fn perform_queries(conn: &mut Connection) -> Result<()> {
    let tx = conn.transaction().await?;

    do_queries_part_1(&tx)?; // tx causes rollback if this fails
    do_queries_part_2(&tx)?; // tx causes rollback if this fails

    tx.commit().await
}

Implementations§

Source§

impl Transaction<'_>

Source

pub async fn new( conn: &mut Connection, behavior: TransactionBehavior, ) -> Result<Transaction<'_>>

Begin a new transaction. Cannot be nested;

Even though we don’t mutate the connection, we take a &mut Connection to prevent nested transactions on the same connection. For cases where this is unacceptable, Transaction::new_unchecked is available.

Source

pub async fn new_unchecked( conn: &Connection, behavior: TransactionBehavior, ) -> Result<Transaction<'_>>

Begin a new transaction, failing if a transaction is open.

If a transaction is already open, this will return an error. Where possible, Transaction::new should be preferred, as it provides a compile-time guarantee that transactions are not nested.

Source

pub async fn prepare(&self, sql: &str) -> Result<Statement>

Source

pub fn drop_behavior(&self) -> DropBehavior

Get the current setting for what happens to the transaction when it is dropped.

Source

pub fn set_drop_behavior(&mut self, drop_behavior: DropBehavior)

Configure the transaction to perform the specified action when it is dropped.

Source

pub async fn commit(self) -> Result<()>

A convenience method which consumes and commits a transaction.

Source

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

A convenience method which consumes and rolls back a transaction.

Source

pub async fn finish(self) -> Result<()>

Consumes the transaction, committing or rolling back according to the current setting (see drop_behavior).

Functionally equivalent to the Drop implementation, but allows callers to see any errors that occur.

Methods from Deref<Target = Connection>§

Source

pub async fn query( &self, sql: impl AsRef<str>, params: impl IntoParams, ) -> Result<Rows>

Query the database with SQL.

Source

pub async fn execute( &self, sql: impl AsRef<str>, params: impl IntoParams, ) -> Result<u64>

Execute SQL statement on the database.

Source

pub async fn execute_batch(&self, sql: impl AsRef<str>) -> Result<()>

Execute a batch of SQL statements on the database.

Source

pub async fn prepare(&self, sql: impl AsRef<str>) -> Result<Statement>

Prepare a SQL statement for later execution.

Source

pub async fn prepare_cached(&self, sql: impl AsRef<str>) -> Result<Statement>

Prepare a SQL statement for later execution, caching it in the connection.

Source

pub async fn pragma_query<F>(&self, pragma_name: &str, f: F) -> Result<()>
where F: FnMut(&Row) -> Result<(), TursoError>,

Query a pragma.

Source

pub async fn pragma_update<V: Display>( &self, pragma_name: &str, pragma_value: V, ) -> Result<Vec<Row>>

Set a pragma value.

Source

pub fn last_insert_rowid(&self) -> i64

Returns the rowid of the last row inserted.

Source

pub fn cacheflush(&self) -> Result<()>

Flush dirty pages to disk. This will write the dirty pages to the WAL.

Source

pub fn is_autocommit(&self) -> Result<bool>

Source

pub fn busy_timeout(&self, duration: Duration) -> Result<()>

Sets maximum total accumuated timeout. If the duration is None or Zero, we unset the busy handler for this Connection

This api defers slighty from: https://www.sqlite.org/c3ref/busy_timeout.html

Instead of sleeping for linear amount of time specified by the user, we will sleep in phases, until the the total amount of time is reached. This means we first sleep of 1ms, then if we still return busy, we sleep for 2 ms, and repeat until a maximum of 100 ms per phase.

Example:

  1. Set duration to 5ms
  2. Step through query -> returns Busy -> sleep/yield for 1 ms
  3. Step through query -> returns Busy -> sleep/yield for 2 ms
  4. Step through query -> returns Busy -> sleep/yield for 2 ms (totaling 5 ms of sleep)
  5. Step through query -> returns Busy -> return Busy to user
Source

pub async fn unchecked_transaction(&self) -> Result<Transaction<'_>>

Begin a new transaction with the default behavior (DEFERRED).

Attempt to open a nested transaction will result in a SQLite error. Connection::transaction prevents this at compile time by taking &mut self, but Connection::unchecked_transaction() may be used to defer the checking until runtime.

See Connection::transaction and Transaction::new_unchecked (which can be used if the default transaction behavior is undesirable).

§Example
async fn perform_queries(conn: Rc<Connection>) -> Result<()> {
    let tx = conn.unchecked_transaction().await?;

    do_queries_part_1(&tx)?; // tx causes rollback if this fails
    do_queries_part_2(&tx)?; // tx causes rollback if this fails

    tx.commit().await
}
§Failure

Will return Err if the underlying SQLite call fails. The specific error returned if transactions are nested is currently unspecified.

Trait Implementations§

Source§

impl<'conn> Debug for Transaction<'conn>

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

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Connection

Dereferences the value.
Source§

impl Drop for Transaction<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'conn> Freeze for Transaction<'conn>

§

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

§

impl<'conn> Send for Transaction<'conn>

§

impl<'conn> Sync for Transaction<'conn>

§

impl<'conn> Unpin for Transaction<'conn>

§

impl<'conn> UnsafeUnpin for Transaction<'conn>

§

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

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more