Struct PostgresProjectionBatch
pub struct PostgresProjectionBatch { /* private fields */ }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
impl PostgresProjectionBatch
pub fn push(
&mut self,
sql: &'static str,
params: impl IntoIterator<Item = PgParam>,
)
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>)
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.
Trait Implementations§
§impl Clone for PostgresProjectionBatch
impl Clone for PostgresProjectionBatch
Auto Trait Implementations§
impl Freeze for PostgresProjectionBatch
impl RefUnwindSafe for PostgresProjectionBatch
impl Send for PostgresProjectionBatch
impl Sync for PostgresProjectionBatch
impl Unpin for PostgresProjectionBatch
impl UnsafeUnpin for PostgresProjectionBatch
impl UnwindSafe for PostgresProjectionBatch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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