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

59 lines
2.3 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>
2024-08-16 22:10:04 +03:00
void sqlite_single_statement(sqlite3* db_hand, const std::string& req_statement) {
sqlite3_stmt* stmt_obj = NULL;
int ret = sqlite3_prepare16_v2(db_hand, req_statement.c_str(), -1, &stmt_obj, NULL);
een9_ASSERT(ret == 0, "Can't compile request expression");
2024-08-16 22:10:04 +03:00
struct Guard1{sqlite3_stmt*& r; ~Guard1(){if (sqlite3_finalize(r) != 0) {abort();}}} guard1{stmt_obj};
while (true) {
2024-08-16 22:10:04 +03:00
ret = sqlite3_step(stmt_obj);
if (ret == SQLITE_DONE)
break;
if (ret != SQLITE_ROW) {
printf("sqlite_row error!!!\n");
2024-08-16 22:10:04 +03:00
printf("%s\n", sqlite3_errmsg(db_hand));
break;
}
2024-08-16 22:10:04 +03:00
int cc = sqlite3_column_count(stmt_obj);
std::vector<int> types(cc);
for (int i = 0; i < cc; i++) {
types[i] = sqlite3_column_type(stmt_obj, i);
}
printf("Column: |");
for (int i = 0; i < cc; i++) {
switch (types[i]) {
#define ccase(tname) case SQLITE_ ## tname: printf(" " #tname " |"); break;
ccase(INTEGER)
ccase(FLOAT)
ccase(BLOB)
ccase(NULL)
case SQLITE3_TEXT:
printf(" TEXT |"); break;
}
}
printf("\n");
}
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");
2024-08-16 22:10:04 +03:00
// sqlite3* db_hand = NULL;
// ret = sqlite3_open(db_path.c_str(), &db_hand);
// een9_ASSERT(ret == 0, "Can't open database");
// struct Guard1{sqlite3*& dhp; ~Guard1(){if (sqlite3_close(dhp) != 0) {abort();}}} guard1{db_hand};
// sqlite_single_statement(db_hand, "CREATE TABLE tb(a INT, b INT);");
// todo: actually write something
}