@@ -207,6 +207,9 @@ mod test;
207
207
208
208
static DEFAULT_PORT : Port = 5432 ;
209
209
210
+ /// A typedef of the result returned by many methods.
211
+ pub type PostgresResult < T > = Result < T , PostgresError > ;
212
+
210
213
/// Trait for types that can handle Postgres notice messages
211
214
pub trait PostgresNoticeHandler {
212
215
/// Handle a Postgres notice message
@@ -412,8 +415,8 @@ impl Drop for InnerPostgresConnection {
412
415
}
413
416
414
417
impl InnerPostgresConnection {
415
- fn connect ( url : & str , ssl : & SslMode )
416
- -> Result < InnerPostgresConnection , PostgresConnectError > {
418
+ fn connect ( url : & str , ssl : & SslMode ) -> Result < InnerPostgresConnection ,
419
+ PostgresConnectError > {
417
420
let Url {
418
421
host,
419
422
port,
@@ -511,8 +514,8 @@ impl InnerPostgresConnection {
511
514
}
512
515
}
513
516
514
- fn handle_auth ( & mut self , user : UserInfo ) ->
515
- Result < ( ) , PostgresConnectError > {
517
+ fn handle_auth ( & mut self , user : UserInfo )
518
+ -> Result < ( ) , PostgresConnectError > {
516
519
match try_pg_conn ! ( self . read_message( ) ) {
517
520
AuthenticationOk => return Ok ( ( ) ) ,
518
521
AuthenticationCleartextPassword => {
@@ -565,7 +568,7 @@ impl InnerPostgresConnection {
565
568
}
566
569
567
570
fn prepare < ' a > ( & mut self , query : & str , conn : & ' a PostgresConnection )
568
- -> Result < PostgresStatement < ' a > , PostgresError > {
571
+ -> PostgresResult < PostgresStatement < ' a > > {
569
572
let stmt_name = format ! ( "statement_{}" , self . next_stmt_id) ;
570
573
self . next_stmt_id += 1 ;
571
574
@@ -626,7 +629,7 @@ impl InnerPostgresConnection {
626
629
}
627
630
628
631
fn set_type_names < ' a , I : Iterator < & ' a mut PostgresType > > ( & mut self , mut it : I )
629
- -> Result < ( ) , PostgresError > {
632
+ -> PostgresResult < ( ) > {
630
633
for ty in it {
631
634
match * ty {
632
635
PgUnknownType { oid, ref mut name } =>
@@ -637,7 +640,7 @@ impl InnerPostgresConnection {
637
640
Ok ( ( ) )
638
641
}
639
642
640
- fn get_type_name ( & mut self , oid : Oid ) -> Result < ~str , PostgresError > {
643
+ fn get_type_name ( & mut self , oid : Oid ) -> PostgresResult < ~str > {
641
644
match self . unknown_types . find ( & oid) {
642
645
Some ( name) => return Ok ( name. clone ( ) ) ,
643
646
None => { }
@@ -653,15 +656,15 @@ impl InnerPostgresConnection {
653
656
self . desynchronized
654
657
}
655
658
656
- fn wait_for_ready ( & mut self ) -> Result < ( ) , PostgresError > {
659
+ fn wait_for_ready ( & mut self ) -> PostgresResult < ( ) > {
657
660
match try_pg ! ( self . read_message( ) ) {
658
661
ReadyForQuery { .. } => Ok ( ( ) ) ,
659
662
_ => unreachable ! ( )
660
663
}
661
664
}
662
665
663
666
fn quick_query ( & mut self , query : & str )
664
- -> Result < Vec < Vec < Option < ~str > > > , PostgresError > {
667
+ -> PostgresResult < Vec < Vec < Option < ~str > > > > {
665
668
check_desync ! ( self ) ;
666
669
try_pg ! ( self . write_messages( [ Query { query: query } ] ) ) ;
667
670
@@ -683,7 +686,7 @@ impl InnerPostgresConnection {
683
686
Ok ( result)
684
687
}
685
688
686
- fn finish_inner ( & mut self ) -> Result < ( ) , PostgresError > {
689
+ fn finish_inner ( & mut self ) -> PostgresResult < ( ) > {
687
690
check_desync ! ( self ) ;
688
691
Ok ( try_pg ! ( self . write_messages( [ Terminate ] ) ) )
689
692
}
@@ -762,7 +765,7 @@ impl PostgresConnection {
762
765
/// Err(err) => fail!("Error preparing statement: {}", err)
763
766
/// };
764
767
pub fn prepare < ' a > ( & ' a self , query : & str )
765
- -> Result < PostgresStatement < ' a > , PostgresError > {
768
+ -> PostgresResult < PostgresStatement < ' a > > {
766
769
self . conn . borrow_mut ( ) . prepare ( query, self )
767
770
}
768
771
@@ -793,7 +796,7 @@ impl PostgresConnection {
793
796
/// # }
794
797
/// ```
795
798
pub fn transaction < ' a > ( & ' a self )
796
- -> Result < PostgresTransaction < ' a > , PostgresError > {
799
+ -> PostgresResult < PostgresTransaction < ' a > > {
797
800
check_desync ! ( self ) ;
798
801
try!( self . quick_query ( "BEGIN" ) ) ;
799
802
Ok ( PostgresTransaction {
@@ -811,7 +814,7 @@ impl PostgresConnection {
811
814
///
812
815
/// On success, returns the number of rows modified or 0 if not applicable.
813
816
pub fn execute ( & self , query : & str , params : & [ & ToSql ] )
814
- -> Result < uint , PostgresError > {
817
+ -> PostgresResult < uint > {
815
818
self . prepare ( query) . and_then ( |stmt| stmt. execute ( params) )
816
819
}
817
820
@@ -837,18 +840,18 @@ impl PostgresConnection {
837
840
/// Functionally equivalent to the `Drop` implementation for
838
841
/// `PostgresConnection` except that it returns any error encountered to
839
842
/// the caller.
840
- pub fn finish ( self ) -> Result < ( ) , PostgresError > {
843
+ pub fn finish ( self ) -> PostgresResult < ( ) > {
841
844
let mut conn = self . conn . borrow_mut ( ) ;
842
845
conn. finished = true ;
843
846
conn. finish_inner ( )
844
847
}
845
848
846
849
fn quick_query ( & self , query : & str )
847
- -> Result < Vec < Vec < Option < ~str > > > , PostgresError > {
850
+ -> PostgresResult < Vec < Vec < Option < ~str > > > > {
848
851
self . conn . borrow_mut ( ) . quick_query ( query)
849
852
}
850
853
851
- fn wait_for_ready ( & self ) -> Result < ( ) , PostgresError > {
854
+ fn wait_for_ready ( & self ) -> PostgresResult < ( ) > {
852
855
self . conn . borrow_mut ( ) . wait_for_ready ( )
853
856
}
854
857
@@ -893,7 +896,7 @@ impl<'conn> Drop for PostgresTransaction<'conn> {
893
896
}
894
897
895
898
impl < ' conn > PostgresTransaction < ' conn > {
896
- fn finish_inner ( & mut self ) -> Result < ( ) , PostgresError > {
899
+ fn finish_inner ( & mut self ) -> PostgresResult < ( ) > {
897
900
if task:: failing ( ) || !self . commit . get ( ) {
898
901
if self . nested {
899
902
try!( self . conn . quick_query ( "ROLLBACK TO sp" ) ) ;
@@ -914,19 +917,19 @@ impl<'conn> PostgresTransaction<'conn> {
914
917
impl < ' conn > PostgresTransaction < ' conn > {
915
918
/// Like `PostgresConnection::prepare`.
916
919
pub fn prepare < ' a > ( & ' a self , query : & str )
917
- -> Result < PostgresStatement < ' a > , PostgresError > {
920
+ -> PostgresResult < PostgresStatement < ' a > > {
918
921
self . conn . prepare ( query)
919
922
}
920
923
921
924
/// Like `PostgresConnection::execute`.
922
925
pub fn execute ( & self , query : & str , params : & [ & ToSql ] )
923
- -> Result < uint , PostgresError > {
926
+ -> PostgresResult < uint > {
924
927
self . conn . execute ( query, params)
925
928
}
926
929
927
930
/// Like `PostgresConnection::transaction`.
928
931
pub fn transaction < ' a > ( & ' a self )
929
- -> Result < PostgresTransaction < ' a > , PostgresError > {
932
+ -> PostgresResult < PostgresTransaction < ' a > > {
930
933
check_desync ! ( self . conn) ;
931
934
try!( self . conn . quick_query ( "SAVEPOINT sp" ) ) ;
932
935
Ok ( PostgresTransaction {
@@ -966,7 +969,7 @@ impl<'conn> PostgresTransaction<'conn> {
966
969
///
967
970
/// Functionally equivalent to the `Drop` implementation of
968
971
/// `PostgresTransaction` except that it returns any error to the caller.
969
- pub fn finish ( mut self ) -> Result < ( ) , PostgresError > {
972
+ pub fn finish ( mut self ) -> PostgresResult < ( ) > {
970
973
self . finished = true ;
971
974
self . finish_inner ( )
972
975
}
@@ -981,8 +984,8 @@ impl<'conn> PostgresTransaction<'conn> {
981
984
stmt : & ' stmt PostgresStatement ,
982
985
params : & [ & ToSql ] ,
983
986
row_limit : uint )
984
- -> Result < PostgresLazyRows < ' trans , ' stmt > ,
985
- PostgresError > {
987
+ -> PostgresResult < PostgresLazyRows
988
+ < ' trans , ' stmt > > {
986
989
if self . conn as * PostgresConnection != stmt. conn as * PostgresConnection {
987
990
return Err ( PgWrongConnection ) ;
988
991
}
@@ -1020,7 +1023,7 @@ impl<'conn> Drop for PostgresStatement<'conn> {
1020
1023
}
1021
1024
1022
1025
impl < ' conn > PostgresStatement < ' conn > {
1023
- fn finish_inner ( & mut self ) -> Result < ( ) , PostgresError > {
1026
+ fn finish_inner ( & mut self ) -> PostgresResult < ( ) > {
1024
1027
check_desync ! ( self . conn) ;
1025
1028
try_pg ! ( self . conn. write_messages( [
1026
1029
Close {
@@ -1042,7 +1045,7 @@ impl<'conn> PostgresStatement<'conn> {
1042
1045
}
1043
1046
1044
1047
fn inner_execute ( & self , portal_name : & str , row_limit : uint , params : & [ & ToSql ] )
1045
- -> Result < ( ) , PostgresError > {
1048
+ -> PostgresResult < ( ) > {
1046
1049
if self . param_types . len ( ) != params. len ( ) {
1047
1050
return Err ( PgWrongParamCount {
1048
1051
expected : self . param_types . len ( ) ,
@@ -1086,7 +1089,7 @@ impl<'conn> PostgresStatement<'conn> {
1086
1089
}
1087
1090
1088
1091
fn lazy_query < ' a > ( & ' a self , row_limit : uint , params : & [ & ToSql ] )
1089
- -> Result < PostgresRows < ' a > , PostgresError > {
1092
+ -> PostgresResult < PostgresRows < ' a > > {
1090
1093
let id = self . next_portal_id . get ( ) ;
1091
1094
self . next_portal_id . set ( id + 1 ) ;
1092
1095
let portal_name = format ! ( "{}_portal_{}" , self . name, id) ;
@@ -1133,7 +1136,7 @@ impl<'conn> PostgresStatement<'conn> {
1133
1136
/// Ok(count) => println!("{} row(s) updated", count),
1134
1137
/// Err(err) => println!("Error executing query: {}", err)
1135
1138
/// }
1136
- pub fn execute ( & self , params : & [ & ToSql ] ) -> Result < uint , PostgresError > {
1139
+ pub fn execute ( & self , params : & [ & ToSql ] ) -> PostgresResult < uint > {
1137
1140
check_desync ! ( self . conn) ;
1138
1141
try!( self . inner_execute ( "" , 0 , params) ) ;
1139
1142
@@ -1186,7 +1189,7 @@ impl<'conn> PostgresStatement<'conn> {
1186
1189
/// }
1187
1190
/// ```
1188
1191
pub fn query < ' a > ( & ' a self , params : & [ & ToSql ] )
1189
- -> Result < PostgresRows < ' a > , PostgresError > {
1192
+ -> PostgresResult < PostgresRows < ' a > > {
1190
1193
check_desync ! ( self . conn) ;
1191
1194
self . lazy_query ( 0 , params)
1192
1195
}
@@ -1195,7 +1198,7 @@ impl<'conn> PostgresStatement<'conn> {
1195
1198
///
1196
1199
/// Functionally identical to the `Drop` implementation of the
1197
1200
/// `PostgresStatement` except that it returns any error to the caller.
1198
- pub fn finish ( mut self ) -> Result < ( ) , PostgresError > {
1201
+ pub fn finish ( mut self ) -> PostgresResult < ( ) > {
1199
1202
self . finished . set ( true ) ;
1200
1203
self . finish_inner ( )
1201
1204
}
@@ -1234,7 +1237,7 @@ impl<'stmt> Drop for PostgresRows<'stmt> {
1234
1237
}
1235
1238
1236
1239
impl < ' stmt > PostgresRows < ' stmt > {
1237
- fn finish_inner ( & mut self ) -> Result < ( ) , PostgresError > {
1240
+ fn finish_inner ( & mut self ) -> PostgresResult < ( ) > {
1238
1241
check_desync ! ( self . stmt. conn) ;
1239
1242
try_pg ! ( self . stmt. conn. write_messages( [
1240
1243
Close {
@@ -1255,7 +1258,7 @@ impl<'stmt> PostgresRows<'stmt> {
1255
1258
Ok ( ( ) )
1256
1259
}
1257
1260
1258
- fn read_rows ( & mut self ) -> Result < ( ) , PostgresError > {
1261
+ fn read_rows ( & mut self ) -> PostgresResult < ( ) > {
1259
1262
loop {
1260
1263
match try_pg ! ( self . stmt. conn. read_message( ) ) {
1261
1264
EmptyQueryResponse |
@@ -1274,7 +1277,7 @@ impl<'stmt> PostgresRows<'stmt> {
1274
1277
self . stmt . conn . wait_for_ready ( )
1275
1278
}
1276
1279
1277
- fn execute ( & mut self ) -> Result < ( ) , PostgresError > {
1280
+ fn execute ( & mut self ) -> PostgresResult < ( ) > {
1278
1281
try_pg ! ( self . stmt. conn. write_messages( [
1279
1282
Execute {
1280
1283
portal: self . name,
@@ -1290,13 +1293,12 @@ impl<'stmt> PostgresRows<'stmt> {
1290
1293
///
1291
1294
/// Functionally identical to the `Drop` implementation on `PostgresRows`
1292
1295
/// except that it returns any error to the caller.
1293
- pub fn finish ( mut self ) -> Result < ( ) , PostgresError > {
1296
+ pub fn finish ( mut self ) -> PostgresResult < ( ) > {
1294
1297
self . finished = true ;
1295
1298
self . finish_inner ( )
1296
1299
}
1297
1300
1298
- fn try_next ( & mut self ) -> Option < Result < PostgresRow < ' stmt > ,
1299
- PostgresError > > {
1301
+ fn try_next ( & mut self ) -> Option < PostgresResult < PostgresRow < ' stmt > > > {
1300
1302
if self . data . is_empty ( ) && self . more_rows {
1301
1303
match self . execute ( ) {
1302
1304
Ok ( ( ) ) => { }
@@ -1344,8 +1346,7 @@ impl<'stmt> PostgresRow<'stmt> {
1344
1346
///
1345
1347
/// Returns an `Error` value if the index does not reference a column or
1346
1348
/// the return type is not compatible with the Postgres type.
1347
- pub fn get < I : RowIndex , T : FromSql > ( & self , idx : I )
1348
- -> Result < T , PostgresError > {
1349
+ pub fn get < I : RowIndex , T : FromSql > ( & self , idx : I ) -> PostgresResult < T > {
1349
1350
let idx = match idx. idx ( self . stmt ) {
1350
1351
Some ( idx) => idx,
1351
1352
None => return Err ( PgInvalidColumn )
@@ -1442,14 +1443,14 @@ pub struct PostgresLazyRows<'trans, 'stmt> {
1442
1443
1443
1444
impl < ' trans , ' stmt > PostgresLazyRows < ' trans , ' stmt > {
1444
1445
/// Like `PostgresRows::finish`.
1445
- pub fn finish ( self ) -> Result < ( ) , PostgresError > {
1446
+ pub fn finish ( self ) -> PostgresResult < ( ) > {
1446
1447
self . result . finish ( )
1447
1448
}
1448
1449
}
1449
1450
1450
- impl < ' trans , ' stmt > Iterator < Result < PostgresRow < ' stmt > , PostgresError > >
1451
+ impl < ' trans , ' stmt > Iterator < PostgresResult < PostgresRow < ' stmt > > >
1451
1452
for PostgresLazyRows < ' trans , ' stmt > {
1452
- fn next ( & mut self ) -> Option < Result < PostgresRow < ' stmt > , PostgresError > > {
1453
+ fn next ( & mut self ) -> Option < PostgresResult < PostgresRow < ' stmt > > > {
1453
1454
self . result . try_next ( )
1454
1455
}
1455
1456
0 commit comments