We provide a new set of bindings over Postgresql
libpq C library, that
focuses to be Rusty but stick to the original library as much as possible.
The main features that we want to support are:
- tracing
- collecting notice messages
- query execution
- pretty-printing results
The main repo is on Github, which hosts the corresponding crate doc too.
Example: catching PostgreSQL notices
A concise unit test that connects via environment variables, installs a notice processor to collect server notices, executes a DO block that emits two notices and a SELECT, prints the result to a file, and asserts on the response and collected notices:
#[test]
fn catch_notices() {
let mut conn =
PgConn::connect_db_env_vars().expect("Failed to create PGconn from connection string.");
conn.trace("trace.log");
let mut w = Vec::new();
let _w_pusher = conn.set_notice_processor(|s| w.push(s));
assert_eq!(conn.status(), ConnStatusType_CONNECTION_OK);
let query = "do $$ begin raise notice 'Hello,'; raise notice 'world!'; end $$; select 1 as one, 2 as two;";
let mut res = conn.exec(query).expect("Failed to execute query.");
res.print("res.out", true, true, "|", true, false, false, false);
println!("Result:\n{}", res);
let s = fs::read_to_string("res.out").expect("Should have been able to read the file");
assert_eq!(res.to_string(), s);
assert_eq!(res.status(), ExecStatusType_PGRES_TUPLES_OK);
assert_eq!(res.error_message(), "");
assert!(res.error_field(PG_DIAG_SEVERITY).is_none());
assert_eq!(res.cmd_status(), "SELECT 1");
assert_eq!(w.len(), 2);
assert_eq!(w[0], "NOTICE: Hello,\n");
assert_eq!(w[1], "NOTICE: world!\n");
}