Skip to main content

PostgresProjectionBatch

Struct PostgresProjectionBatch 

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

A Postgres projection batch: an owned list of statements that has been sent to the server exactly never.

It holds no connection, no transaction and no borrow, so it is Send and 'static — which is what lets the port’s type Batch; carry no lifetime and no Send bound while this adapter implements the Send flavour.

begin is the only way to obtain one, and that is the whole of the foreign-batch defence: stamped is private, so a batch always carries the identity of the store that began it.

Implementations§

§

impl PostgresProjectionBatch

pub fn push( &mut self, sql: &'static str, params: impl IntoIterator<Item = PgParam>, )

Queues a statement to run when the batch commits.

§Security

sql is &'static str, so it cannot be assembled at run time, and the values are bound rather than interpolated. A statement built out of decoded event data is a SQL injection whose source is the log — and it commits inside the same transaction that advances the checkpoint, so the projection never replays those events and nothing re-derives the rows it corrupted. push_raw_sql is the escape hatch, and it is separately named so that reaching for it is a decision.

A statement written in source compiles:

use happenstance_postgres::projection_store::{PgParam, PostgresProjectionBatch};

fn queue(batch: &mut PostgresProjectionBatch, account: &str) {
    batch.push(
        "DELETE FROM balance WHERE account = $1",
        [PgParam::text(account)],
    );
}

and the same statement with the value interpolated does not:

ⓘ
use happenstance_postgres::projection_store::{PgParam, PostgresProjectionBatch};

fn queue(batch: &mut PostgresProjectionBatch, account: &str) {
    batch.push(
        format!("DELETE FROM balance WHERE account = '{account}'"),
        core::iter::empty::<PgParam>(),
    );
}
§Parameter count

The values must match the highest $n that sql uses, one for one. Nothing counts them here. At commit (and reset) the batch counts them before binding, and refuses a mismatch in either direction as CommitError::Store (or ResetError::Store) carrying ParameterCount. Neither the batch’s statements nor the checkpoint move. The server alone would refuse only too few values (ADR-0084).

pub fn push_raw_sql(&mut self, sql: impl Into<Box<str>>, params: Vec<PgParam>)

Queues a statement whose shape is computed at run time.

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

§Parameter count

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

pub fn len(&self) -> usize

How many statements are queued.

pub fn is_empty(&self) -> bool

Whether nothing has been queued.

An empty batch is still worth committing: the checkpoint write is the point, and a runner that skipped the commit because its events produced no read-model change would replay them forever.

Trait Implementations§

§

impl Clone for PostgresProjectionBatch

§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for PostgresProjectionBatch

§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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