Struct LivePostgresBatch
pub struct LivePostgresBatch { /* private fields */ }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
impl LivePostgresBatch
pub async fn execute(
&mut self,
sql: &'static str,
params: impl IntoIterator<Item = PgParam>,
) -> Result<(), PostgresProjectionStoreError>
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
ParameterCountif the values do not match the highest$ninsql;Poisonedif an earlier statement in this batch was refused that way;PostgresProjectionStoreError::Driverif the server refused the statement or the connection failed.
pub async fn execute_raw_sql(
&mut self,
sql: &str,
params: Vec<PgParam>,
) -> Result<(), PostgresProjectionStoreError>
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
ParameterCountif the values do not match the highest$ninsql;Poisonedif an earlier statement in this batch was refused that way;PostgresProjectionStoreError::Driverif the server refused the statement or the connection failed.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for LivePostgresBatch
impl !UnwindSafe for LivePostgresBatch
impl Freeze for LivePostgresBatch
impl Send for LivePostgresBatch
impl Sync for LivePostgresBatch
impl Unpin for LivePostgresBatch
impl UnsafeUnpin for LivePostgresBatch
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
§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