Skip to content

Commit 39ab89c

Browse files
committed
Document stuff!
1 parent ecfe9be commit 39ab89c

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,32 @@ r2d2-postgres
22
=============
33

44
Documentation is available at https://sfackler.github.io/doc/r2d2_postgres
5+
6+
# Example
7+
8+
```rust
9+
extern crate r2d2;
10+
extern crate r2d2_postgres;
11+
extern crate postgres;
12+
13+
use std::sync::Arc;
14+
use std::default::Default;
15+
use postgres::SslMode;
16+
use r2d2_postgres::PostgresPoolManager;
17+
18+
fn main() {
19+
let config = Default::default();
20+
let manager = PostgresPoolManager::new("postgres://postgres@localhost",
21+
SslMode::None);
22+
let error_handler = r2d2::LoggingErrorHandler;
23+
let pool = Arc::new(r2d2::Pool::new(config, manager, error_handler).unwrap());
24+
25+
for i in range(0, 10i32) {
26+
let pool = pool.clone();
27+
spawn(proc() {
28+
let conn = pool.get().unwrap();
29+
conn.execute("INSERT INTO foo (bar) VALUES ($1)", &[&i]).unwrap();
30+
});
31+
}
32+
}
33+
```

src/lib.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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)]
35
extern crate r2d2;
46
extern crate postgres;
57

@@ -13,8 +15,11 @@ use std::rc::Rc;
1315
use postgres::{IntoConnectParams, SslMode};
1416
use postgres::types::ToSql;
1517

18+
/// A unified enum of errors returned by postgres::Connection
1619
pub 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+
/// ```
4681
pub struct PostgresPoolManager {
4782
params: Result<postgres::ConnectParams, postgres::ConnectError>,
4883
ssl_mode: SslMode,
4984
}
5085

5186
impl 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`.
79119
pub 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.
91135
pub struct StatementCachingManager {
92136
manager: PostgresPoolManager,
93137
config: Config,
94138
}
95139

96140
impl 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.
125175
pub 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.
145197
pub 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`.
194247
pub struct Transaction<'a> {
195248
conn: &'a Connection,
196249
trans: postgres::Transaction<'a>

0 commit comments

Comments
 (0)