@@ -6,7 +6,7 @@ use tokio::net::TcpStream;
66use tokio_postgres:: error:: SqlState ;
77use tokio_postgres:: tls:: { NoTls , NoTlsStream } ;
88use tokio_postgres:: types:: { Kind , Type } ;
9- use tokio_postgres:: { Client , Config , Connection , Error } ;
9+ use tokio_postgres:: { Client , Config , Connection , Error , SimpleQueryMessage } ;
1010
1111mod parse;
1212#[ cfg( feature = "runtime" ) ]
@@ -114,12 +114,10 @@ async fn pipelined_prepare() {
114114async fn insert_select ( ) {
115115 let mut client = connect ( "user=postgres" ) . await ;
116116
117- let setup = client
118- . prepare ( "CREATE TEMPORARY TABLE foo (id SERIAL, name TEXT)" )
117+ client
118+ . batch_execute ( "CREATE TEMPORARY TABLE foo (id SERIAL, name TEXT)" )
119119 . await
120120 . unwrap ( ) ;
121- client. execute ( & setup, & [ ] ) . await . unwrap ( ) ;
122- drop ( setup) ;
123121
124122 let insert = client. prepare ( "INSERT INTO foo (name) VALUES ($1), ($2)" ) ;
125123 let select = client. prepare ( "SELECT id, name FROM foo ORDER BY id" ) ;
@@ -142,8 +140,8 @@ async fn insert_select() {
142140async fn custom_enum ( ) {
143141 let mut client = connect ( "user=postgres" ) . await ;
144142
145- let create = client
146- . prepare (
143+ client
144+ . batch_execute (
147145 "CREATE TYPE pg_temp.mood AS ENUM (
148146 'sad',
149147 'ok',
@@ -152,7 +150,6 @@ async fn custom_enum() {
152150 )
153151 . await
154152 . unwrap ( ) ;
155- client. execute ( & create, & [ ] ) . await . unwrap ( ) ;
156153
157154 let select = client. prepare ( "SELECT $1::mood" ) . await . unwrap ( ) ;
158155
@@ -172,11 +169,10 @@ async fn custom_enum() {
172169async fn custom_domain ( ) {
173170 let mut client = connect ( "user=postgres" ) . await ;
174171
175- let create = client
176- . prepare ( "CREATE DOMAIN pg_temp.session_id AS bytea CHECK(octet_length(VALUE) = 16)" )
172+ client
173+ . batch_execute ( "CREATE DOMAIN pg_temp.session_id AS bytea CHECK(octet_length(VALUE) = 16)" )
177174 . await
178175 . unwrap ( ) ;
179- client. execute ( & create, & [ ] ) . await . unwrap ( ) ;
180176
181177 let select = client. prepare ( "SELECT $1::session_id" ) . await . unwrap ( ) ;
182178
@@ -206,17 +202,16 @@ async fn custom_array() {
206202async fn custom_composite ( ) {
207203 let mut client = connect ( "user=postgres" ) . await ;
208204
209- let create = client
210- . prepare (
205+ client
206+ . batch_execute (
211207 "CREATE TYPE pg_temp.inventory_item AS (
212- name TEXT,
213- supplier INTEGER,
214- price NUMERIC
215- )" ,
208+ name TEXT,
209+ supplier INTEGER,
210+ price NUMERIC
211+ )" ,
216212 )
217213 . await
218214 . unwrap ( ) ;
219- client. execute ( & create, & [ ] ) . await . unwrap ( ) ;
220215
221216 let select = client. prepare ( "SELECT $1::inventory_item" ) . await . unwrap ( ) ;
222217
@@ -239,16 +234,15 @@ async fn custom_composite() {
239234async fn custom_range ( ) {
240235 let mut client = connect ( "user=postgres" ) . await ;
241236
242- let create = client
243- . prepare (
237+ client
238+ . batch_execute (
244239 "CREATE TYPE pg_temp.floatrange AS RANGE (
245- subtype = float8,
246- subtype_diff = float8mi
247- )" ,
240+ subtype = float8,
241+ subtype_diff = float8mi
242+ )" ,
248243 )
249244 . await
250245 . unwrap ( ) ;
251- client. execute ( & create, & [ ] ) . await . unwrap ( ) ;
252246
253247 let select = client. prepare ( "SELECT $1::floatrange" ) . await . unwrap ( ) ;
254248
@@ -257,6 +251,52 @@ async fn custom_range() {
257251 assert_eq ! ( & Kind :: Range ( Type :: FLOAT8 ) , ty. kind( ) ) ;
258252}
259253
254+ #[ tokio:: test]
255+ async fn simple_query ( ) {
256+ let mut client = connect ( "user=postgres" ) . await ;
257+
258+ let messages = client
259+ . simple_query (
260+ "CREATE TEMPORARY TABLE foo (
261+ id SERIAL,
262+ name TEXT
263+ );
264+ INSERT INTO foo (name) VALUES ('steven'), ('joe');
265+ SELECT * FROM foo ORDER BY id;" ,
266+ )
267+ . try_collect :: < Vec < _ > > ( )
268+ . await
269+ . unwrap ( ) ;
270+
271+ match messages[ 0 ] {
272+ SimpleQueryMessage :: CommandComplete ( 0 ) => { }
273+ _ => panic ! ( "unexpected message" ) ,
274+ }
275+ match messages[ 1 ] {
276+ SimpleQueryMessage :: CommandComplete ( 2 ) => { }
277+ _ => panic ! ( "unexpected message" ) ,
278+ }
279+ match & messages[ 2 ] {
280+ SimpleQueryMessage :: Row ( row) => {
281+ assert_eq ! ( row. get( 0 ) , Some ( "1" ) ) ;
282+ assert_eq ! ( row. get( 1 ) , Some ( "steven" ) ) ;
283+ }
284+ _ => panic ! ( "unexpected message" ) ,
285+ }
286+ match & messages[ 3 ] {
287+ SimpleQueryMessage :: Row ( row) => {
288+ assert_eq ! ( row. get( 0 ) , Some ( "2" ) ) ;
289+ assert_eq ! ( row. get( 1 ) , Some ( "joe" ) ) ;
290+ }
291+ _ => panic ! ( "unexpected message" ) ,
292+ }
293+ match messages[ 4 ] {
294+ SimpleQueryMessage :: CommandComplete ( 2 ) => { }
295+ _ => panic ! ( "unexpected message" ) ,
296+ }
297+ assert_eq ! ( messages. len( ) , 5 ) ;
298+ }
299+
260300/*
261301#[test]
262302fn query_portal() {
@@ -675,56 +715,6 @@ fn transaction_builder_around_moved_client() {
675715 runtime.run().unwrap();
676716}
677717
678- #[test]
679- fn simple_query() {
680- let _ = env_logger::try_init();
681- let mut runtime = Runtime::new().unwrap();
682-
683- let (mut client, connection) = runtime.block_on(connect("user=postgres")).unwrap();
684- let connection = connection.map_err(|e| panic!("{}", e));
685- runtime.handle().spawn(connection).unwrap();
686-
687- let f = client
688- .simple_query(
689- "CREATE TEMPORARY TABLE foo (
690- id SERIAL,
691- name TEXT
692- );
693- INSERT INTO foo (name) VALUES ('steven'), ('joe');
694- SELECT * FROM foo ORDER BY id;",
695- )
696- .collect();
697- let messages = runtime.block_on(f).unwrap();
698-
699- match messages[0] {
700- SimpleQueryMessage::CommandComplete(0) => {}
701- _ => panic!("unexpected message"),
702- }
703- match messages[1] {
704- SimpleQueryMessage::CommandComplete(2) => {}
705- _ => panic!("unexpected message"),
706- }
707- match &messages[2] {
708- SimpleQueryMessage::Row(row) => {
709- assert_eq!(row.get(0), Some("1"));
710- assert_eq!(row.get(1), Some("steven"));
711- }
712- _ => panic!("unexpected message"),
713- }
714- match &messages[3] {
715- SimpleQueryMessage::Row(row) => {
716- assert_eq!(row.get(0), Some("2"));
717- assert_eq!(row.get(1), Some("joe"));
718- }
719- _ => panic!("unexpected message"),
720- }
721- match messages[4] {
722- SimpleQueryMessage::CommandComplete(2) => {}
723- _ => panic!("unexpected message"),
724- }
725- assert_eq!(messages.len(), 5);
726- }
727-
728718#[test]
729719fn poll_idle_running() {
730720 struct DelayStream(Delay);
0 commit comments