@@ -9,6 +9,7 @@ use crate::query::RowStream;
99use crate :: slice_iter;
1010#[ cfg( feature = "runtime" ) ]
1111use crate :: tls:: MakeTlsConnect ;
12+ use pin_utils:: pin_mut;
1213use crate :: tls:: TlsConnect ;
1314use crate :: to_statement:: ToStatement ;
1415use crate :: types:: { Oid , ToSql , Type } ;
@@ -195,6 +196,13 @@ impl Client {
195196
196197 /// Executes a statement, returning a vector of the resulting rows.
197198 ///
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+ ///
198206 /// # Panics
199207 ///
200208 /// Panics if the number of parameters provided does not match the number expected.
@@ -212,8 +220,52 @@ impl Client {
212220 . await
213221 }
214222
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+
215260 /// The maximally flexible version of [`query`].
216261 ///
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+ ///
217269 /// # Panics
218270 ///
219271 /// Panics if the number of parameters provided does not match the number expected.
@@ -231,6 +283,13 @@ impl Client {
231283
232284 /// Executes a statement, returning the number of rows modified.
233285 ///
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+ ///
234293 /// If the statement does not modify any rows (e.g. `SELECT`), 0 is returned.
235294 ///
236295 /// # Panics
@@ -249,6 +308,13 @@ impl Client {
249308
250309 /// The maximally flexible version of [`execute`].
251310 ///
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+ ///
252318 /// # Panics
253319 ///
254320 /// Panics if the number of parameters provided does not match the number expected.
0 commit comments