// // Created by tobias on 08/07/19. // #include #include #include #include #include #include "schedule_dbo.h" #include "globals.h" #include "period.h" static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt) { int rc; uint16_t *periods_blob = schedule->periods->to_db_blob(); int blob_size = sizeof(uint16_t) * ((periods_blob[0] * 2) + 1); sqlite3_bind_text(stmt, 1, schedule->id, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 2, schedule->name, -1, SQLITE_STATIC); sqlite3_bind_blob(stmt, 3, periods_blob, blob_size, SQLITE_STATIC); rc = sqlite3_step(stmt); sqlite3_finalize(stmt); free(periods_blob); if (rc != SQLITE_DONE) { LOG_ERROR << "ERROR inserting/updating data: " << sqlite3_errmsg(globals::db); return false; } return true; } static schedule_dbo* schedule_db_select_mapper(sqlite3_stmt *stmt) { auto new_schedule = new schedule_dbo(); for(int i = 0; i < sqlite3_column_count(stmt); i++) { const char *name = sqlite3_column_name(stmt, i); switch(name[0]) { case 'i': // id strncpy(new_schedule->id, (const char*)sqlite3_column_text(stmt, i), 32); new_schedule->id[32] = '\0'; break; case 'n': // name strncpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), 127); new_schedule->name[127] = '\0'; break; case 'p': // periods new_schedule->periods = new period_list((const uint16_t*)sqlite3_column_blob(stmt, i)); break; default: // ignore columns not implemented break; } } return new_schedule; } static schedule_dbo** schedule_db_select(sqlite3_stmt *stmt) { auto **all_schedules = (schedule_dbo**)malloc(sizeof(schedule_dbo*)); int row = 0; while(true) { int s; s = sqlite3_step(stmt); if (s == SQLITE_ROW) { schedule_dbo *new_schedule = schedule_db_select_mapper(stmt); row++; all_schedules = (schedule_dbo**)realloc(all_schedules, sizeof(schedule_dbo*) * (row + 1)); all_schedules[row - 1] = new_schedule; } else { if(s == SQLITE_DONE) { break; } else { LOG_ERROR << "Error Selecting schedules from database: " << sqlite3_errstr(s); break; } } } sqlite3_finalize(stmt); all_schedules[row] = nullptr; return all_schedules; } bool schedule_dbo::update() { sqlite3_stmt *stmt; sqlite3_prepare_v2(globals::db, "UPDATE schedules SET name = ?2, periods = ?3 WHERE id=?1;", -1, &stmt, nullptr); return schedule_db_update_insert(this, stmt); } bool schedule_dbo::insert() { sqlite3_stmt *stmt; sqlite3_prepare_v2(globals::db, "INSERT INTO schedules(id, name, periods) values (?1, ?2, ?3);", -1, &stmt, nullptr); return schedule_db_update_insert(this, stmt); } bool schedule_dbo::remove() { sqlite3_stmt *stmt; int rc; sqlite3_prepare_v2(globals::db, "DELETE FROM schedules WHERE id=?1;", -1, &stmt, nullptr); sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC); rc = sqlite3_step(stmt); sqlite3_finalize(stmt); return rc == SQLITE_DONE; } schedule_dbo::~schedule_dbo() { delete this->periods; } Json::Value schedule_dbo::to_json() { Json::Value schedule_json; schedule_json["name"] = this->name; schedule_json["id"] = this->id; schedule_json["periods"] = this->periods->to_json(); return schedule_json; } schedule_dbo** schedule_dbo::get_all() { sqlite3_stmt *stmt; sqlite3_prepare_v2(globals::db, "SELECT * FROM schedules;", -1, &stmt, nullptr); return schedule_db_select(stmt); } schedule_dbo** schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func) { helpers::sql_filter_builder *filters[1]; helpers::sql_filter_builder filter { key, value, bind_func, ";" }; filters[0] = &filter; sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM schedules WHERE", filters); return schedule_db_select(stmt); } void schedule_dbo::free_list(schedule_dbo **schedules_list) { for(int i = 0; schedules_list[i] != nullptr; i++) { delete schedules_list[i]; } free(schedules_list); }