Make examples consistent
This commit is contained in:
parent
e13cca9fe8
commit
a9effb427a
85
src/lib.rs
85
src/lib.rs
@ -268,8 +268,7 @@ pub struct CancelData {
|
|||||||
/// thread::spawn(move || {
|
/// thread::spawn(move || {
|
||||||
/// conn.execute("SOME EXPENSIVE QUERY", &[]).unwrap();
|
/// conn.execute("SOME EXPENSIVE QUERY", &[]).unwrap();
|
||||||
/// });
|
/// });
|
||||||
/// # let _ =
|
/// postgres::cancel_query(url, &SslMode::None, cancel_data).unwrap();
|
||||||
/// postgres::cancel_query(url, &SslMode::None, cancel_data);
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn cancel_query<T>(params: T,
|
pub fn cancel_query<T>(params: T,
|
||||||
ssl: &SslMode,
|
ssl: &SslMode,
|
||||||
@ -873,25 +872,24 @@ impl Connection {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use postgres::{Connection, SslMode};
|
/// use postgres::{Connection, SslMode};
|
||||||
/// # fn f() -> Result<(), ::postgres::error::ConnectError> {
|
///
|
||||||
/// let url = "postgresql://postgres:hunter2@localhost:2994/foodb";
|
/// let url = "postgresql://postgres:hunter2@localhost:2994/foodb";
|
||||||
/// let conn = try!(Connection::connect(url, &SslMode::None));
|
/// let conn = Connection::connect(url, &SslMode::None).unwrap();
|
||||||
/// # Ok(()) };
|
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use postgres::{Connection, SslMode};
|
/// use postgres::{Connection, SslMode};
|
||||||
/// # fn f() -> Result<(), ::postgres::error::ConnectError> {
|
///
|
||||||
/// let url = "postgresql://postgres@%2Frun%2Fpostgres";
|
/// let url = "postgresql://postgres@%2Frun%2Fpostgres";
|
||||||
/// let conn = try!(Connection::connect(url, &SslMode::None));
|
/// let conn = Connection::connect(url, &SslMode::None).unwrap();
|
||||||
/// # Ok(()) };
|
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use postgres::{Connection, UserInfo, ConnectParams, SslMode, ConnectTarget};
|
/// use postgres::{Connection, UserInfo, ConnectParams, SslMode, ConnectTarget};
|
||||||
|
///
|
||||||
/// # #[cfg(feature = "unix_socket")]
|
/// # #[cfg(feature = "unix_socket")]
|
||||||
/// # fn f() -> Result<(), ::postgres::error::ConnectError> {
|
/// # fn f() {
|
||||||
/// # let some_crazy_path = Path::new("");
|
/// # let some_crazy_path = Path::new("");
|
||||||
/// let params = ConnectParams {
|
/// let params = ConnectParams {
|
||||||
/// target: ConnectTarget::Unix(some_crazy_path),
|
/// target: ConnectTarget::Unix(some_crazy_path),
|
||||||
@ -903,8 +901,8 @@ impl Connection {
|
|||||||
/// database: None,
|
/// database: None,
|
||||||
/// options: vec![],
|
/// options: vec![],
|
||||||
/// };
|
/// };
|
||||||
/// let conn = try!(Connection::connect(params, &SslMode::None));
|
/// let conn = Connection::connect(params, &SslMode::None).unwrap();
|
||||||
/// # Ok(()) };
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn connect<T>(params: T, ssl: &SslMode) -> result::Result<Connection, ConnectError>
|
pub fn connect<T>(params: T, ssl: &SslMode) -> result::Result<Connection, ConnectError>
|
||||||
where T: IntoConnectParams
|
where T: IntoConnectParams
|
||||||
@ -937,12 +935,14 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use postgres::{Connection, SslMode};
|
/// # use postgres::{Connection, SslMode};
|
||||||
|
/// # let x = 10i32;
|
||||||
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
|
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
|
||||||
/// let maybe_stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1");
|
/// let stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1").unwrap();
|
||||||
/// let stmt = match maybe_stmt {
|
/// for row in stmt.query(&[&x]).unwrap() {
|
||||||
/// Ok(stmt) => stmt,
|
/// let foo: String = row.get(0);
|
||||||
/// Err(err) => panic!("Error preparing statement: {:?}", err)
|
/// println!("foo: {}", foo);
|
||||||
/// };
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn prepare<'a>(&'a self, query: &str) -> Result<Statement<'a>> {
|
pub fn prepare<'a>(&'a self, query: &str) -> Result<Statement<'a>> {
|
||||||
self.conn.borrow_mut().prepare(query, self)
|
self.conn.borrow_mut().prepare(query, self)
|
||||||
}
|
}
|
||||||
@ -958,14 +958,13 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use postgres::{Connection, SslMode};
|
/// # use postgres::{Connection, SslMode};
|
||||||
/// # fn f() -> postgres::Result<()> {
|
|
||||||
/// # let x = 10i32;
|
/// # let x = 10i32;
|
||||||
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
|
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
|
||||||
/// let stmt = try!(conn.prepare_cached("SELECT foo FROM bar WHERE baz = $1"));
|
/// let stmt = conn.prepare_cached("SELECT foo FROM bar WHERE baz = $1").unwrap();
|
||||||
/// for row in try!(stmt.query(&[&x])) {
|
/// for row in stmt.query(&[&x]).unwrap() {
|
||||||
/// println!("foo: {}", row.get::<_, String>(0));
|
/// let foo: String = row.get(0);
|
||||||
|
/// println!("foo: {}", foo);
|
||||||
/// }
|
/// }
|
||||||
/// # Ok(()) };
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn prepare_cached<'a>(&'a self, query: &str) -> Result<Statement<'a>> {
|
pub fn prepare_cached<'a>(&'a self, query: &str) -> Result<Statement<'a>> {
|
||||||
self.conn.borrow_mut().prepare_cached(query, self)
|
self.conn.borrow_mut().prepare_cached(query, self)
|
||||||
@ -989,15 +988,12 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use postgres::{Connection, SslMode};
|
/// # use postgres::{Connection, SslMode};
|
||||||
/// # fn foo() -> Result<(), postgres::error::Error> {
|
|
||||||
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
|
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
|
||||||
/// let trans = try!(conn.transaction());
|
/// let trans = conn.transaction().unwrap();
|
||||||
/// try!(trans.execute("UPDATE foo SET bar = 10", &[]));
|
/// trans.execute("UPDATE foo SET bar = 10", &[]).unwrap();
|
||||||
/// // ...
|
/// // ...
|
||||||
///
|
///
|
||||||
/// try!(trans.commit());
|
/// trans.commit().unwrap();
|
||||||
/// # Ok(())
|
|
||||||
/// # }
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn transaction<'a>(&'a self) -> Result<Transaction<'a>> {
|
pub fn transaction<'a>(&'a self) -> Result<Transaction<'a>> {
|
||||||
let mut conn = self.conn.borrow_mut();
|
let mut conn = self.conn.borrow_mut();
|
||||||
@ -1074,23 +1070,22 @@ impl Connection {
|
|||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use postgres::{Connection, Result};
|
/// # use postgres::{Connection, SslMode, Result};
|
||||||
/// fn init_db(conn: &Connection) -> Result<()> {
|
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
|
||||||
/// conn.batch_execute("
|
/// conn.batch_execute("
|
||||||
/// CREATE TABLE person (
|
/// CREATE TABLE person (
|
||||||
/// id SERIAL PRIMARY KEY,
|
/// id SERIAL PRIMARY KEY,
|
||||||
/// name NOT NULL
|
/// name NOT NULL
|
||||||
/// );
|
/// );
|
||||||
///
|
///
|
||||||
/// CREATE TABLE purchase (
|
/// CREATE TABLE purchase (
|
||||||
/// id SERIAL PRIMARY KEY,
|
/// id SERIAL PRIMARY KEY,
|
||||||
/// person INT NOT NULL REFERENCES person (id),
|
/// person INT NOT NULL REFERENCES person (id),
|
||||||
/// time TIMESTAMPTZ NOT NULL,
|
/// time TIMESTAMPTZ NOT NULL,
|
||||||
/// );
|
/// );
|
||||||
///
|
///
|
||||||
/// CREATE INDEX ON purchase (time);
|
/// CREATE INDEX ON purchase (time);
|
||||||
/// ")
|
/// ").unwrap();
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn batch_execute(&self, query: &str) -> Result<()> {
|
pub fn batch_execute(&self, query: &str) -> Result<()> {
|
||||||
self.conn.borrow_mut().quick_query(query).map(|_| ())
|
self.conn.borrow_mut().quick_query(query).map(|_| ())
|
||||||
|
11
src/rows.rs
11
src/rows.rs
@ -225,11 +225,12 @@ impl<'a> Row<'a> {
|
|||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use postgres::{Connection, SslMode};
|
/// # use postgres::{Connection, SslMode};
|
||||||
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
|
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
|
||||||
/// # let stmt = conn.prepare("").unwrap();
|
/// let stmt = conn.prepare("SELECT foo, bar from BAZ").unwrap();
|
||||||
/// # let mut result = stmt.query(&[]).unwrap();
|
/// for row in stmt.query(&[]).unwrap() {
|
||||||
/// # let row = result.iter().next().unwrap();
|
/// let foo: i32 = row.get(0);
|
||||||
/// let foo: i32 = row.get(0);
|
/// let bar: String = row.get("bar");
|
||||||
/// let bar: String = row.get("bar");
|
/// println!("{}: {}", foo, bar);
|
||||||
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn get<I, T>(&self, idx: I) -> T
|
pub fn get<I, T>(&self, idx: I) -> T
|
||||||
where I: RowIndex + fmt::Debug + Clone,
|
where I: RowIndex + fmt::Debug + Clone,
|
||||||
|
12
src/stmt.rs
12
src/stmt.rs
@ -159,10 +159,8 @@ impl<'conn> Statement<'conn> {
|
|||||||
/// # let bar = 1i32;
|
/// # let bar = 1i32;
|
||||||
/// # let baz = true;
|
/// # let baz = true;
|
||||||
/// let stmt = conn.prepare("UPDATE foo SET bar = $1 WHERE baz = $2").unwrap();
|
/// let stmt = conn.prepare("UPDATE foo SET bar = $1 WHERE baz = $2").unwrap();
|
||||||
/// match stmt.execute(&[&bar, &baz]) {
|
/// let rows_updated = stmt.execute(&[&bar, &baz]).unwrap();
|
||||||
/// Ok(count) => println!("{} row(s) updated", count),
|
/// println!("{} rows updated", rows_updated);
|
||||||
/// Err(err) => println!("Error executing query: {:?}", err)
|
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn execute(&self, params: &[&ToSql]) -> Result<u64> {
|
pub fn execute(&self, params: &[&ToSql]) -> Result<u64> {
|
||||||
check_desync!(self.conn);
|
check_desync!(self.conn);
|
||||||
@ -231,11 +229,7 @@ impl<'conn> Statement<'conn> {
|
|||||||
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
|
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
|
||||||
/// let stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1").unwrap();
|
/// let stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1").unwrap();
|
||||||
/// # let baz = true;
|
/// # let baz = true;
|
||||||
/// let rows = match stmt.query(&[&baz]) {
|
/// for row in stmt.query(&[&baz]).unwrap() {
|
||||||
/// Ok(rows) => rows,
|
|
||||||
/// Err(err) => panic!("Error running query: {:?}", err)
|
|
||||||
/// };
|
|
||||||
/// for row in &rows {
|
|
||||||
/// let foo: i32 = row.get("foo");
|
/// let foo: i32 = row.get("foo");
|
||||||
/// println!("foo: {}", foo);
|
/// println!("foo: {}", foo);
|
||||||
/// }
|
/// }
|
||||||
|
Loading…
Reference in New Issue
Block a user