@@ -68,10 +68,11 @@ use io::{TlsStream, TlsHandshake};
68
68
use message:: { Backend , RowDescriptionEntry , ReadMessage } ;
69
69
use notification:: { Notifications , Notification } ;
70
70
use params:: { ConnectParams , IntoConnectParams , UserInfo } ;
71
+ use priv_io:: MessageStream ;
71
72
use rows:: { Rows , LazyRows } ;
72
73
use stmt:: { Statement , Column } ;
73
- use types:: { IsNull , Kind , Type , SessionInfo , Oid , Other , WrongType , ToSql , FromSql , Field } ;
74
74
use transaction:: { Transaction , IsolationLevel } ;
75
+ use types:: { IsNull , Kind , Type , SessionInfo , Oid , Other , WrongType , ToSql , FromSql , Field } ;
75
76
76
77
#[ macro_use]
77
78
mod macros;
@@ -127,9 +128,9 @@ impl HandleNotice for LoggingNoticeHandler {
127
128
#[ derive( Copy , Clone , Debug ) ]
128
129
pub struct CancelData {
129
130
/// The process ID of the session.
130
- pub process_id : u32 ,
131
+ pub process_id : i32 ,
131
132
/// The secret key for the session.
132
- pub secret_key : u32 ,
133
+ pub secret_key : i32 ,
133
134
}
134
135
135
136
/// Attempts to cancel an in-progress query.
@@ -167,8 +168,8 @@ pub fn cancel_query<T>(params: T,
167
168
let mut socket = try!( priv_io:: initialize_stream ( & params, tls) ) ;
168
169
169
170
let message = frontend:: CancelRequest {
170
- process_id : data. process_id as i32 ,
171
- secret_key : data. secret_key as i32 ,
171
+ process_id : data. process_id ,
172
+ secret_key : data. secret_key ,
172
173
} ;
173
174
let mut buf = vec ! [ ] ;
174
175
try!( frontend:: Message :: write ( & message, & mut buf) ) ;
@@ -208,8 +209,7 @@ struct StatementInfo {
208
209
}
209
210
210
211
struct InnerConnection {
211
- stream : BufStream < Box < TlsStream > > ,
212
- io_buf : Vec < u8 > ,
212
+ stream : MessageStream ,
213
213
notice_handler : Box < HandleNotice > ,
214
214
notifications : VecDeque < Notification > ,
215
215
cancel_data : CancelData ,
@@ -250,8 +250,7 @@ impl InnerConnection {
250
250
} ;
251
251
252
252
let mut conn = InnerConnection {
253
- stream : BufStream :: new ( stream) ,
254
- io_buf : vec ! [ ] ,
253
+ stream : MessageStream :: new ( stream) ,
255
254
next_stmt_id : 0 ,
256
255
notice_handler : Box :: new ( LoggingNoticeHandler ) ,
257
256
notifications : VecDeque :: new ( ) ,
@@ -280,7 +279,7 @@ impl InnerConnection {
280
279
options. push ( ( "database" . to_owned ( ) , database) ) ;
281
280
}
282
281
283
- try!( conn. write_message ( & frontend:: StartupMessage {
282
+ try!( conn. stream . write_message ( & frontend:: StartupMessage {
284
283
parameters : & options,
285
284
} ) ) ;
286
285
try!( conn. stream . flush ( ) ) ;
@@ -290,8 +289,8 @@ impl InnerConnection {
290
289
loop {
291
290
match try!( conn. read_message ( ) ) {
292
291
Backend :: BackendKeyData { process_id, secret_key } => {
293
- conn. cancel_data . process_id = process_id;
294
- conn. cancel_data . secret_key = secret_key;
292
+ conn. cancel_data . process_id = process_id as i32 ;
293
+ conn. cancel_data . secret_key = secret_key as i32 ;
295
294
}
296
295
Backend :: ReadyForQuery { .. } => break ,
297
296
Backend :: ErrorResponse { fields } => return DbError :: new_connect ( fields) ,
@@ -302,16 +301,6 @@ impl InnerConnection {
302
301
Ok ( conn)
303
302
}
304
303
305
- fn write_message < M > ( & mut self , message : & M ) -> std_io:: Result < ( ) >
306
- where M : frontend:: Message
307
- {
308
- debug_assert ! ( !self . desynchronized) ;
309
- self . io_buf . clear ( ) ;
310
- try!( message. write ( & mut self . io_buf ) ) ;
311
- try_desync ! ( self , self . stream. write_all( & self . io_buf) ) ;
312
- Ok ( ( ) )
313
- }
314
-
315
304
fn read_message_with_notification ( & mut self ) -> std_io:: Result < Backend > {
316
305
debug_assert ! ( !self . desynchronized) ;
317
306
loop {
@@ -388,7 +377,7 @@ impl InnerConnection {
388
377
let pass = try!( user. password . ok_or_else ( || {
389
378
ConnectError :: ConnectParams ( "a password was requested but not provided" . into ( ) )
390
379
} ) ) ;
391
- try!( self . write_message ( & frontend:: PasswordMessage { password : & pass } ) ) ;
380
+ try!( self . stream . write_message ( & frontend:: PasswordMessage { password : & pass } ) ) ;
392
381
try!( self . stream . flush ( ) ) ;
393
382
}
394
383
Backend :: AuthenticationMD5Password { salt } => {
@@ -403,7 +392,7 @@ impl InnerConnection {
403
392
hasher. input ( output. as_bytes ( ) ) ;
404
393
hasher. input ( & salt) ;
405
394
let output = format ! ( "md5{}" , hasher. result_str( ) ) ;
406
- try!( self . write_message ( & frontend:: PasswordMessage { password : & output } ) ) ;
395
+ try!( self . stream . write_message ( & frontend:: PasswordMessage { password : & output } ) ) ;
407
396
try!( self . stream . flush ( ) ) ;
408
397
}
409
398
Backend :: AuthenticationKerberosV5 |
@@ -431,16 +420,16 @@ impl InnerConnection {
431
420
fn raw_prepare ( & mut self , stmt_name : & str , query : & str ) -> Result < ( Vec < Type > , Vec < Column > ) > {
432
421
debug ! ( "preparing query with name `{}`: {}" , stmt_name, query) ;
433
422
434
- try!( self . write_message ( & frontend:: Parse {
423
+ try!( self . stream . write_message ( & frontend:: Parse {
435
424
name : stmt_name,
436
425
query : query,
437
426
param_types : & [ ] ,
438
427
} ) ) ;
439
- try!( self . write_message ( & frontend:: Describe {
428
+ try!( self . stream . write_message ( & frontend:: Describe {
440
429
variant : b'S' ,
441
430
name : stmt_name,
442
431
} ) ) ;
443
- try!( self . write_message ( & frontend:: Sync ) ) ;
432
+ try!( self . stream . write_message ( & frontend:: Sync ) ) ;
444
433
try!( self . stream . flush ( ) ) ;
445
434
446
435
match try!( self . read_message ( ) ) {
@@ -496,10 +485,10 @@ impl InnerConnection {
496
485
return DbError :: new ( fields) ;
497
486
}
498
487
Backend :: CopyInResponse { .. } => {
499
- try!( self . write_message ( & frontend:: CopyFail {
488
+ try!( self . stream . write_message ( & frontend:: CopyFail {
500
489
message : "COPY queries cannot be directly executed" ,
501
490
} ) ) ;
502
- try!( self . write_message ( & frontend:: Sync ) ) ;
491
+ try!( self . stream . write_message ( & frontend:: Sync ) ) ;
503
492
try!( self . stream . flush ( ) ) ;
504
493
}
505
494
Backend :: CopyOutResponse { .. } => {
@@ -545,18 +534,18 @@ impl InnerConnection {
545
534
}
546
535
}
547
536
548
- try!( self . write_message ( & frontend:: Bind {
537
+ try!( self . stream . write_message ( & frontend:: Bind {
549
538
portal : portal_name,
550
539
statement : & stmt_name,
551
540
formats : & [ 1 ] ,
552
541
values : & values,
553
542
result_formats : & [ 1 ] ,
554
543
} ) ) ;
555
- try!( self . write_message ( & frontend:: Execute {
544
+ try!( self . stream . write_message ( & frontend:: Execute {
556
545
portal : portal_name,
557
546
max_rows : row_limit,
558
547
} ) ) ;
559
- try!( self . write_message ( & frontend:: Sync ) ) ;
548
+ try!( self . stream . write_message ( & frontend:: Sync ) ) ;
560
549
try!( self . stream . flush ( ) ) ;
561
550
562
551
match try!( self . read_message ( ) ) {
@@ -611,11 +600,11 @@ impl InnerConnection {
611
600
}
612
601
613
602
fn close_statement ( & mut self , name : & str , type_ : u8 ) -> Result < ( ) > {
614
- try!( self . write_message ( & frontend:: Close {
603
+ try!( self . stream . write_message ( & frontend:: Close {
615
604
variant : type_,
616
605
name : name,
617
606
} ) ) ;
618
- try!( self . write_message ( & frontend:: Sync ) ) ;
607
+ try!( self . stream . write_message ( & frontend:: Sync ) ) ;
619
608
try!( self . stream . flush ( ) ) ;
620
609
let resp = match try!( self . read_message ( ) ) {
621
610
Backend :: CloseComplete => Ok ( ( ) ) ,
@@ -815,7 +804,7 @@ impl InnerConnection {
815
804
fn quick_query ( & mut self , query : & str ) -> Result < Vec < Vec < Option < String > > > > {
816
805
check_desync ! ( self ) ;
817
806
debug ! ( "executing query: {}" , query) ;
818
- try!( self . write_message ( & frontend:: Query { query : query } ) ) ;
807
+ try!( self . stream . write_message ( & frontend:: Query { query : query } ) ) ;
819
808
try!( self . stream . flush ( ) ) ;
820
809
821
810
let mut result = vec ! [ ] ;
@@ -830,10 +819,10 @@ impl InnerConnection {
830
819
. collect ( ) ) ;
831
820
}
832
821
Backend :: CopyInResponse { .. } => {
833
- try!( self . write_message ( & frontend:: CopyFail {
822
+ try!( self . stream . write_message ( & frontend:: CopyFail {
834
823
message : "COPY queries cannot be directly executed" ,
835
824
} ) ) ;
836
- try!( self . write_message ( & frontend:: Sync ) ) ;
825
+ try!( self . stream . write_message ( & frontend:: Sync ) ) ;
837
826
try!( self . stream . flush ( ) ) ;
838
827
}
839
828
Backend :: ErrorResponse { fields } => {
@@ -848,7 +837,7 @@ impl InnerConnection {
848
837
849
838
fn finish_inner ( & mut self ) -> Result < ( ) > {
850
839
check_desync ! ( self ) ;
851
- try!( self . write_message ( & frontend:: Terminate ) ) ;
840
+ try!( self . stream . write_message ( & frontend:: Terminate ) ) ;
852
841
try!( self . stream . flush ( ) ) ;
853
842
Ok ( ( ) )
854
843
}
0 commit comments