2019-07-12 19:05:56 +00:00
|
|
|
//
|
2019-07-19 09:41:39 +00:00
|
|
|
// Created by tobias on 08/07/19.
|
2019-07-12 19:05:56 +00:00
|
|
|
//
|
|
|
|
|
2019-07-19 09:41:39 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2019-07-20 12:51:45 +00:00
|
|
|
#include <cstring>
|
2019-07-19 09:41:39 +00:00
|
|
|
#include <trantor/utils/Logger.h>
|
2019-07-20 21:33:17 +00:00
|
|
|
#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;
|
|
|
|
uint16_t *periods_blob = schedule->periods->to_db_blob();
|
2019-07-20 12:51:45 +00:00
|
|
|
int blob_size = sizeof(uint16_t) * ((periods_blob[0] * 2) + 1);
|
2019-07-19 09:41:39 +00:00
|
|
|
|
|
|
|
sqlite3_bind_text(stmt, 1, schedule->id, -1, SQLITE_STATIC);
|
|
|
|
sqlite3_bind_text(stmt, 2, schedule->name, -1, SQLITE_STATIC);
|
2019-07-20 12:51:45 +00:00
|
|
|
sqlite3_bind_blob(stmt, 3, 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
|
|
|
|
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
|
2019-07-20 12:51:45 +00:00
|
|
|
new_schedule->periods = new period_list((const uint16_t*)sqlite3_column_blob(stmt, i));
|
|
|
|
break;
|
|
|
|
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
|
|
|
|
{
|
2019-07-20 21:33:17 +00:00
|
|
|
if(s == SQLITE_DONE)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-20 21:33:17 +00:00
|
|
|
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;
|
|
|
|
|
2019-07-20 12:51:45 +00:00
|
|
|
sqlite3_prepare_v2(globals::db, "UPDATE schedules SET name = ?2, periods = ?3 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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-07-20 21:33:17 +00:00
|
|
|
schedule_dbo**
|
|
|
|
schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
2019-07-20 21:33:17 +00:00
|
|
|
helpers::sql_filter_builder *filters[1];
|
|
|
|
helpers::sql_filter_builder filter
|
|
|
|
{
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
bind_func,
|
|
|
|
";"
|
|
|
|
};
|
|
|
|
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)
|
|
|
|
{
|
2019-07-20 21:33:17 +00:00
|
|
|
sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM schedules WHERE", filters);
|
2019-07-19 12:42:36 +00:00
|
|
|
|
2019-07-20 21:33:17 +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);
|
|
|
|
}
|
|
|
|
|