remove: lmdb
add: sqlite add: new commands
This commit is contained in:
parent
a37bdc4870
commit
9602e6e937
33 changed files with 1403 additions and 752 deletions
src/models
90
src/models/junction_relay_schedule.c
Normal file
90
src/models/junction_relay_schedule.c
Normal file
|
@ -0,0 +1,90 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <models/junction_relay_schedule.h>
|
||||
#include <logger.h>
|
||||
#include <macros.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)
|
||||
{
|
||||
LOGGER_ERR("error inserting data: %s", sqlite3_errmsg(global_database));
|
||||
return false;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int
|
||||
junction_relay_schedule_insert_weekdays(int relay_id, int *schedule_ids)
|
||||
{
|
||||
int rc;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
static const char query_base[] = "INSERT INTO junction_relay_schedule (weekday, schedule_id, relay_id) VALUES";
|
||||
static const char query_extender[] = " (?, ?, ?)";
|
||||
|
||||
size_t query_len = STRLEN(query_base) + (7 * (STRLEN(query_extender) + 1)) + 1;
|
||||
char *query = malloc(sizeof(char) * query_len + 1);
|
||||
strncpy(query, query_base, query_len);
|
||||
query_len -= STRLEN(query_base);
|
||||
for(int i = 0; i < 7; ++i)
|
||||
{
|
||||
strncat(query, query_extender, query_len);
|
||||
query_len -= STRLEN(query_extender);
|
||||
char *query_divider = (i < 7 - 1) ? "," : ";";
|
||||
strncat(query, query_divider, query_len);
|
||||
query_len -= 1;
|
||||
}
|
||||
|
||||
sqlite3_prepare_v2(global_database, query, -1, &stmt, NULL);
|
||||
|
||||
for(int i = 0; i < 7; ++i)
|
||||
{
|
||||
sqlite3_bind_int(stmt, i * 3 + 1, i);
|
||||
sqlite3_bind_int(stmt, i * 3 + 2, schedule_ids[i]);
|
||||
sqlite3_bind_int(stmt, i * 3 + 3, relay_id);
|
||||
}
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE)
|
||||
{
|
||||
LOGGER_ERR("error inserting data: %s", sqlite3_errmsg(global_database));
|
||||
return false;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue