Skip to content

Commit 83ad914

Browse files
committed
Documentation for everything else
1 parent 7729cda commit 83ad914

5 files changed

Lines changed: 62 additions & 10 deletions

File tree

src/postgres/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ macro_rules! make_errors(
22
($($code:pat => $error:ident),+) => (
33
// TODO: Get rid of this module when mozilla/rust#4375 is fixed
44
pub mod hack {
5+
/// SQLSTATE error codes
56
#[deriving(ToStr, Eq)]
67
pub enum PostgresSqlState {
78
$($error,)+

src/postgres/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// Needed for rustdoc-ng
55
#[link(name="postgres")];
66

7+
#[warn(missing_doc)];
8+
79
extern mod extra;
810

911
use extra::container::Deque;

src/postgres/message.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[allow(missing_doc)];
2+
13
use std::str;
24
use std::rt::io::{Decorator, Reader, Writer};
35
use std::rt::io::extensions::{ReaderUtil, ReaderByteConversions,

src/postgres/pool/mod.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ impl InnerConnectionPool {
2626
}
2727
}
2828

29+
/// A simple fixed-size Postgres connection pool.
30+
///
31+
/// It can be shared across tasks.
2932
// Should be a newtype, but blocked by mozilla/rust#9155
3033
#[deriving(Clone)]
3134
pub struct PostgresConnectionPool {
3235
priv pool: MutexArc<InnerConnectionPool>
3336
}
3437

3538
impl PostgresConnectionPool {
39+
/// Attempts to create a new pool with the specified number of connections.
40+
///
41+
/// Returns an error if the specified number of connections cannot be
42+
/// created.
3643
pub fn try_new(url: &str, pool_size: uint)
3744
-> Result<PostgresConnectionPool, PostgresConnectError> {
3845
let mut pool = InnerConnectionPool {
@@ -52,15 +59,20 @@ impl PostgresConnectionPool {
5259
})
5360
}
5461

62+
/// A convenience function wrapping `try_new`.
63+
///
64+
/// Fails if the pool cannot be created.
5565
pub fn new(url: &str, pool_size: uint) -> PostgresConnectionPool {
5666
match PostgresConnectionPool::try_new(url, pool_size) {
5767
Ok(pool) => pool,
5868
Err(err) => fail!("Unable to initialize pool: %s", err.to_str())
5969
}
6070
}
6171

62-
pub fn try_get_connection(&self) -> Result<PooledPostgresConnection,
63-
PostgresConnectError> {
72+
/// Retrieves a connection from the pool.
73+
///
74+
/// If all connections are in use, blocks until one becomes available.
75+
pub fn get_connection(&self) -> PooledPostgresConnection {
6476
let conn = unsafe {
6577
do self.pool.unsafe_access_cond |pool, cvar| {
6678
while pool.pool.is_empty() {
@@ -71,20 +83,17 @@ impl PostgresConnectionPool {
7183
}
7284
};
7385

74-
Ok(PooledPostgresConnection {
86+
PooledPostgresConnection {
7587
pool: self.clone(),
7688
conn: Some(conn)
77-
})
78-
}
79-
80-
pub fn get_connection(&self) -> PooledPostgresConnection {
81-
match self.try_get_connection() {
82-
Ok(conn) => conn,
83-
Err(err) => fail!("Unable to get connection: %s", err.to_str())
8489
}
8590
}
8691
}
8792

93+
/// A Postgres connection pulled from a connection pool.
94+
///
95+
/// It will be returned to the pool when it falls out of scope, even due to
96+
/// task failure.
8897
pub struct PooledPostgresConnection {
8998
priv pool: PostgresConnectionPool,
9099
// TODO remove the Option wrapper when drop takes self by value
@@ -102,24 +111,29 @@ impl Drop for PooledPostgresConnection {
102111
}
103112

104113
impl PooledPostgresConnection {
114+
/// Like `PostgresConnection::try_prepare`.
105115
pub fn try_prepare<'a>(&'a self, query: &str)
106116
-> Result<NormalPostgresStatement<'a>, PostgresDbError> {
107117
self.conn.get_ref().try_prepare(query)
108118
}
109119

120+
/// Like `PostgresConnection::prepare`.
110121
pub fn prepare<'a>(&'a self, query: &str) -> NormalPostgresStatement<'a> {
111122
self.conn.get_ref().prepare(query)
112123
}
113124

125+
/// Like `PostgresConnection::try_update`.
114126
pub fn try_update(&self, query: &str, params: &[&ToSql])
115127
-> Result<uint, PostgresDbError> {
116128
self.conn.get_ref().try_update(query, params)
117129
}
118130

131+
/// Like `PostgresConnection::update`.
119132
pub fn update(&self, query: &str, params: &[&ToSql]) -> uint {
120133
self.conn.get_ref().update(query, params)
121134
}
122135

136+
/// `PostgresConnection::in_transaction`.
123137
pub fn in_transaction<T>(&self, blk: &fn(&PostgresTransaction) -> T) -> T {
124138
self.conn.get_ref().in_transaction(blk)
125139
}

src/postgres/types.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::rt::io::extensions::{WriterByteConversions, ReaderByteConversions};
99
use std::rt::io::mem::{MemWriter, BufReader};
1010
use std::str;
1111

12+
/// A Postgres OID
1213
pub type Oid = i32;
1314

1415
// Values from pg_type.h
@@ -34,27 +35,45 @@ static NSEC_PER_USEC: i64 = 1_000;
3435
// Number of seconds from 1970-01-01 to 2000-01-01
3536
static TIME_SEC_CONVERSION: i64 = 946684800;
3637

38+
/// A Postgres type
3739
#[deriving(Eq)]
3840
pub enum PostgresType {
41+
/// BOOL
3942
PgBool,
43+
/// BYTEA
4044
PgByteA,
45+
/// "char"
4146
PgChar,
47+
/// INT8/BIGINT
4248
PgInt8,
49+
/// INT2/SMALLINT
4350
PgInt2,
51+
/// INT4/INT
4452
PgInt4,
53+
/// TEXT
4554
PgText,
55+
/// JSON
4656
PgJson,
57+
/// FLOAT4/REAL
4758
PgFloat4,
59+
/// FLOAT8/DOUBLE PRECISION
4860
PgFloat8,
61+
/// TIMESTAMP
4962
PgTimestamp,
63+
/// TIMESTAMP WITH TIME ZONE
5064
PgTimestampZ,
65+
/// CHAR(n)/CHARACTER(n)
5166
PgCharN,
67+
/// VARCHAR/CHARACTER VARYING
5268
PgVarchar,
69+
/// UUID
5370
PgUuid,
71+
/// An unknown type along with its OID
5472
PgUnknownType(Oid)
5573
}
5674

5775
impl PostgresType {
76+
/// Creates a PostgresType from a Postgres OID.
5877
pub fn from_oid(oid: Oid) -> PostgresType {
5978
match oid {
6079
BOOLOID => PgBool,
@@ -76,6 +95,7 @@ impl PostgresType {
7695
}
7796
}
7897

98+
/// Returns the wire format needed for the value of `self`.
7999
pub fn result_format(&self) -> Format {
80100
match *self {
81101
PgUnknownType(*) => Text,
@@ -84,6 +104,7 @@ impl PostgresType {
84104
}
85105
}
86106

107+
/// The wire format of a Postgres value
87108
pub enum Format {
88109
Text = 0,
89110
Binary = 1
@@ -98,7 +119,13 @@ macro_rules! check_types(
98119
)
99120
)
100121

122+
/// A trait for things that can be created from a Postgres value
101123
pub trait FromSql {
124+
/// Creates a new value of this type from a buffer of Postgres data.
125+
///
126+
/// If the value was `NULL`, the buffer will be `None`.
127+
///
128+
/// Fails if this type can not be created from the provided Postgres type.
102129
fn from_sql(ty: PostgresType, raw: &Option<~[u8]>) -> Self;
103130
}
104131

@@ -188,7 +215,13 @@ from_map_impl!(PgTimestamp | PgTimestampZ, Timespec, |buf| {
188215
})
189216
from_option_impl!(Timespec)
190217

218+
/// A trait for types that can be converted into Postgres values
191219
pub trait ToSql {
220+
/// Converts the value of `self` into a format appropriate for the Postgres
221+
/// backend.
222+
///
223+
/// Fails if this type cannot be converted into the specified Postgres
224+
/// type.
192225
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>);
193226
}
194227

0 commit comments

Comments
 (0)