Skip to content

Commit 7c9717d

Browse files
committed
Finish Client docs
1 parent 2c78658 commit 7c9717d

1 file changed

Lines changed: 117 additions & 23 deletions

File tree

postgres/src/client.rs

Lines changed: 117 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ pub struct Client(tokio_postgres::Client);
1919
impl Client {
2020
/// A convenience function which parses a configuration string into a `Config` and then connects to the database.
2121
///
22+
/// See the documentation for [`Config`] for information about the connection syntax.
23+
///
2224
/// Requires the `runtime` Cargo feature (enabled by default).
25+
///
26+
/// [`Config`]: config/struct.Config.html
2327
#[cfg(feature = "runtime")]
2428
pub fn connect<T>(params: &str, tls_mode: T) -> Result<Client, Error>
2529
where
@@ -39,22 +43,6 @@ impl Client {
3943
Config::new()
4044
}
4145

42-
/// Creates a new prepared statement.
43-
///
44-
/// Prepared statements can be executed repeatedly, and may contain query parameters (indicated by `$1`, `$2`, etc),
45-
/// which are set when executed. Prepared statements can only be used with the connection that created them.
46-
pub fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
47-
self.0.prepare(query).wait()
48-
}
49-
50-
/// Like `prepare`, but allows the types of query parameters to be explicitly specified.
51-
///
52-
/// The list of types may be smaller than the number of parameters - the types of the remaining parameters will be
53-
/// inferred. For example, `client.prepare_typed(query, &[])` is equivalent to `client.prepare(query)`.
54-
pub fn prepare_typed(&mut self, query: &str, types: &[Type]) -> Result<Statement, Error> {
55-
self.0.prepare_typed(query, types).wait()
56-
}
57-
5846
/// Executes a statement, returning the number of rows modified.
5947
///
6048
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
@@ -172,10 +160,80 @@ impl Client {
172160
Ok(QueryIter::new(self.0.query(&statement, params)))
173161
}
174162

163+
/// Creates a new prepared statement.
164+
///
165+
/// Prepared statements can be executed repeatedly, and may contain query parameters (indicated by `$1`, `$2`, etc),
166+
/// which are set when executed. Prepared statements can only be used with the connection that created them.
167+
///
168+
/// # Examples
169+
///
170+
/// ```no_run
171+
/// use postgres::{Client, NoTls};
172+
///
173+
/// # fn main() -> Result<(), postgres::Error> {
174+
/// let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
175+
///
176+
/// let statement = client.prepare("SELECT name FROM people WHERE id = $1")?;
177+
///
178+
/// for id in 0..10 {
179+
/// let rows = client.query(&statement, &[&id])?;
180+
/// let name: &str = rows[0].get(0);
181+
/// println!("name: {}", name);
182+
/// }
183+
/// # Ok(())
184+
/// # }
185+
/// ```
186+
pub fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
187+
self.0.prepare(query).wait()
188+
}
189+
190+
/// Like `prepare`, but allows the types of query parameters to be explicitly specified.
191+
///
192+
/// The list of types may be smaller than the number of parameters - the types of the remaining parameters will be
193+
/// inferred. For example, `client.prepare_typed(query, &[])` is equivalent to `client.prepare(query)`.
194+
///
195+
/// # Examples
196+
///
197+
/// ```no_run
198+
/// use postgres::{Client, NoTls};
199+
/// use postgres::types::Type;
200+
///
201+
/// # fn main() -> Result<(), postgres::Error> {
202+
/// let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
203+
///
204+
/// let statement = client.prepare_typed(
205+
/// "SELECT name FROM people WHERE id = $1",
206+
/// &[Type::INT8],
207+
/// )?;
208+
///
209+
/// for id in 0..10 {
210+
/// let rows = client.query(&statement, &[&id])?;
211+
/// let name: &str = rows[0].get(0);
212+
/// println!("name: {}", name);
213+
/// }
214+
/// # Ok(())
215+
/// # }
216+
pub fn prepare_typed(&mut self, query: &str, types: &[Type]) -> Result<Statement, Error> {
217+
self.0.prepare_typed(query, types).wait()
218+
}
219+
175220
/// Executes a `COPY FROM STDIN` statement, returning the number of rows created.
176221
///
177222
/// The `query` argument can either be a `Statement`, or a raw query string. The data in the provided reader is
178223
/// passed along to the server verbatim; it is the caller's responsibility to ensure it uses the proper format.
224+
///
225+
/// # Examples
226+
///
227+
/// ```no_run
228+
/// use postgres::{Client, NoTls};
229+
///
230+
/// # fn main() -> Result<(), postgres::Error> {
231+
/// let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
232+
///
233+
/// client.copy_in("COPY people FROM stdin", &[], &mut "1\tjohn\n2\tjane\n".as_bytes())?;
234+
/// # Ok(())
235+
/// # }
236+
/// ```
179237
pub fn copy_in<T, R>(
180238
&mut self,
181239
query: &T,
@@ -195,6 +253,22 @@ impl Client {
195253
/// Executes a `COPY TO STDOUT` statement, returning a reader of the resulting data.
196254
///
197255
/// The `query` argument can either be a `Statement`, or a raw query string.
256+
///
257+
/// # Examples
258+
///
259+
/// ```no_run
260+
/// use postgres::{Client, NoTls};
261+
/// use std::io::Read;
262+
///
263+
/// # fn main() -> Result<(), Box<std::error::Error>> {
264+
/// let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
265+
///
266+
/// let mut reader = client.copy_out("COPY people TO stdout", &[])?;
267+
/// let mut buf = vec![];
268+
/// reader.read_to_end(&mut buf)?;
269+
/// # Ok(())
270+
/// # }
271+
/// ```
198272
pub fn copy_out<T>(
199273
&mut self,
200274
query: &T,
@@ -227,13 +301,8 @@ impl Client {
227301
self.simple_query_iter(query)?.collect()
228302
}
229303

230-
/// Executes a sequence of SQL statements using the simple query protocol.
231-
///
232-
/// Statements should be separated by semicolons. If an error occurs, execution of the sequence will stop at that
233-
/// point. The simple query protocol returns the values in rows as strings rather than in their binary encodings,
234-
/// so the associated row type doesn't work with the `FromSql` trait. Rather than simply returning the rows, this
235-
/// method returns a sequence of an enum which indicates either the completion of one of the commands, or a row of
236-
/// data. This preserves the framing between the separate statements in the request.
304+
/// Like `simple_query`, except that it returns a fallible iterator over the resulting values rather than buffering
305+
/// the response in memory.
237306
///
238307
/// # Warning
239308
///
@@ -245,23 +314,48 @@ impl Client {
245314
}
246315

247316
/// Begins a new database transaction.
317+
///
318+
/// The transaction will roll back by default - use the `commit` method to commit it.
319+
///
320+
/// # Examples
321+
///
322+
/// ```no_run
323+
/// use postgres::{Client, NoTls};
324+
///
325+
/// # fn main() -> Result<(), postgres::Error> {
326+
/// let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
327+
///
328+
/// let mut transaction = client.transaction()?;
329+
/// transaction.execute("UPDATE foo SET bar = 10", &[])?;
330+
/// // ...
331+
///
332+
/// transaction.commit()?;
333+
/// # Ok(())
334+
/// # }
335+
/// ```
248336
pub fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
249337
self.simple_query("BEGIN")?;
250338
Ok(Transaction::new(self))
251339
}
252340

341+
/// Determines if the client's connection has already closed.
342+
///
343+
/// If this returns `true`, the client is no longer usable.
253344
pub fn is_closed(&self) -> bool {
254345
self.0.is_closed()
255346
}
256347

348+
/// Returns a shared reference to the inner nonblocking client.
257349
pub fn get_ref(&self) -> &tokio_postgres::Client {
258350
&self.0
259351
}
260352

353+
/// Returns a mutable reference to the inner nonblocking client.
261354
pub fn get_mut(&mut self) -> &mut tokio_postgres::Client {
262355
&mut self.0
263356
}
264357

358+
/// Consumes the client, returning the inner nonblocking client.
265359
pub fn into_inner(self) -> tokio_postgres::Client {
266360
self.0
267361
}

0 commit comments

Comments
 (0)