Skip to content

Commit c6b7215

Browse files
committed
Added utility methods
1 parent bbf2817 commit c6b7215

5 files changed

Lines changed: 193 additions & 8 deletions

File tree

src/postgres/lib.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

src/postgres/test.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern mod postgres;
2+
3+
#[test]
4+
fn test_conn() {
5+
printfln!("%?", postgres::open("postgres://postgres@localhost"));
6+
}

src/sql/lib.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
// pub trait Connection<'self, R: Rows<'self>> {
3+
// fn query(&self, query: &str, params: &[@ToSqlStr]) -> Result<~R, ~str>;
4+
// }
5+
6+
// pub trait Rows<'self, R: Row, I: Iterator<&'self R>>: Container {
7+
// fn iter(&self) -> I;
8+
// }
9+
10+
// pub trait Row: Container {
11+
// fn get<T: FromSqlStr>(&self) -> Option<T>;
12+
// }
13+
14+
pub trait ToSqlStr {
15+
fn to_sql_str(&self) -> ~str;
16+
}
17+
18+
impl ToSqlStr for int {
19+
fn to_sql_str(&self) -> ~str {
20+
self.to_str()
21+
}
22+
}
23+
24+
impl ToSqlStr for uint {
25+
fn to_sql_str(&self) -> ~str {
26+
self.to_str()
27+
}
28+
}
29+
30+
pub trait FromSqlStr {
31+
fn from_sql_str(&str) -> Option<Self>;
32+
}
33+
34+
impl FromSqlStr for int {
35+
fn from_sql_str(s: &str) -> Option<int> {
36+
FromStr::from_str(s)
37+
}
38+
}
39+
40+
impl FromSqlStr for uint {
41+
fn from_sql_str(s: &str) -> Option<uint> {
42+
FromStr::from_str(s)
43+
}
44+
}

src/sqlite3/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ impl Connection {
7878
_ => Err(self.get_error())
7979
}
8080
}
81+
82+
pub fn update(&self, query: &str) -> Result<uint, ~str> {
83+
self.prepare(query).chain(|stmt| stmt.update())
84+
}
85+
86+
pub fn query<T>(&self, query: &str, blk: &fn (&mut ResultIterator) -> T)
87+
-> Result<T, ~str> {
88+
let stmt = match self.prepare(query) {
89+
Ok(stmt) => stmt,
90+
Err(err) => return Err(err)
91+
};
92+
93+
let mut it = match stmt.query() {
94+
Ok(it) => it,
95+
Err(err) => return Err(err)
96+
};
97+
98+
Ok(blk(&mut it))
99+
}
81100
}
82101

83102
pub struct PreparedStatement<'self> {

src/sqlite3/test.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ extern mod sqlite3;
33
#[test]
44
fn test() {
55
let conn = sqlite3::open("db").unwrap();
6-
conn.prepare("DROP TABLE IF EXISTS foo").unwrap().update();
7-
conn.prepare("CREATE TABLE foo (
6+
conn.update("DROP TABLE IF EXISTS foo");
7+
conn.update("CREATE TABLE foo (
88
id BIGINT PRIMARY KEY
9-
)").unwrap().update();
10-
conn.prepare("INSERT INTO foo (id) VALUES (101), (102)").unwrap().update();
11-
let stmt = conn.prepare("SELECT id FROM foo").unwrap();
12-
for stmt.query().unwrap().advance |row| {
13-
printfln!("%u %d", row.len(), row.get(0).get());
14-
}
9+
)");
10+
conn.update("INSERT INTO foo (id) VALUES (101), (102)");
11+
12+
do conn.query("SELECT id FROM foo") |it| {
13+
for it.advance |row| {
14+
printfln!("%u %d", row.len(), row.get(0).get());
15+
}
16+
};
1517
}

0 commit comments

Comments
 (0)