181 lines
4.3 KiB
C++
181 lines
4.3 KiB
C++
//
|
|
// Created by tobias on 08/07/19.
|
|
//
|
|
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <string.h>
|
|
#include <trantor/utils/Logger.h>
|
|
#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();
|
|
|
|
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, sizeof(uint16_t) * ((periods_blob[0] * 2) + 1), SQLITE_STATIC);
|
|
|
|
rc = sqlite3_step(stmt);
|
|
if (rc != SQLITE_DONE)
|
|
{
|
|
printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db));
|
|
free(periods_blob);
|
|
return false;
|
|
}
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
free(periods_blob);
|
|
|
|
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
|
|
auto periods_blob = (const uint16_t*)sqlite3_column_blob(stmt, i);
|
|
new_schedule->periods = new period_list(periods_blob);
|
|
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";
|
|
return nullptr;
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
return rc == SQLITE_DONE;
|
|
|
|
}
|
|
|
|
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_one_by(const char *key, const char *value)
|
|
{
|
|
sqlite3_stmt *stmt;
|
|
char* sql;
|
|
|
|
asprintf(&sql, "SELECT * FROM schedules WHERE %s=?1;", key);
|
|
|
|
sqlite3_prepare_v2(globals::db, sql, -1, &stmt, nullptr);
|
|
sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
|
|
|
|
return schedule_db_select(stmt)[0];
|
|
}
|
|
|
|
void
|
|
schedule_dbo::free_list(schedule_dbo **schedules_list)
|
|
{
|
|
for(int i = 0; schedules_list[i] != nullptr; i++)
|
|
{
|
|
delete schedules_list[i]->periods;
|
|
delete schedules_list[i];
|
|
}
|
|
free(schedules_list);
|
|
}
|
|
|