Crate postgres

source ·
Expand description

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§

  • Error types.
  • Types and traits for SSL adaptors.
  • Query result rows.
  • Traits dealing with Postgres data types

Macros§

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

Structs§

Enums§

  • Specifies the target server to connect to.
  • An enumeration of transaction isolation levels.
  • Specifies the SSL support requested for a new connection.

Traits§

  • A trait allowing abstraction over connections and transactions
  • Trait for types that can handle Postgres notice messages
  • A trait implemented by types that can be converted into a ConnectParams.

Functions§

Type Aliases§

  • A type alias of the result returned by many methods.