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

Commit9200080

Browse files
committed
refactoring
1 parent4fc3e95 commit9200080

File tree

6 files changed

+199
-201
lines changed

6 files changed

+199
-201
lines changed

‎postgres/src/client.rs‎

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,49 @@ impl Client {
275275
.block_on(self.client.query_typed(query, params))
276276
}
277277

278+
/// Like `query_one`, but requires the types of query parameters to be explicitly specified.
279+
///
280+
/// Compared to `query_one`, this method allows performing queries without three round trips (for
281+
/// prepare, execute, and close) by requiring the caller to specify parameter values along with
282+
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't
283+
/// supported (such as Cloudflare Workers with Hyperdrive).
284+
///
285+
/// Executes a statement which returns a single row, returning it.
286+
///
287+
/// Returns an error if the query does not return exactly one row.
288+
///
289+
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
290+
/// provided, 1-indexed.
291+
pubfnquery_typed_one(
292+
&mutself,
293+
query:&str,
294+
params:&[(&(dynToSql +Sync),Type)],
295+
) ->Result<Row,Error>{
296+
self.connection
297+
.block_on(self.client.query_typed_one(query, params))
298+
}
299+
300+
/// Like `query_opt`, but requires the types of query parameters to be explicitly specified.
301+
///
302+
/// Compared to `query_opt`, this method allows performing queries without three round trips (for
303+
/// prepare, execute, and close) by requiring the caller to specify parameter values along with
304+
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't
305+
/// supported (such as Cloudflare Workers with Hyperdrive).
306+
/// Executes a statement which returns zero or one rows, returning it.
307+
///
308+
/// Returns an error if the query returns more than one row.
309+
///
310+
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
311+
/// provided, 1-indexed.
312+
pubfnquery_typed_opt(
313+
&mutself,
314+
query:&str,
315+
params:&[(&(dynToSql +Sync),Type)],
316+
) ->Result<Option<Row>,Error>{
317+
self.connection
318+
.block_on(self.client.query_typed_opt(query, params))
319+
}
320+
278321
/// The maximally flexible version of [`query_typed`].
279322
///
280323
/// Compared to `query`, this method allows performing queries without three round trips (for
@@ -621,47 +664,4 @@ impl Client {
621664
}
622665
})
623666
}
624-
625-
/// Like `query_one`, but requires the types of query parameters to be explicitly specified.
626-
///
627-
/// Compared to `query_one`, this method allows performing queries without three round trips (for
628-
/// prepare, execute, and close) by requiring the caller to specify parameter values along with
629-
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't
630-
/// supported (such as Cloudflare Workers with Hyperdrive).
631-
///
632-
/// Executes a statement which returns a single row, returning it.
633-
///
634-
/// Returns an error if the query does not return exactly one row.
635-
///
636-
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
637-
/// provided, 1-indexed.
638-
pubfnquery_typed_one(
639-
&mutself,
640-
query:&str,
641-
params:&[(&(dynToSql +Sync),Type)],
642-
) ->Result<Row,Error>{
643-
self.connection
644-
.block_on(self.client.query_typed_one(query, params))
645-
}
646-
647-
/// Like `query_opt`, but requires the types of query parameters to be explicitly specified.
648-
///
649-
/// Compared to `query_opt`, this method allows performing queries without three round trips (for
650-
/// prepare, execute, and close) by requiring the caller to specify parameter values along with
651-
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't
652-
/// supported (such as Cloudflare Workers with Hyperdrive).
653-
/// Executes a statement which returns zero or one rows, returning it.
654-
///
655-
/// Returns an error if the query returns more than one row.
656-
///
657-
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
658-
/// provided, 1-indexed.
659-
pubfnquery_typed_opt(
660-
&mutself,
661-
query:&str,
662-
params:&[(&(dynToSql +Sync),Type)],
663-
) ->Result<Option<Row>,Error>{
664-
self.connection
665-
.block_on(self.client.query_typed_opt(query, params))
666-
}
667667
}

‎postgres/src/generic_client.rs‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ pub trait GenericClient: private::Sealed {
5151
params:&[(&(dynToSql +Sync),Type)],
5252
) ->Result<Vec<Row>,Error>;
5353

54-
/// Like [`Client::query_typed_raw`]
55-
fnquery_typed_raw<P,I>(&mutself,statement:&str,params:I) ->Result<RowIter<'_>,Error>
56-
where
57-
P:BorrowToSql,
58-
I:IntoIterator<Item =(P,Type)> +Sync +Send;
59-
6054
/// Like `Client::query_typed_one`.
6155
fnquery_typed_one(
6256
&mutself,
@@ -71,6 +65,12 @@ pub trait GenericClient: private::Sealed {
7165
params:&[(&(dynToSql +Sync),Type)],
7266
) ->Result<Option<Row>,Error>;
7367

68+
/// Like [`Client::query_typed_raw`]
69+
fnquery_typed_raw<P,I>(&mutself,statement:&str,params:I) ->Result<RowIter<'_>,Error>
70+
where
71+
P:BorrowToSql,
72+
I:IntoIterator<Item =(P,Type)> +Sync +Send;
73+
7474
/// Like `Client::prepare`.
7575
fnprepare(&mutself,query:&str) ->Result<Statement,Error>;
7676

@@ -150,6 +150,22 @@ impl GenericClient for Client {
150150
self.query_typed(statement, params)
151151
}
152152

153+
fnquery_typed_one(
154+
&mutself,
155+
query:&str,
156+
params:&[(&(dynToSql +Sync),Type)],
157+
) ->Result<Row,Error>{
158+
self.query_typed_one(query, params)
159+
}
160+
161+
fnquery_typed_opt(
162+
&mutself,
163+
query:&str,
164+
params:&[(&(dynToSql +Sync),Type)],
165+
) ->Result<Option<Row>,Error>{
166+
self.query_typed_opt(query, params)
167+
}
168+
153169
fnquery_typed_raw<P,I>(&mutself,statement:&str,params:I) ->Result<RowIter<'_>,Error>
154170
where
155171
P:BorrowToSql,
@@ -191,24 +207,6 @@ impl GenericClient for Client {
191207
fntransaction(&mutself) ->Result<Transaction<'_>,Error>{
192208
self.transaction()
193209
}
194-
195-
fnquery_typed_one(
196-
&mutself,
197-
query:&str,
198-
199-
params:&[(&(dynToSql +Sync),Type)],
200-
) ->Result<Row,Error>{
201-
self.query_typed_one(query, params)
202-
}
203-
204-
fnquery_typed_opt(
205-
&mutself,
206-
query:&str,
207-
208-
params:&[(&(dynToSql +Sync),Type)],
209-
) ->Result<Option<Row>,Error>{
210-
self.query_typed_opt(query, params)
211-
}
212210
}
213211

214212
impl private::SealedforTransaction<'_>{}

‎postgres/src/transaction.rs‎

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,34 @@ impl<'a> Transaction<'a> {
129129
)
130130
}
131131

132+
/// Like `Client::query_typed_one`.
133+
pubfnquery_typed_one(
134+
&mutself,
135+
query:&str,
136+
params:&[(&(dynToSql +Sync),Type)],
137+
) ->Result<Row,Error>{
138+
self.connection.block_on(
139+
self.transaction
140+
.as_ref()
141+
.unwrap()
142+
.query_typed_one(query, params),
143+
)
144+
}
145+
146+
/// Like `Client::query_typed_opt`.
147+
pubfnquery_typed_opt(
148+
&mutself,
149+
query:&str,
150+
params:&[(&(dynToSql +Sync),Type)],
151+
) ->Result<Option<Row>,Error>{
152+
self.connection.block_on(
153+
self.transaction
154+
.as_ref()
155+
.unwrap()
156+
.query_typed_opt(query, params),
157+
)
158+
}
159+
132160
/// Like `Client::query_typed_raw`.
133161
pubfnquery_typed_raw<P,I>(&mutself,query:&str,params:I) ->Result<RowIter<'_>,Error>
134162
where
@@ -247,32 +275,4 @@ impl<'a> Transaction<'a> {
247275
.block_on(self.transaction.as_mut().unwrap().savepoint(name))?;
248276
Ok(Transaction::new(self.connection.as_ref(), transaction))
249277
}
250-
251-
/// Like `Client::query_typed_one`.
252-
pubfnquery_typed_one(
253-
&mutself,
254-
query:&str,
255-
params:&[(&(dynToSql +Sync),Type)],
256-
) ->Result<Row,Error>{
257-
self.connection.block_on(
258-
self.transaction
259-
.as_ref()
260-
.unwrap()
261-
.query_typed_one(query, params),
262-
)
263-
}
264-
265-
/// Like `Client::query_typed_opt`.
266-
pubfnquery_typed_opt(
267-
&mutself,
268-
query:&str,
269-
params:&[(&(dynToSql +Sync),Type)],
270-
) ->Result<Option<Row>,Error>{
271-
self.connection.block_on(
272-
self.transaction
273-
.as_ref()
274-
.unwrap()
275-
.query_typed_opt(query, params),
276-
)
277-
}
278278
}

‎tokio-postgres/src/client.rs‎

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
use bytes::{Buf,BytesMut};
2020
use fallible_iterator::FallibleIterator;
2121
use futures_channel::mpsc;
22-
use futures_util::{pin_mut,StreamExt,TryStreamExt};
22+
use futures_util::{StreamExt,TryStreamExt};
2323
use parking_lot::Mutex;
2424
use postgres_protocol::message::backend::Message;
2525
use postgres_protocol::message::frontend;
@@ -323,70 +323,6 @@ impl Client {
323323
Ok(first)
324324
}
325325

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

391+
/// Like `query_one`, but requires the types of query parameters to be explicitly specified.
392+
///
393+
/// Compared to `query_one`, this method allows performing queries without three round trips (for
394+
/// prepare, execute, and close) by requiring the caller to specify parameter values along with
395+
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't
396+
/// supported (such as Cloudflare Workers with Hyperdrive).
397+
///
398+
/// Executes a statement which returns a single row, returning it.
399+
///
400+
/// Returns an error if the query does not return exactly one row.
401+
///
402+
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
403+
/// provided, 1-indexed.
404+
///
405+
pubasyncfnquery_typed_one(
406+
&self,
407+
statement:&str,
408+
params:&[(&(dynToSql +Sync),Type)],
409+
) ->Result<Row,Error>{
410+
self.query_typed_opt(statement, params)
411+
.await
412+
.and_then(|res| res.ok_or_else(Error::row_count))
413+
}
414+
415+
/// Like `query_one`, but requires the types of query parameters to be explicitly specified.
416+
///
417+
/// Compared to `query_one`, this method allows performing queries without three round trips (for
418+
/// prepare, execute, and close) by requiring the caller to specify parameter values along with
419+
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't
420+
/// supported (such as Cloudflare Workers with Hyperdrive).
421+
///
422+
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the
423+
/// parameter of the list provided, 1-indexed.
424+
/// Executes a statements which returns zero or one rows, returning it.
425+
///
426+
/// Returns an error if the query returns more than one row.
427+
pubasyncfnquery_typed_opt(
428+
&self,
429+
statement:&str,
430+
params:&[(&(dynToSql +Sync),Type)],
431+
) ->Result<Option<Row>,Error>{
432+
letmut stream =pin!(
433+
self.query_typed_raw(statement, params.iter().map(|(v, t)|(*v, t.clone())))
434+
.await?
435+
);
436+
437+
letmut first =None;
438+
439+
// Originally this was two calls to `try_next().await?`,
440+
// once for the first element, and second to error if more than one.
441+
//
442+
// However, this new form with only one .await in a loop generates
443+
// slightly smaller codegen/stack usage for the resulting future.
444+
whileletSome(row) = stream.try_next().await?{
445+
if first.is_some(){
446+
returnErr(Error::row_count());
447+
}
448+
449+
first =Some(row);
450+
}
451+
452+
Ok(first)
453+
}
454+
455455
/// The maximally flexible version of [`query_typed`].
456456
///
457457
/// Compared to `query`, this method allows performing queries without three round trips (for

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp