Index: src/SL3Connection.h ================================================================== --- src/SL3Connection.h +++ src/SL3Connection.h @@ -42,8 +42,11 @@ - (instancetype)initWithPath: (OFString *)path; - (instancetype)initWithPath: (OFString *)path flags: (int)flags OF_DESIGNATED_INITIALIZER; - (SL3PreparedStatement *)prepareStatement: (OFConstantString *)SQL; - (void)executeStatement: (OFConstantString *)SQL; +#ifdef OF_HAVE_BLOCKS +- (void)transactionWithBlock: (bool (^)(void))block; +#endif @end OF_ASSUME_NONNULL_END Index: src/SL3Connection.m ================================================================== --- src/SL3Connection.m +++ src/SL3Connection.m @@ -88,6 +88,27 @@ if (code != SQLITE_OK) @throw [SL3ExecuteStatementFailedException 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