Changeset View
Changeset View
Standalone View
Standalone View
native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp
#include "SQLiteQueryExecutor.h" | #include "SQLiteQueryExecutor.h" | ||||
#include "CommSecureStore.h" | #include "CommSecureStore.h" | ||||
#include "Logger.h" | #include "Logger.h" | ||||
#include "sqlite_orm.h" | #include "sqlite_orm.h" | ||||
#include "entities/Metadata.h" | #include "entities/Metadata.h" | ||||
#include <fstream> | #include <fstream> | ||||
#include <iostream> | #include <iostream> | ||||
#include <thread> | #include <thread> | ||||
#ifdef __ANDROID__ | |||||
#include <fbjni/fbjni.h> | |||||
#endif | |||||
#define ACCOUNT_ID 1 | #define ACCOUNT_ID 1 | ||||
namespace comm { | namespace comm { | ||||
using namespace sqlite_orm; | using namespace sqlite_orm; | ||||
std::string SQLiteQueryExecutor::sqliteFilePath; | std::string SQLiteQueryExecutor::sqliteFilePath; | ||||
std::string SQLiteQueryExecutor::encryptionKey; | std::string SQLiteQueryExecutor::encryptionKey; | ||||
▲ Show 20 Lines • Show All 473 Lines • ▼ Show 20 Lines | if (use_encryption_key) { | ||||
set_encryption_key(db); | set_encryption_key(db); | ||||
} | } | ||||
sqlite3_exec( | sqlite3_exec( | ||||
db, "SELECT COUNT(*) FROM sqlite_master;", nullptr, nullptr, &err_msg); | db, "SELECT COUNT(*) FROM sqlite_master;", nullptr, nullptr, &err_msg); | ||||
sqlite3_close(db); | sqlite3_close(db); | ||||
return !err_msg; | return !err_msg; | ||||
} | } | ||||
void run_with_native_accessible(std::function<void()> &&task) { | |||||
// Some methods of SQLiteQueryExecutor are meant to be executed on | |||||
// auxiliary threads. In case they require access to native Java | |||||
// API we need to temporarily attach the thread to JVM | |||||
// This function attaches thread to JVM for the time | |||||
// lambda passed to this function will be executing. | |||||
#ifdef __ANDROID__ | |||||
facebook::jni::ThreadScope::WithClassLoader(std::move(task)); | |||||
#else | |||||
task(); | |||||
#endif | |||||
} | |||||
void validate_encryption() { | void validate_encryption() { | ||||
std::string temp_encrypted_db_path = | std::string temp_encrypted_db_path = | ||||
SQLiteQueryExecutor::sqliteFilePath + "_temp_encrypted"; | SQLiteQueryExecutor::sqliteFilePath + "_temp_encrypted"; | ||||
bool temp_encrypted_exists = file_exists(temp_encrypted_db_path); | bool temp_encrypted_exists = file_exists(temp_encrypted_db_path); | ||||
bool default_location_exists = | bool default_location_exists = | ||||
file_exists(SQLiteQueryExecutor::sqliteFilePath); | file_exists(SQLiteQueryExecutor::sqliteFilePath); | ||||
▲ Show 20 Lines • Show All 583 Lines • ▼ Show 20 Lines | |||||
void SQLiteQueryExecutor::clearSensitiveData() { | void SQLiteQueryExecutor::clearSensitiveData() { | ||||
if (file_exists(SQLiteQueryExecutor::sqliteFilePath) && | if (file_exists(SQLiteQueryExecutor::sqliteFilePath) && | ||||
std::remove(SQLiteQueryExecutor::sqliteFilePath.c_str())) { | std::remove(SQLiteQueryExecutor::sqliteFilePath.c_str())) { | ||||
std::ostringstream errorStream; | std::ostringstream errorStream; | ||||
errorStream << "Failed to delete database file. Details: " | errorStream << "Failed to delete database file. Details: " | ||||
<< strerror(errno); | << strerror(errno); | ||||
throw std::system_error(errno, std::generic_category(), errorStream.str()); | throw std::system_error(errno, std::generic_category(), errorStream.str()); | ||||
} | } | ||||
auto native_dependent_task = []() { | |||||
SQLiteQueryExecutor::assign_encryption_key(); | SQLiteQueryExecutor::assign_encryption_key(); | ||||
}; | |||||
run_with_native_accessible(native_dependent_task); | |||||
SQLiteQueryExecutor::migrate(); | SQLiteQueryExecutor::migrate(); | ||||
} | } | ||||
} // namespace comm | } // namespace comm |