Skip to content

Commit 9b42077

Browse files
committed
Correct the check for ~ character in the db file location string. Introduce #define for SQLCIPHER.
1 parent a2ab6c7 commit 9b42077

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/ios/SQLite.m

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ -(id) getDBPath:(NSString *)dbFile at:(NSString *)atkey {
172172
targetBundleDirPath = [targetBundleDirPath stringByAppendingPathComponent: @"www"];
173173
assetFilePath = [targetBundleDirPath stringByAppendingPathComponent: dbfilename];
174174
RCTLog(@"Built path to pre-populated DB asset from app bundle www subdirectory: %@",assetFilePath);
175-
} else if ([assetFilePath characterAtIndex:0 == '~']) {
175+
} else if ([assetFilePath characterAtIndex:0] == '~') {
176176
assetFilePath = [assetFilePath substringFromIndex:1];
177177
NSString *targetBundleDirPath = [[NSBundle mainBundle] resourcePath];
178178
assetFilePath = [targetBundleDirPath stringByAppendingPathComponent: assetFilePath];
@@ -215,21 +215,28 @@ -(id) getDBPath:(NSString *)dbFile at:(NSString *)atkey {
215215
return;
216216
} else {
217217
sqlite3_create_function(db, "regexp", 2, SQLITE_ANY, NULL, &sqlite_regexp, NULL, NULL);
218-
219-
// for SQLCipher version:
220-
// NSString *dbkey = options[@"key"];
221-
// const char *key = NULL;
222-
// if (dbkey != NULL) key = [dbkey UTF8String];
223-
// if (key != NULL) sqlite3_key(db, key, strlen(key));
224-
218+
const char *key = NULL;
219+
220+
#ifdef SQLCIPHER
221+
NSString *dbkey = options[@"key"];
222+
if (dbkey != NULL) {
223+
key = [dbkey UTF8String];
224+
if (key != NULL) {
225+
sqlite3_key(db, key, strlen(key));
226+
}
227+
}
228+
#endif
225229
// Attempt to read the SQLite master table [to support SQLCipher version]:
226230
if(sqlite3_exec(db, (const char*)"SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
227231
NSValue *dbPointer = [NSValue valueWithPointer:db];
228232
openDBs[dbfilename] = @{ @"dbPointer": dbPointer, @"dbPath" : dbname};
229-
pluginResult = [SQLiteResult resultWithStatus:SQLiteStatus_OK messageAsString:@"Database opened"];
233+
NSString *msg = (key != NULL) ? @"Secure database opened" : @"Database opened";
234+
pluginResult = [SQLiteResult resultWithStatus:SQLiteStatus_OK messageAsString: msg];
235+
RCTLog(msg);
230236
} else {
231-
pluginResult = [SQLiteResult resultWithStatus:SQLiteStatus_ERROR messageAsString:@"Unable to open DB with key"];
232-
RCTLog(@"Unable to open db with key");
237+
NSString *msg = [NSString stringWithFormat:@"Unable to open %@", (key != NULL) ? @"secure database with key" : @"database"];
238+
pluginResult = [SQLiteResult resultWithStatus:SQLiteStatus_ERROR messageAsString:msg];
239+
RCTLog(msg);
233240
sqlite3_close (db);
234241
[openDBs removeObjectForKey:dbfilename];
235242
}

0 commit comments

Comments
 (0)