#include "actions.h" #include #include "str_fields_check.h" #include #include #include #include #include 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 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 }