add: functions to insert list (for junctions)

This commit is contained in:
Tobias Reisinger 2020-08-18 13:35:15 +02:00
parent 9fb525530f
commit d23655eb61
7 changed files with 155 additions and 26 deletions

View file

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7) cmake_minimum_required (VERSION 3.7)
project(core project(core
VERSION 0.2.15 VERSION 0.2.16
LANGUAGES C) LANGUAGES C)
add_executable(core src/main.c) add_executable(core src/main.c)

View file

@ -7,4 +7,7 @@ junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id);
int int
junction_relay_schedule_remove_for_relay(int relay_id); 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 */ #endif /* CORE_MODELS_JUNCTION_RELAY_SCHEDULE_H */

View file

@ -16,6 +16,9 @@ junction_tag_get_tags_for_schedule_id(int schedule_id);
int int
junction_tag_insert(int tag_id, int relay_id, int schedule_id); 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 int
junction_tag_remove(int tag_id, int relay_id, int schedule_id); junction_tag_remove(int tag_id, int relay_id, int schedule_id);

View file

@ -226,33 +226,40 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
{ {
LOGGER_DEBUG("cleaning tags"); LOGGER_DEBUG("cleaning tags");
junction_tag_remove_for_relay(relay->id); junction_tag_remove_for_relay(relay->id);
}
cJSON_ArrayForEach(json_tag, json_tags)
{
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
{
LOGGER_DEBUG("invalid tag in tags\n");
if(opened_transaction) 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))
{ {
database_transaction_rollback(); LOGGER_DEBUG("invalid tag in tags\n");
if(opened_transaction)
{
database_transaction_rollback();
}
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));
return;
} }
const char *tag = json_tag->valuestring;
relay_free(relay); int tag_id = tag_get_id(tag);
cJSON_Delete(json); if(tag_id == 0)
{
static const char content[] = "invalid tag in tags"; tag_save(tag_id, tag);
endpoint_response_text(response, 400, content, STRLEN(content)); tag_id = tag_get_id(tag);
return; }
tag_ids[i++] = tag_id;
} }
const char *tag = json_tag->valuestring; junction_tag_insert_list(tag_ids, relay->id, 0, json_tags_count);
int tag_id = tag_get_id(tag);
if(tag_id == 0)
{
tag_save(tag_id, tag);
tag_id = tag_get_id(tag);
}
junction_tag_insert(tag_id, relay->id, 0);
} }
if(opened_transaction) if(opened_transaction)

View file

@ -4,6 +4,7 @@
#include <models/junction_relay_schedule.h> #include <models/junction_relay_schedule.h>
#include <logger.h> #include <logger.h>
#include <macros.h>
#include <database.h> #include <database.h>
int int
@ -30,6 +31,49 @@ junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id)
return true; 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 int
junction_relay_schedule_remove_for_relay(int relay_id) junction_relay_schedule_remove_for_relay(int relay_id)
{ {

View file

@ -4,6 +4,7 @@
#include <cache.h> #include <cache.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <logger.h> #include <logger.h>
#include <macros.h>
#include <models/junction_tag.h> #include <models/junction_tag.h>
#include <database.h> #include <database.h>
@ -13,7 +14,7 @@ junction_tag_insert(int tag_id, int relay_id, int schedule_id)
int rc; int rc;
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "INSERT INTO junction_tag(tag_id, schedule_id, relay_id) values (?1, ?2, ?3);", -1, &stmt, NULL); sqlite3_prepare_v2(global_database, "INSERT INTO junction_tag (tag_id, schedule_id, relay_id) values (?1, ?2, ?3);", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, tag_id); sqlite3_bind_int(stmt, 1, tag_id);
@ -56,6 +57,75 @@ junction_tag_insert(int tag_id, int relay_id, int schedule_id)
return 0; 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* int*
junction_tag_get_relays_for_tag_id(int tag_id) junction_tag_get_relays_for_tag_id(int tag_id)
{ {

View file

@ -167,10 +167,12 @@ relay_save(relay_t *relay)
junction_relay_schedule_remove_for_relay(relay->id); junction_relay_schedule_remove_for_relay(relay->id);
LOGGER_DEBUG("rebuilding relay_schedule junction\n"); LOGGER_DEBUG("rebuilding relay_schedule junction\n");
int schedule_ids[7];
for(int i = 0; i < 7; ++i) 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) if(opened_transaction)
{ {