@@ -41,9 +41,15 @@ std::string get_db_path(std::string const &db_name,
4141 return location + " /" + db_name;
4242}
4343
44+ #ifdef OP_SQLITE_USE_SQLCIPHER
4445BridgeResult opsqlite_open (std::string const &dbName,
45- std::string const &lastPath) {
46- std::string dbPath = get_db_path (dbName, lastPath);
46+ std::string const &last_path,
47+ std::string const &encryptionKey) {
48+ #else
49+ BridgeResult opsqlite_open (std::string const &dbName,
50+ std::string const &last_path) {
51+ #endif
52+ std::string dbPath = get_db_path (dbName, last_path);
4753
4854 int sqlOpenFlags =
4955 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
@@ -57,7 +63,12 @@ BridgeResult opsqlite_open(std::string const &dbName,
5763 }
5864
5965 dbMap[dbName] = db;
60-
66+ #ifdef OP_SQLITE_USE_SQLCIPHER
67+ auto encryptionResult =
68+ opsqlite_execute (dbName, " PRAGMA key = '" + encryptionKey + " '" , nullptr ,
69+ nullptr , nullptr );
70+ LOGD (" Encrypting database" );
71+ #endif
6172 return BridgeResult{.type = SQLiteOk, .affectedRows = 0 };
6273}
6374
@@ -321,7 +332,7 @@ sqlite3_stmt *opsqlite_prepare_statement(std::string const &dbName,
321332
322333 if (statementStatus == SQLITE_ERROR) {
323334 const char *message = sqlite3_errmsg (db);
324- throw std::runtime_error (" [op-sqlite] SQL statement error: " +
335+ throw std::runtime_error (" [op-sqlite] SQL prepare statement error: " +
325336 std::string (message));
326337 }
327338
@@ -359,15 +370,15 @@ opsqlite_execute(std::string const &dbName, std::string const &query,
359370 const char *message = sqlite3_errmsg (db);
360371 return {
361372 .type = SQLiteError,
362- .message = " [op-sqlite] SQL statement error: " +
363- std::to_string (statementStatus) +
364- " description: " + std::string (message) +
373+ .message = " [op-sqlite] SQL statement error on opsqlite_execute: \n " +
374+ std::to_string (statementStatus) + " description: \n " +
375+ std::string (message) +
365376 " . See error codes: https://www.sqlite.org/rescode.html" ,
366377 };
367378 }
368379
369- // The statement did not fail to parse but there is nothing to do, just skip
370- // to the end
380+ // The statement did not fail to parse but there is nothing to do, just
381+ // skip to the end
371382 if (statement == NULL ) {
372383 continue ;
373384 }
@@ -502,8 +513,8 @@ opsqlite_execute(std::string const &dbName, std::string const &query,
502513 .insertId = static_cast <double >(latestInsertRowId)};
503514}
504515
505- // / Executes returning data in raw arrays, a small performance optimization for
506- // / certain use cases
516+ // / Executes returning data in raw arrays, a small performance optimization
517+ // / for certain use cases
507518BridgeResult
508519opsqlite_execute_raw (std::string const &dbName, std::string const &query,
509520 const std::vector<JSVariant> *params,
@@ -540,8 +551,8 @@ opsqlite_execute_raw(std::string const &dbName, std::string const &query,
540551 };
541552 }
542553
543- // The statement did not fail to parse but there is nothing to do, just skip
544- // to the end
554+ // The statement did not fail to parse but there is nothing to do, just
555+ // skip to the end
545556 if (statement == NULL ) {
546557 continue ;
547558 }
@@ -661,8 +672,8 @@ opsqlite_execute_raw(std::string const &dbName, std::string const &query,
661672
662673void opsqlite_close_all () {
663674 for (auto const &x : dbMap) {
664- // Interrupt will make all pending operations to fail with SQLITE_INTERRUPT
665- // The ongoing work from threads will then fail ASAP
675+ // Interrupt will make all pending operations to fail with
676+ // SQLITE_INTERRUPT The ongoing work from threads will then fail ASAP
666677 sqlite3_interrupt (x.second );
667678 // Each DB connection can then be safely interrupted
668679 sqlite3_close_v2 (x.second );
0 commit comments