add: functions to insert list (for junctions)
This commit is contained in:
parent
9fb525530f
commit
d23655eb61
7 changed files with 155 additions and 26 deletions
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required (VERSION 3.7)
|
||||
project(core
|
||||
VERSION 0.2.15
|
||||
VERSION 0.2.16
|
||||
LANGUAGES C)
|
||||
|
||||
add_executable(core src/main.c)
|
||||
|
|
|
@ -7,4 +7,7 @@ junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id);
|
|||
int
|
||||
junction_relay_schedule_remove_for_relay(int relay_id);
|
||||
|
||||
int
|
||||
junction_relay_schedule_insert_weekdays(int relay_id, int *schedule_ids);
|
||||
|
||||
#endif /* CORE_MODELS_JUNCTION_RELAY_SCHEDULE_H */
|
||||
|
|
|
@ -16,6 +16,9 @@ junction_tag_get_tags_for_schedule_id(int schedule_id);
|
|||
int
|
||||
junction_tag_insert(int tag_id, int relay_id, int schedule_id);
|
||||
|
||||
int
|
||||
junction_tag_insert_list(int *tag_ids, int relay_id, int schedule_id, int count);
|
||||
|
||||
int
|
||||
junction_tag_remove(int tag_id, int relay_id, int schedule_id);
|
||||
|
||||
|
|
|
@ -226,7 +226,11 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
|||
{
|
||||
LOGGER_DEBUG("cleaning tags");
|
||||
junction_tag_remove_for_relay(relay->id);
|
||||
}
|
||||
|
||||
int json_tags_count = cJSON_GetArraySize(json_tags);
|
||||
int *tag_ids = malloc(sizeof(int) * json_tags_count);
|
||||
|
||||
int i = 0;
|
||||
cJSON_ArrayForEach(json_tag, json_tags)
|
||||
{
|
||||
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
|
||||
|
@ -240,6 +244,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
|||
|
||||
relay_free(relay);
|
||||
cJSON_Delete(json);
|
||||
free(tag_ids);
|
||||
|
||||
static const char content[] = "invalid tag in tags";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
|
@ -252,7 +257,9 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
|||
tag_save(tag_id, tag);
|
||||
tag_id = tag_get_id(tag);
|
||||
}
|
||||
junction_tag_insert(tag_id, relay->id, 0);
|
||||
tag_ids[i++] = tag_id;
|
||||
}
|
||||
junction_tag_insert_list(tag_ids, relay->id, 0, json_tags_count);
|
||||
}
|
||||
|
||||
if(opened_transaction)
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <models/junction_relay_schedule.h>
|
||||
#include <logger.h>
|
||||
#include <macros.h>
|
||||
#include <database.h>
|
||||
|
||||
int
|
||||
|
@ -30,6 +31,49 @@ junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id)
|
|||
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));
|
||||
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)
|
||||
{
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <cache.h>
|
||||
#include <sqlite3.h>
|
||||
#include <logger.h>
|
||||
#include <macros.h>
|
||||
#include <models/junction_tag.h>
|
||||
#include <database.h>
|
||||
|
||||
|
@ -56,6 +57,75 @@ junction_tag_insert(int tag_id, int relay_id, int schedule_id)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
junction_tag_insert_list(int *tag_ids, int relay_id, int schedule_id, int count)
|
||||
{
|
||||
int rc;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
|
||||
static const char query_base[] = "INSERT INTO junction_tag(tag_id, schedule_id, relay_id) VALUES";
|
||||
static const char query_extender[] = " (?, ?, ?)";
|
||||
|
||||
size_t query_len = STRLEN(query_base) + (count * (STRLEN(query_extender) + 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 < count; ++i)
|
||||
{
|
||||
strncat(query, query_extender, query_len);
|
||||
query_len -= STRLEN(query_extender);
|
||||
char *query_divider = (i < count - 1) ? "," : ";";
|
||||
strncat(query, query_divider, query_len);
|
||||
query_len -= 1;
|
||||
}
|
||||
|
||||
sqlite3_prepare_v2(global_database, query, -1, &stmt, NULL);
|
||||
|
||||
for(int i = 0; i < count; ++i)
|
||||
{
|
||||
sqlite3_bind_int(stmt, i * 3 + 1, tag_ids[i]);
|
||||
|
||||
if(schedule_id)
|
||||
{
|
||||
sqlite3_bind_int(stmt, i * 3 + 2, schedule_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlite3_bind_null(stmt, i * 3 + 2);
|
||||
}
|
||||
|
||||
if(relay_id)
|
||||
{
|
||||
sqlite3_bind_int(stmt, i * 3 + 3, relay_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlite3_bind_null(stmt, i * 3 + 3);
|
||||
}
|
||||
}
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE)
|
||||
{
|
||||
printf("ERROR inserting data: %s\n", sqlite3_errmsg(global_database));
|
||||
return 1;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if(relay_id)
|
||||
{
|
||||
cache_invalidate_relay(relay_id);
|
||||
}
|
||||
if(schedule_id)
|
||||
{
|
||||
cache_invalidate_schedule(schedule_id);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int*
|
||||
junction_tag_get_relays_for_tag_id(int tag_id)
|
||||
{
|
||||
|
|
|
@ -167,10 +167,12 @@ relay_save(relay_t *relay)
|
|||
junction_relay_schedule_remove_for_relay(relay->id);
|
||||
|
||||
LOGGER_DEBUG("rebuilding relay_schedule junction\n");
|
||||
int schedule_ids[7];
|
||||
for(int i = 0; i < 7; ++i)
|
||||
{
|
||||
junction_relay_schedule_insert(i, relay->id, relay->schedules[i]->id);
|
||||
schedule_ids[i] = relay->schedules[i]->id;
|
||||
}
|
||||
junction_relay_schedule_insert_weekdays(relay->id, schedule_ids);
|
||||
|
||||
if(opened_transaction)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue