core-legacy/models/junction_relay_schedule_dbo.cc

113 lines
2.8 KiB
C++
Raw Normal View History

#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;
}