core-legacy/models/schedule_dbo.cc

304 lines
7.2 KiB
C++
Raw Normal View History

2019-07-20 12:51:45 +00:00
#include <cstring>
2019-07-19 09:41:39 +00:00
#include <trantor/utils/Logger.h>
#include <helpers.h>
2019-07-12 19:05:56 +00:00
#include "schedule_dbo.h"
2020-04-28 19:50:19 +00:00
#include "junction_tag_dbo.h"
#include "tag_dbo.h"
2019-07-19 09:41:39 +00:00
#include "globals.h"
#include "period.h"
static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt)
{
int rc;
2020-04-18 19:29:52 +00:00
uint16_t *periods_blob = schedule->periods->to_blob();
2019-09-08 21:42:48 +00:00
int blob_size = (int)sizeof(uint16_t) * ((periods_blob[0] * 2) + 1);
2019-07-19 09:41:39 +00:00
2020-04-19 00:44:35 +00:00
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);
2019-07-19 09:41:39 +00:00
rc = sqlite3_step(stmt);
2019-07-19 12:42:36 +00:00
sqlite3_finalize(stmt);
free(periods_blob);
2019-07-19 09:41:39 +00:00
if (rc != SQLITE_DONE)
{
2019-07-20 12:51:45 +00:00
LOG_ERROR << "ERROR inserting/updating data: " << sqlite3_errmsg(globals::db);
2019-07-19 09:41:39 +00:00
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
2020-04-19 00:44:35 +00:00
new_schedule->id = sqlite3_column_int(stmt, i);
2019-07-19 09:41:39 +00:00
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
2019-07-20 12:51:45 +00:00
new_schedule->periods = new period_list((const uint16_t*)sqlite3_column_blob(stmt, i));
break;
2020-04-19 00:44:35 +00:00
case 'u': // uid
uuid_copy(new_schedule->uid, (const unsigned char*)sqlite3_column_blob(stmt, i));
break;
2019-07-20 12:51:45 +00:00
default: // ignore columns not implemented
2019-07-19 09:41:39 +00:00
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)
2019-07-19 09:41:39 +00:00
{
break;
}
else
{
LOG_ERROR << "Error Selecting schedules from database: " << sqlite3_errstr(s);
break;
2019-07-19 09:41:39 +00:00
}
}
}
2019-07-19 12:42:36 +00:00
sqlite3_finalize(stmt);
2019-07-19 09:41:39 +00:00
all_schedules[row] = nullptr;
return all_schedules;
}
bool
schedule_dbo::update()
{
sqlite3_stmt *stmt;
2020-04-19 00:44:35 +00:00
sqlite3_prepare_v2(globals::db, "UPDATE schedules SET uid = ?2, name = ?3, periods = ?4 WHERE id=?1;", -1, &stmt, nullptr);
2019-07-19 09:41:39 +00:00
return schedule_db_update_insert(this, stmt);
}
bool
schedule_dbo::insert()
{
sqlite3_stmt *stmt;
2020-04-19 00:44:35 +00:00
sqlite3_prepare_v2(globals::db, "INSERT INTO schedules(uid, name, periods) values (?2, ?3, ?4);", -1, &stmt, nullptr);
2019-07-19 09:41:39 +00:00
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);
2020-04-19 00:44:35 +00:00
sqlite3_bind_int(stmt, 1, this->id);
2019-07-19 09:41:39 +00:00
rc = sqlite3_step(stmt);
2019-07-19 12:42:36 +00:00
sqlite3_finalize(stmt);
2019-07-19 09:41:39 +00:00
return rc == SQLITE_DONE;
}
2019-07-19 12:42:36 +00:00
schedule_dbo::~schedule_dbo()
{
delete this->periods;
}
2019-07-19 09:41:39 +00:00
Json::Value
schedule_dbo::to_json()
{
char id_str[37];
2020-04-19 00:44:35 +00:00
schedule_dbo::unparse_uid(this->uid, id_str);
2019-07-19 09:41:39 +00:00
Json::Value schedule_json;
schedule_json["name"] = this->name;
schedule_json["id"] = id_str;
2019-07-19 09:41:39 +00:00
schedule_json["periods"] = this->periods->to_json();
2020-04-28 19:50:19 +00:00
int *tags_ids = junction_tag_dbo::get_tags_for_schedule_id(this->id);
if(tags_ids != nullptr)
{
Json::Value tags_json(Json::arrayValue);
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;
}
2019-07-19 09:41:39 +00:00
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)
2019-07-19 09:41:39 +00:00
{
helpers::sql_filter_builder *filters[1];
helpers::sql_filter_builder filter
{
key,
value,
bind_func,
bind_func_param,
";"
};
filters[0] = &filter;
2019-07-20 22:29:05 +00:00
sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM schedules WHERE", filters);
return schedule_db_select(stmt);
}
2020-04-23 15:00:12 +00:00
schedule_dbo*
2020-04-28 19:50:19 +00:00
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)
2020-04-23 15:00:12 +00:00
{
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;
}
2019-07-20 22:29:05 +00:00
schedule_dbo**
schedule_dbo::get_by(helpers::sql_filter_builder **filters)
{
sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM schedules WHERE", filters);
2019-07-19 12:42:36 +00:00
return schedule_db_select(stmt);
2019-07-19 09:41:39 +00:00
}
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
2020-04-19 00:44:35 +00:00
schedule_dbo::parse_uid(const char *uid_str, uuid_t result)
{
2020-04-19 00:44:35 +00:00
if(strcmp("off", uid_str) == 0)
{
memset(result, 0, sizeof(uuid_t));
memcpy(result, "off", 3);
return 0;
}
2020-04-19 00:44:35 +00:00
if(strcmp("on", uid_str) == 0)
{
memset(result, 0, sizeof(uuid_t));
memcpy(result, "on", 2);
return 0;
}
2020-04-19 00:44:35 +00:00
if(uuid_parse(uid_str, result))
{
return 1;
}
return 0;
}
void
2020-04-19 00:44:35 +00:00
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);
2020-04-19 00:44:35 +00:00
if(uuid_compare(uid, tmp_uuid) == 0)
{
strcpy(result, "off");
return;
}
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "on", 2);
2020-04-19 00:44:35 +00:00
if(uuid_compare(uid, tmp_uuid) == 0)
{
strcpy(result, "on");
return;
}
2020-04-19 00:44:35 +00:00
uuid_unparse(uid, result);
}