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