@@ -3,7 +3,6 @@ use std::c_str::{ToCStr, CString};
33use std:: str;
44use std:: ptr;
55use std:: libc:: { c_void, c_char, c_int} ;
6- use std:: cast;
76use std:: iterator:: RandomAccessIterator ;
87use 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
190176pub struct PostgresStatement < ' self > {
191- priv conn : & ' self PostgresConnection < ' self > ,
177+ priv conn : & ' self PostgresConnection ,
192178 priv name : ~str ,
193179 priv num_params : uint
194180}
0 commit comments