95 lines
4.0 KiB
C++
95 lines
4.0 KiB
C++
#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>
|
|
#include <unistd.h>
|
|
#include <assert.h>
|
|
|
|
void sqlite_single_statement(sqlite3* db_hand, const std::string& req_statement) {
|
|
sqlite3_stmt* stmt_obj = NULL;
|
|
int ret = sqlite3_prepare_v2(db_hand, req_statement.c_str(), -1, &stmt_obj, NULL);
|
|
een9_ASSERT(ret == 0, "Can't compile request expression");
|
|
assert(sqlite3_errcode(db_hand) == SQLITE_OK);
|
|
struct Guard1{sqlite3_stmt*& r; ~Guard1(){if (sqlite3_finalize(r) != 0) {abort();}}} guard1{stmt_obj};
|
|
while (true) {
|
|
ret = sqlite3_step(stmt_obj);
|
|
if (ret == SQLITE_DONE)
|
|
break;
|
|
if (ret != SQLITE_ROW) {
|
|
printf("sqlite_row error!!!\n");
|
|
printf("%s\n", sqlite3_errmsg(db_hand));
|
|
break;
|
|
}
|
|
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;
|
|
default:
|
|
een9_THROW("AAAAAA");
|
|
}
|
|
}
|
|
printf("\n");
|
|
printf("Values: | ");
|
|
for (int i = 0; i < cc; i++) {
|
|
if (types[i] == SQLITE_INTEGER) {
|
|
printf("%ld | ", sqlite3_column_int64(stmt_obj, i));
|
|
} else if (types[i] == SQLITE_FLOAT) {
|
|
printf("%lf | ", sqlite3_column_double(stmt_obj, i));
|
|
} else if (types[i] == SQLITE_BLOB) {
|
|
const void* blob = sqlite3_column_blob(stmt_obj, i);
|
|
een9_ASSERT(sqlite3_errcode(db_hand) == SQLITE_OK, "oom in sqlite3_column_blob");
|
|
size_t sz = sqlite3_column_bytes(stmt_obj, i);
|
|
printf("Blob of size %s | ", sz);
|
|
} else if (types[i] == SQLITE_NULL) {
|
|
printf("NULL | ");
|
|
} else {
|
|
const unsigned char* text = sqlite3_column_text(stmt_obj, i);
|
|
een9_ASSERT(sqlite3_errcode(db_hand) != SQLITE_NOMEM, "oom in sqlite3_column_text");
|
|
printf("%s | ", (const char*)text);
|
|
// todo: THIS F. B.S. IS NOT SAFE
|
|
}
|
|
}
|
|
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");
|
|
if (een9::isRegularFile(db_path)) {
|
|
// todo: plaese, don't do this
|
|
ret = unlink(db_path.c_str());
|
|
een9_ASSERT_pl(ret == 0);
|
|
}
|
|
een9_ASSERT(!een9::isRegularFile(db_path), "Database file exists prior to initialization. "
|
|
"Can't preceed withut harming existing data");
|
|
sqlite3* db_hand = NULL;
|
|
ret = sqlite3_open(db_path.c_str(), &db_hand);
|
|
een9_ASSERT(ret == 0, "Can't open database");
|
|
assert(sqlite3_errcode(db_hand) == SQLITE_OK);
|
|
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)");
|
|
sqlite_single_statement(db_hand, "INSERT INTO tb(a) VALUES (111)");
|
|
sqlite_single_statement(db_hand, "INSERT INTO tb(b) VALUES ('yaeyahiyagdohzghz5echp')");
|
|
sqlite_single_statement(db_hand, "INSERT INTO tb(a, b) VALUES (1123123, 'string')");
|
|
sqlite_single_statement(db_hand, "SELECT * FROM tb");
|
|
// todo: actually write something
|
|
} |