core-legacy/models/schedule_dbo.cc

302 lines
7.2 KiB
C++

#include <cstring>
#include <trantor/utils/Logger.h>
#include <helpers.h>
#include "schedule_dbo.h"
#include "junction_tag_dbo.h"
#include "tag_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_blob();
int blob_size = (int)sizeof(uint16_t) * ((periods_blob[0] * 2) + 1);
sqlite3_bind_int(stmt, 1, schedule->id);
sqlite3_bind_blob(stmt, 2, schedule->uid, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, schedule->name, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 4, 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
new_schedule->id = sqlite3_column_int(stmt, i);
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;
case 'u': // uid
uuid_copy(new_schedule->uid, (const unsigned char*)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 uid = ?2, name = ?3, periods = ?4 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(uid, name, periods) values (?2, ?3, ?4);", -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_int(stmt, 1, this->id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
schedule_dbo::~schedule_dbo()
{
delete this->periods;
}
Json::Value
schedule_dbo::to_json()
{
char id_str[37];
schedule_dbo::unparse_uid(this->uid, id_str);
Json::Value schedule_json;
schedule_json["name"] = this->name;
schedule_json["id"] = id_str;
schedule_json["periods"] = this->periods->to_json();
Json::Value tags_json(Json::arrayValue);
int *tags_ids = junction_tag_dbo::get_tags_for_schedule_id(this->id);
if(tags_ids != nullptr)
{
int tags_count;
for(tags_count = 0; tags_ids[tags_count] != 0; ++tags_count);
char **tags = (char**)malloc(sizeof(char*) * tags_count);
for(int i = 0; i < tags_count; ++i)
{
tags[i] = tag_dbo::get_tag(tags_ids[i]);
tags_json[i] = tags[i];
}
}
schedule_json["tags"] = tags_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, int bind_func_param)
{
helpers::sql_filter_builder *filters[1];
helpers::sql_filter_builder filter
{
key,
value,
bind_func,
bind_func_param,
";"
};
filters[0] = &filter;
sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM schedules WHERE", filters);
return schedule_db_select(stmt);
}
schedule_dbo*
schedule_dbo::get_by_id(int id)
{
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", &id, (intptr_t)&sqlite3_bind_int, 0);
schedule_dbo *result = schedules[0];
free(schedules);
return result;
}
schedule_dbo*
schedule_dbo::get_by_id_or_off(int id)
{
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", &id, (intptr_t)&sqlite3_bind_int, 0);
if(!schedules[0])
{
free(schedules);
uuid_t off_uuid;
memset(off_uuid, 0, sizeof(uuid_t));
memcpy(off_uuid, "off", 3);
schedules = schedule_dbo::get_by_simple("uid", off_uuid, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
if(!schedules[0])
{
LOG_FATAL << "schedule with uid 'off' not found";
exit(1);
}
}
schedule_dbo *schedule = schedules[0];
free(schedules);
return schedule;
}
schedule_dbo**
schedule_dbo::get_by(helpers::sql_filter_builder **filters)
{
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);
}
int
schedule_dbo::parse_uid(const char *uid_str, uuid_t result)
{
if(strcmp("off", uid_str) == 0)
{
memset(result, 0, sizeof(uuid_t));
memcpy(result, "off", 3);
return 0;
}
if(strcmp("on", uid_str) == 0)
{
memset(result, 0, sizeof(uuid_t));
memcpy(result, "on", 2);
return 0;
}
if(uuid_parse(uid_str, result))
{
return 1;
}
return 0;
}
void
schedule_dbo::unparse_uid(const uuid_t uid, char *result)
{
uuid_t tmp_uuid;
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "off", 3);
if(uuid_compare(uid, tmp_uuid) == 0)
{
strcpy(result, "off");
return;
}
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "on", 2);
if(uuid_compare(uid, tmp_uuid) == 0)
{
strcpy(result, "on");
return;
}
uuid_unparse(uid, result);
}