Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8f4d22b

Browse files
committed
add other query_typed helper functions
1 parente1cd6be commit8f4d22b

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

‎tokio-postgres/src/client.rs‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,70 @@ impl Client {
321321
Ok(first)
322322
}
323323

324+
/// Like `query_one`, but requires the types of query parameters to be explicitly specified.
325+
///
326+
/// Compared to `query_one`, this method allows performing queries without three round trips (for
327+
/// prepare, execute, and close) by requiring the caller to specify parameter values along with
328+
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't
329+
/// supported (such as Cloudflare Workers with Hyperdrive).
330+
///
331+
/// Executes a statement which returns a single row, returning it.
332+
///
333+
/// Returns an error if the query does not return exactly one row.
334+
///
335+
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
336+
/// provided, 1-indexed.
337+
///
338+
pubasyncfnquery_typed_one(
339+
&self,
340+
statement:&str,
341+
params:&[(&(dynToSql +Sync),Type)],
342+
) ->Result<Row,Error>{
343+
self.query_typed_opt(statement, params)
344+
.await
345+
.and_then(|res| res.ok_or_else(Error::row_count))
346+
}
347+
348+
/// Like `query_one`, but requires the types of query parameters to be explicitly specified.
349+
///
350+
/// Compared to `query_one`, this method allows performing queries without three round trips (for
351+
/// prepare, execute, and close) by requiring the caller to specify parameter values along with
352+
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't
353+
/// supported (such as Cloudflare Workers with Hyperdrive).
354+
///
355+
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the
356+
/// parameter of the list provided, 1-indexed.
357+
/// Executes a statements which returns zero or one rows, returning it.
358+
///
359+
/// Returns an error if the query returns more than one row.
360+
pubasyncfnquery_typed_opt(
361+
&self,
362+
statement:&str,
363+
params:&[(&(dynToSql +Sync),Type)],
364+
) ->Result<Option<Row>,Error>{
365+
let stream =self
366+
.query_typed_raw(statement, params.iter().map(|(v, t)|(*v, t.clone())))
367+
.await?;
368+
pin_mut!(stream);
369+
370+
letmut first =None;
371+
372+
// Originally this was two calls to `try_next().await?`,
373+
// once for the first element, and second to error if more than one.
374+
//
375+
// However, this new form with only one .await in a loop generates
376+
// slightly smaller codegen/stack usage for the resulting future.
377+
whileletSome(row) = stream.try_next().await?{
378+
if first.is_some(){
379+
returnErr(Error::row_count());
380+
}
381+
382+
first =Some(row);
383+
}
384+
385+
Ok(first)
386+
}
387+
324388
/// The maximally flexible version of [`query`].
325389
///
326390
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list

‎tokio-postgres/src/generic_client.rs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ pub trait GenericClient: private::Sealed {
6969
P:BorrowToSql,
7070
I:IntoIterator<Item =(P,Type)> +Sync +Send;
7171

72+
/// Like [`Client::query_one_typed`].
73+
asyncfnquery_typed_one(
74+
&self,
75+
statement:&str,
76+
params:&[(&(dynToSql +Sync),Type)],
77+
) ->Result<Row,Error>;
78+
79+
/// Like [`Client::query_opt_typed`].
80+
asyncfnquery_typed_opt(
81+
&self,
82+
statement:&str,
83+
params:&[(&(dynToSql +Sync),Type)],
84+
) ->Result<Option<Row>,Error>;
85+
7286
/// Like [`Client::prepare`].
7387
asyncfnprepare(&self,query:&str) ->Result<Statement,Error>;
7488

@@ -195,6 +209,23 @@ impl GenericClient for Client {
195209
fnclient(&self) ->&Client{
196210
self
197211
}
212+
213+
asyncfnquery_typed_one(
214+
&self,
215+
statement:&str,
216+
params:&[(&(dynToSql +Sync),Type)],
217+
) ->Result<Row,Error>{
218+
self.query_typed_one(statement, params).await
219+
}
220+
221+
/// Like [`Client::query_opt_typed`].
222+
asyncfnquery_typed_opt(
223+
&self,
224+
statement:&str,
225+
params:&[(&(dynToSql +Sync),Type)],
226+
) ->Result<Option<Row>,Error>{
227+
self.query_typed_opt(statement, params).await
228+
}
198229
}
199230

200231
impl private::SealedforTransaction<'_>{}
@@ -302,4 +333,21 @@ impl GenericClient for Transaction<'_> {
302333
fnclient(&self) ->&Client{
303334
self.client()
304335
}
336+
337+
asyncfnquery_typed_one(
338+
&self,
339+
statement:&str,
340+
params:&[(&(dynToSql +Sync),Type)],
341+
) ->Result<Row,Error>{
342+
self.query_typed_one(statement, params).await
343+
}
344+
345+
/// Like [`Client::query_opt_typed`].
346+
asyncfnquery_typed_opt(
347+
&self,
348+
statement:&str,
349+
params:&[(&(dynToSql +Sync),Type)],
350+
) ->Result<Option<Row>,Error>{
351+
self.query_typed_opt(statement, params).await
352+
}
305353
}

‎tokio-postgres/src/transaction.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,22 @@ impl<'a> Transaction<'a> {
329329
pubfnclient(&self) ->&Client{
330330
self.client
331331
}
332+
333+
/// Like `Client::query_typed_one`.
334+
pubasyncfnquery_typed_one(
335+
&self,
336+
statement:&str,
337+
params:&[(&(dynToSql +Sync),Type)],
338+
) ->Result<Row,Error>{
339+
self.client.query_typed_one(statement, params).await
340+
}
341+
342+
/// Like `Client::query_typed_opt`.
343+
pubasyncfnquery_typed_opt(
344+
&self,
345+
statement:&str,
346+
params:&[(&(dynToSql +Sync),Type)],
347+
) ->Result<Option<Row>,Error>{
348+
self.client.query_typed_opt(statement, params).await
349+
}
332350
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp