@@ -208,105 +208,108 @@ async fn it_encodes_decodes_json() -> anyhow::Result<()> {
208208 . await ?;
209209
210210 assert_eq ! ( result. 0 , test_data) ;
211- #[ sqlx_macros:: test]
212- async fn it_can_query_by_string_args( ) -> sqlx:: Result < ( ) > {
213- install_default_drivers ( ) ;
211+ #[ sqlx_macros:: test]
212+ async fn it_can_query_by_string_args ( ) -> sqlx:: Result < ( ) > {
213+ install_default_drivers ( ) ;
214214
215- let mut conn = new :: < Any > ( ) . await ?;
215+ let mut conn = new :: < Any > ( ) . await ?;
216216
217- let string = "Hello, world!" . to_string ( ) ;
218- let ref tuple = ( "Hello, world!" . to_string ( ) , ) ;
217+ let string = "Hello, world!" . to_string ( ) ;
218+ let ref tuple = ( "Hello, world!" . to_string ( ) , ) ;
219219
220- #[ cfg( feature = "postgres" ) ]
221- const SQL : & str =
220+ #[ cfg( feature = "postgres" ) ]
221+ const SQL : & str =
222222 "SELECT 'Hello, world!' as string where 'Hello, world!' in ($1, $2, $3, $4, $5, $6, $7)" ;
223223
224- #[ cfg( not( feature = "postgres" ) ) ]
225- const SQL : & str =
226- "SELECT 'Hello, world!' as string where 'Hello, world!' in (?, ?, ?, ?, ?, ?, ?)" ;
224+ #[ cfg( not( feature = "postgres" ) ) ]
225+ const SQL : & str =
226+ "SELECT 'Hello, world!' as string where 'Hello, world!' in (?, ?, ?, ?, ?, ?, ?)" ;
227227
228- {
229- let query = sqlx:: query ( SQL )
230- // validate flexibility of lifetimes
231- . bind ( & string)
232- . bind ( & string[ ..] )
233- . bind ( Some ( & string) )
234- . bind ( Some ( & string[ ..] ) )
235- . bind ( & Option :: < String > :: None )
236- . bind ( & string. clone ( ) )
237- . bind ( & tuple. 0 ) ; // should not get "temporary value is freed at the end of this statement" here
228+ {
229+ let query = sqlx:: query ( SQL )
230+ // validate flexibility of lifetimes
231+ . bind ( & string)
232+ . bind ( & string[ ..] )
233+ . bind ( Some ( & string) )
234+ . bind ( Some ( & string[ ..] ) )
235+ . bind ( & Option :: < String > :: None )
236+ . bind ( & string. clone ( ) )
237+ . bind ( & tuple. 0 ) ; // should not get "temporary value is freed at the end of this statement" here
238238
239- let result = query. fetch_one ( & mut conn) . await ?;
239+ let result = query. fetch_one ( & mut conn) . await ?;
240240
241- let column_0: String = result. try_get ( 0 ) ?;
241+ let column_0: String = result. try_get ( 0 ) ?;
242242
243- assert_eq ! ( column_0, string) ;
244- }
243+ assert_eq ! ( column_0, string) ;
244+ }
245245
246- {
247- let mut query = sqlx:: query ( SQL ) ;
246+ {
247+ let mut query = sqlx:: query ( SQL ) ;
248248
249- let query = || -> Result < _ , BoxDynError > {
250- // validate flexibility of lifetimes
251- query. try_bind ( & string) ?;
252- query. try_bind ( & string[ ..] ) ?;
253- query. try_bind ( Some ( & string) ) ?;
254- query. try_bind ( Some ( & string[ ..] ) ) ?;
255- query. try_bind ( & Option :: < String > :: None ) ?;
256- query. try_bind ( & string. clone ( ) ) ?;
257- query. try_bind ( & tuple. 0 ) ?;
249+ let query = || -> Result < _ , BoxDynError > {
250+ // validate flexibility of lifetimes
251+ query. try_bind ( & string) ?;
252+ query. try_bind ( & string[ ..] ) ?;
253+ query. try_bind ( Some ( & string) ) ?;
254+ query. try_bind ( Some ( & string[ ..] ) ) ?;
255+ query. try_bind ( & Option :: < String > :: None ) ?;
256+ query. try_bind ( & string. clone ( ) ) ?;
257+ query. try_bind ( & tuple. 0 ) ?;
258258
259- Ok ( query)
260- } ( )
261- . map_err ( Error :: Encode ) ?;
259+ Ok ( query)
260+ } ( )
261+ . map_err ( Error :: Encode ) ?;
262262
263- let result = query. fetch_one ( & mut conn) . await ?;
263+ let result = query. fetch_one ( & mut conn) . await ?;
264264
265- let column_0: String = result. try_get ( 0 ) ?;
265+ let column_0: String = result. try_get ( 0 ) ?;
266266
267- assert_eq ! ( column_0, string) ;
267+ assert_eq ! ( column_0, string) ;
268+ }
268269 }
269- #[ cfg( feature = "json" ) ]
270- #[ sqlx_macros:: test]
271- async fn it_encodes_decodes_json ( ) -> anyhow:: Result < ( ) > {
272- sqlx:: any:: install_default_drivers ( ) ;
273270
274- // Create new connection
275- let mut conn = new :: < Any > ( ) . await ?;
271+ #[ cfg( feature = "json" ) ]
272+ #[ sqlx_macros:: test]
273+ async fn it_encodes_decodes_json ( ) -> anyhow:: Result < ( ) > {
274+ sqlx:: any:: install_default_drivers ( ) ;
275+
276+ // Create new connection
277+ let mut conn = new :: < Any > ( ) . await ?;
278+
279+ // Test with serde_json::Value
280+ let json_value = serde_json:: json!( {
281+ "name" : "test" ,
282+ "value" : 42 ,
283+ "items" : [ 1 , 2 , 3 ]
284+ } ) ;
285+
286+ // This will work by encoding JSON as text and decoding it back
287+ let result: serde_json:: Value = sqlx:: query_scalar ( "SELECT ?" )
288+ . bind ( Json ( & json_value) )
289+ . fetch_one ( & mut conn)
290+ . await ?;
291+
292+ assert_eq ! ( result, json_value) ;
293+
294+ // Test with custom struct
295+ #[ derive( Serialize , Deserialize , Debug , PartialEq ) ]
296+ struct TestData {
297+ name : String ,
298+ count : i32 ,
299+ }
276300
277- // Test with serde_json::Value
278- let json_value = serde_json:: json!( {
279- "name" : "test" ,
280- "value" : 42 ,
281- "items" : [ 1 , 2 , 3 ]
282- } ) ;
301+ let test_data = TestData {
302+ name : "example" . to_string ( ) ,
303+ count : 100 ,
304+ } ;
283305
284- // This will work by encoding JSON as text and decoding it back
285- let result: serde_json:: Value = sqlx:: query_scalar ( "SELECT ?" )
286- . bind ( Json ( & json_value) )
287- . fetch_one ( & mut conn)
288- . await ?;
306+ let result: Json < TestData > = sqlx:: query_scalar ( "SELECT ?" )
307+ . bind ( Json ( & test_data) )
308+ . fetch_one ( & mut conn)
309+ . await ?;
289310
290- assert_eq ! ( result, json_value ) ;
311+ assert_eq ! ( result. 0 , test_data ) ;
291312
292- // Test with custom struct
293- #[ derive( Serialize , Deserialize , Debug , PartialEq ) ]
294- struct TestData {
295- name : String ,
296- count : i32 ,
313+ Ok ( ( ) )
297314 }
298-
299- let test_data = TestData {
300- name : "example" . to_string ( ) ,
301- count : 100 ,
302- } ;
303-
304- let result: Json < TestData > = sqlx:: query_scalar ( "SELECT ?" )
305- . bind ( Json ( & test_data) )
306- . fetch_one ( & mut conn)
307- . await ?;
308-
309- assert_eq ! ( result. 0 , test_data) ;
310-
311- Ok ( ( ) )
312315}
0 commit comments