Add -[transactionWithBlock:]

FossilOrigin-Name: 1ecc15c9b801a9531314936f644a43a863fc7811768c7c257b409adad349d6e6
This commit is contained in:
Jonathan Schleifer 2020-10-04 01:28:44 +00:00
parent f16cfb5059
commit dd1d737f0f
2 changed files with 24 additions and 0 deletions

View file

@ -90,4 +90,25 @@
exceptionWithConnection: self
errorCode: code];
}
#ifdef OF_HAVE_BLOCKS
- (void)transactionWithBlock: (bool (^)(void))block
{
bool commit;
[self executeStatement: @"BEGIN TRANSACTION"];
@try {
commit = block();
} @catch (id e) {
[self executeStatement: @"ROLLBACK TRANSACTION"];
@throw e;
}
if (commit)
[self executeStatement: @"COMMIT TRANSACTION"];
else
[self executeStatement: @"ROLLBACK TRANSACTION"];
}
#endif
@end