Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "database.h"
- #include <string>
- #include <general/sqlite3.h>
- #include <nlohmann/json.hpp>
- #include <ios_dokkan/instances.h>
- #include <parallel_hashmap/phmap.h>
- using json = nlohmann::json;
- using namespace phmap;
- sqlite3 *dokkansql_open() {
- sqlite3 *DatabaseInstance;
- NSString *DocPathNSString = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
- std::string DocumentFolder = std::string([DocPathNSString UTF8String]);
- std::string AssetsFolder = DocumentFolder+"/assets";
- std::string DatabasePath_en = AssetsFolder+"/current/en/database.db";
- std::string DatabasePath_fr = AssetsFolder+"/current/fr/database.db";
- std::string DatabasePassword = "9bf9c6ed9d537c399a6c4513e92ab24717e1a488381e3338593abd923fc8a13b";
- iD_LOGI("SQL - dokkansql_open databaseManager");
- const char* database = DatabasePath_fr.c_str();
- iD_LOGI("SQL - dokkansql_open %s",database);
- const char* key = DatabasePassword.c_str();
- int rc;
- iD_LOGI("SQL - dokkansql_open %s length:%i",key,(int)strlen(key));
- rc = sqlite3_open_v2(database, &DatabaseInstance, SQLITE_OPEN_READWRITE, NULL);
- if(rc != SQLITE_OK){
- iD_LOGE("SQL - failed to open database, trying to key it.");
- }
- rc = sqlite3_key(DatabaseInstance, key, strlen(key));
- if(rc != SQLITE_OK){
- iD_LOGE("SQL - failed to key database...");
- } else {
- iD_LOGI("SQL - key database ok !");
- }
- iD_LOGI("SQL - Database openned");
- return DatabaseInstance;
- }
- void dokkansql_exec(std::string_view sql) {
- char *err_msg = nullptr;
- sqlite3 *DatabaseInstance = nullptr;
- iD_LOGI("SQL - dokkansql_exec");
- if (DatabaseInstance == nullptr || DatabaseInstance == NULL) {
- iD_LOGE("SQL - handle is null, openning it");
- DatabaseInstance = dokkansql_open();
- } else {
- iD_LOGE("SQL - handle is not null, it should be.");
- }
- iD_LOGI("SQL - sqlite3_exec");
- sqlite3_exec(DatabaseInstance, sql.data(), nullptr, nullptr, &err_msg);
- if (err_msg != SQLITE_OK) {
- iD_LOGE("SQL Error preparing sql statement: %s", err_msg);
- throw std::runtime_error("error preparing query");
- } else {
- iD_LOGI("SQL - executed");
- }
- }
- json dokkansql_select(std::string_view sql) {
- iD_LOGI("SQL - select databaseManager");
- sqlite3 *DatabaseInstance = nullptr;
- if (DatabaseInstance == nullptr || DatabaseInstance == NULL) {
- iD_LOGE("SQL - handle is null, openning it");
- DatabaseInstance = dokkansql_open();
- } else {
- iD_LOGE("SQL - handle is not null somehow, it should be.");
- }
- iD_LOGI("SQL - sqlite3_prepare_v3 sql.data() : %s", sql.data());
- sqlite3_stmt *stmt;
- auto status = sqlite3_prepare_v3(DatabaseInstance, sql.data(), sql.size(), 0x0, &stmt, NULL);
- if (status != SQLITE_OK) {
- iD_LOGE("SQL - Error preparing sql statement: %d", status);
- throw std::runtime_error("error preparing query");
- }
- status = sqlite3_step(stmt);
- auto columnCount = sqlite3_column_count(stmt);
- std::vector<const char *> columnNames{};
- if (status == SQLITE_ROW) {
- for (int i = 0; i < columnCount; i++) {
- columnNames.emplace_back(sqlite3_column_name(stmt, i));
- }
- }
- auto result = json::array();
- while (status == SQLITE_ROW) {
- auto rowData = json::object();
- for (int i = 0; i < columnCount; i++) {
- auto columnType = sqlite3_column_type(stmt, i);
- auto columnName = columnNames[i];
- if (columnType == SQLITE_INTEGER) {
- auto colVal = sqlite3_column_int64(stmt, i);
- rowData[columnName] = colVal;
- } else if (columnType == SQLITE_FLOAT) {
- auto colVal = sqlite3_column_double(stmt, i);
- rowData[columnName] = colVal;
- } else if (columnType == SQLITE_TEXT) {
- auto colVal = sqlite3_column_text(stmt, i);
- auto colSize = sqlite3_column_bytes(stmt, i);
- rowData[columnName] = std::string(reinterpret_cast<const char *>(colVal), colSize);
- } else if (columnType == SQLITE_BLOB) {
- auto colData = sqlite3_column_text(stmt, i);
- auto colSize = sqlite3_column_bytes(stmt, i);
- rowData[columnName] = std::basic_string<unsigned char>(colData, colSize);
- } else if (columnType == SQLITE_NULL) {
- rowData[columnName] = nullptr;
- } else {
- iD_LOGE("SQL - unknown sqlite3 column type");
- throw std::runtime_error("unknown sqlite3 column type");
- }
- }
- result.push_back(rowData);
- status = sqlite3_step(stmt);
- }
- if (status != SQLITE_DONE) {
- iD_LOGE("SQL - Error stepping query %d %s", status, sql.data());
- throw std::runtime_error("error stepping query");
- }
- sqlite3_finalize(stmt);
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement