Baoulettes

database.cpp

Dec 4th, 2021
1,002
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "database.h"
  2. #include <string>
  3. #include <general/sqlite3.h>
  4. #include <nlohmann/json.hpp>
  5. #include <ios_dokkan/instances.h>
  6. #include <parallel_hashmap/phmap.h>
  7. using json = nlohmann::json;
  8. using namespace phmap;
  9. sqlite3 *DatabaseInstance;
  10. sqlite3 *dokkansql_open() {
  11.   //sqlite3 *DatabaseInstance;
  12.   NSString *DocPathNSString = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  13.     std::string DocumentFolder = std::string([DocPathNSString UTF8String]);
  14.     std::string AssetsFolder = DocumentFolder+"/assets";
  15.     std::string DatabasePath_en = AssetsFolder+"/sqlite/current/en/database.db";
  16.     std::string DatabasePath_fr = AssetsFolder+"/sqlite/current/fr/database.db";
  17.     std::string DatabasePassword = "9bf9c6ed9d537c399a6c4513e92ab24717e1a488381e3338593abd923fc8a13b";
  18.   iD_LOGI("SQL - dokkansql_open databaseManager");
  19.   const char* database = DatabasePath_fr.c_str();
  20.   const char* key = DatabasePassword.c_str();
  21.   int rc;
  22.   iD_LOGI("SQL - dokkansql_open %s length:%i",key,(int)strlen(key));
  23.   rc = sqlite3_open_v2(database, &DatabaseInstance, SQLITE_OPEN_READWRITE, NULL);
  24.   if(rc != SQLITE_OK){
  25.     iD_LOGE("SQL - failed to open database, trying to key it.");
  26.   }
  27.   rc = sqlite3_key(DatabaseInstance, key, strlen(key));
  28.   if(rc != SQLITE_OK){
  29.     iD_LOGE("SQL - failed to key database...");
  30.   } else {
  31.     iD_LOGI("SQL - key database ok !");
  32.   }
  33.   if (sqlite3_exec(DatabaseInstance, "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
  34.     iD_LOGI("SQL - key is correct");
  35.   } else {
  36.     iD_LOGE("SQL - Key is incorrect");
  37.   }
  38.   char *err_msg = nullptr;
  39.   std::string InitDB = "PRAGMA cipher_store_pass = 1;"
  40.     "PRAGMA cipher_memory_security = OFF;"
  41.     "PRAGMA cipher_compatibility = 3;";
  42.   iD_LOGI("SQL - PRAGMA cipher_compatibility");
  43.   auto res = sqlite3_exec(DatabaseInstance, InitDB.c_str(), nullptr, nullptr, &err_msg);
  44.   if (res != SQLITE_OK) {
  45.     iD_LOGE("SQL Error preparing sql statement: %d %s", res, err_msg);
  46.     throw std::runtime_error("error preparing query");
  47.   }
  48.   iD_LOGI("SQL - Database fully opened");
  49.   return DatabaseInstance;
  50. }
  51. void dokkansql_exec(std::string_view sql) {
  52.   char *err_msg = nullptr;
  53.   iD_LOGI("SQL - dokkansql_exec");
  54.   if (DatabaseInstance == nullptr || DatabaseInstance == NULL) {
  55.     iD_LOGE("SQL - handle is null, opening it");
  56.     DatabaseInstance = dokkansql_open();
  57.   } else {
  58.     iD_LOGI("SQL - handle is not null, continuing");
  59.   }
  60.   iD_LOGI("SQL - sqlite3_exec");
  61.   auto res = sqlite3_exec(DatabaseInstance, sql.data(), nullptr, nullptr, &err_msg);
  62.   if (res != SQLITE_OK) {
  63.     iD_LOGE("SQL Error %d executing sql statement: %s", res, err_msg);
  64.     throw std::runtime_error("error preparing query");
  65.   }
  66. }
  67. json dokkansql_select(std::string_view sql) {
  68.   iD_LOGI("SQL - select databaseManager");
  69.   if (DatabaseInstance == nullptr || DatabaseInstance == NULL) {
  70.     iD_LOGE("SQL - handle is null, opening it");
  71.     DatabaseInstance = dokkansql_open();
  72.   } else {
  73.     iD_LOGI("SQL - handle is not null continuing.");
  74.   }
  75.   iD_LOGI("SQL - sqlite3_prepare_v3 sql.data() : %s", sql.data());
  76.   sqlite3_stmt *stmt;
  77.   auto status = sqlite3_prepare_v3(DatabaseInstance, sql.data(), sql.size(), 0x0, &stmt, NULL);
  78.   if (status != SQLITE_OK) {
  79.     iD_LOGE("SQL Error preparing sql statement: %d", status);
  80.     throw std::runtime_error("error preparing query");
  81.   }
  82.   status = sqlite3_step(stmt);
  83.   auto columnCount = sqlite3_column_count(stmt);
  84.   std::vector<const char *> columnNames{};
  85.   if (status == SQLITE_ROW) {
  86.     for (int i = 0; i < columnCount; i++) {
  87.       columnNames.emplace_back(sqlite3_column_name(stmt, i));
  88.     }
  89.   }
  90.   auto result = json::array();
  91.   while (status == SQLITE_ROW) {
  92.     auto rowData = json::object();
  93.     for (int i = 0; i < columnCount; i++) {
  94.       auto columnType = sqlite3_column_type(stmt, i);
  95.       auto columnName = columnNames[i];
  96.       if (columnType == SQLITE_INTEGER) {
  97.         auto colVal = sqlite3_column_int64(stmt, i);
  98.         rowData[columnName] = colVal;
  99.       } else if (columnType == SQLITE_FLOAT) {
  100.         auto colVal = sqlite3_column_double(stmt, i);
  101.         rowData[columnName] = colVal;
  102.       } else if (columnType == SQLITE_TEXT) {
  103.         auto colVal = sqlite3_column_text(stmt, i);
  104.         auto colSize = sqlite3_column_bytes(stmt, i);
  105.         rowData[columnName] = std::string(reinterpret_cast<const char *>(colVal), colSize);
  106.       } else if (columnType == SQLITE_BLOB) {
  107.         auto colData = sqlite3_column_text(stmt, i);
  108.         auto colSize = sqlite3_column_bytes(stmt, i);
  109.         rowData[columnName] = std::basic_string<unsigned char>(colData, colSize);
  110.       } else if (columnType == SQLITE_NULL) {
  111.         rowData[columnName] = nullptr;
  112.       } else {
  113.         iD_LOGE("SQL - unknown sqlite3 column type");
  114.         throw std::runtime_error("unknown sqlite3 column type");
  115.       }
  116.     }
  117.     result.push_back(rowData);
  118.     status = sqlite3_step(stmt);
  119.   }
  120.   if (status != SQLITE_DONE) {
  121.       iD_LOGE("SQL - Error stepping query %d %s", status, sql.data());
  122.       throw std::runtime_error("error stepping query");
  123.   }
  124.   sqlite3_finalize(stmt);
  125.   return result;
  126. }
  127.  
RAW Paste Data