pub struct PgConnection { /* private fields */ }Expand description
The connection string expected by PgConnection::establish
should be a PostgreSQL connection string, as documented at
https://www.postgresql.org/docs/9.4/static/libpq-connect.html#LIBPQ-CONNSTRING
§Supported loading model implementations
If you are unsure which loading mode is the correct one for your application,
you likely want to use the DefaultLoadingMode as that one offers
generally better performance.
Due to the fact that PgConnection supports multiple loading modes
it is required to always specify the used loading mode
when calling RunQueryDsl::load_iter
§DefaultLoadingMode
By using this mode PgConnection defaults to loading all response values at once
and only performs deserialization afterward for the DefaultLoadingMode.
Generally this mode will be more performant as it.
This loading mode allows users to perform hold more than one iterator at once using the same connection:
use diesel::connection::DefaultLoadingMode;
let iter1 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?;
let iter2 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?;
for r in iter1 {
let (id, name) = r?;
println!("Id: {} Name: {}", id, name);
}
for r in iter2 {
let (id, name) = r?;
println!("Id: {} Name: {}", id, name);
}§PgRowByRowLoadingMode
By using this mode PgConnection defaults to loading each row of the result set
separately. This might be desired for huge result sets.
This loading mode prevents creating more than one iterator at once using the same connection. The following code is not allowed:
use diesel::pg::PgRowByRowLoadingMode;
let iter1 = users::table.load_iter::<(i32, String), PgRowByRowLoadingMode>(connection)?;
// creating a second iterator generates an compiler error
let iter2 = users::table.load_iter::<(i32, String), PgRowByRowLoadingMode>(connection)?;
for r in iter1 {
let (id, name) = r?;
println!("Id: {} Name: {}", id, name);
}
for r in iter2 {
let (id, name) = r?;
println!("Id: {} Name: {}", id, name);
}Implementations§
Source§impl PgConnection
impl PgConnection
Sourcepub fn build_transaction(&mut self) -> TransactionBuilder<'_, PgConnection>
pub fn build_transaction(&mut self) -> TransactionBuilder<'_, PgConnection>
Build a transaction, specifying additional details such as isolation level
See TransactionBuilder for more examples.
conn.build_transaction()
.read_only()
.serializable()
.deferrable()
.run(|conn| Ok(()))Sourcepub fn notifications_iter(
&mut self,
) -> impl Iterator<Item = Result<PgNotification, Error>>
pub fn notifications_iter( &mut self, ) -> impl Iterator<Item = Result<PgNotification, Error>>
See Postgres documentation for SQL commands NOTIFY and LISTEN
The returned iterator can yield items even after a None value when new notifications have been received.
The iterator can be polled again after a None value was received as new notifications might have
been send in the mean time.
§Example
// register the notifications channel we want to receive notifications for
diesel::sql_query("LISTEN example_channel").execute(connection)?;
// send some notification
// this is usually done from a different connection/thread/application
diesel::sql_query("NOTIFY example_channel, 'additional data'").execute(connection)?;
for result in connection.notifications_iter() {
let notification = result.unwrap();
assert_eq!(notification.channel, "example_channel");
assert_eq!(notification.payload, "additional data");
println!(
"Notification received from server process with id {}.",
notification.process_id
);
}Trait Implementations§
Source§impl Connection for PgConnection
impl Connection for PgConnection
Source§fn establish(database_url: &str) -> Result<PgConnection, ConnectionError>
fn establish(database_url: &str) -> Result<PgConnection, ConnectionError>
Source§fn set_instrumentation(&mut self, instrumentation: impl Instrumentation)
fn set_instrumentation(&mut self, instrumentation: impl Instrumentation)
Instrumentation implementation for this connectionSource§fn set_prepared_statement_cache_size(&mut self, size: CacheSize)
fn set_prepared_statement_cache_size(&mut self, size: CacheSize)
CacheSize for this connectionSource§fn transaction<T, E, F>(&mut self, f: F) -> Result<T, E>
fn transaction<T, E, F>(&mut self, f: F) -> Result<T, E>
Source§impl<T, A> ExecuteCopyFromDsl<PgConnection> for CopyFromQuery<T, A>where
A: CopyFromExpression<T>,
impl<T, A> ExecuteCopyFromDsl<PgConnection> for CopyFromQuery<T, A>where
A: CopyFromExpression<T>,
Source§impl<B> LoadConnection<B> for PgConnectionwhere
PgConnection: PgLoadingMode<B>,
impl<B> LoadConnection<B> for PgConnectionwhere
PgConnection: PgLoadingMode<B>,
Source§type Cursor<'conn, 'query> = <PgConnection as PgLoadingMode<B>>::Cursor<'conn, 'query>
type Cursor<'conn, 'query> = <PgConnection as PgLoadingMode<B>>::Cursor<'conn, 'query>
LoadConnection::load Read moreSource§type Row<'conn, 'query> = <PgConnection as PgLoadingMode<B>>::Row<'conn, 'query>
type Row<'conn, 'query> = <PgConnection as PgLoadingMode<B>>::Row<'conn, 'query>
Iterator::Item for the iterator implementation
of LoadConnection::CursorSource§impl MigrationConnection for PgConnection
impl MigrationConnection for PgConnection
Source§impl SimpleConnection for PgConnection
impl SimpleConnection for PgConnection
Source§impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for PgConnectionwhere
Changes: Copy + AsChangeset<Target = <Changes as HasTable>::Table> + IntoUpdateTarget,
UpdateStatement<<Changes as HasTable>::Table, <Changes as IntoUpdateTarget>::WhereClause, <Changes as AsChangeset>::Changeset>: LoadQuery<'b, PgConnection, Output>,
<<Changes as HasTable>::Table as Table>::AllColumns: ValidGrouping<()>,
<<<Changes as HasTable>::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate: MixedAggregates<No, Output = No>,
impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for PgConnectionwhere
Changes: Copy + AsChangeset<Target = <Changes as HasTable>::Table> + IntoUpdateTarget,
UpdateStatement<<Changes as HasTable>::Table, <Changes as IntoUpdateTarget>::WhereClause, <Changes as AsChangeset>::Changeset>: LoadQuery<'b, PgConnection, Output>,
<<Changes as HasTable>::Table as Table>::AllColumns: ValidGrouping<()>,
<<<Changes as HasTable>::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate: MixedAggregates<No, Output = No>,
Source§fn update_and_fetch(&mut self, changeset: Changes) -> Result<Output, Error>
fn update_and_fetch(&mut self, changeset: Changes) -> Result<Output, Error>
impl Send for PgConnection
Auto Trait Implementations§
impl Freeze for PgConnection
impl !RefUnwindSafe for PgConnection
impl !Sync for PgConnection
impl Unpin for PgConnection
impl !UnwindSafe for PgConnection
Blanket Implementations§
Source§impl<T> AggregateExpressionMethods for T
impl<T> AggregateExpressionMethods for T
Source§fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
DISTINCT modifier for aggregate functions Read moreSource§fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
ALL modifier for aggregate functions Read moreSource§fn aggregate_filter<P>(self, f: P) -> Self::Output
fn aggregate_filter<P>(self, f: P) -> Self::Output
Source§fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
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
Source§impl<C> BoxableConnection<<C as Connection>::Backend> for Cwhere
C: Connection + Any,
impl<C> BoxableConnection<<C as Connection>::Backend> for Cwhere
C: Connection + Any,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self to an expression for Diesel’s query builder. Read more