Skip to content

Commit 970b5f6

Browse files
committed
Allow custom notice handling
It was a tossup between a ~fn and a ~Trait *shrug*
1 parent db634b5 commit 970b5f6

2 files changed

Lines changed: 47 additions & 9 deletions

File tree

src/lib.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,16 @@ use types::{PostgresType, ToSql, FromSql};
5454
mod message;
5555
mod types;
5656

57-
fn handle_notice_response(fields: ~[(u8, ~str)]) {
58-
let err = PostgresDbError::new(fields);
59-
info2!("{}: {}", err.severity, err.message);
57+
pub trait PostgresNoticeHandler {
58+
fn handle(&mut self, notice: PostgresDbError);
59+
}
60+
61+
pub struct DefaultNoticeHandler;
62+
63+
impl PostgresNoticeHandler for DefaultNoticeHandler {
64+
fn handle(&mut self, notice: PostgresDbError) {
65+
info2!("{}: {}", notice.severity, notice.message);
66+
}
6067
}
6168

6269
#[deriving(ToStr)]
@@ -121,7 +128,8 @@ impl PostgresDbError {
121128

122129
pub struct PostgresConnection {
123130
priv stream: Cell<TcpStream>,
124-
priv next_stmt_id: Cell<int>
131+
priv next_stmt_id: Cell<int>,
132+
priv notice_handler: Cell<~PostgresNoticeHandler>
125133
}
126134

127135
impl Drop for PostgresConnection {
@@ -171,7 +179,8 @@ impl PostgresConnection {
171179

172180
let conn = PostgresConnection {
173181
stream: Cell::new(stream),
174-
next_stmt_id: Cell::new(0)
182+
next_stmt_id: Cell::new(0),
183+
notice_handler: Cell::new(~DefaultNoticeHandler as ~PostgresNoticeHandler)
175184
};
176185

177186
args.push((~"client_encoding", ~"UTF8"));
@@ -250,10 +259,13 @@ impl PostgresConnection {
250259
};
251260

252261
match msg {
253-
NoticeResponse { fields } =>
254-
handle_notice_response(fields),
262+
NoticeResponse { fields } => {
263+
let mut handler = self.notice_handler.take();
264+
handler.handle(PostgresDbError::new(fields));
265+
self.notice_handler.put_back(handler);
266+
}
255267
ParameterStatus { parameter, value } =>
256-
info!("Parameter %s = %s", parameter, value),
268+
debug!("Parameter %s = %s", parameter, value),
257269
msg => return msg
258270
}
259271
}
@@ -298,6 +310,13 @@ impl PostgresConnection {
298310
}
299311
}
300312

313+
pub fn set_notice_handler(&self, handler: ~PostgresNoticeHandler)
314+
-> ~PostgresNoticeHandler {
315+
let old_handler = self.notice_handler.take();
316+
self.notice_handler.put_back(handler);
317+
old_handler
318+
}
319+
301320
pub fn prepare<'a>(&'a self, query: &str) -> NormalPostgresStatement<'a> {
302321
match self.try_prepare(query) {
303322
Ok(stmt) => stmt,

src/test.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use extra::uuid::Uuid;
88
use std::f32;
99
use std::f64;
1010

11-
use postgres::{DbError,
11+
use postgres::{PostgresNoticeHandler,
12+
DbError,
1213
DnsError,
1314
MissingPassword,
1415
Position,
@@ -338,6 +339,24 @@ fn test_get_named_fail() {
338339
let _: i32 = result.next().unwrap()["asdf"];
339340
}
340341

342+
#[test]
343+
fn test_custom_notice_handler() {
344+
static mut count: uint = 0;
345+
struct Handler;
346+
347+
impl PostgresNoticeHandler for Handler {
348+
fn handle(&mut self, _notice: PostgresDbError) {
349+
unsafe { count += 1; }
350+
}
351+
}
352+
353+
let conn = PostgresConnection::connect("postgres://postgres@localhost");
354+
conn.set_notice_handler(~Handler as ~PostgresNoticeHandler);
355+
conn.update("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []);
356+
357+
assert_eq!(unsafe { count }, 1);
358+
}
359+
341360
#[test]
342361
fn test_plaintext_pass() {
343362
PostgresConnection::connect("postgres://pass_user:password@localhost");

0 commit comments

Comments
 (0)