Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use inDatabase: in FMDatabaseQueue. Respect crashOnErrors for open #649

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/fmdb/FMDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ - (BOOL)open {

int err = sqlite3_open([self sqlitePath], (sqlite3**)&_db );
if(err != SQLITE_OK) {
if (_crashOnErrors) {
NSAssert(false, @"The FMDatabase could not be opened (err: %d) at path: %s", err, [self sqlitePath]);
}
NSLog(@"error opening!: %d", err);
return NO;
}
Expand Down Expand Up @@ -209,6 +212,9 @@ - (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName {

int err = sqlite3_open_v2([self sqlitePath], (sqlite3**)&_db, flags, [vfsName UTF8String]);
if(err != SQLITE_OK) {
if (_crashOnErrors) {
NSAssert(false, @"The FMDatabase could not be opened (err: %d) at path: %s", err, [self sqlitePath]);
}
NSLog(@"error opening!: %d", err);
return NO;
}
Expand Down
47 changes: 25 additions & 22 deletions src/fmdb/FMDatabaseQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ - (void)dealloc {

- (void)close {
FMDBRetain(self);
dispatch_sync(_queue, ^() {
[self->_db close];
[self inDatabase:^(FMDatabase * _Nonnull db) {
[db close];
FMDBRelease(_db);
self->_db = 0x00;
});
}];
FMDBRelease(self);
}

Expand Down Expand Up @@ -188,8 +188,12 @@ - (void)inDatabase:(void (^)(FMDatabase *db))block {
FMDBRetain(self);

dispatch_sync(_queue, ^() {

FMDatabase *db = [self database];
if (!db) {
NSLog(@"Error: attempt to use closed database");
return;
}
FMDBRetain(db);

block(db);

Expand All @@ -204,38 +208,38 @@ - (void)inDatabase:(void (^)(FMDatabase *db))block {
}
#endif
}
FMDBRelease(db);
});

FMDBRelease(self);
}

- (void)beginTransaction:(FMDBTransaction)transaction withBlock:(void (^)(FMDatabase *db, BOOL *rollback))block {
FMDBRetain(self);
dispatch_sync(_queue, ^() {

[self inDatabase:^(FMDatabase * _Nonnull db) {
BOOL shouldRollback = NO;

switch (transaction) {
case FMDBTransactionExclusive:
[[self database] beginTransaction];
[db beginTransaction];
break;
case FMDBTransactionDeferred:
[[self database] beginDeferredTransaction];
[db beginDeferredTransaction];
break;
case FMDBTransactionImmediate:
[[self database] beginImmediateTransaction];
[db beginImmediateTransaction];
break;
}

block([self database], &shouldRollback);
block(db, &shouldRollback);

if (shouldRollback) {
[[self database] rollback];
[db rollback];
}
else {
[[self database] commit];
[db commit];
}
});
}];

FMDBRelease(self);
}
Expand All @@ -261,24 +265,23 @@ - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {
static unsigned long savePointIdx = 0;
__block NSError *err = 0x00;
FMDBRetain(self);
dispatch_sync(_queue, ^() {
[self inDatabase:^(FMDatabase * _Nonnull db) {

NSString *name = [NSString stringWithFormat:@"savePoint%ld", savePointIdx++];

BOOL shouldRollback = NO;

if ([[self database] startSavePointWithName:name error:&err]) {
if ([db startSavePointWithName:name error:&err]) {

block([self database], &shouldRollback);
block(db, &shouldRollback);

if (shouldRollback) {
// We need to rollback and release this savepoint to remove it
[[self database] rollbackToSavePointWithName:name error:&err];
}
[[self database] releaseSavePointWithName:name error:&err];

[db releaseSavePointWithName:name error:&err];
}
});
}];
FMDBRelease(self);
return err;
#else
Expand All @@ -304,9 +307,9 @@ - (BOOL)checkpoint:(FMDBCheckpointMode)mode name:(NSString *)name logFrameCount:
__block NSError *blockError;

FMDBRetain(self);
dispatch_sync(_queue, ^() {
result = [self.database checkpoint:mode name:name logFrameCount:NULL checkpointCount:NULL error:&blockError];
});
[self inDatabase:^(FMDatabase * _Nonnull db) {
result = [db checkpoint:mode name:name logFrameCount:NULL checkpointCount:NULL error:&blockError];
}];
FMDBRelease(self);

if (error) {
Expand Down