1+ //! Postgres support for the `r2d2` connection pool.
12#![ doc( html_root_url="https://sfackler.github.io/doc" ) ]
23#![ feature( if_let) ]
4+ #![ warn( missing_docs) ]
35extern crate r2d2;
46extern crate postgres;
57
@@ -13,8 +15,11 @@ use std::rc::Rc;
1315use postgres:: { IntoConnectParams , SslMode } ;
1416use postgres:: types:: ToSql ;
1517
18+ /// A unified enum of errors returned by postgres::Connection
1619pub enum Error {
20+ /// A postgres::ConnectError
1721 Connect ( postgres:: ConnectError ) ,
22+ /// An postgres::Error
1823 Other ( postgres:: Error ) ,
1924}
2025
@@ -43,12 +48,46 @@ impl error::Error for Error {
4348 }
4449}
4550
51+ /// An `r2d2::PoolManager` for `postgres::Connection`s.
52+ ///
53+ /// ## Example
54+ ///
55+ /// ```rust,no_run
56+ /// extern crate r2d2;
57+ /// extern crate r2d2_postgres;
58+ /// extern crate postgres;
59+ ///
60+ /// use std::sync::Arc;
61+ /// use std::default::Default;
62+ /// use postgres::SslMode;
63+ /// use r2d2_postgres::PostgresPoolManager;
64+ ///
65+ /// fn main() {
66+ /// let config = Default::default();
67+ /// let manager = PostgresPoolManager::new("postgres://postgres@localhost",
68+ /// SslMode::None);
69+ /// let error_handler = r2d2::LoggingErrorHandler;
70+ /// let pool = Arc::new(r2d2::Pool::new(config, manager, error_handler).unwrap());
71+ ///
72+ /// for i in range(0, 10i32) {
73+ /// let pool = pool.clone();
74+ /// spawn(proc() {
75+ /// let conn = pool.get().unwrap();
76+ /// conn.execute("INSERT INTO foo (bar) VALUES ($1)", &[&i]).unwrap();
77+ /// });
78+ /// }
79+ /// }
80+ /// ```
4681pub struct PostgresPoolManager {
4782 params : Result < postgres:: ConnectParams , postgres:: ConnectError > ,
4883 ssl_mode : SslMode ,
4984}
5085
5186impl PostgresPoolManager {
87+ /// Creates a new `PostgresPoolManager`.
88+ ///
89+ /// See `postgres::Connection::connect` for a description of the parameter
90+ /// types.
5291 pub fn new < T : IntoConnectParams > ( params : T , ssl_mode : SslMode ) -> PostgresPoolManager {
5392 PostgresPoolManager {
5493 params : params. into_connect_params ( ) ,
@@ -76,7 +115,11 @@ impl r2d2::PoolManager<postgres::Connection, Error> for PostgresPoolManager {
76115 }
77116}
78117
118+ /// Configuration options for the `CachingStatementManager`.
79119pub struct Config {
120+ /// The number of `postgres::Statement`s that will be internally cached.
121+ ///
122+ /// Defaults to 10
80123 pub statement_pool_size : uint ,
81124}
82125
@@ -88,12 +131,17 @@ impl Default for Config {
88131 }
89132}
90133
134+ /// An `r2d2::PoolManager` for `Connection`s, which cache prepared statements.
91135pub struct StatementCachingManager {
92136 manager : PostgresPoolManager ,
93137 config : Config ,
94138}
95139
96140impl StatementCachingManager {
141+ /// Creates a new `StatementCachingManager`.
142+ ///
143+ /// See `postgres::Connection::Connect` for details of the first two
144+ /// parameter types.
97145 pub fn new < T > ( params : T , ssl_mode : SslMode , config : Config ) -> StatementCachingManager
98146 where T : IntoConnectParams {
99147 StatementCachingManager {
@@ -122,6 +170,8 @@ impl r2d2::PoolManager<Connection, Error> for StatementCachingManager {
122170 }
123171}
124172
173+ /// A trait abstracting over functionality provided by `Connection`s and
174+ /// `Transaction`s.
125175pub trait GenericConnection {
126176 /// Like `postgres::Connection::prepare`.
127177 fn prepare < ' a > ( & ' a self , query : & str ) -> postgres:: Result < Rc < postgres:: Statement < ' a > > > ;
@@ -142,6 +192,8 @@ pub trait GenericConnection {
142192 fn batch_execute ( & self , query : & str ) -> postgres:: Result < ( ) > ;
143193}
144194
195+ /// Like a `postgres::Connection`, but maintains a cache of
196+ /// `postgres::Statement`s.
145197pub struct Connection {
146198 conn : Box < postgres:: Connection > ,
147199 stmts : * mut ( ) ,
@@ -191,6 +243,7 @@ impl GenericConnection for Connection {
191243 }
192244}
193245
246+ /// Like `postgres::Transaction`.
194247pub struct Transaction < ' a > {
195248 conn : & ' a Connection ,
196249 trans : postgres:: Transaction < ' a >
0 commit comments