Crate postgres [] [src]

Rust-Postgres is a pure-Rust frontend for the popular PostgreSQL database. It exposes a high level interface in the vein of JDBC or Go's database/sql package.

extern crate postgres;

use postgres::{Connection, SslMode};

struct Person {
    id: i32,
    name: String,
    data: Option<Vec<u8>>
}

fn main() {
    let conn = Connection::connect("postgresql://postgres@localhost", &SslMode::None)
            .unwrap();

    conn.execute("CREATE TABLE person (
                    id              SERIAL PRIMARY KEY,
                    name            VARCHAR NOT NULL,
                    data            BYTEA
                  )", &[]).unwrap();
    let me = Person {
        id: 0,
        name: "Steven".to_string(),
        data: None
    };
    conn.execute("INSERT INTO person (name, data) VALUES ($1, $2)",
                 &[&me.name, &me.data]).unwrap();

    let stmt = conn.prepare("SELECT id, name, data FROM person").unwrap();
    for row in stmt.query(&[]).unwrap() {
        let person = Person {
            id: row.get(0),
            name: row.get(1),
            data: row.get(2)
        };
        println!("Found person {}", person.name);
    }
}

Modules

types

Traits dealing with Postgres data types

Macros

accepts!

Generates a simple implementation of ToSql::accepts which accepts the types passed to it.

to_sql_checked!

Generates an implementation of ToSql::to_sql_checked.

Structs

CancelData

Contains information necessary to cancel queries for a session.

Column

Information about a column of the result of a query.

ConnectParams

Information necessary to open a new connection to a Postgres server.

Connection

A connection to a Postgres database.

CopyInStatement

A prepared COPY FROM STDIN statement.

DbError

A Postgres error or notice.

LazyRows

A lazily-loaded iterator over the resulting rows of a query.

LoggingNoticeHandler

A notice handler which logs at the info level.

Notification

An asynchronous notification.

Notifications

An iterator over asynchronous notifications.

Row

A single result row of a query.

Rows

The resulting rows of a query.

RowsIntoIter

An owning iterator over Rows.

RowsIter

An iterator over Rows.

Slice

An adapter type mapping slices to Postgres arrays.

Statement

A prepared statement.

Transaction

Represents a transaction on a database connection.

UserInfo

Authentication information.

VecStreamIterator

An adapter type implementing StreamIterator for a Vec<Box<ToSql>>.

Enums

ConnectError

Reasons a new Postgres connection could fail.

ConnectTarget

Specifies the target server to connect to.

Error

An error encountered when communicating with the Postgres server.

ErrorPosition

Represents the position of an error in a query.

IsolationLevel

An enumeration of transaction isolation levels.

Kind

Represents the kind of a Postgres type.

SqlState

SQLSTATE error codes

SslMode

Specifies the SSL support requested for a new connection.

Type

A Postgres type.

Traits

FromSql

A trait for types that can be created from a Postgres value.

GenericConnection

A trait allowing abstraction over connections and transactions

HandleNotice

Trait for types that can handle Postgres notice messages

IntoConnectParams

A trait implemented by types that can be converted into a ConnectParams.

RowIndex

A trait implemented by types that can index into columns of a row.

StreamIterator

An Iterator variant which returns borrowed values.

ToSql

A trait for types that can be converted into Postgres values.

Functions

cancel_query

Attempts to cancel an in-progress query.

Type Definitions

Oid

A Postgres OID.

Result

A type alias of the result returned by many methods.