@@ -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 ;
@@ -84,7 +84,11 @@ + (LPDatabase *)sharedDatabase
8484 */
8585+ (NSString *)sqliteFilePath
8686{
87+ #if LP_NOT_TV
8788 return [[LPFileManager documentsDirectory ] stringByAppendingPathComponent: LEANPLUM_SQLITE_NAME];
89+ #else
90+ return [[LPFileManager cachesDirectory ] stringByAppendingPathComponent: LEANPLUM_SQLITE_NAME];
91+ #endif
8892}
8993
9094/* *
@@ -190,40 +194,45 @@ - (NSArray *)rowsFromQuery:(NSString *)query bindObjects:(NSArray *)objectsToBin
190194 }
191195
192196 @synchronized (self) {
193- NSMutableArray *rows = [NSMutableArray new ];
194- sqlite3_stmt *statement = [self sqliteStatementFromQuery: query
195- bindObjects: objectsToBind];
196- if (!statement) {
197- return @[];
198- }
199-
200- // Iterate through rows.
201- while (sqlite3_step (statement) == SQLITE_ROW) {
202- // Get column data as dictionary where column name is the key
203- // and value will be a blob or a string. This is a safe conversion.
204- // Details: http://www.sqlite.org/c3ref/column_blob.html
205- NSMutableDictionary *columnData = [NSMutableDictionary new ];
206- int columnsCount = sqlite3_column_count (statement);
207- for (int i=0 ; i<columnsCount; i++){
208- char *columnKeyUTF8 = (char *)sqlite3_column_name (statement, i);
209- NSString *columnKey = [NSString stringWithUTF8String: columnKeyUTF8];
210-
211- if (sqlite3_column_type (statement, i) == SQLITE_BLOB) {
212- NSData *columnBytes = [[NSData alloc ] initWithBytes: sqlite3_column_blob (statement, i)
213- length: sqlite3_column_bytes (statement, i)];
214- columnData[columnKey] = [NSKeyedUnarchiver unarchiveObjectWithData: columnBytes];
215- } else {
216- char *columnValueUTF8 = (char *)sqlite3_column_text (statement, i);
217- if (columnValueUTF8) {
218- NSString *columnValue = [NSString stringWithUTF8String: columnValueUTF8];
219- 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+ }
220226 }
221227 }
228+ [rows addObject: columnData];
222229 }
223- [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,
224235 }
225- sqlite3_finalize (statement);
226- return rows;
227236 }
228237 return @[];
229238}
0 commit comments