[][src]Struct orma::Connection

pub struct Connection { /* fields omitted */ }

Wrapper over tokio_postgres::Client

Example

This code runs with edition 2018
 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

impl Connection[src]

pub async fn batch_execute<'_, '_>(
    &'_ self,
    query: &'_ str
) -> Result<(), DbError>
[src]

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.

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

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.

pub async fn prepare<'_, '_>(
    &'_ self,
    query: &'_ str
) -> Result<Statement, DbError>
[src]

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.

pub async fn simple_query<'_, '_>(
    &'_ self,
    query: &'_ str
) -> Result<Vec<SimpleQueryMessage>, DbError>
[src]

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

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.

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

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.

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

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.

pub async fn transaction<'_>(&'_ mut self) -> Result<(), DbError>[src]

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
}

pub async fn commit<'_>(&'_ mut self) -> Result<(), DbError>[src]

Commits a transaction or releases a savepoint

pub async fn rollback<'_>(&'_ mut self) -> Result<(), DbError>[src]

Rolls back a transaction or a savepoint

Trait Implementations

impl From<Client> for Connection[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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