Skip to content

Commit 58571ca

Browse files
committed
Add examples!
1 parent 999804a commit 58571ca

3 files changed

Lines changed: 128 additions & 4 deletions

File tree

src/lib.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,24 @@ pub struct PostgresCancelData {
267267
/// A `PostgresCancelData` object can be created via
268268
/// `PostgresConnection::cancel_data`. The object can cancel any query made on
269269
/// that connection.
270+
///
271+
/// # Example
272+
///
273+
/// ```rust
274+
/// # extern crate postgres;
275+
/// # fn main() {}
276+
/// # fn foo() {
277+
/// # use postgres::{PostgresConnection, NoSsl};
278+
/// # let url = "";
279+
/// let conn = PostgresConnection::connect(url, &NoSsl);
280+
/// let cancel_data = conn.cancel_data();
281+
/// spawn(proc() {
282+
/// conn.execute("SOME EXPENSIVE QUERY", []);
283+
/// });
284+
/// # let _ =
285+
/// postgres::cancel_query(url, &NoSsl, cancel_data);
286+
/// # }
287+
/// ```
270288
pub fn cancel_query(url: &str, ssl: &SslMode, data: PostgresCancelData)
271289
-> Result<(), PostgresConnectError> {
272290
let Url { host, port, .. }: Url = match FromStr::from_str(url) {
@@ -691,6 +709,21 @@ impl PostgresConnection {
691709
/// The password may be omitted if not required. The default Postgres port
692710
/// (5432) is used if none is specified. The database name defaults to the
693711
/// username if not specified.
712+
///
713+
/// # Example
714+
///
715+
/// ```rust
716+
/// # fn main() {}
717+
/// # fn foo() {
718+
/// # use postgres::{PostgresConnection, NoSsl};
719+
/// let url = "postgres://postgres:hunter2@localhost:2994/foodb";
720+
/// let maybe_conn = PostgresConnection::try_connect(url, &NoSsl);
721+
/// let conn = match maybe_conn {
722+
/// Ok(conn) => conn,
723+
/// Err(err) => fail!("Error connecting: {}", err)
724+
/// };
725+
/// # }
726+
/// ```
694727
pub fn try_connect(url: &str, ssl: &SslMode)
695728
-> Result<PostgresConnection, PostgresConnectError> {
696729
InnerPostgresConnection::try_connect(url, ssl).map(|conn| {
@@ -736,6 +769,20 @@ impl PostgresConnection {
736769
///
737770
/// The statement is associated with the connection that created it and may
738771
/// not outlive that connection.
772+
///
773+
/// # Example
774+
///
775+
/// ```rust
776+
/// # use postgres::{PostgresConnection, NoSsl};
777+
/// # fn main() {}
778+
/// # fn foo() {
779+
/// # let conn = PostgresConnection::connect("", &NoSsl);
780+
/// let maybe_stmt = conn.try_prepare("SELECT foo FROM bar WHERE baz = $1");
781+
/// let stmt = match maybe_stmt {
782+
/// Ok(stmt) => stmt,
783+
/// Err(err) => fail!("Error preparing statement: {}", err)
784+
/// };
785+
/// # }
739786
pub fn try_prepare<'a>(&'a self, query: &str)
740787
-> Result<NormalPostgresStatement<'a>, PostgresError> {
741788
self.conn.with_mut(|conn| conn.try_prepare(query, self))
@@ -761,6 +808,26 @@ impl PostgresConnection {
761808
/// is active until the `PostgresTransaction` object falls out of scope.
762809
/// A transaction will commit by default unless the task fails or the
763810
/// transaction is set to roll back.
811+
///
812+
/// # Example
813+
///
814+
/// ```rust
815+
/// # use postgres::{PostgresConnection, NoSsl};
816+
/// # fn main() {}
817+
/// # fn foo() -> Result<(), postgres::error::PostgresError> {
818+
/// # let conn = PostgresConnection::connect("", &NoSsl);
819+
/// let trans = try!(conn.try_transaction());
820+
/// trans.execute("UPDATE foo SET bar = 10", []);
821+
///
822+
/// # let something_bad_happened = true;
823+
/// if something_bad_happened {
824+
/// trans.set_rollback();
825+
/// }
826+
///
827+
/// drop(trans);
828+
/// # Ok(())
829+
/// # }
830+
/// ```
764831
pub fn try_transaction<'a>(&'a self)
765832
-> Result<PostgresTransaction<'a>, PostgresError> {
766833
check_desync!(self);

src/pool.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ impl InnerConnectionPool {
3333
/// A simple fixed-size Postgres connection pool.
3434
///
3535
/// It can be shared across tasks.
36+
///
37+
/// # Example
38+
///
39+
/// ```rust
40+
/// # use postgres::NoSsl;
41+
/// # use postgres::pool::PostgresConnectionPool;
42+
/// # fn main() {}
43+
/// # fn foo() {
44+
/// let pool = PostgresConnectionPool::new("postgres://postgres@localhost",
45+
/// NoSsl, 5);
46+
/// for _ in range(0, 10) {
47+
/// let pool = pool.clone();
48+
/// spawn(proc() {
49+
/// let conn = pool.get_connection();
50+
/// conn.execute("UPDATE foo SET bar = 1", []);
51+
/// });
52+
/// }
53+
/// # }
54+
/// ```
3655
#[deriving(Clone)]
3756
pub struct PostgresConnectionPool {
3857
priv pool: MutexArc<InnerConnectionPool>

src/stmt.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ pub trait PostgresStatement {
3737
///
3838
/// Fails if the number or types of the provided parameters do not match
3939
/// the parameters of the statement.
40+
///
41+
/// # Example
42+
///
43+
/// ```rust
44+
/// # use postgres::{PostgresConnection, NoSsl, PostgresStatement};
45+
/// # use postgres::types::ToSql;
46+
/// # fn main() {}
47+
/// # fn foo() {
48+
/// # let conn = PostgresConnection::connect("", &NoSsl);
49+
/// # let bar = 1i32;
50+
/// # let baz = true;
51+
/// let stmt = conn.prepare("UPDATE foo SET bar = $1 WHERE baz = $2");
52+
/// match stmt.try_execute([&bar as &ToSql, &baz as &ToSql]) {
53+
/// Ok(count) => println!("{} row(s) updated", count),
54+
/// Err(err) => println!("Error executing query: {}", err)
55+
/// }
56+
/// # }
4057
fn try_execute(&self, params: &[&ToSql]) -> Result<uint, PostgresError>;
4158

4259
/// A convenience function wrapping `try_execute`.
@@ -47,7 +64,7 @@ pub trait PostgresStatement {
4764
fn execute(&self, params: &[&ToSql]) -> uint {
4865
match self.try_execute(params) {
4966
Ok(count) => count,
50-
Err(err) => fail!("Error running query\n{}", err.to_str())
67+
Err(err) => fail!("Error running query\n{}", err)
5168
}
5269
}
5370

@@ -58,6 +75,27 @@ pub trait PostgresStatement {
5875
///
5976
/// Fails if the number or types of the provided parameters do not match
6077
/// the parameters of the statement.
78+
///
79+
/// # Example
80+
///
81+
/// ```rust
82+
/// # use postgres::{PostgresConnection, NoSsl, PostgresStatement};
83+
/// # use postgres::types::ToSql;
84+
/// # fn main() {}
85+
/// # fn foo() {
86+
/// # let conn = PostgresConnection::connect("", &NoSsl);
87+
/// let stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1");
88+
/// # let baz = true;
89+
/// let mut rows = match stmt.try_query([&baz as &ToSql]) {
90+
/// Ok(rows) => rows,
91+
/// Err(err) => fail!("Error running query: {}", err)
92+
/// };
93+
/// for row in rows {
94+
/// let foo: i32 = row["foo"];
95+
/// println!("foo: {}", foo);
96+
/// }
97+
/// # }
98+
/// ```
6199
fn try_query<'a>(&'a self, params: &[&ToSql])
62100
-> Result<PostgresResult<'a>, PostgresError>;
63101

@@ -69,7 +107,7 @@ pub trait PostgresStatement {
69107
fn query<'a>(&'a self, params: &[&ToSql]) -> PostgresResult<'a> {
70108
match self.try_query(params) {
71109
Ok(result) => result,
72-
Err(err) => fail!("Error executing query:\n{}", err.to_str())
110+
Err(err) => fail!("Error executing query:\n{}", err)
73111
}
74112
}
75113

@@ -431,8 +469,8 @@ impl<'stmt> PostgresResult<'stmt> {
431469

432470
/// Like `PostgresResult::next` except that it returns any errors to the
433471
/// caller instead of failing.
434-
pub fn try_next(&mut self)
435-
-> Result<Option<PostgresRow<'stmt>>, PostgresError> {
472+
pub fn try_next(&mut self) -> Result<Option<PostgresRow<'stmt>>,
473+
PostgresError> {
436474
if self.data.is_empty() && self.more_rows {
437475
try!(self.execute());
438476
}

0 commit comments

Comments
 (0)