@@ -41,7 +41,9 @@ use std::{
4141 iter,
4242 num:: NonZeroUsize ,
4343 ptr:: null_mut,
44- str, thread,
44+ str,
45+ sync:: Arc ,
46+ thread,
4547 time:: { Duration , Instant } ,
4648} ;
4749
@@ -571,6 +573,37 @@ fn into_cursor(profile: &Profile) {
571573 assert_eq ! ( expected, actual) ;
572574}
573575
576+ /// We want to be able to create multiple statements with the utilizing the same connection, yet we
577+ /// in some cases it might be hard to keep track of the ownership of the connection separately. So
578+ /// essentially we want our statements to be owning an `Arc` to connection.
579+ #[ test_case( MSSQL ; "Microsoft SQL Server" ) ]
580+ #[ test_case( MARIADB ; "Maria DB" ) ]
581+ #[ test_case( SQLITE_3 ; "SQLite 3" ) ]
582+ #[ test_case( POSTGRES ; "PostgreSQL" ) ]
583+ fn shared_ownership_of_connections_by_statement ( profile : & Profile ) {
584+ // Given
585+ let table_name = table_name ! ( ) ;
586+ let ( conn, table) = Given :: new ( & table_name)
587+ . column_types ( & [ "INT" ] )
588+ . values_by_column ( & [ & [ Some ( "42" ) ] ] )
589+ . build ( profile)
590+ . unwrap ( ) ;
591+
592+ // When
593+ let conn = Arc :: new ( conn) ;
594+ let cursor = conn
595+ . clone ( )
596+ . execute_arc ( & table. sql_all_ordered_by_id ( ) , ( ) , None )
597+ . unwrap ( )
598+ . unwrap ( ) ;
599+ // We can drop the connection, even though, the cursor still exists.
600+ drop ( conn) ;
601+
602+ // Then
603+ let expected = "42" ;
604+ assert_eq ! ( expected, cursor_to_string( cursor) ) ;
605+ }
606+
574607/// Strong exception safety for `into_cursor`. Our first query will fail, because it will query a
575608/// non-existing table, but our second one using the same connection will succeed. This is one
576609/// scenario in which it is useful not to "swallow" the connection in case of an error.
@@ -1483,20 +1516,20 @@ fn var_char_slice_mut_as_input_output_parameter(profile: &Profile) {
14831516 let conn = profile. connection ( ) . unwrap ( ) ;
14841517 conn. execute (
14851518 r#"
1486- IF EXISTS (SELECT name FROM sysobjects WHERE name = 'TestInOutText')
1487- DROP PROCEDURE TestInOutText
1519+ IF EXISTS (SELECT name FROM sysobjects WHERE name = 'TestInOutText')
1520+ DROP PROCEDURE TestInOutText
14881521 "# ,
14891522 ( ) ,
14901523 None ,
14911524 )
14921525 . unwrap ( ) ;
14931526
14941527 conn. execute (
1495- r#"CREATE PROCEDURE TestInOutText
1496- @OutParm VARCHAR(15) OUTPUT
1528+ r#"CREATE PROCEDURE TestInOutText
1529+ @OutParm VARCHAR(15) OUTPUT
14971530 AS
1498- SELECT @OutParm = 'Hello, World!'
1499- RETURN 99
1531+ SELECT @OutParm = 'Hello, World!'
1532+ RETURN 99
15001533 "# ,
15011534 ( ) ,
15021535 None ,
@@ -2822,20 +2855,20 @@ fn output_parameter(profile: &Profile) {
28222855 let conn = profile. connection ( ) . unwrap ( ) ;
28232856 conn. execute (
28242857 r#"
2825- IF EXISTS (SELECT name FROM sysobjects WHERE name = 'TestOutputParam')
2826- DROP PROCEDURE TestOutputParam
2858+ IF EXISTS (SELECT name FROM sysobjects WHERE name = 'TestOutputParam')
2859+ DROP PROCEDURE TestOutputParam
28272860 "# ,
28282861 ( ) ,
28292862 None ,
28302863 )
28312864 . unwrap ( ) ;
28322865
28332866 conn. execute (
2834- r#"CREATE PROCEDURE TestOutputParam
2835- @OutParm int OUTPUT
2867+ r#"CREATE PROCEDURE TestOutputParam
2868+ @OutParm int OUTPUT
28362869 AS
2837- SELECT @OutParm = @OutParm + 5
2838- RETURN 99
2870+ SELECT @OutParm = @OutParm + 5
2871+ RETURN 99
28392872 "# ,
28402873 ( ) ,
28412874 None ,
0 commit comments