1
+ //! An asynchronous Postgres driver.
2
+ #![ warn( missing_docs) ]
3
+
1
4
extern crate fallible_iterator;
2
5
extern crate futures;
3
6
extern crate futures_state_stream;
@@ -28,11 +31,11 @@ use std::sync::mpsc::{self, Sender, Receiver};
28
31
use tokio_core:: reactor:: Handle ;
29
32
30
33
#[ doc( inline) ]
31
- pub use postgres_shared:: { params, Column , RowIndex } ;
34
+ pub use postgres_shared:: { params, CancelData } ;
32
35
33
36
use error:: { ConnectError , Error , DbError , SqlState } ;
34
37
use params:: { ConnectParams , IntoConnectParams } ;
35
- use stmt:: Statement ;
38
+ use stmt:: { Statement , Column } ;
36
39
use stream:: PostgresStream ;
37
40
use tls:: Handshake ;
38
41
use transaction:: Transaction ;
@@ -55,18 +58,27 @@ const TYPEINFO_QUERY: &'static str = "__typeinfo";
55
58
const TYPEINFO_ENUM_QUERY : & ' static str = "__typeinfo_enum" ;
56
59
const TYPEINFO_COMPOSITE_QUERY : & ' static str = "__typeinfo_composite" ;
57
60
61
+ /// Specifies the TLS support required for a new connection.
58
62
pub enum TlsMode {
63
+ /// The connection must use TLS.
59
64
Require ( Box < Handshake > ) ,
65
+ /// The connection will use TLS if available.
60
66
Prefer ( Box < Handshake > ) ,
67
+ /// The connection will not use TLS.
61
68
None ,
62
69
}
63
70
64
- #[ derive( Debug , Copy , Clone ) ]
65
- pub struct CancelData {
66
- pub process_id : i32 ,
67
- pub secret_key : i32 ,
68
- }
69
-
71
+ /// Attempts to cancel an in-progress query.
72
+ ///
73
+ /// The backend provides no information about whether a cancellation attempt
74
+ /// was successful or not. An error will only be returned if the driver was
75
+ /// unable to connect to the database.
76
+ ///
77
+ /// A `CancelData` object can be created via `Connection::cancel_data`. The
78
+ /// object can cancel any query made on that connection.
79
+ ///
80
+ /// Only the host and port of the connection info are used. See
81
+ /// `Connection::connect` for details of the `params` argument.
70
82
pub fn cancel_query < T > ( params : T ,
71
83
tls_mode : TlsMode ,
72
84
cancel_data : CancelData ,
@@ -161,6 +173,7 @@ impl Sink for InnerConnection {
161
173
}
162
174
}
163
175
176
+ /// A connection to a Postgres database.
164
177
pub struct Connection ( InnerConnection ) ;
165
178
166
179
// FIXME fill out
@@ -172,6 +185,24 @@ impl fmt::Debug for Connection {
172
185
}
173
186
174
187
impl Connection {
188
+ /// Creates a new connection to a Postgres database.
189
+ ///
190
+ /// Most applications can use a URL string in the normal format:
191
+ ///
192
+ /// ```notrust
193
+ /// postgresql://user[:password]@host[:port][/database][?param1=val1[[¶m2=val2]...]]
194
+ /// ```
195
+ ///
196
+ /// The password may be omitted if not required. The default Postgres port
197
+ /// (5432) is used if none is specified. The database name defaults to the
198
+ /// username if not specified.
199
+ ///
200
+ /// To connect to the server via Unix sockets, `host` should be set to the
201
+ /// absolute path of the directory containing the socket file. Since `/` is
202
+ /// a reserved character in URLs, the path should be URL encoded. If the
203
+ /// path contains non-UTF 8 characters, a `ConnectParams` struct should be
204
+ /// created manually and passed in. Note that Postgres does not support TLS
205
+ /// over Unix sockets.
175
206
pub fn connect < T > ( params : T ,
176
207
tls_mode : TlsMode ,
177
208
handle : & Handle )
@@ -421,6 +452,19 @@ impl Connection {
421
452
. boxed ( )
422
453
}
423
454
455
+ /// Execute a sequence of SQL statements.
456
+ ///
457
+ /// Statements should be separated by `;` characters. If an error occurs,
458
+ /// execution of the sequence will stop at that point. This is intended for
459
+ /// execution of batches of non-dynamic statements - for example, creation
460
+ /// of a schema for a fresh database.
461
+ ///
462
+ /// # Warning
463
+ ///
464
+ /// Prepared statements should be used for any SQL statement which contains
465
+ /// user-specified data, as it provides functionality to safely embed that
466
+ /// data in the statement. Do not form statements via string concatenation
467
+ /// and feed them into this method.
424
468
pub fn batch_execute ( self , query : & str ) -> BoxFuture < Connection , Error > {
425
469
self . simple_query ( query)
426
470
. map ( |r| r. 1 )
@@ -873,6 +917,7 @@ impl Connection {
873
917
. boxed ( )
874
918
}
875
919
920
+ /// Creates a new prepared statement.
876
921
pub fn prepare ( mut self , query : & str ) -> BoxFuture < ( Statement , Connection ) , Error > {
877
922
let id = self . 0 . next_stmt_id ;
878
923
self . 0 . next_stmt_id += 1 ;
@@ -888,12 +933,24 @@ impl Connection {
888
933
. boxed ( )
889
934
}
890
935
936
+ /// Executes a statement, returning the number of rows modified.
937
+ ///
938
+ /// # Panics
939
+ ///
940
+ /// Panics if the number of parameters provided does not match the number
941
+ /// expected.
891
942
pub fn execute ( self , statement : & Statement , params : & [ & ToSql ] ) -> BoxFuture < ( u64 , Connection ) , Error > {
892
943
self . raw_execute ( statement. name ( ) , "" , statement. parameters ( ) , params)
893
944
. and_then ( |conn| conn. finish_execute ( ) )
894
945
. boxed ( )
895
946
}
896
947
948
+ /// Executes a statement, returning a stream over the resulting rows.
949
+ ///
950
+ /// # Panics
951
+ ///
952
+ /// Panics if the number of parameters provided does not match the number
953
+ /// expected.
897
954
pub fn query ( self ,
898
955
statement : & Statement ,
899
956
params : & [ & ToSql ] )
@@ -905,24 +962,26 @@ impl Connection {
905
962
. boxed ( )
906
963
}
907
964
965
+ /// Starts a new transaction.
908
966
pub fn transaction ( self ) -> BoxFuture < Transaction , Error > {
909
967
self . simple_query ( "BEGIN" )
910
968
. map ( |( _, c) | Transaction :: new ( c) )
911
969
. boxed ( )
912
970
}
913
971
914
- pub fn close ( self ) -> BoxFuture < ( ) , Error > {
915
- let mut terminate = vec ! [ ] ;
916
- frontend:: terminate ( & mut terminate) ;
917
- self . 0 . send ( terminate)
918
- . map ( |_| ( ) )
919
- . map_err ( Error :: Io )
920
- . boxed ( )
921
- }
922
-
972
+ /// Returns information used to cancel pending queries.
973
+ ///
974
+ /// Used with the `cancel_query` function. The object returned can be used
975
+ /// to cancel any query executed by the connection it was created from.
923
976
pub fn cancel_data ( & self ) -> CancelData {
924
977
self . 0 . cancel_data
925
978
}
979
+
980
+ /// Returns the value of the specified Postgres backend parameter, such as
981
+ /// `timezone` or `server_version`.
982
+ pub fn parameter ( & self , param : & str ) -> Option < & str > {
983
+ self . 0 . parameters . get ( param) . map ( |s| & * * s)
984
+ }
926
985
}
927
986
928
987
fn connect_err ( fields : & mut ErrorFields ) -> ConnectError {
0 commit comments