2020-05-05 09:42:02 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include <models/junction_relay_schedule.h>
|
|
|
|
#include <logger.h>
|
|
|
|
#include <database.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
sqlite3_prepare_v2(global_database, "INSERT INTO junction_relay_schedule(weekday, schedule_id, relay_id) values (?1, ?2, ?3);", -1, &stmt, NULL);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
LOG_ERROR("error inserting data: %s", sqlite3_errmsg(global_database));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:50:25 +00:00
|
|
|
static int*
|
|
|
|
get_ids(sqlite3_stmt *stmt)
|
2020-05-05 09:42:02 +00:00
|
|
|
{
|
2020-05-11 12:50:25 +00:00
|
|
|
int *ids = malloc(sizeof(int));
|
|
|
|
int new_id;
|
2020-05-05 09:42:02 +00:00
|
|
|
|
2020-05-11 12:50:25 +00:00
|
|
|
int row = 0;
|
2020-05-05 09:42:02 +00:00
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = sqlite3_step(stmt);
|
|
|
|
if (s == SQLITE_ROW)
|
|
|
|
{
|
2020-05-11 12:50:25 +00:00
|
|
|
new_id = sqlite3_column_int(stmt, 0);
|
|
|
|
row++;
|
|
|
|
|
|
|
|
ids = (int*)realloc(ids, sizeof(int) * (row + 1));
|
|
|
|
ids[row - 1] = new_id;
|
2020-05-05 09:42:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (s == SQLITE_DONE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-11 12:50:25 +00:00
|
|
|
LOG_ERROR("error selecting junction ids from database: %s\n", sqlite3_errstr(s));
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
return NULL;
|
2020-05-05 09:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_finalize(stmt);
|
2020-05-11 12:50:25 +00:00
|
|
|
ids[row] = 0;
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
sqlite3_prepare_v2(global_database, "SELECT schedule_id FROM junction_relay_schedule WHERE weekday=?1 AND relay_id=?2 LIMIT 1;", -1, &stmt, NULL);
|
|
|
|
sqlite3_bind_int(stmt, 1, weekday);
|
|
|
|
sqlite3_bind_int(stmt, 2, relay_id);
|
|
|
|
|
|
|
|
int *id_list = get_ids(stmt);
|
|
|
|
int result = id_list[0];
|
2020-05-05 09:42:02 +00:00
|
|
|
|
2020-05-11 12:50:25 +00:00
|
|
|
free(id_list);
|
2020-05-05 09:42:02 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:50:25 +00:00
|
|
|
int*
|
|
|
|
junction_relay_schedule_get_relays_ids(int schedule_id)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
sqlite3_prepare_v2(global_database, "SELECT relay_id FROM junction_relay_schedule WHERE schedule_id=?1;", -1, &stmt, NULL);
|
|
|
|
sqlite3_bind_int(stmt, 1, schedule_id);
|
|
|
|
|
|
|
|
return get_ids(stmt);
|
|
|
|
}
|
|
|
|
|
2020-05-05 09:42:02 +00:00
|
|
|
int
|
|
|
|
junction_relay_schedule_remove(uint8_t weekday, int relay_id, int schedule_id)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
sqlite3_prepare_v2(global_database, "DELETE FROM junction_relay_schedule WHERE weekday=?1 AND schedule_id=?2 AND relay_id=?3;", -1, &stmt, NULL);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
junction_relay_schedule_remove_for_relay(int relay_id)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
sqlite3_prepare_v2(global_database, "DELETE FROM junction_relay_schedule WHERE relay_id=?1;", -1, &stmt, NULL);
|
|
|
|
sqlite3_bind_int(stmt, 1, relay_id);
|
|
|
|
rc = sqlite3_step(stmt);
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
return rc == SQLITE_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
junction_relay_schedule_remove_for_schedule(int schedule_id)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
sqlite3_prepare_v2(global_database, "DELETE FROM junction_relay_schedule WHERE schedule_id=?1;", -1, &stmt, NULL);
|
|
|
|
sqlite3_bind_int(stmt, 1, schedule_id);
|
|
|
|
rc = sqlite3_step(stmt);
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
return rc == SQLITE_DONE;
|
|
|
|
}
|
|
|
|
|