mirror of
				https://github.com/isar/rusqlite.git
				synced 2025-10-31 22:08:55 +08:00 
			
		
		
		
	Add test and fix for nested savepoint rollbacks.
This requires giving savepoints distinct names.
This commit is contained in:
		| @@ -1,3 +1,4 @@ | |||||||
|  | use std::borrow::Cow; | ||||||
| use std::ops::Deref; | use std::ops::Deref; | ||||||
| use {Result, Connection}; | use {Result, Connection}; | ||||||
|  |  | ||||||
| @@ -91,10 +92,11 @@ impl<'conn> Transaction<'conn> { | |||||||
|     /// } |     /// } | ||||||
|     /// ``` |     /// ``` | ||||||
|     pub fn savepoint(&mut self) -> Result<Transaction> { |     pub fn savepoint(&mut self) -> Result<Transaction> { | ||||||
|         self.conn.execute_batch("SAVEPOINT sp").map(|_| { |         let new_depth = self.depth + 1; | ||||||
|  |         self.conn.execute_batch(&format!("SAVEPOINT sp{}", new_depth)).map(|_| { | ||||||
|             Transaction { |             Transaction { | ||||||
|                 conn: self.conn, |                 conn: self.conn, | ||||||
|                 depth: self.depth + 1, |                 depth: new_depth, | ||||||
|                 commit: false, |                 commit: false, | ||||||
|                 finished: false, |                 finished: false, | ||||||
|             } |             } | ||||||
| @@ -128,11 +130,12 @@ impl<'conn> Transaction<'conn> { | |||||||
|  |  | ||||||
|     fn commit_(&mut self) -> Result<()> { |     fn commit_(&mut self) -> Result<()> { | ||||||
|         self.finished = true; |         self.finished = true; | ||||||
|         self.conn.execute_batch(if self.depth == 0 { |         let sql = if self.depth == 0 { | ||||||
|             "COMMIT" |             Cow::Borrowed("COMMIT") | ||||||
|         } else { |         } else { | ||||||
|             "RELEASE sp" |             Cow::Owned(format!("RELEASE sp{}", self.depth)) | ||||||
|         }) |         }; | ||||||
|  |         self.conn.execute_batch(&sql) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// A convenience method which consumes and rolls back a transaction. |     /// A convenience method which consumes and rolls back a transaction. | ||||||
| @@ -142,11 +145,12 @@ impl<'conn> Transaction<'conn> { | |||||||
|  |  | ||||||
|     fn rollback_(&mut self) -> Result<()> { |     fn rollback_(&mut self) -> Result<()> { | ||||||
|         self.finished = true; |         self.finished = true; | ||||||
|         self.conn.execute_batch(if self.depth == 0 { |         let sql = if self.depth == 0 { | ||||||
|             "ROLLBACK" |             Cow::Borrowed("ROLLBACK") | ||||||
|         } else { |         } else { | ||||||
|             "ROLLBACK TO sp" |             Cow::Owned(format!("ROLLBACK TO sp{}", self.depth)) | ||||||
|         }) |         }; | ||||||
|  |         self.conn.execute_batch(&sql) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Consumes the transaction, committing or rolling back according to the current setting |     /// Consumes the transaction, committing or rolling back according to the current setting | ||||||
| @@ -235,29 +239,40 @@ mod test { | |||||||
|  |  | ||||||
|     #[test] |     #[test] | ||||||
|     fn test_savepoint() { |     fn test_savepoint() { | ||||||
|  |         fn assert_current_sum(x: i32, conn: &Connection) { | ||||||
|  |             let i = conn.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0)).unwrap(); | ||||||
|  |             assert_eq!(x, i); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         let mut db = checked_memory_handle(); |         let mut db = checked_memory_handle(); | ||||||
|         { |         { | ||||||
|             let mut tx = db.transaction().unwrap(); |             let mut tx = db.transaction().unwrap(); | ||||||
|             tx.execute_batch("INSERT INTO foo VALUES(1)").unwrap(); |             tx.execute_batch("INSERT INTO foo VALUES(1)").unwrap(); | ||||||
|  |             assert_current_sum(1, &tx); | ||||||
|             tx.set_commit(); |             tx.set_commit(); | ||||||
|             { |             { | ||||||
|                 let mut sp1 = tx.savepoint().unwrap(); |                 let mut sp1 = tx.savepoint().unwrap(); | ||||||
|                 sp1.execute_batch("INSERT INTO foo VALUES(2)").unwrap(); |                 sp1.execute_batch("INSERT INTO foo VALUES(2)").unwrap(); | ||||||
|                 sp1.set_commit(); |                 assert_current_sum(3, &sp1); | ||||||
|  |                 // will rollback sp1 | ||||||
|                 { |                 { | ||||||
|                     let mut sp2 = sp1.savepoint().unwrap(); |                     let mut sp2 = sp1.savepoint().unwrap(); | ||||||
|                     sp2.execute_batch("INSERT INTO foo VALUES(4)").unwrap(); |                     sp2.execute_batch("INSERT INTO foo VALUES(4)").unwrap(); | ||||||
|  |                     assert_current_sum(7, &sp2); | ||||||
|                     // will rollback sp2 |                     // will rollback sp2 | ||||||
|                     { |                     { | ||||||
|                         let sp3 = sp2.savepoint().unwrap(); |                         let sp3 = sp2.savepoint().unwrap(); | ||||||
|                         sp3.execute_batch("INSERT INTO foo VALUES(8)").unwrap(); |                         sp3.execute_batch("INSERT INTO foo VALUES(8)").unwrap(); | ||||||
|  |                         assert_current_sum(15, &sp3); | ||||||
|                         sp3.commit().unwrap(); |                         sp3.commit().unwrap(); | ||||||
|                         // committed sp3, but will be erased by sp2 rollback |                         // committed sp3, but will be erased by sp2 rollback | ||||||
|                     } |                     } | ||||||
|  |                     assert_current_sum(15, &sp2); | ||||||
|                 } |                 } | ||||||
|  |                 assert_current_sum(3, &sp1); | ||||||
|             } |             } | ||||||
|  |             assert_current_sum(1, &tx); | ||||||
|         } |         } | ||||||
|         assert_eq!(3i32, |         assert_current_sum(1, &db); | ||||||
|                    db.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0)).unwrap()); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user