@@ -11,9 +11,15 @@ use tokio_postgres::{Error, Row, SimpleQueryMessage};
1111use crate :: Config ;
1212use crate :: { CopyOutReader , QueryIter , SimpleQueryIter , Statement , ToStatement , Transaction } ;
1313
14+ /// A synchronous PostgreSQL client.
15+ ///
16+ /// This is a lightweight wrapper over the asynchronous tokio_postgres `Client`.
1417pub struct Client ( tokio_postgres:: Client ) ;
1518
1619impl Client {
20+ /// A convenience function which parses a configuration string into a `Config` and then connects to the database.
21+ ///
22+ /// Requires the `runtime` Cargo feature (enabled by default).
1723 #[ cfg( feature = "runtime" ) ]
1824 pub fn connect < T > ( params : & str , tls_mode : T ) -> Result < Client , Error >
1925 where
@@ -25,19 +31,64 @@ impl Client {
2531 params. parse :: < Config > ( ) ?. connect ( tls_mode)
2632 }
2733
34+ /// Returns a new `Config` object which can be used to configure and connect to a database.
35+ ///
36+ /// Requires the `runtime` Cargo feature (enabled by default).
2837 #[ cfg( feature = "runtime" ) ]
2938 pub fn configure ( ) -> Config {
3039 Config :: new ( )
3140 }
3241
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.
3346 pub fn prepare ( & mut self , query : & str ) -> Result < Statement , Error > {
3447 self . 0 . prepare ( query) . wait ( )
3548 }
3649
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)`.
3754 pub fn prepare_typed ( & mut self , query : & str , types : & [ Type ] ) -> Result < Statement , Error > {
3855 self . 0 . prepare_typed ( query, types) . wait ( )
3956 }
4057
58+ /// Executes a statement, returning the number of rows modified.
59+ ///
60+ /// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
61+ /// provided, 1-indexed.
62+ ///
63+ /// If the statement does not modify any rows (e.g. `SELECT`), 0 is returned.
64+ ///
65+ /// The `query` argument can either be a `Statement`, or a raw query string. If the same statement will be
66+ /// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
67+ /// with the `prepare` method.
68+ ///
69+ /// # Panics
70+ ///
71+ /// Panics if the number of parameters provided does not match the number expected.
72+ ///
73+ /// # Example
74+ ///
75+ /// ```no_run
76+ /// use postgres::{Client, NoTls};
77+ ///
78+ /// # fn main() -> Result<(), postgres::Error> {
79+ /// let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
80+ ///
81+ /// let bar = 1i32;
82+ /// let baz = true;
83+ /// let rows_updated = client.execute(
84+ /// "UPDATE foo SET bar = $1 WHERE baz = $2",
85+ /// &[&bar, &baz],
86+ /// )?;
87+ ///
88+ /// println!("{} rows updated", rows_updated);
89+ /// # Ok(())
90+ /// # }
91+ /// ```
4192 pub fn execute < T > ( & mut self , query : & T , params : & [ & dyn ToSql ] ) -> Result < u64 , Error >
4293 where
4394 T : ?Sized + ToStatement ,
@@ -46,13 +97,69 @@ impl Client {
4697 self . 0 . execute ( & statement, params) . wait ( )
4798 }
4899
100+ /// Executes a statement, returning the resulting rows.
101+ ///
102+ /// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
103+ /// provided, 1-indexed.
104+ ///
105+ /// The `query` argument can either be a `Statement`, or a raw query string. If the same statement will be
106+ /// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
107+ /// with the `prepare` method.
108+ ///
109+ /// The `query_iter` method can be used to avoid buffering all rows in memory at once.
110+ ///
111+ /// # Panics
112+ ///
113+ /// Panics if the number of parameters provided does not match the number expected.
114+ ///
115+ /// # Examples
116+ ///
117+ /// ```no_run
118+ /// use postgres::{Client, NoTls};
119+ ///
120+ /// # fn main() -> Result<(), postgres::Error> {
121+ /// let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
122+ ///
123+ /// let baz = true;
124+ /// for row in client.query("SELECT foo FROM bar WHERE baz = $1", &[&baz])? {
125+ /// let foo: i32 = row.get("foo");
126+ /// println!("foo: {}", foo);
127+ /// }
128+ /// # Ok(())
129+ /// # }
130+ /// ```
49131 pub fn query < T > ( & mut self , query : & T , params : & [ & dyn ToSql ] ) -> Result < Vec < Row > , Error >
50132 where
51133 T : ?Sized + ToStatement ,
52134 {
53135 self . query_iter ( query, params) ?. collect ( )
54136 }
55137
138+ /// Like `query`, except that it returns a fallible iterator over the resulting rows rather than buffering the
139+ /// response in memory.
140+ ///
141+ /// # Panics
142+ ///
143+ /// Panics if the number of parameters provided does not match the number expected.
144+ ///
145+ /// # Examples
146+ ///
147+ /// ```no_run
148+ /// use postgres::{Client, NoTls};
149+ /// use fallible_iterator::FallibleIterator;
150+ ///
151+ /// # fn main() -> Result<(), postgres::Error> {
152+ /// let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
153+ ///
154+ /// let baz = true;
155+ /// let mut it = client.query_iter("SELECT foo FROM bar WHERE baz = $1", &[&baz])?;
156+ ///
157+ /// while let Some(row) = it.next()? {
158+ /// let foo: i32 = row.get("foo");
159+ /// println!("foo: {}", foo);
160+ /// }
161+ /// # Ok(())
162+ /// # }
56163 pub fn query_iter < T > (
57164 & mut self ,
58165 query : & T ,
@@ -65,6 +172,10 @@ impl Client {
65172 Ok ( QueryIter :: new ( self . 0 . query ( & statement, params) ) )
66173 }
67174
175+ /// Executes a `COPY FROM STDIN` statement, returning the number of rows created.
176+ ///
177+ /// The `query` argument can either be a `Statement`, or a raw query string. The data in the provided reader is
178+ /// passed along to the server verbatim; it is the caller's responsibility to ensure it uses the proper format.
68179 pub fn copy_in < T , R > (
69180 & mut self ,
70181 query : & T ,
@@ -81,6 +192,9 @@ impl Client {
81192 . wait ( )
82193 }
83194
195+ /// Executes a `COPY TO STDOUT` statement, returning a reader of the resulting data.
196+ ///
197+ /// The `query` argument can either be a `Statement`, or a raw query string.
84198 pub fn copy_out < T > (
85199 & mut self ,
86200 query : & T ,
@@ -94,14 +208,43 @@ impl Client {
94208 CopyOutReader :: new ( stream)
95209 }
96210
211+ /// Executes a sequence of SQL statements using the simple query protocol.
212+ ///
213+ /// Statements should be separated by semicolons. If an error occurs, execution of the sequence will stop at that
214+ /// point. The simple query protocol returns the values in rows as strings rather than in their binary encodings,
215+ /// so the associated row type doesn't work with the `FromSql` trait. Rather than simply returning the rows, this
216+ /// method returns a sequence of an enum which indicates either the completion of one of the commands, or a row of
217+ /// data. This preserves the framing between the separate statements in the request.
218+ ///
219+ /// This is a simple convenience method over `simple_query_iter`.
220+ ///
221+ /// # Warning
222+ ///
223+ /// Prepared statements should be use for any query which contains user-specified data, as they provided the
224+ /// functionality to safely imbed that data in the request. Do not form statements via string concatenation and pass
225+ /// them to this method!
97226 pub fn simple_query ( & mut self , query : & str ) -> Result < Vec < SimpleQueryMessage > , Error > {
98227 self . simple_query_iter ( query) ?. collect ( )
99228 }
100229
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.
237+ ///
238+ /// # Warning
239+ ///
240+ /// Prepared statements should be use for any query which contains user-specified data, as they provided the
241+ /// functionality to safely imbed that data in the request. Do not form statements via string concatenation and pass
242+ /// them to this method!
101243 pub fn simple_query_iter ( & mut self , query : & str ) -> Result < SimpleQueryIter < ' _ > , Error > {
102244 Ok ( SimpleQueryIter :: new ( self . 0 . simple_query ( query) ) )
103245 }
104246
247+ /// Begins a new database transaction.
105248 pub fn transaction ( & mut self ) -> Result < Transaction < ' _ > , Error > {
106249 self . simple_query ( "BEGIN" ) ?;
107250 Ok ( Transaction :: new ( self ) )
0 commit comments