iu9-ca-web-chat/src/web_chat/initialize.cpp

40 lines
1.6 KiB
C++
Raw Normal View History

#include "actions.h"
#include <engine_engine_number_9/baza_throw.h>
#include "str_fields_check.h"
#include <sqlite3.h>
#include <engine_engine_number_9/os_utils.h>
#include <find_db.h>
void sqlite_single_statement(sqlite3* db_hand_ptr, const std::string& req_statement) {
sqlite3_stmt* stmt_obj_ptr = NULL;
int ret = sqlite3_prepare16_v2(db_hand_ptr, req_statement.c_str(), -1, &stmt_obj_ptr, NULL);
een9_ASSERT(ret == 0, "Can't compile request expression");
struct Guard1{sqlite3_stmt*& r; ~Guard1(){if (sqlite3_finalize(r) != 0) {abort();}}} guard1{stmt_obj_ptr};
while (true) {
ret = sqlite3_step(stmt_obj_ptr);
if (ret == SQLITE_DONE)
break;
if (ret != SQLITE_ROW) {
printf("sqlite_row error!!!\n");
break;
}
}
printf("Request steps are done\n");
}
void initialize_website(const json::JSON& config, const std::string& root_pw) {
printf("Initialization...\n");
een9_ASSERT(check_password(root_pw), "Bad root password");
std::string db_path;
int ret;
ret = find_db_sqlite_file_path(config, db_path);
een9_ASSERT(ret == 0, "Invalid settings[\"database\"] field");
een9_ASSERT(!een9::isRegularFile(db_path), "Database file exists prior to initialization. "
"Can't preceed withut harming existing data");
sqlite3* db_hand_ptr = NULL;
ret = sqlite3_open(db_path.c_str(), &db_hand_ptr);
een9_ASSERT(ret == 0, "Can't open database");
struct Guard1{sqlite3*& dhp; ~Guard1(){if (sqlite3_close(dhp) != 0) {abort();}}} guard1{db_hand_ptr};
sqlite_single_statement(db_hand_ptr, "CREATE TABLE tb(a INT, b INT);");
}