Skip to content

Commit a680a52

Browse files
authored
Merge pull request #132 from Leanplum/feature/sqlite
refactor(sqlite): add possible failure handling around sqlite multithreading
2 parents 2893e72 + 6782561 commit a680a52

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

Leanplum-SDK/Classes/LPDatabase.m

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ - (id)init
5050
- (sqlite3 *)initSQLite
5151
{
5252
const char *sqliteFilePath = [[LPDatabase sqliteFilePath] UTF8String];
53-
int result = sqlite3_open(sqliteFilePath, &sqlite);
53+
int result = sqlite3_open_v2(sqliteFilePath, &sqlite, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL);
5454
if (result != SQLITE_OK) {
5555
[self handleSQLiteError:@"SQLite fail to open" errorResult:result query:nil];
5656
return nil;
@@ -194,40 +194,45 @@ - (NSArray *)rowsFromQuery:(NSString *)query bindObjects:(NSArray *)objectsToBin
194194
}
195195

196196
@synchronized (self) {
197-
NSMutableArray *rows = [NSMutableArray new];
198-
sqlite3_stmt *statement = [self sqliteStatementFromQuery:query
199-
bindObjects:objectsToBind];
200-
if (!statement) {
201-
return @[];
202-
}
203-
204-
// Iterate through rows.
205-
while (sqlite3_step(statement) == SQLITE_ROW) {
206-
// Get column data as dictionary where column name is the key
207-
// and value will be a blob or a string. This is a safe conversion.
208-
// Details: http://www.sqlite.org/c3ref/column_blob.html
209-
NSMutableDictionary *columnData = [NSMutableDictionary new];
210-
int columnsCount = sqlite3_column_count(statement);
211-
for (int i=0; i<columnsCount; i++){
212-
char *columnKeyUTF8 = (char *)sqlite3_column_name(statement, i);
213-
NSString *columnKey = [NSString stringWithUTF8String:columnKeyUTF8];
214-
215-
if (sqlite3_column_type(statement, i) == SQLITE_BLOB) {
216-
NSData *columnBytes = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, i)
217-
length:sqlite3_column_bytes(statement, i)];
218-
columnData[columnKey] = [NSKeyedUnarchiver unarchiveObjectWithData:columnBytes];
219-
} else {
220-
char *columnValueUTF8 = (char *)sqlite3_column_text(statement, i);
221-
if (columnValueUTF8) {
222-
NSString *columnValue = [NSString stringWithUTF8String:columnValueUTF8];
223-
columnData[columnKey] = columnValue;
197+
@try {
198+
NSMutableArray *rows = [NSMutableArray new];
199+
sqlite3_stmt *statement = [self sqliteStatementFromQuery:query
200+
bindObjects:objectsToBind];
201+
if (!statement) {
202+
return @[];
203+
}
204+
205+
// Iterate through rows.
206+
while (sqlite3_step(statement) == SQLITE_ROW) {
207+
// Get column data as dictionary where column name is the key
208+
// and value will be a blob or a string. This is a safe conversion.
209+
// Details: http://www.sqlite.org/c3ref/column_blob.html
210+
NSMutableDictionary *columnData = [NSMutableDictionary new];
211+
int columnsCount = sqlite3_column_count(statement);
212+
for (int i=0; i<columnsCount; i++){
213+
char *columnKeyUTF8 = (char *)sqlite3_column_name(statement, i);
214+
NSString *columnKey = [NSString stringWithUTF8String:columnKeyUTF8];
215+
216+
if (sqlite3_column_type(statement, i) == SQLITE_BLOB) {
217+
NSData *columnBytes = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, i)
218+
length:sqlite3_column_bytes(statement, i)];
219+
columnData[columnKey] = [NSKeyedUnarchiver unarchiveObjectWithData:columnBytes];
220+
} else {
221+
char *columnValueUTF8 = (char *)sqlite3_column_text(statement, i);
222+
if (columnValueUTF8) {
223+
NSString *columnValue = [NSString stringWithUTF8String:columnValueUTF8];
224+
columnData[columnKey] = columnValue;
225+
}
224226
}
225227
}
228+
[rows addObject:columnData];
226229
}
227-
[rows addObject:columnData];
230+
sqlite3_finalize(statement);
231+
return rows;
232+
} @catch (NSException *e) {
233+
LPLog(LPError, @"SQLite operation failed.");
234+
// TODO: Make sure to catch this when new logging is in place,
228235
}
229-
sqlite3_finalize(statement);
230-
return rows;
231236
}
232237
return @[];
233238
}

0 commit comments

Comments
 (0)