Struct sqlx::postgres::PgListener [−][src]
pub struct PgListener { /* fields omitted */ }
postgres
only.Expand description
A stream of asynchronous notifications from Postgres.
This listener will auto-reconnect. If the active connection being used ever dies, this listener will detect that event, create a new connection, will re-subscribe to all of the originally specified channels, and will resume operations as normal.
Implementations
Starts listening for notifications on a channel. The channel name is quoted here to ensure case sensitivity.
pub async fn listen_all<'_>(
&'_ mut self,
channels: impl IntoIterator<Item = &'_ str>
) -> Result<(), Error>
pub async fn listen_all<'_>(
&'_ mut self,
channels: impl IntoIterator<Item = &'_ str>
) -> Result<(), Error>
Starts listening for notifications on all channels.
Stops listening for notifications on a channel. The channel name is quoted here to ensure case sensitivity.
Stops listening for notifications on all channels.
Receives the next notification available from any of the subscribed channels.
If the connection to PostgreSQL is lost, it is automatically reconnected on the next
call to recv()
, and should be entirely transparent (as long as it was just an
intermittent network failure or long-lived connection reaper).
As notifications are transient, any received while the connection was lost, will not
be returned. If you’d prefer the reconnection to be explicit and have a chance to
do something before, please see try_recv
.
Example
loop {
// ask for next notification, re-connecting (transparently) if needed
let notification = listener.recv().await?;
// handle notification, do something interesting
}
Receives the next notification available from any of the subscribed channels.
If the connection to PostgreSQL is lost, None
is returned, and the connection is
reconnected on the next call to try_recv()
.
Example
loop {
// start handling notifications, connecting if needed
while let Some(notification) = listener.try_recv().await? {
// handle notification
}
// connection lost, do something interesting
}
Consume this listener, returning a Stream
of notifications.
The backing connection will be automatically reconnected should it be lost.
This has the same potential drawbacks as recv
.
Trait Implementations
pub fn fetch_many<'e, 'q, E>(
self,
query: E
) -> Pin<Box<dyn Stream<Item = Result<Either<PgQueryResult, PgRow>, Error>> + Send + 'e, Global>> where
'q: 'e,
'c: 'e,
E: 'q + Execute<'q, <&'c mut PgListener as Executor<'c>>::Database>,
pub fn fetch_many<'e, 'q, E>(
self,
query: E
) -> Pin<Box<dyn Stream<Item = Result<Either<PgQueryResult, PgRow>, Error>> + Send + 'e, Global>> where
'q: 'e,
'c: 'e,
E: 'q + Execute<'q, <&'c mut PgListener as Executor<'c>>::Database>,
Execute multiple queries and return the generated results as a stream from each query, in a stream. Read more
Execute the query and returns at most one row.
pub fn prepare_with<'e, 'q>(
self,
query: &'q str,
parameters: &'e [PgTypeInfo]
) -> Pin<Box<dyn Future<Output = Result<PgStatement<'q>, Error>> + Send + 'e, Global>> where
'q: 'e,
'c: 'e,
pub fn prepare_with<'e, 'q>(
self,
query: &'q str,
parameters: &'e [PgTypeInfo]
) -> Pin<Box<dyn Future<Output = Result<PgStatement<'q>, Error>> + Send + 'e, Global>> where
'q: 'e,
'c: 'e,
Prepare the SQL query, with parameter type information, to inspect the type information about its parameters and results. Read more
Execute the query and return the total number of rows affected.
Execute multiple queries and return the rows affected from each query, in a stream.
Execute the query and return the generated results as a stream.
Execute the query and return all the generated results, collected into a Vec
.
Execute the query and returns exactly one row.