Add -[transactionWithBlock:]
FossilOrigin-Name: 1ecc15c9b801a9531314936f644a43a863fc7811768c7c257b409adad349d6e6
This commit is contained in:
parent
f16cfb5059
commit
dd1d737f0f
2 changed files with 24 additions and 0 deletions
|
@ -44,6 +44,9 @@ OF_ASSUME_NONNULL_BEGIN
|
||||||
flags: (int)flags OF_DESIGNATED_INITIALIZER;
|
flags: (int)flags OF_DESIGNATED_INITIALIZER;
|
||||||
- (SL3PreparedStatement *)prepareStatement: (OFConstantString *)SQL;
|
- (SL3PreparedStatement *)prepareStatement: (OFConstantString *)SQL;
|
||||||
- (void)executeStatement: (OFConstantString *)SQL;
|
- (void)executeStatement: (OFConstantString *)SQL;
|
||||||
|
#ifdef OF_HAVE_BLOCKS
|
||||||
|
- (void)transactionWithBlock: (bool (^)(void))block;
|
||||||
|
#endif
|
||||||
@end
|
@end
|
||||||
|
|
||||||
OF_ASSUME_NONNULL_END
|
OF_ASSUME_NONNULL_END
|
||||||
|
|
|
@ -90,4 +90,25 @@
|
||||||
exceptionWithConnection: self
|
exceptionWithConnection: self
|
||||||
errorCode: code];
|
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
|
@end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue