Skip to content

Commit 4497035

Browse files
committed
Remove configurable notice handler
1 parent 5b416ce commit 4497035

2 files changed

Lines changed: 34 additions & 32 deletions

File tree

src/postgres/lib.rs

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::c_str::{ToCStr, CString};
33
use std::str;
44
use std::ptr;
55
use std::libc::{c_void, c_char, c_int};
6-
use std::cast;
76
use std::iterator::RandomAccessIterator;
87
use std::vec;
98

@@ -55,55 +54,46 @@ mod ffi {
5554
}
5655
}
5756

58-
pub struct PostgresConnection<'self> {
57+
pub struct PostgresConnection {
5958
priv conn: *ffi::PGconn,
6059
priv next_stmt_id: Cell<uint>,
61-
priv notice_handler: &'self fn(~str)
6260
}
6361

64-
pub fn log_notice_handler(notice: ~str) {
65-
if notice.starts_with("DEBUG") {
66-
debug!("%s", notice);
67-
} else if notice.starts_with("NOTICE") ||
68-
notice.starts_with("INFO") ||
69-
notice.starts_with("LOG") {
70-
info!("%s", notice);
71-
} else if notice.starts_with("WARNING") {
72-
warn!("%s", notice);
62+
extern "C" fn notice_handler(_arg: *c_void, message: *c_char) {
63+
let message = unsafe { str::raw::from_c_str(message) };
64+
if message.starts_with("DEBUG") {
65+
debug!("%s", message);
66+
} else if message.starts_with("NOTICE") ||
67+
message.starts_with("INFO") ||
68+
message.starts_with("LOG") {
69+
info!("%s", message);
70+
} else if message.starts_with("WARNING") {
71+
warn!("%s", message);
7372
} else {
74-
error!("%s", notice);
75-
}
76-
}
77-
78-
extern "C" fn notice_handler(arg: *c_void, message: *c_char) {
79-
unsafe {
80-
let conn: *PostgresConnection = cast::transmute(arg);
81-
((*conn).notice_handler)(str::raw::from_c_str(message));
73+
error!("%s", message);
8274
}
8375
}
8476

8577
#[unsafe_destructor]
86-
impl<'self> Drop for PostgresConnection<'self> {
78+
impl Drop for PostgresConnection {
8779
fn drop(&self) {
8880
unsafe { ffi::PQfinish(self.conn) }
8981
}
9082
}
9183

92-
impl<'self> PostgresConnection<'self> {
84+
impl PostgresConnection {
9385
fn get_error(&self) -> ~str {
9486
unsafe { str::raw::from_c_str(ffi::PQerrorMessage(self.conn)) }
9587
}
9688
}
9789

98-
impl<'self> PostgresConnection<'self> {
90+
impl PostgresConnection {
9991
pub fn new(uri: &str) -> Result<~PostgresConnection, ~str> {
10092
unsafe {
10193
let conn = ~PostgresConnection {conn: do uri.with_c_str |c_uri| {
10294
ffi::PQconnectdb(c_uri)
103-
}, next_stmt_id: Cell::new(0), notice_handler: log_notice_handler};
104-
let arg: *PostgresConnection = &*conn;
105-
ffi::PQsetNoticeProcessor(conn.conn, notice_handler,
106-
arg as *c_void);
95+
}, next_stmt_id: Cell::new(0)};
96+
ffi::PQsetNoticeProcessor(conn.conn, notice_handler, ptr::null());
10797

10898
match ffi::PQstatus(conn.conn) {
10999
ffi::CONNECTION_OK => Ok(conn),
@@ -112,10 +102,6 @@ impl<'self> PostgresConnection<'self> {
112102
}
113103
}
114104

115-
pub fn set_notice_handler(&mut self, handler: &'self fn(~str)) {
116-
self.notice_handler = handler;
117-
}
118-
119105
pub fn prepare<'a>(&'a self, query: &str)
120106
-> Result<~PostgresStatement<'a>, ~str> {
121107
let id = self.next_stmt_id.take();
@@ -188,7 +174,7 @@ impl<'self> PostgresConnection<'self> {
188174
}
189175

190176
pub struct PostgresStatement<'self> {
191-
priv conn: &'self PostgresConnection<'self>,
177+
priv conn: &'self PostgresConnection,
192178
priv name: ~str,
193179
priv num_params: uint
194180
}

src/postgres/test.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ fn test_basic() {
3737
};
3838
}
3939

40+
#[test]
41+
fn test_multiple_stmts() {
42+
let conn = chk!(PostgresConnection::new("postgres://postgres@localhost"));
43+
44+
do conn.in_transaction |conn| {
45+
chk!(conn.update("CREATE TABLE foo (id INT PRIMARY KEY)", []));
46+
let stmt1 = chk!(conn.prepare("INSERT INTO foo (id) VALUES (101)"));
47+
let stmt2 = chk!(conn.prepare("INSERT INTO foo (id) VALUES (102)"));
48+
49+
chk!(stmt1.update([]));
50+
chk!(stmt2.update([]));
51+
52+
Err::<(), ~str>(~"")
53+
};
54+
}
55+
4056
#[test]
4157
fn test_params() {
4258
let conn = chk!(PostgresConnection::new("postgres://postgres@localhost"));

0 commit comments

Comments
 (0)