Baoulettes

Dokkan iOS database.h

Dec 4th, 2021
1,001
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.  
  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+"/current/en/database.db";
  16.     std::string DatabasePath_fr = AssetsFolder+"/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.   iD_LOGI("SQL - dokkansql_open %s",database);
  21.   const char* key = DatabasePassword.c_str();
  22.   int rc;
  23.   iD_LOGI("SQL - dokkansql_open %s length:%i",key,(int)strlen(key));
  24.   rc = sqlite3_open_v2(database, &DatabaseInstance, SQLITE_OPEN_READWRITE, NULL);
  25.   if(rc != SQLITE_OK){
  26.     iD_LOGE("SQL - failed to open database, trying to key it.");
  27.   }
  28.   rc = sqlite3_key(DatabaseInstance, key, strlen(key));
  29.   if(rc != SQLITE_OK){
  30.     iD_LOGE("SQL - failed to key database...");
  31.   } else {
  32.     iD_LOGI("SQL - key database ok !");
  33.   }
  34.   iD_LOGI("SQL - Database openned");
  35.   return DatabaseInstance;
  36. }
  37. void dokkansql_exec(std::string_view sql) {
  38.   char *err_msg = nullptr;
  39.   sqlite3 *DatabaseInstance = nullptr;
  40.   iD_LOGI("SQL - dokkansql_exec");
  41.   if (DatabaseInstance == nullptr || DatabaseInstance == NULL) {
  42.     iD_LOGE("SQL - handle is null, openning it");
  43.     DatabaseInstance = dokkansql_open();
  44.   } else {
  45.     iD_LOGE("SQL - handle is not null, it should be.");
  46.   }
  47.   iD_LOGI("SQL - sqlite3_exec");
  48.   sqlite3_exec(DatabaseInstance, sql.data(), nullptr, nullptr, &err_msg);
  49.   if (err_msg != SQLITE_OK) {
  50.     iD_LOGE("SQL Error preparing sql statement: %s", err_msg);
  51.     throw std::runtime_error("error preparing query");
  52.   } else {
  53.     iD_LOGI("SQL - executed");
  54.   }
  55. }
  56. json dokkansql_select(std::string_view sql) {
  57.   iD_LOGI("SQL - select databaseManager");
  58.   sqlite3 *DatabaseInstance = nullptr;
  59.   if (DatabaseInstance == nullptr || DatabaseInstance == NULL) {
  60.     iD_LOGE("SQL - handle is null, openning it");
  61.     DatabaseInstance = dokkansql_open();
  62.   } else {
  63.     iD_LOGE("SQL - handle is not null somehow, it should be.");
  64.   }
  65.   iD_LOGI("SQL - sqlite3_prepare_v3 sql.data() : %s", sql.data());
  66.   sqlite3_stmt *stmt;
  67.   auto status = sqlite3_prepare_v3(DatabaseInstance, sql.data(), sql.size(), 0x0, &stmt, NULL);
  68.   if (status != SQLITE_OK) {
  69.     iD_LOGE("SQL - Error preparing sql statement: %d", status);
  70.     throw std::runtime_error("error preparing query");
  71.   }
  72.   status = sqlite3_step(stmt);
  73.   auto columnCount = sqlite3_column_count(stmt);
  74.   std::vector<const char *> columnNames{};
  75.   if (status == SQLITE_ROW) {
  76.     for (int i = 0; i < columnCount; i++) {
  77.       columnNames.emplace_back(sqlite3_column_name(stmt, i));
  78.     }
  79.   }
  80.   auto result = json::array();
  81.   while (status == SQLITE_ROW) {
  82.     auto rowData = json::object();
  83.     for (int i = 0; i < columnCount; i++) {
  84.       auto columnType = sqlite3_column_type(stmt, i);
  85.       auto columnName = columnNames[i];
  86.       if (columnType == SQLITE_INTEGER) {
  87.         auto colVal = sqlite3_column_int64(stmt, i);
  88.         rowData[columnName] = colVal;
  89.       } else if (columnType == SQLITE_FLOAT) {
  90.         auto colVal = sqlite3_column_double(stmt, i);
  91.         rowData[columnName] = colVal;
  92.       } else if (columnType == SQLITE_TEXT) {
  93.         auto colVal = sqlite3_column_text(stmt, i);
  94.         auto colSize = sqlite3_column_bytes(stmt, i);
  95.         rowData[columnName] = std::string(reinterpret_cast<const char *>(colVal), colSize);
  96.       } else if (columnType == SQLITE_BLOB) {
  97.         auto colData = sqlite3_column_text(stmt, i);
  98.         auto colSize = sqlite3_column_bytes(stmt, i);
  99.         rowData[columnName] = std::basic_string<unsigned char>(colData, colSize);
  100.       } else if (columnType == SQLITE_NULL) {
  101.         rowData[columnName] = nullptr;
  102.       } else {
  103.         iD_LOGE("SQL - unknown sqlite3 column type");
  104.         throw std::runtime_error("unknown sqlite3 column type");
  105.       }
  106.     }
  107.     result.push_back(rowData);
  108.     status = sqlite3_step(stmt);
  109.   }
  110.   if (status != SQLITE_DONE) {
  111.       iD_LOGE("SQL - Error stepping query %d %s", status, sql.data());
  112.       throw std::runtime_error("error stepping query");
  113.   }
  114.   sqlite3_finalize(stmt);
  115.   return result;
  116. }
  117.  
RAW Paste Data