Skip to main content

Connection

Struct Connection 

Source
pub struct Connection { /* private fields */ }
Expand description

Wrapper over tokio_postgres::Client

§Example

 use std::env;
 use orma::{Connection};

async fn create_connection() -> Connection {
    let connection_string = format!(
        "host={host} port={port} dbname={dbname} user={user} password={password}",
        host = &env::var("INTRARED_DB_HOSTNAME").unwrap_or_else(|_| "localhost".to_string()),
        port = env::var("INTRARED_DB_PORT").unwrap_or_else(|_| "5433".to_string()),
        dbname = env::var("INTRARED_DB_NAME").unwrap_or_else(|_| "pgactix".to_string()),
        user = env::var("INTRARED_DB_USERNAME").unwrap_or_else(|_| "pgactix".to_string()),
        password = env::var("INTRARED_DB_PASSWORD").unwrap_or_else(|_| "pgactix".to_string()),
    );
    let (client, conn) = tokio_postgres::connect(&connection_string, tokio_postgres::NoTls)
        .await
        .unwrap();
    tokio::spawn(conn);
    Connection::from(client)
}

Implementations§

Source§

impl Connection

Source

pub async fn batch_execute(&self, query: &str) -> Result<(), DbError>

Executes a sequence of SQL statements using the simple query protocol.

Statements should be separated by semicolons. If an error occurs, execution of the sequence will stop at that point. This is intended for use when, for example, initializing a database schema.

Source

pub async fn execute<T>( &self, statement: &T, params: &[&(dyn ToSql + Sync)], ) -> Result<u64, DbError>
where T: ?Sized + ToStatement,

Executes a statement, returning the number of rows modified.

A statement may contain parameters, specified by $n, where n is the index of the parameter of the list provided, 1-indexed.

The statement argument can either be a Statement, or a raw query string. If the same statement will be repeatedly executed (perhaps with different query parameters), consider preparing the statement up front with the prepare method.

If the statement does not modify any rows (e.g. SELECT), 0 is returned.

Source

pub async fn prepare(&self, query: &str) -> Result<Statement, DbError>

Creates a new prepared statement.

Prepared statements can be executed repeatedly, and may contain query parameters (indicated by $1, $2, etc), which are set when executed.

Prepared statements can only be used with the connection that created them.

Source

pub async fn simple_query( &self, query: &str, ) -> Result<Vec<SimpleQueryMessage>, DbError>

Source

pub async fn query<T>( &self, statement: &T, params: &[&(dyn ToSql + Sync)], ) -> Result<Vec<Row>, DbError>
where T: ?Sized + ToStatement,

Executes a statement, returning a vector of the resulting rows.

A statement may contain parameters, specified by $n, where n is the index of the parameter of the list provided, 1-indexed.

The statement argument can either be a Statement, or a raw query string. If the same statement will be repeatedly executed (perhaps with different query parameters), consider preparing the statement up front with the prepare method.

Source

pub async fn query_one<T>( &self, statement: &T, params: &[&(dyn ToSql + Sync)], ) -> Result<Row, DbError>
where T: ?Sized + ToStatement,

Executes a statement, returning a single row.

A statement may contain parameters, specified by $n, where n is the index of the parameter of the list provided, 1-indexed.

The statement argument can either be a Statement, or a raw query string. If the same statement will be repeatedly executed (perhaps with different query parameters), consider preparing the statement up front with the prepare method.

Source

pub async fn query_opt<T>( &self, statement: &T, params: &[&(dyn ToSql + Sync)], ) -> Result<Option<Row>, DbError>
where T: ?Sized + ToStatement,

Executes a statement, returning zero or one row.

A statement may contain parameters, specified by $n, where n is the index of the parameter of the list provided, 1-indexed.

The statement argument can either be a Statement, or a raw query string. If the same statement will be repeatedly executed (perhaps with different query parameters), consider preparing the statement up front with the prepare method.

Source

pub async fn transaction(&mut self) -> Result<(), DbError>

Begins a transaction or creates a savepoint if a transaction already started

§Example

from dbjoin.rs

pub async fn save_items(&self, conn: &mut Connection) -> Result<(), DbError> {
    conn.transaction().await?;
    let result = async {
        match (self.join_table.as_ref(), self.items_fk.as_ref()) {
            (Some(join_table), Some(items_fk)) => {
                self.save_items_table_join(&join_table, &items_fk, conn)
                    .await?;
            }
            _ => {
                self.save_items_simple_join(conn).await?;
            }
        };
        conn.commit().await?;
        Ok(())
    }
    .await;
    if result.is_err() {
        conn.rollback().await?;
    }
    result
}
Source

pub async fn commit(&mut self) -> Result<(), DbError>

Commits a transaction or releases a savepoint

Source

pub async fn rollback(&mut self) -> Result<(), DbError>

Rolls back a transaction or a savepoint

Trait Implementations§

Source§

impl From<Client> for Connection

Source§

fn from(client: Client) -> Self

Converts to this type from the input type.

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<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> Same for T

Source§

type Output = T

Should always be Self
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