@@ -1507,4 +1507,47 @@ mod test {
15071507
15081508 Ok ( ( ) )
15091509 }
1510+
1511+ #[ test]
1512+ fn test_multiple_memory_databases ( ) -> Result < ( ) > {
1513+ // Unnamed :memory: connections are isolated
1514+ {
1515+ let mem1 = Connection :: open_in_memory ( ) ?;
1516+ let mem2 = Connection :: open_in_memory ( ) ?;
1517+
1518+ mem1. execute_batch ( "CREATE TABLE test (id INTEGER)" ) ?;
1519+ mem1. execute ( "INSERT INTO test VALUES (1)" , [ ] ) ?;
1520+
1521+ mem2. execute_batch ( "CREATE TABLE test (id INTEGER)" ) ?;
1522+ mem2. execute ( "INSERT INTO test VALUES (2)" , [ ] ) ?;
1523+
1524+ let value1: i32 = mem1. query_row ( "SELECT id FROM test" , [ ] , |r| r. get ( 0 ) ) ?;
1525+ assert_eq ! ( value1, 1 ) ;
1526+
1527+ let value2: i32 = mem2. query_row ( "SELECT id FROM test" , [ ] , |r| r. get ( 0 ) ) ?;
1528+ assert_eq ! ( value2, 2 ) ;
1529+ }
1530+
1531+ // try_clone() shares the same database
1532+ {
1533+ let shared = Connection :: open_in_memory ( ) ?;
1534+
1535+ shared. execute_batch ( "CREATE TABLE shared_table (id INTEGER)" ) ?;
1536+ shared. execute ( "INSERT INTO shared_table VALUES (123)" , [ ] ) ?;
1537+
1538+ let cloned = shared. try_clone ( ) ?;
1539+
1540+ // Cloned connection can see the original's tables
1541+ let value: i32 = cloned. query_row ( "SELECT id FROM shared_table" , [ ] , |r| r. get ( 0 ) ) ?;
1542+ assert_eq ! ( value, 123 ) ;
1543+
1544+ cloned. execute ( "INSERT INTO shared_table VALUES (456)" , [ ] ) ?;
1545+
1546+ // Original connection can see cloned's insert
1547+ let count: i64 = shared. query_row ( "SELECT COUNT(*) FROM shared_table" , [ ] , |r| r. get ( 0 ) ) ?;
1548+ assert_eq ! ( count, 2 ) ;
1549+ }
1550+
1551+ Ok ( ( ) )
1552+ }
15101553}
0 commit comments