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 sepreatly. 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

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

The backend this type connects to
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
The transaction manager implementation used by this connection
Establishes a new connection to the database Read more
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 Read more
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
Executes the given function inside of a database transaction Read more
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 Read more
Executes the given function inside a transaction, but does not commit it. Panics if the given function returns an error. Read more
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
The cursor type returned by LoadConnection::load Read more
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
The row type used as Iterator::Item for the iterator implementation of ConnectionGatWorkaround::Cursor Read more
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
The cursor type returned by LoadConnection::load Read more
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
The row type used as Iterator::Item for the iterator implementation of ConnectionGatWorkaround::Cursor Read more
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Get the PgMetadataCache
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
Setup the following table: Read more
Check if a connection is still valid
Checks if the connection is broken and should not be reused Read more
Execute multiple SQL statements within the same string. Read more
See the traits documentation.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Convert self to an expression for Diesel’s query builder. Read more
Convert &self to an expression for Diesel’s query builder. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.