@@ -9,6 +9,7 @@ use crate::query::RowStream;
9
9
use crate :: slice_iter;
10
10
#[ cfg( feature = "runtime" ) ]
11
11
use crate :: tls:: MakeTlsConnect ;
12
+ use pin_utils:: pin_mut;
12
13
use crate :: tls:: TlsConnect ;
13
14
use crate :: to_statement:: ToStatement ;
14
15
use crate :: types:: { Oid , ToSql , Type } ;
@@ -195,6 +196,13 @@ impl Client {
195
196
196
197
/// Executes a statement, returning a vector of the resulting rows.
197
198
///
199
+ /// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
200
+ /// provided, 1-indexed.
201
+ ///
202
+ /// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
203
+ /// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
204
+ /// with the `prepare` method.
205
+ ///
198
206
/// # Panics
199
207
///
200
208
/// Panics if the number of parameters provided does not match the number expected.
@@ -212,8 +220,52 @@ impl Client {
212
220
. await
213
221
}
214
222
223
+ /// Executes a statement which returns a single row, returning it.
224
+ ///
225
+ /// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
226
+ /// provided, 1-indexed.
227
+ ///
228
+ /// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
229
+ /// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
230
+ /// with the `prepare` method.
231
+ ///
232
+ /// Returns an error if the query does not return exactly one row.
233
+ ///
234
+ /// # Panics
235
+ ///
236
+ /// Panics if the number of parameters provided does not match the number expected.
237
+ pub async fn query_one < T > (
238
+ & self ,
239
+ statement : & T ,
240
+ params : & [ & ( dyn ToSql + Sync ) ] ,
241
+ ) -> Result < Row , Error >
242
+ where
243
+ T : ?Sized + ToStatement
244
+ {
245
+ let stream = self . query_raw ( statement, slice_iter ( params) ) . await ?;
246
+ pin_mut ! ( stream) ;
247
+
248
+ let row = match stream. try_next ( ) . await ? {
249
+ Some ( row) => row,
250
+ None => return Err ( Error :: row_count ( ) ) ,
251
+ } ;
252
+
253
+ if stream. try_next ( ) . await ?. is_some ( ) {
254
+ return Err ( Error :: row_count ( ) ) ;
255
+ }
256
+
257
+ Ok ( row)
258
+ }
259
+
215
260
/// The maximally flexible version of [`query`].
216
261
///
262
+ /// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
263
+ /// provided, 1-indexed.
264
+ ///
265
+ /// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
266
+ /// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
267
+ /// with the `prepare` method.
268
+ ///
217
269
/// # Panics
218
270
///
219
271
/// Panics if the number of parameters provided does not match the number expected.
@@ -231,6 +283,13 @@ impl Client {
231
283
232
284
/// Executes a statement, returning the number of rows modified.
233
285
///
286
+ /// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
287
+ /// provided, 1-indexed.
288
+ ///
289
+ /// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
290
+ /// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
291
+ /// with the `prepare` method.
292
+ ///
234
293
/// If the statement does not modify any rows (e.g. `SELECT`), 0 is returned.
235
294
///
236
295
/// # Panics
@@ -249,6 +308,13 @@ impl Client {
249
308
250
309
/// The maximally flexible version of [`execute`].
251
310
///
311
+ /// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
312
+ /// provided, 1-indexed.
313
+ ///
314
+ /// The `statement` argument can either be a `Statement`, or a raw query string. If the same statement will be
315
+ /// repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
316
+ /// with the `prepare` method.
317
+ ///
252
318
/// # Panics
253
319
///
254
320
/// Panics if the number of parameters provided does not match the number expected.
0 commit comments