fix: relay-schedules now as database junction

This commit is contained in:
Tobias Reisinger 2020-04-27 23:33:56 +02:00
parent 5b508b3735
commit 5bc849ab40
7 changed files with 188 additions and 47 deletions

View file

@ -29,7 +29,6 @@ static bool controller_db_update_insert(controller_dbo *controller, sqlite3_stmt
sqlite3_bind_int(stmt, 4, controller->active);
sqlite3_bind_int(stmt, 5, controller->port);
sqlite3_bind_int(stmt, 6, controller->relay_count);
sqlite3_bind_text(stmt, 7, controller->tag, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
@ -47,7 +46,6 @@ static controller_dbo*
controller_db_select_mapper(sqlite3_stmt *stmt)
{
auto *new_controller = new controller_dbo();
const char* new_tag;
for(int i = 0; i < sqlite3_column_count(stmt); i++)
{
const char *name = sqlite3_column_name(stmt, i);
@ -78,14 +76,6 @@ controller_db_select_mapper(sqlite3_stmt *stmt)
case 'r': // relay_count
new_controller->relay_count = sqlite3_column_int(stmt, i);
break;
case 't': // tag
new_tag = (const char*)sqlite3_column_text(stmt, i);
new_controller->tag[0] = '\0';
if(new_tag)
{
strncpy(new_controller->tag, (const char*)sqlite3_column_text(stmt, i), 63);
}
break;
default: // ignore columns not implemented
break;
}
@ -140,7 +130,7 @@ controller_dbo::update()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "UPDATE controllers set name = ?2, ip = ?3, active = ?4, port = ?5, relay_count = ?6, tag = ?7 WHERE id = ?1;", -1, &stmt, nullptr);
sqlite3_prepare_v2(globals::db, "UPDATE controllers set name = ?2, ip = ?3, active = ?4, port = ?5, relay_count = ?6 WHERE id = ?1;", -1, &stmt, nullptr);
return controller_db_update_insert(this, stmt);
}
@ -150,7 +140,7 @@ controller_dbo::insert()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "INSERT INTO controllers(id, name, ip, active, port, relay_count, tag) values (?1, ?2, ?3, ?4, ?5, ?6, ?7);", -1, &stmt, nullptr);
sqlite3_prepare_v2(globals::db, "INSERT INTO controllers(id, name, ip, active, port, relay_count) values (?1, ?2, ?3, ?4, ?5, ?6);", -1, &stmt, nullptr);
return controller_db_update_insert(this, stmt);
}
@ -181,7 +171,6 @@ controller_dbo::to_json()
controller_json["ip"] = this->ip;
controller_json["relay_count"] = this->relay_count;
controller_json["active"] = this->active;
controller_json["tag"] = this->tag;
controller_json["relays"] = Json::arrayValue;

View file

@ -18,7 +18,6 @@ public:
bool active;
int port;
int relay_count;
char tag[64];
relay_dbo **relays;
~controller_dbo();

View file

@ -0,0 +1,112 @@
#include <cstdio>
#include <cstring>
#include <trantor/utils/Logger.h>
#include <helpers.h>
#include "junction_relay_schedule_dbo.h"
#include "globals.h"
bool
junction_relay_schedule_dbo::insert(uint8_t weekday, int relay_id, int schedule_id)
{
int rc;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "INSERT INTO junction_relay_schedule(weekday, schedule_id, relay_id) values (?1, ?2, ?3);", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, weekday);
sqlite3_bind_int(stmt, 2, schedule_id);
sqlite3_bind_int(stmt, 3, relay_id);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db));
return false;
}
sqlite3_finalize(stmt);
return true;
}
int
junction_relay_schedule_dbo::get_schedule_id(uint8_t weekday, int relay_id)
{
int result = 0;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "SELECT schedule_id FROM junction_relay_schedule WHERE weekday=?1 AND relay_id=?2 LIMIT 1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, weekday);
sqlite3_bind_int(stmt, 2, relay_id);
while(true)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
result = sqlite3_column_int(stmt, 0);
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR << "Error Selecting relays from database: " << sqlite3_errstr(s);
break;
}
}
}
sqlite3_finalize(stmt);
return result;
}
bool
junction_relay_schedule_dbo::remove(uint8_t weekday, int relay_id, int schedule_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM junction_relay_schedule WHERE weekday=?1 AND schedule_id=?2 AND relay_id=?3;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, weekday);
sqlite3_bind_int(stmt, 2, schedule_id);
sqlite3_bind_int(stmt, 3, relay_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
bool
junction_relay_schedule_dbo::remove_for_relay(int relay_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM junction_relay_schedule WHERE relay_id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, relay_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
bool
junction_relay_schedule_dbo::remove_for_schedule(int schedule_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM junction_relay_schedule WHERE schedule_id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, schedule_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}

View file

@ -0,0 +1,29 @@
#ifndef EMGAUWA_CORE_JUNCTION_RELAY_SCHEDULE_DBO_H
#define EMGAUWA_CORE_JUNCTION_RELAY_SCHEDULE_DBO_H
#include <string>
#include <sqlite3.h>
#include <json/value.h>
#include <uuid/uuid.h>
#include <helpers.h>
namespace junction_relay_schedule_dbo
{
int
get_schedule_id(uint8_t weekday, int relay_id);
bool
insert(uint8_t weekday, int relay_id, int schedule_id);
bool
remove(uint8_t weekday, int relay_id, int schedule_id);
bool
remove_for_relay(int relay_id);
bool
remove_for_schedule(int schedule_id);
};
#endif //EMGAUWA_CORE_JUNCTION_RELAY_SCHEDULE_DBO_H

View file

@ -4,6 +4,7 @@
#include <helpers.h>
#include "relay_dbo.h"
#include "globals.h"
#include "junction_relay_schedule_dbo.h"
#include "controller_dbo.h"
#include "schedule_dbo.h"
@ -11,18 +12,16 @@ static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt)
{
int rc;
uint32_t schedules_ids[7];
junction_relay_schedule_dbo::remove_for_relay(relay->id);
for(int i = 0; i < 7; ++i)
{
schedules_ids[i] = relay->schedules[i]->id;
junction_relay_schedule_dbo::insert(i, relay->id, relay->schedules[i]->id);
}
sqlite3_bind_int(stmt, 1, relay->id);
sqlite3_bind_int(stmt, 2, relay->number);
sqlite3_bind_text(stmt, 3, relay->name, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 4, schedules_ids, sizeof(uint32_t) * 7, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 5, relay->controller_id, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_text(stmt, 6, relay->tag, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 4, relay->controller_id, sizeof(uuid_t), SQLITE_STATIC);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
@ -40,8 +39,6 @@ static relay_dbo*
relay_db_select_mapper(sqlite3_stmt *stmt)
{
auto *new_relay = new relay_dbo();
const char *new_tag;
const uint32_t *new_relays_ids;
for(int i = 0; i < sqlite3_column_count(stmt); i++)
{
const char *name = sqlite3_column_name(stmt, i);
@ -66,25 +63,15 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
break;
}
break;
case 's': // schedules_ids
new_relays_ids = (const uint32_t*)sqlite3_column_blob(stmt, i);
for(int i = 0; i < 7; ++i)
{
new_relay->schedules[i] = schedule_dbo::get_by_id_or_off(new_relays_ids[i]);
}
break;
case 't': // tag
new_tag = (const char*)sqlite3_column_text(stmt, i);
new_relay->tag[0] = '\0';
if(new_tag)
{
strncpy(new_relay->tag, (const char*)sqlite3_column_text(stmt, i), 63);
}
break;
default: // ignore columns not implemented
break;
}
}
for(int i = 0; i < 7; ++i)
{
int schedule_id = junction_relay_schedule_dbo::get_schedule_id(i, new_relay->id);
new_relay->schedules[i] = schedule_dbo::get_by_id_or_off(schedule_id);
}
return new_relay;
}
@ -134,7 +121,7 @@ relay_dbo::update()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "UPDATE relays set number = ?2, name = ?3, schedules_ids = ?4, controller_id = ?5, tag = ?6 WHERE id = ?1;", -1, &stmt, nullptr);
sqlite3_prepare_v2(globals::db, "UPDATE relays set number = ?2, name = ?3, controller_id = ?4 WHERE id = ?1;", -1, &stmt, nullptr);
return relay_db_update_insert(this, stmt);
}
@ -144,7 +131,7 @@ relay_dbo::insert()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, schedules_ids, controller_id, tag) values (?2, ?3, ?4, ?5, ?6);", -1, &stmt, nullptr);
sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, controller_id) values (?2, ?3, ?4);", -1, &stmt, nullptr);
return relay_db_update_insert(this, stmt);
}
@ -187,7 +174,6 @@ relay_dbo::to_json()
relay_json["controller_id"] = controller_id_str;
relay_json["active_schedule"] = this->active_schedule->to_json();
relay_json["schedules"] = schedules_json;
relay_json["tag"] = this->tag;
return relay_json;
}

View file

@ -17,7 +17,6 @@ public:
int number;
uuid_t controller_id;
int active_schedule_id;
char tag[64];
schedule_dbo *active_schedule;
schedule_dbo *schedules[7];

View file

@ -14,7 +14,6 @@ create table controllers
ip VARCHAR(16),
port INTEGER,
relay_count INTEGER,
tag varchar(64),
active BOOLEAN
NOT NULL
);
@ -29,10 +28,7 @@ create table relays
NOT NULL,
controller_id VARCHAR(33)
NOT NULL
REFERENCES CONTROLLERS (ID),
schedules_ids INTEGER
REFERENCES SCHEDULES (ID),
tag vARCHAR(64)
REFERENCES controllers (id)
);
create table schedules
@ -44,8 +40,39 @@ create table schedules
NOT NULL
UNIQUE,
name VARCHAR(128),
periods BLOB,
tag vARCHAR(64)
periods BLOB
);
create table tags
id INTEGER
PRIMARY KEY
AUTOINCREMENT,
tag VARCHAR(128)
NOT NULL
UNIQUE
)
create table junction_tag
(
tag_id INTEGER
NOT NULL
REFERENCES tags (id),
relay_id INTEGER
REFERENCES relays (id),
schedule_id INTEGER
REFERENCES schedules (id)
);
create table junction_relay_schedule
(
weekday SMALLINT
NOT NULL,
relay_id INTEGER
NOT NULL
REFERENCES relays (id),
schedule_id INTEGER
NOT NULL
REFERENCES schedules (id)
);
INSERT INTO schedules (uid, name, periods) VALUES (x'6f666600000000000000000000000000', 'off', x'00');