@@ -12,7 +12,7 @@ use extra::url::{UserInfo, Url};
12
12
use std:: cell:: Cell ;
13
13
use std:: hashmap:: HashMap ;
14
14
use std:: rt:: io:: { Writer , io_error, Decorator } ;
15
- use std:: rt:: io:: mem :: MemWriter ;
15
+ use std:: rt:: io:: buffered :: BufferedStream ;
16
16
use std:: rt:: io:: net;
17
17
use std:: rt:: io:: net:: ip;
18
18
use std:: rt:: io:: net:: ip:: SocketAddr ;
@@ -131,15 +131,15 @@ impl PostgresDbError {
131
131
}
132
132
133
133
pub struct PostgresConnection {
134
- priv stream : Cell < TcpStream > ,
134
+ priv stream : Cell < BufferedStream < TcpStream > > ,
135
135
priv next_stmt_id : Cell < int > ,
136
136
priv notice_handler : Cell < ~PostgresNoticeHandler >
137
137
}
138
138
139
139
impl Drop for PostgresConnection {
140
140
fn drop ( & self ) {
141
141
do io_error:: cond. trap ( |_| { } ) . inside {
142
- self . write_message ( & Terminate ) ;
142
+ self . write_messages ( [ & Terminate ] ) ;
143
143
}
144
144
}
145
145
}
@@ -182,7 +182,7 @@ impl PostgresConnection {
182
182
} ;
183
183
184
184
let conn = PostgresConnection {
185
- stream : Cell :: new ( stream) ,
185
+ stream : Cell :: new ( BufferedStream :: new ( stream) ) ,
186
186
next_stmt_id : Cell :: new ( 0 ) ,
187
187
notice_handler : Cell :: new ( ~DefaultNoticeHandler
188
188
as ~PostgresNoticeHandler )
@@ -197,10 +197,10 @@ impl PostgresConnection {
197
197
if !path. is_empty ( ) {
198
198
args. push ( ( ~"database", path) ) ;
199
199
}
200
- conn. write_message ( & StartupMessage {
200
+ conn. write_messages ( [ & StartupMessage {
201
201
version : message:: PROTOCOL_VERSION ,
202
202
parameters : args. as_slice ( )
203
- } ) ;
203
+ } ] ) ;
204
204
205
205
match conn. handle_auth ( user) {
206
206
Some ( err) => return Err ( err) ,
@@ -242,18 +242,11 @@ impl PostgresConnection {
242
242
}
243
243
244
244
fn write_messages ( & self , messages : & [ & FrontendMessage ] ) {
245
- let mut buf = MemWriter :: new ( ) ;
246
- for & message in messages. iter ( ) {
247
- buf. write_message ( message) ;
248
- }
249
- do self. stream . with_mut_ref |s| {
250
- s. write ( buf. inner_ref ( ) . as_slice ( ) ) ;
251
- }
252
- }
253
-
254
- fn write_message ( & self , message : & FrontendMessage ) {
255
245
do self . stream . with_mut_ref |s| {
256
- s. write_message ( message) ;
246
+ for & message in messages. iter ( ) {
247
+ s. write_message ( message) ;
248
+ }
249
+ s. flush ( ) ;
257
250
}
258
251
}
259
252
@@ -284,7 +277,7 @@ impl PostgresConnection {
284
277
Some ( pass) => pass,
285
278
None => return Some ( MissingPassword )
286
279
} ;
287
- self . write_message ( & PasswordMessage { password : pass } ) ;
280
+ self . write_messages ( [ & PasswordMessage { password : pass } ] ) ;
288
281
}
289
282
AuthenticationMD5Password { salt } => {
290
283
let UserInfo { user, pass } = user;
@@ -300,9 +293,9 @@ impl PostgresConnection {
300
293
md5. input_str ( output) ;
301
294
md5. input ( salt) ;
302
295
let output = "md5" + md5. result_str ( ) ;
303
- self . write_message ( & PasswordMessage {
296
+ self . write_messages ( [ & PasswordMessage {
304
297
password : output. as_slice ( )
305
- } ) ;
298
+ } ] ) ;
306
299
}
307
300
AuthenticationKerberosV5
308
301
| AuthenticationSCMCredential
@@ -422,13 +415,13 @@ impl PostgresConnection {
422
415
423
416
pub fn try_update ( & self , query : & str , params : & [ & ToSql ] )
424
417
-> Result < uint , PostgresDbError > {
425
- do self . try_prepare ( query) . chain |stmt| {
418
+ do self . try_prepare ( query) . and_then |stmt| {
426
419
stmt. try_update ( params)
427
420
}
428
421
}
429
422
430
423
fn quick_query ( & self , query : & str ) {
431
- self . write_message ( & Query { query : query } ) ;
424
+ self . write_messages ( [ & Query { query : query } ] ) ;
432
425
433
426
loop {
434
427
match self . read_message ( ) {
0 commit comments