[][src]Crate roa_pg

Stable Test codecov Rust Docs Crate version Download Version License: MIT

This crate provides integration with tokio-postgres.

Example

use roa::{App, Context, throw};
use roa::http::StatusCode;
use roa_pg::{connect, Client};
use std::sync::Arc;
use std::error::Error;
use roa::query::query_parser;
use roa::preload::*;
use async_std::task::spawn;

#[derive(Clone)]
struct State {
    pg: Arc<Client>
}

impl State {
    pub async fn new(pg_url: &str) -> Result<Self, Box<dyn Error>> {
        let (client, conn) = connect(&pg_url.parse()?).await?;
        spawn(conn);
        Ok(Self {pg: Arc::new(client)})
    }
}

async fn query(ctx: &mut Context<State>) -> roa::Result {
    let id: u32 = ctx.must_query("id")?.parse()?;
    match ctx.pg.query_opt("SELECT * FROM user WHERE id=$1", &[&id]).await? {
        Some(row) => {
            let value: String = row.get(0);
            ctx.write(value);
            Ok(())
        }
        None => throw!(StatusCode::NOT_FOUND),
    }
}

#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let url = "postgres://fred:secret@localhost/test";
    let state = State::new(url).await?;
    App::new(state)
        .gate(query_parser)
        .end(query)
        .listen("127.0.0.1:0", |addr| {
            println!("Server is listening on {}", addr)
        })?.await?;
    Ok(())
}

Modules

binary_copy

Utilities for working with the PostgreSQL binary copy format.

config

Connection configuration.

error

Errors.

row

Rows.

tls

TLS support.

types

Types.

Structs

CancelToken

The capability to request cancellation of in-progress queries on a connection.

Client

An asynchronous PostgreSQL client.

ClientConfig

Common configuration for (typically) all connections made by a program.

Column

Information about a column of a query.

Config

Connection configuration.

Connection

A connection to a PostgreSQL database.

CopyInSink

A sink for COPY ... FROM STDIN query data.

CopyOutStream

A stream of COPY ... TO STDOUT query data.

Error

An error communicating with the Postgres server.

NoTls

A MakeTlsConnect and TlsConnect implementation which simply returns an error.

Notification

An asynchronous notification.

Portal

A portal.

Row

A row of data returned from the database by a query.

RowStream

A stream of table rows.

SimpleQueryRow

A row of data returned from the database by a simple query.

SimpleQueryStream

A stream of simple query results.

Statement

A prepared statement.

TlsStream

A wrapper for tokio_rustls::client::TlsStream.

Transaction

A representation of a PostgreSQL database transaction.

TransactionBuilder

A builder for database transactions.

Enums

AsyncMessage

An asynchronous message from the server.

IsolationLevel

The isolation level of a database transaction.

SimpleQueryMessage

Message returned by the SimpleQuery stream.

Traits

GenericClient

A trait allowing abstraction over connections and transactions.

ToStatement

A trait abstracting over prepared and unprepared statements.

Functions

connect

Connect to postgres server.

connect_tls

Connect to postgres server with tls.