1+ //! An asynchronous Postgres driver.
2+ #![ warn( missing_docs) ]
3+
14extern crate fallible_iterator;
25extern crate futures;
36extern crate futures_state_stream;
@@ -28,11 +31,11 @@ use std::sync::mpsc::{self, Sender, Receiver};
2831use tokio_core:: reactor:: Handle ;
2932
3033#[ doc( inline) ]
31- pub use postgres_shared:: { params, Column , RowIndex } ;
34+ pub use postgres_shared:: { params, CancelData } ;
3235
3336use error:: { ConnectError , Error , DbError , SqlState } ;
3437use params:: { ConnectParams , IntoConnectParams } ;
35- use stmt:: Statement ;
38+ use stmt:: { Statement , Column } ;
3639use stream:: PostgresStream ;
3740use tls:: Handshake ;
3841use transaction:: Transaction ;
@@ -55,18 +58,27 @@ const TYPEINFO_QUERY: &'static str = "__typeinfo";
5558const TYPEINFO_ENUM_QUERY : & ' static str = "__typeinfo_enum" ;
5659const TYPEINFO_COMPOSITE_QUERY : & ' static str = "__typeinfo_composite" ;
5760
61+ /// Specifies the TLS support required for a new connection.
5862pub enum TlsMode {
63+ /// The connection must use TLS.
5964 Require ( Box < Handshake > ) ,
65+ /// The connection will use TLS if available.
6066 Prefer ( Box < Handshake > ) ,
67+ /// The connection will not use TLS.
6168 None ,
6269}
6370
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.
7082pub fn cancel_query < T > ( params : T ,
7183 tls_mode : TlsMode ,
7284 cancel_data : CancelData ,
@@ -161,6 +173,7 @@ impl Sink for InnerConnection {
161173 }
162174}
163175
176+ /// A connection to a Postgres database.
164177pub struct Connection ( InnerConnection ) ;
165178
166179// FIXME fill out
@@ -172,6 +185,24 @@ impl fmt::Debug for Connection {
172185}
173186
174187impl 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.
175206 pub fn connect < T > ( params : T ,
176207 tls_mode : TlsMode ,
177208 handle : & Handle )
@@ -421,6 +452,19 @@ impl Connection {
421452 . boxed ( )
422453 }
423454
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.
424468 pub fn batch_execute ( self , query : & str ) -> BoxFuture < Connection , Error > {
425469 self . simple_query ( query)
426470 . map ( |r| r. 1 )
@@ -873,6 +917,7 @@ impl Connection {
873917 . boxed ( )
874918 }
875919
920+ /// Creates a new prepared statement.
876921 pub fn prepare ( mut self , query : & str ) -> BoxFuture < ( Statement , Connection ) , Error > {
877922 let id = self . 0 . next_stmt_id ;
878923 self . 0 . next_stmt_id += 1 ;
@@ -888,12 +933,24 @@ impl Connection {
888933 . boxed ( )
889934 }
890935
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.
891942 pub fn execute ( self , statement : & Statement , params : & [ & ToSql ] ) -> BoxFuture < ( u64 , Connection ) , Error > {
892943 self . raw_execute ( statement. name ( ) , "" , statement. parameters ( ) , params)
893944 . and_then ( |conn| conn. finish_execute ( ) )
894945 . boxed ( )
895946 }
896947
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.
897954 pub fn query ( self ,
898955 statement : & Statement ,
899956 params : & [ & ToSql ] )
@@ -905,24 +962,26 @@ impl Connection {
905962 . boxed ( )
906963 }
907964
965+ /// Starts a new transaction.
908966 pub fn transaction ( self ) -> BoxFuture < Transaction , Error > {
909967 self . simple_query ( "BEGIN" )
910968 . map ( |( _, c) | Transaction :: new ( c) )
911969 . boxed ( )
912970 }
913971
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.
923976 pub fn cancel_data ( & self ) -> CancelData {
924977 self . 0 . cancel_data
925978 }
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+ }
926985}
927986
928987fn connect_err ( fields : & mut ErrorFields ) -> ConnectError {
0 commit comments