pub struct PgEngine { /* private fields */ }Expand description
PostgreSQL query engine that implements the Prax QueryEngine
trait.
Two modes, controlled by the tx_conn field:
- Pool mode (
tx_conn == None, the default): each query acquires a fresh connection fromPgPooland drops it after the call. - Transaction mode (
tx_conn == Some(conn)): each query routes through the single pinneddeadpool_postgres::Object. The tx-bound engine is built byPgEngine::transaction, which issues a rawBEGIN; the outer future then runsCOMMITorROLLBACKon the same connection based on the closure’sOk/Errresult.
We lean on raw BEGIN / COMMIT / ROLLBACK strings instead of
tokio_postgres::Transaction<'_> because Transaction<'_> borrows
from its owning Client, and bundling both into a heap cell
requires mem::transmute gymnastics to launder the lifetime to
'static. Since Object implements Deref<Target = Client> and
Client::query / execute take &self, an Arc<Object> is all
we need — every engine clone can share it freely, and the last
clone drops the Arc, which drops the Object back to the pool.
This path is explicitly sanctioned by the task plan’s “fall back”
guardrail.
Implementations§
Source§impl PgEngine
impl PgEngine
Sourcepub fn new(pool: PgPool) -> Self
pub fn new(pool: PgPool) -> Self
Create a new PostgreSQL engine with the given connection pool.
Sourcepub async fn notify(&self, channel: &str, payload: &str) -> QueryResult<()>
pub async fn notify(&self, channel: &str, payload: &str) -> QueryResult<()>
Send a notification on channel (SELECT pg_notify($1, $2)).
On the transaction-bound engine that
PraxClient::<PgEngine>::transaction hands its closure (reach it with
tx.engine()), the notification is part of that transaction:
delivered when it commits, discarded if it rolls back. Otherwise it
runs on a pooled connection and is delivered at once.
The channel name is matched verbatim (case preserved) by a
PgListener subscribed to the same
string. It must be non-empty, at most 63 bytes and free of NUL, and
the payload shorter than 8000 bytes; both are checked before
anything is sent (an invalid-input error otherwise).
Trait Implementations§
Source§impl QueryEngine for PgEngine
impl QueryEngine for PgEngine
Source§fn dialect(&self) -> &dyn SqlDialect
fn dialect(&self) -> &dyn SqlDialect
Source§fn query_many<T: Model + FromRow + Send + 'static>(
&self,
sql: &str,
params: Vec<FilterValue>,
) -> BoxFuture<'_, QueryResult<Vec<T>>>
fn query_many<T: Model + FromRow + Send + 'static>( &self, sql: &str, params: Vec<FilterValue>, ) -> BoxFuture<'_, QueryResult<Vec<T>>>
Source§fn query_one<T: Model + FromRow + Send + 'static>(
&self,
sql: &str,
params: Vec<FilterValue>,
) -> BoxFuture<'_, QueryResult<T>>
fn query_one<T: Model + FromRow + Send + 'static>( &self, sql: &str, params: Vec<FilterValue>, ) -> BoxFuture<'_, QueryResult<T>>
Source§fn query_optional<T: Model + FromRow + Send + 'static>(
&self,
sql: &str,
params: Vec<FilterValue>,
) -> BoxFuture<'_, QueryResult<Option<T>>>
fn query_optional<T: Model + FromRow + Send + 'static>( &self, sql: &str, params: Vec<FilterValue>, ) -> BoxFuture<'_, QueryResult<Option<T>>>
Source§fn execute_insert<T: Model + FromRow + Send + 'static>(
&self,
sql: &str,
params: Vec<FilterValue>,
) -> BoxFuture<'_, QueryResult<T>>
fn execute_insert<T: Model + FromRow + Send + 'static>( &self, sql: &str, params: Vec<FilterValue>, ) -> BoxFuture<'_, QueryResult<T>>
Source§fn execute_update<T: Model + FromRow + Send + 'static>(
&self,
sql: &str,
params: Vec<FilterValue>,
) -> BoxFuture<'_, QueryResult<Vec<T>>>
fn execute_update<T: Model + FromRow + Send + 'static>( &self, sql: &str, params: Vec<FilterValue>, ) -> BoxFuture<'_, QueryResult<Vec<T>>>
Source§fn execute_delete(
&self,
sql: &str,
params: Vec<FilterValue>,
) -> BoxFuture<'_, QueryResult<u64>>
fn execute_delete( &self, sql: &str, params: Vec<FilterValue>, ) -> BoxFuture<'_, QueryResult<u64>>
Source§fn execute_raw(
&self,
sql: &str,
params: Vec<FilterValue>,
) -> BoxFuture<'_, QueryResult<u64>>
fn execute_raw( &self, sql: &str, params: Vec<FilterValue>, ) -> BoxFuture<'_, QueryResult<u64>>
Source§fn count(
&self,
sql: &str,
params: Vec<FilterValue>,
) -> BoxFuture<'_, QueryResult<u64>>
fn count( &self, sql: &str, params: Vec<FilterValue>, ) -> BoxFuture<'_, QueryResult<u64>>
Source§fn aggregate_query(
&self,
sql: &str,
params: Vec<FilterValue>,
) -> BoxFuture<'_, QueryResult<Vec<HashMap<String, FilterValue>>>>
fn aggregate_query( &self, sql: &str, params: Vec<FilterValue>, ) -> BoxFuture<'_, QueryResult<Vec<HashMap<String, FilterValue>>>>
crate::filter::FilterValue
per result row. Read more