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

Commitc6d7728

Browse files
committed
add the same methods to sync version and add tests
1 parent8f4d22b commitc6d7728

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

‎postgres/src/client.rs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,4 +621,47 @@ impl Client {
621621
}
622622
})
623623
}
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+
}
624667
}

‎postgres/src/generic_client.rs‎

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

60+
/// Like `Client::query_typed_one`.
61+
fnquery_typed_one(
62+
&mutself,
63+
query:&str,
64+
params:&[(&(dynToSql +Sync),Type)],
65+
) ->Result<Row,Error>;
66+
67+
/// Like `Client::query_typed_opt`.
68+
fnquery_typed_opt(
69+
&mutself,
70+
query:&str,
71+
params:&[(&(dynToSql +Sync),Type)],
72+
) ->Result<Option<Row>,Error>;
73+
6074
/// Like `Client::prepare`.
6175
fnprepare(&mutself,query:&str) ->Result<Statement,Error>;
6276

@@ -177,6 +191,24 @@ impl GenericClient for Client {
177191
fntransaction(&mutself) ->Result<Transaction<'_>,Error>{
178192
self.transaction()
179193
}
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+
}
180212
}
181213

182214
impl private::SealedforTransaction<'_>{}
@@ -273,4 +305,20 @@ impl GenericClient for Transaction<'_> {
273305
fntransaction(&mutself) ->Result<Transaction<'_>,Error>{
274306
self.transaction()
275307
}
308+
309+
fnquery_typed_one(
310+
&mutself,
311+
query:&str,
312+
params:&[(&(dynToSql +Sync),Type)],
313+
) ->Result<Row,Error>{
314+
self.query_typed_one(query, params)
315+
}
316+
317+
fnquery_typed_opt(
318+
&mutself,
319+
query:&str,
320+
params:&[(&(dynToSql +Sync),Type)],
321+
) ->Result<Option<Row>,Error>{
322+
self.query_typed_opt(query, params)
323+
}
276324
}

‎postgres/src/transaction.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,32 @@ impl<'a> Transaction<'a> {
247247
.block_on(self.transaction.as_mut().unwrap().savepoint(name))?;
248248
Ok(Transaction::new(self.connection.as_ref(), transaction))
249249
}
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+
}
250278
}

‎tokio-postgres/tests/test/main.rs‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,38 @@ async fn query_one() {
901901
.unwrap();
902902
}
903903

904+
#[tokio::test]
905+
asyncfnquery_typed_one(){
906+
let client =connect("user=postgres").await;
907+
908+
client
909+
.batch_execute(
910+
"
911+
CREATE TEMPORARY TABLE foo (
912+
name TEXT
913+
);
914+
INSERT INTO foo (name) VALUES ('alice'), ('bob'), ('carol');
915+
",
916+
)
917+
.await
918+
.unwrap();
919+
920+
client
921+
.query_typed_one("SELECT * FROM foo WHERE name = 'dave'",&[])
922+
.await
923+
.err()
924+
.unwrap();
925+
client
926+
.query_typed_one("SELECT * FROM foo WHERE name = 'alice'",&[])
927+
.await
928+
.unwrap();
929+
client
930+
.query_typed_one("SELECT * FROM foo",&[])
931+
.await
932+
.err()
933+
.unwrap();
934+
}
935+
904936
#[tokio::test]
905937
asyncfnquery_opt(){
906938
let client =connect("user=postgres").await;
@@ -933,6 +965,38 @@ async fn query_opt() {
933965
.err()
934966
.unwrap();
935967
}
968+
#[tokio::test]
969+
asyncfnquery_typed_opt(){
970+
let client =connect("user=postgres").await;
971+
972+
client
973+
.batch_execute(
974+
"
975+
CREATE TEMPORARY TABLE foo (
976+
name TEXT
977+
);
978+
INSERT INTO foo (name) VALUES ('alice'), ('bob'), ('carol');
979+
",
980+
)
981+
.await
982+
.unwrap();
983+
984+
assert!(client
985+
.query_typed_opt("SELECT * FROM foo WHERE name = 'dave'",&[])
986+
.await
987+
.unwrap()
988+
.is_none());
989+
client
990+
.query_typed_opt("SELECT * FROM foo WHERE name = 'alice'",&[])
991+
.await
992+
.unwrap()
993+
.unwrap();
994+
client
995+
.query_typed_one("SELECT * FROM foo",&[])
996+
.await
997+
.err()
998+
.unwrap();
999+
}
9361000

9371001
#[tokio::test]
9381002
asyncfndeferred_constraint(){

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp