Skip to main content

LivePostgresBatch

Struct LivePostgresBatch 

pub struct LivePostgresBatch { /* private fields */ }
Available on crate feature projection-store only.
Expand description

A Postgres projection batch that is an open transaction.

It owns a pooled connection with BEGIN already issued on it. Statements issued through execute reach the server as they are made; nothing becomes durable until commit.

Transaction<'static, Postgres> owns its PoolConnection and borrows nothing, which is what lets the port’s type Batch; carry no lifetime while this batch holds a live handle — the finding PS-5 was built on.

Dropping it without committing rolls the transaction back: sqlx queues a ROLLBACK on the connection as it returns to the pool. That is PS-7 met by the driver rather than by this adapter, and dropped_batch_leaves_store_usable is the rule that checks it actually happens.

Implementations§

§

impl LivePostgresBatch

pub async fn execute( &mut self, sql: &'static str, params: impl IntoIterator<Item = PgParam>, ) -> Result<(), PostgresProjectionStoreError>

Issues one statement through the open transaction.

The statement reaches the server now. If the server refuses it, the transaction is aborted and every later statement, the checkpoint upsert included, is refused, until a ROLLBACK TO SAVEPOINT for a SAVEPOINT issued earlier through this batch undoes it. Without one, hand the batch to rollback or drop it.

§Security

sql is &'static str, so it cannot be assembled at run time, and the values are bound rather than interpolated — the same two guards as PostgresProjectionBatch::push, for the same reason: a statement built out of decoded event data is a SQL injection whose source is the log, and here it does not even wait for commit to reach the server. execute_raw_sql is the escape hatch, separately named so that reaching for it is a decision.

A statement written in source compiles:

use happenstance_postgres::error::PostgresProjectionStoreError;
use happenstance_postgres::live_projection_store::LivePostgresBatch;
use happenstance_postgres::projection_store::PgParam;

async fn queue(
    batch: &mut LivePostgresBatch,
    account: &str,
) -> Result<(), PostgresProjectionStoreError> {
    batch
        .execute(
            "DELETE FROM balance WHERE account = $1",
            [PgParam::text(account)],
        )
        .await
}

and the same statement with the value interpolated does not:

ⓘ
use happenstance_postgres::error::PostgresProjectionStoreError;
use happenstance_postgres::live_projection_store::LivePostgresBatch;
use happenstance_postgres::projection_store::PgParam;

async fn queue(
    batch: &mut LivePostgresBatch,
    account: &str,
) -> Result<(), PostgresProjectionStoreError> {
    batch
        .execute(
            format!("DELETE FROM balance WHERE account = '{account}'"),
            core::iter::empty::<PgParam>(),
        )
        .await
}
§Parameter count

The values must match the highest $n that sql uses, one for one. This call counts them before binding and refuses a mismatch in either direction as ParameterCount, at this call, and the batch can then no longer commit. Nothing reaches the server. The server alone would refuse only too few values (ADR-0084).

§Errors

pub async fn execute_raw_sql( &mut self, sql: &str, params: Vec<PgParam>, ) -> Result<(), PostgresProjectionStoreError>

Issues one statement whose shape is computed at run time.

The honest case is an IN (…) list sized by the number of parameters. Everything execute says about binding still applies: values belong in params, never in sql.

§Parameter count

As for execute. A computed IN (…) list is where a count goes wrong: build the placeholders and the values from the same collection.

§Errors

Trait Implementations§

§

impl Debug for LivePostgresBatch

§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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

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

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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