|
| 1 | +extern mod sql; |
| 2 | + |
| 3 | +use sql::{ToSqlStr, FromSqlStr}; |
| 4 | + |
| 5 | +use std::str; |
| 6 | +use std::ptr; |
| 7 | + |
| 8 | +mod ffi { |
| 9 | + use std::libc::{c_char, c_int, c_uint, c_void}; |
| 10 | + |
| 11 | + pub type PGconn = c_void; |
| 12 | + pub type PGresult = c_void; |
| 13 | + pub type OId = c_uint; |
| 14 | + |
| 15 | + pub enum ConnStatusType { |
| 16 | + CONNECTION_OK, |
| 17 | + CONNECTION_BAD, |
| 18 | + CONNECTION_STARTED, |
| 19 | + CONNECTION_MADE, |
| 20 | + CONNECTION_AWAITING_RESPONSE, |
| 21 | + CONNECTION_AUTH_OK, |
| 22 | + CONNECTION_SETENV, |
| 23 | + CONNECTION_SSL_STARTUP, |
| 24 | + CONNECTION_NEEDED |
| 25 | + } |
| 26 | + |
| 27 | + pub enum ExecStatusType { |
| 28 | + PGRES_EMPTY_QUERY = 0, |
| 29 | + PGRES_COMMAND_OK, |
| 30 | + PGRES_TUPLES_OK, |
| 31 | + PGRES_COPY_OUT, |
| 32 | + PGRES_COPY_IN, |
| 33 | + PGRES_BAD_RESPONSE, |
| 34 | + PGRES_NONFATAL_ERROR, |
| 35 | + PGRES_FATAL_ERROR, |
| 36 | + PGRES_COPY_BOTH, |
| 37 | + PGRES_SINGLE_TUPLE |
| 38 | + } |
| 39 | + |
| 40 | + #[link_args = "-lpq"] |
| 41 | + extern "C" { |
| 42 | + fn PQconnectdb(conninfo: *c_char) -> *PGconn; |
| 43 | + fn PQfinish(conn: *PGconn); |
| 44 | + fn PQstatus(conn: *PGconn) -> ConnStatusType; |
| 45 | + fn PQerrorMessage(conn: *PGconn) -> *c_char; |
| 46 | + fn PQexecParams(conn: *PGconn, command: *c_char, nParams: c_int, |
| 47 | + paramTypes: *OId, paramValues: **c_char, |
| 48 | + paramLengths: *c_int, paramFormats: *c_int, |
| 49 | + resultFormat: c_int) -> *PGresult; |
| 50 | + fn PQresultStatus(res: *PGresult) -> ExecStatusType; |
| 51 | + fn PQresultErrorMessage(res: *PGresult) -> *c_char; |
| 52 | + fn PQclear(res: *PGresult); |
| 53 | + fn PQntuples(res: *PGresult) -> c_int; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +fn open(name: &str) -> Result<~Connection, ~str> { |
| 58 | + unsafe { |
| 59 | + let conn = ~Connection {conn: do name.as_c_str |c_name| { |
| 60 | + ffi::PQconnectdb(c_name) |
| 61 | + }}; |
| 62 | + |
| 63 | + match ffi::PQstatus(conn.conn) { |
| 64 | + ffi::CONNECTION_OK => Ok(conn), |
| 65 | + _ => Err(str::raw::from_c_str(ffi::PQerrorMessage(conn.conn))) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +pub struct Connection { |
| 71 | + priv conn: *ffi::PGconn |
| 72 | +} |
| 73 | + |
| 74 | +impl Drop for Connection { |
| 75 | + fn drop(&self) { |
| 76 | + unsafe { |
| 77 | + ffi::PQfinish(self.conn) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl Connection { |
| 83 | + fn query(&self, query: &str, params: &[@ToSqlStr]) -> Result<~RowIterator, ~str> { |
| 84 | + Err(~"foo") |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +pub struct RowIterator { |
| 89 | + priv res: *ffi::PGresult, |
| 90 | + priv row: Row |
| 91 | +} |
| 92 | + |
| 93 | +impl Drop for RowIterator { |
| 94 | + fn drop(&self) { |
| 95 | + unsafe { |
| 96 | + ffi::PQclear(self.res) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl<'self> Iterator<&'self Row<'self>> for RowIterator { |
| 102 | + fn next(&mut self) -> Option<&'self Row> { |
| 103 | + unsafe { |
| 104 | + if ffi::PQntuples(self.res) == self. { |
| 105 | + return None; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +pub struct Row<'self> { |
| 112 | + priv res: *ffi::PGresult, |
| 113 | + priv row: uint |
| 114 | +} |
0 commit comments