diesel::prelude

Struct PgConnection

Source
pub struct PgConnection { /* private fields */ }
Available on crate features 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

Source

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

Source§

type Backend = Pg

The backend this type connects to
Source§

type TransactionManager = AnsiTransactionManager

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
The transaction manager implementation used by this connection
Source§

fn establish(database_url: &str) -> ConnectionResult<PgConnection>

Establishes a new connection to the database Read more
Source§

fn execute_returning_count<T>(&mut self, source: &T) -> QueryResult<usize>
where T: QueryFragment<Pg> + QueryId,

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Execute a single SQL statements given by a query and return number of affected rows
Source§

fn transaction_state(&mut self) -> &mut AnsiTransactionManager
where Self: Sized,

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Get access to the current transaction state of this connection Read more
Source§

fn instrumentation(&mut self) -> &mut dyn Instrumentation

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Get the instrumentation instance stored in this connection
Source§

fn set_instrumentation(&mut self, instrumentation: impl Instrumentation)

Set a specific Instrumentation implementation for this connection
Source§

fn transaction<T, E, F>(&mut self, f: F) -> Result<T, E>
where F: FnOnce(&mut Self) -> Result<T, E>, E: From<Error>,

Executes the given function inside of a database transaction Read more
Source§

fn begin_test_transaction(&mut self) -> QueryResult<()>

Creates a transaction that will never be committed. This is useful for tests. Panics if called while inside of a transaction or if called with a connection containing a broken transaction
Source§

fn test_transaction<T, E, F>(&mut self, f: F) -> T
where F: FnOnce(&mut Self) -> Result<T, E>, E: Debug,

Executes the given function inside a transaction, but does not commit it. Panics if the given function returns an error. Read more
Source§

impl<T, A> ExecuteCopyFromDsl<PgConnection> for CopyFromQuery<T, A>
where A: CopyFromExpression<T>,

Source§

type Error = <A as CopyFromExpression<T>>::Error

The error type returned by the execute function
Source§

fn execute(self, conn: &mut PgConnection) -> Result<usize, A::Error>

See the trait documentation for details
Source§

impl GetPgMetadataCache for PgConnection

Source§

fn get_metadata_cache(&mut self) -> &mut PgMetadataCache

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Get the PgMetadataCache
Source§

impl<B> LoadConnection<B> for PgConnection
where Self: PgLoadingMode<B>,

Source§

type Cursor<'conn, 'query> = <PgConnection as PgLoadingMode<B>>::Cursor<'conn, 'query>

The cursor type returned by LoadConnection::load Read more
Source§

type Row<'conn, 'query> = <PgConnection as PgLoadingMode<B>>::Row<'conn, 'query>

The row type used as 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>,

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Executes a given query and returns any requested values Read more
Source§

impl MigrationConnection for PgConnection

Source§

fn setup(&mut self) -> QueryResult<usize>

Setup the following table: Read more
Source§

impl MultiConnectionHelper for PgConnection

Source§

fn to_any<'a>( lookup: &mut <Self::Backend as TypeMetadata>::MetadataLookup, ) -> &mut (dyn Any + 'a)

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Convert the lookup type to any
Source§

fn from_any( lookup: &mut dyn Any, ) -> Option<&mut <Self::Backend as TypeMetadata>::MetadataLookup>

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Get the lookup type from any
Source§

impl R2D2Connection for PgConnection

Available on crate feature r2d2 only.
Source§

fn ping(&mut self) -> QueryResult<()>

Check if a connection is still valid
Source§

fn is_broken(&mut self) -> bool

Checks if the connection is broken and should not be reused Read more
Source§

impl SimpleConnection for PgConnection

Source§

fn batch_execute(&mut self, query: &str) -> QueryResult<()>

Execute multiple SQL statements within the same string. Read more
Source§

impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for PgConnection
where 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>

See the traits documentation.
Source§

impl ConnectionSealed for PgConnection

Source§

impl Send for PgConnection

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<C> BoxableConnection<<C as Connection>::Backend> for C
where C: Connection + Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Maps the current connection to std::any::Any
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoSql for T

Source§

fn into_sql<T>(self) -> AsExprOf<Self, T>

Convert self to an expression for Diesel’s query builder. Read more
Source§

fn as_sql<'a, T>(&'a self) -> AsExprOf<&'a Self, T>
where &'a Self: AsExpression<T>, T: SqlType + TypedExpressionType,

Convert &self to an expression for Diesel’s query builder. Read more
Source§

impl<T> PgMetadataLookup for T

Source§

fn lookup_type( &mut self, type_name: &str, schema: Option<&str>, ) -> PgTypeMetadata

Available on crate feature postgres_backend only.
Determine the type metadata for the given type_name Read more
Source§

fn as_any<'a>(&mut self) -> &mut (dyn Any + 'a)
where T: 'a,

Available on crate features postgres_backend and i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Convert this lookup instance to a std::any::Any pointer Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T