@@ -11,9 +11,15 @@ use tokio_postgres::{Error, Row, SimpleQueryMessage};
11
11
use crate :: Config ;
12
12
use crate :: { CopyOutReader , QueryIter , SimpleQueryIter , Statement , ToStatement , Transaction } ;
13
13
14
+ /// A synchronous PostgreSQL client.
15
+ ///
16
+ /// This is a lightweight wrapper over the asynchronous tokio_postgres `Client`.
14
17
pub struct Client ( tokio_postgres:: Client ) ;
15
18
16
19
impl 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).
17
23
#[ cfg( feature = "runtime" ) ]
18
24
pub fn connect < T > ( params : & str , tls_mode : T ) -> Result < Client , Error >
19
25
where
@@ -25,19 +31,64 @@ impl Client {
25
31
params. parse :: < Config > ( ) ?. connect ( tls_mode)
26
32
}
27
33
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).
28
37
#[ cfg( feature = "runtime" ) ]
29
38
pub fn configure ( ) -> Config {
30
39
Config :: new ( )
31
40
}
32
41
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.
33
46
pub fn prepare ( & mut self , query : & str ) -> Result < Statement , Error > {
34
47
self . 0 . prepare ( query) . wait ( )
35
48
}
36
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)`.
37
54
pub fn prepare_typed ( & mut self , query : & str , types : & [ Type ] ) -> Result < Statement , Error > {
38
55
self . 0 . prepare_typed ( query, types) . wait ( )
39
56
}
40
57
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
+ /// ```
41
92
pub fn execute < T > ( & mut self , query : & T , params : & [ & dyn ToSql ] ) -> Result < u64 , Error >
42
93
where
43
94
T : ?Sized + ToStatement ,
@@ -46,13 +97,69 @@ impl Client {
46
97
self . 0 . execute ( & statement, params) . wait ( )
47
98
}
48
99
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
+ /// ```
49
131
pub fn query < T > ( & mut self , query : & T , params : & [ & dyn ToSql ] ) -> Result < Vec < Row > , Error >
50
132
where
51
133
T : ?Sized + ToStatement ,
52
134
{
53
135
self . query_iter ( query, params) ?. collect ( )
54
136
}
55
137
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
+ /// # }
56
163
pub fn query_iter < T > (
57
164
& mut self ,
58
165
query : & T ,
@@ -65,6 +172,10 @@ impl Client {
65
172
Ok ( QueryIter :: new ( self . 0 . query ( & statement, params) ) )
66
173
}
67
174
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.
68
179
pub fn copy_in < T , R > (
69
180
& mut self ,
70
181
query : & T ,
@@ -81,6 +192,9 @@ impl Client {
81
192
. wait ( )
82
193
}
83
194
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.
84
198
pub fn copy_out < T > (
85
199
& mut self ,
86
200
query : & T ,
@@ -94,14 +208,43 @@ impl Client {
94
208
CopyOutReader :: new ( stream)
95
209
}
96
210
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!
97
226
pub fn simple_query ( & mut self , query : & str ) -> Result < Vec < SimpleQueryMessage > , Error > {
98
227
self . simple_query_iter ( query) ?. collect ( )
99
228
}
100
229
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!
101
243
pub fn simple_query_iter ( & mut self , query : & str ) -> Result < SimpleQueryIter < ' _ > , Error > {
102
244
Ok ( SimpleQueryIter :: new ( self . 0 . simple_query ( query) ) )
103
245
}
104
246
247
+ /// Begins a new database transaction.
105
248
pub fn transaction ( & mut self ) -> Result < Transaction < ' _ > , Error > {
106
249
self . simple_query ( "BEGIN" ) ?;
107
250
Ok ( Transaction :: new ( self ) )
0 commit comments