Go to file
Steven Fackler 9283f91429 Move doc URL
2013-12-12 22:12:31 -08:00
travis Create hstore extension for travis 2013-12-04 21:23:56 -08:00
types Rename 'self lifetimes 2013-12-11 19:22:18 -08:00
util Ignore dead code warnings in util 2013-12-11 20:27:56 -08:00
.gitignore Switch back to rustpkg test infrastructure 2013-11-02 15:06:19 -07:00
.travis.yml Upgrade to Postgres 9.3 on Travis 2013-12-10 08:45:53 -08:00
error.rs Fixes for Rust updates 2013-11-26 18:27:34 -08:00
lib.rs Switch docs over to github 2013-12-12 22:09:58 -08:00
LICENSE Initial commit 2013-07-22 21:43:51 -07:00
message.rs Use read_until in read_string 2013-12-06 21:32:44 -08:00
pool.rs Minor cleanup 2013-12-08 21:56:43 -08:00
README.md Move doc URL 2013-12-12 22:12:31 -08:00
test.rs Upgrade to Postgres 9.3 on Travis 2013-12-10 08:45:53 -08:00
THIRD_PARTY Add back md5 support 2013-11-05 22:04:12 -08:00

Rust-Postgres

A native PostgreSQL driver for Rust.

Documentation is available at http://sfackler.github.io/rust-postgres/doc/rust-postgres/.

Build Status

Overview

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 mod postgres = "github.com/sfackler/rust-postgres";
extern mod extra;

use extra::time;
use extra::time::Timespec;

use postgres::{PostgresConnection, PostgresStatement, NoSsl};
use postgres::types::ToSql;

struct Person {
    id: i32,
    name: ~str,
    time_created: Timespec,
    data: Option<~[u8]>
}

fn main() {
    let conn = PostgresConnection::connect("postgres://postgres@localhost",
                                           &NoSsl);

    conn.update("CREATE TABLE person (
                    id              SERIAL PRIMARY KEY,
                    name            VARCHAR NOT NULL,
                    time_created    TIMESTAMP NOT NULL,
                    data            BYTEA
                 )", []);
    let me = Person {
        id: 0,
        name: ~"Steven",
        time_created: time::get_time(),
        data: None
    };
    conn.update("INSERT INTO person (name, time_created, data)
                    VALUES ($1, $2, $3)",
                 [&me.name as &ToSql, &me.time_created as &ToSql,
                  &me.data as &ToSql]);

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

Requirements

  • Rust - Rust-Postgres is developed against the master branch of the Rust repository. It will most likely not build against the releases on http://www.rust-lang.org.

  • PostgreSQL 7.4 or later - Rust-Postgres speaks version 3 of the PostgreSQL protocol, which corresponds to versions 7.4 and later. If your version of Postgres was compiled in the last decade, you should be okay.

Usage

Connecting

Connect to a Postgres server using the standard URI format:

let conn = PostgresConnection::connect("postgres://user:pass@host:port/database?arg1=val1&arg2=val2",
                                       &NoSsl);

pass may be omitted if not needed. port defaults to 5432 and database defaults to the value of user if not specified. The driver supports trust, password, and md5 authentication.

Statement Preparation

Prepared statements can have parameters, represented as $n where n is an index into the parameter array starting from 1:

let stmt = conn.prepare("SELECT * FROM foo WHERE bar = $1 AND baz = $2");

Querying

A prepared statement can be executed with the query and update methods. Both methods take an array of parameters to bind to the query represented as &ToSql trait objects. update returns the number of rows affected by the query (or 0 if not applicable):

let stmt = conn.prepare("UPDATE foo SET bar = $1 WHERE baz = $2");
let updates = stmt.update([&1i32 as &ToSql, & &"biz" as &ToSql]);
println!("{} rows were updated", updates);

query returns an iterator over the rows returned from the database. The fields in a row can be accessed either by their indices or their column names, though access by index is more efficient. Like statement parameters, result columns are one-indexed.

let stmt = conn.prepare("SELECT bar, baz FROM foo");
for row in stmt.query([]) {
    let bar: i32 = row[1];
    let baz: ~str = row["baz"];
    println!("bar: {}, baz: {}", bar, baz);
}

In addition, PostgresConnection has a utility update method which is useful if a statement is only going to be executed once:

let updates = conn.update("UPDATE foo SET bar = $1 WHERE baz = $2",
                          [&1i32 as &ToSql, & &"biz" as &ToSql]);
println!("{} rows were updated", updates);

Transactions

The transaction method will start a new transaction. It returns a PostgresTransaction object which has the functionality of a PostgresConnection as well as methods to control the result of the transaction:

let trans = conn.transaction();
trans.update(...);
let stmt = trans.prepare(...);

if a_bad_thing_happened {
    trans.set_rollback();
}

if the_coast_is_clear {
    trans.set_commit();
}

drop(trans);

The transaction will be active until the PostgresTransaction object falls out of scope. A transaction will commit by default. Nested transactions are supported via savepoints.

Error Handling

The methods described above will fail if there is an error. For each of these methods, there is a second variant prefixed with try_ which returns a Result:

match conn.try_update(query, params) {
    Ok(updates) => println!("{} rows were updated", updates),
    Err(err) => match err.code {
        NotNullViolation => println!("Something was NULL that shouldn't be"),
        SyntaxError => println!("Invalid query syntax"),
        _ => println!("A bad thing happened: {}", err.message),
    }
}

Connection Pooling

A very basic fixed-size connection pool is provided in the pool module. A single pool can be shared across tasks and get_connection will block until a connection is available.

let pool = PostgresConnectionPool::new("postgres://postgres@localhost",
                                       NoSsl, 5);

for _ in range(0, 10) {
    let pool = pool.clone();
    do task::spawn {
        let conn = pool.get_connection();
        conn.query(...);
    }
}

Type Correspondence

Rust-Postgres enforces a strict correspondence between Rust types and Postgres types. The driver currently supports the following conversions:

Rust Type Postgres Type
bool BOOL
i8 "char"
i16 SMALLINT, SMALLSERIAL
i32 INT, SERIAL
i64 BIGINT, BIGSERIAL
f32 REAL
f64 DOUBLE PRECISION
str VARCHAR, CHAR(n), TEXT
[u8] BYTEA
extra::json::Json JSON
extra::uuid::Uuid UUID
extra::time::Timespec TIMESTAMP, TIMESTAMP WITH TIME ZONE
types::range::Range<i32> INT4RANGE
types::range::Range<i64> INT8RANGE
types::range::Range<Timespec> TSRANGE, TSTZRANGE
types::array::ArrayBase<Option<bool>> BOOL[], BOOL[][], ...
types::array::ArrayBase<Option<~[u8]>> BYTEA[], BYTEA[][], ...
types::array::ArrayBase<Option<i8>> "char"[], "char"[][], ...
types::array::ArrayBase<Option<i16>> INT2[], INT2[][], ...
types::array::ArrayBase<Option<i32>> INT4[], INT4[][], ...
types::array::ArrayBase<Option<~str>> TEXT[], CHAR(n)[], VARCHAR[], TEXT[][], ...
types::array::ArrayBase<Option<Json>> JSON[], JSON[][], ...
types::array::ArrayBase<Option<i64>> INT8[], INT8[][], ...
types::array::ArrayBase<Option<Timespec>> TIMESTAMP[], TIMESTAMPTZ[], TIMESTAMP[][], ...
types::array::ArrayBase<Option<f32>> FLOAT4[], FLOAT4[][], ...
types::array::ArrayBase<Option<f64>> FLOAT8[], FLOAT8[][], ...
types::array::ArrayBase<Option<Uuid>> UUID[], UUID[][], ...
types::array::ArrayBase<Option<Range<i32>>> INT4RANGE[], INT4RANGE[][], ...
types::array::ArrayBase<Option<Range<Timespec>>> TSRANGE[], TSTZRANGE[], TSRANGE[][], ...
types::array::ArrayBase<Option<Range<i64>>> INT8RANGE[], INT8RANGE[][], ...
std::hashmap::HashMap<~str, Option<~str>> HSTORE

More conversions can be defined by implementing the ToSql and FromSql traits.

Development

Like Rust itself, Rust-Postgres is still in the early stages of development, so don't be surprised if APIs change and things break. If something's not working properly, file an issue or submit a pull request!