core-legacy/models/schedule_dbo.cc

250 lines
5.8 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"
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();
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);
}
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);
}