pub struct PgConnection { /* private fields */ }
postgres
and postgres_backend
only.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<'_, Self>
pub fn build_transaction(&mut self) -> TransactionBuilder<'_, Self>
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(()))
Trait Implementations§
source§impl Connection for PgConnection
impl Connection for PgConnection
source§type TransactionManager = AnsiTransactionManager
type TransactionManager = AnsiTransactionManager
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.source§fn establish(database_url: &str) -> ConnectionResult<PgConnection>
fn establish(database_url: &str) -> ConnectionResult<PgConnection>
source§fn execute_returning_count<T>(&mut self, source: &T) -> QueryResult<usize>
fn execute_returning_count<T>(&mut self, source: &T) -> QueryResult<usize>
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.source§fn transaction_state(&mut self) -> &mut AnsiTransactionManagerwhere
Self: Sized,
fn transaction_state(&mut self) -> &mut AnsiTransactionManagerwhere
Self: Sized,
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.source§fn instrumentation(&mut self) -> &mut dyn Instrumentation
fn instrumentation(&mut self) -> &mut dyn Instrumentation
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.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§fn begin_test_transaction(&mut self) -> QueryResult<()>
fn begin_test_transaction(&mut self) -> QueryResult<()>
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 GetPgMetadataCache for PgConnection
impl GetPgMetadataCache for PgConnection
source§fn get_metadata_cache(&mut self) -> &mut PgMetadataCache
fn get_metadata_cache(&mut self) -> &mut PgMetadataCache
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.PgMetadataCache
source§impl<B> LoadConnection<B> for PgConnectionwhere
Self: PgLoadingMode<B>,
impl<B> LoadConnection<B> for PgConnectionwhere
Self: 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::Cursor
source§fn load<'conn, 'query, T>(
&'conn mut self,
source: T,
) -> QueryResult<Self::Cursor<'conn, 'query>>where
T: Query + QueryFragment<Self::Backend> + QueryId + 'query,
Self::Backend: QueryMetadata<T::SqlType>,
fn load<'conn, 'query, T>(
&'conn mut self,
source: T,
) -> QueryResult<Self::Cursor<'conn, 'query>>where
T: Query + QueryFragment<Self::Backend> + QueryId + 'query,
Self::Backend: QueryMetadata<T::SqlType>,
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.source§impl MigrationConnection for PgConnection
impl MigrationConnection for PgConnection
source§impl MultiConnectionHelper for PgConnection
impl MultiConnectionHelper for PgConnection
source§fn to_any<'a>(
lookup: &mut <Self::Backend as TypeMetadata>::MetadataLookup,
) -> &mut (dyn Any + 'a)
fn to_any<'a>( lookup: &mut <Self::Backend as TypeMetadata>::MetadataLookup, ) -> &mut (dyn Any + 'a)
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.source§fn from_any(
lookup: &mut dyn Any,
) -> Option<&mut <Self::Backend as TypeMetadata>::MetadataLookup>
fn from_any( lookup: &mut dyn Any, ) -> Option<&mut <Self::Backend as TypeMetadata>::MetadataLookup>
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.source§impl R2D2Connection for PgConnection
Available on crate feature r2d2
only.
impl R2D2Connection for PgConnection
r2d2
only.source§impl SimpleConnection for PgConnection
impl SimpleConnection for PgConnection
source§fn batch_execute(&mut self, query: &str) -> QueryResult<()>
fn batch_execute(&mut self, query: &str) -> QueryResult<()>
source§impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for PgConnectionwhere
Changes: Copy + AsChangeset<Target = <Changes as HasTable>::Table> + IntoUpdateTarget,
Update<Changes, Changes>: LoadQuery<'b, PgConnection, Output>,
<Changes::Table as Table>::AllColumns: ValidGrouping<()>,
<<Changes::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,
Update<Changes, Changes>: LoadQuery<'b, PgConnection, Output>,
<Changes::Table as Table>::AllColumns: ValidGrouping<()>,
<<Changes::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate: MixedAggregates<No, Output = No>,
source§fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output>
fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output>
impl ConnectionSealed for PgConnection
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> 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>
. Box<dyn Any>
can
then be further downcast
into Box<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>
. Rc<Any>
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> IntoSql for T
impl<T> IntoSql for T
source§impl<T> PgMetadataLookup for T
impl<T> PgMetadataLookup for T
source§fn lookup_type(
&mut self,
type_name: &str,
schema: Option<&str>,
) -> PgTypeMetadata
fn lookup_type( &mut self, type_name: &str, schema: Option<&str>, ) -> PgTypeMetadata
postgres_backend
only.type_name
Read more