Skip to content

drhodes/rust-postgres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bindings for postgres (libpq 9.1.1)

These aren't ready yet :)

Testing

To test the bindings you'll need to get into postgres as a superuser and create a user

Here's what my session looked like

~$ psql
psql (9.1.1)
Type "help" for help.

postgres=# CREATE USER rustuser WITH PASSWORD 'rustpass';
CREATE ROLE

postgres=# CREATE DATABASE rust_test_db;
CREATE DATABASE

Here's one of the tests

totally up in the air, I'm still learning rust. If you see something obviously brain dead please make an issue.

#[test]
fn AlgebraTest() {
    // grab a connection
    let conn = TestConnect();

    // define a person 
    let person = Table("person", [
        ("did",    [Unique], SerialM),
        ("name",   [Insert], VarCharM(255))
    ]);

    // define a movie
    let movie = Table("movie_algebra", [
        ("did",      [Unique],                                 SerialM),
        ("title",    [Insert],                                 VarCharM(255)),
        ("year",     [Insert],                                 Int32M),
        ("director", [Insert, ForeignKey(copy person, "did")], Int32M ) 
    ]);

    // Drop some tables, just in case they're there.
    Assure(conn.Exec(DropTable(person)));
    Assure(conn.Exec(DropTable(movie)));

    // Create some tables
    Assure(conn.Exec(CreateTable(person)));
    Assure(conn.Exec(CreateTable(movie)));

    // Test the fancy insert.
    let q1 = person.Insert( [VarChar("lucas")] );

    assert q1 == "insert into person (name) VALUES ('lucas')";
    Assure(conn.Exec(q1));
                                                                                                                     
    unsafe {
        // no fancy select yet. 
        // Grab the person.did of lucas
        let res = Assure(conn.Exec("select * from person where name = 'lucas'"));
        let lucas_did = copy GetRow(res, 0)[0]; // this index munging goes away

		// 
        let q2 =  movie.Insert( [VarChar("starwars"), Int32(1977), lucas_did]);
        assert q2 ==
            "insert into movie_algebra (title,year,director) VALUES ('starwars',1977,1)";
        Assure(conn.Exec(q2));		   
    }	

Preliminary Results

If postgres is setup correctly and rustc can find your libpq then you might be able to get the following output

$ rustc --bin pq.rc --test -W no-old-vecs && ./pq

 ...
 result: ok. 22 passed; 0 failed; 0 ignored

About

rust bindings for postgres

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages