add: macro endpoints

add: basic macro support
fix: database locking with lock-pointer
fix: memory leaks
This commit is contained in:
Tobias Reisinger 2020-09-04 00:28:49 +02:00
parent 6a2b94ef1c
commit 9d2c48d645
30 changed files with 606 additions and 213 deletions

View file

@ -117,7 +117,8 @@ controller_db_select(sqlite3_stmt *stmt)
int
controller_save(controller_t *controller)
{
int opened_transaction = database_transaction_begin();
database_transaction_lock lock;
database_transaction_begin(&lock);
sqlite3_stmt *stmt;
if(controller->id)
@ -142,10 +143,7 @@ controller_save(controller_t *controller)
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
}
if(opened_transaction)
{
database_transaction_rollback();
}
database_transaction_rollback(&lock);
}
else
{
@ -154,10 +152,7 @@ controller_save(controller_t *controller)
controller->id = sqlite3_last_insert_rowid(global_database);
}
if(opened_transaction)
{
database_transaction_commit();
}
database_transaction_commit(&lock);
}
cache_invalidate_controller(controller->id);

View file

@ -1,33 +0,0 @@
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <models/junction_macro.h>
#include <logger.h>
#include <macros.h>
#include <database.h>
int
junction_macro_insert(int macro_id, int relay_id, int schedule_id, uint8_t weekday)
{
int rc;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "INSERT INTO junction_macro(macro_id, schedule_id, relay_id, weekday) values (?1, ?2, ?3);", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, macro_id);
sqlite3_bind_int(stmt, 2, relay_id);
sqlite3_bind_int(stmt, 3, schedule_id);
sqlite3_bind_int(stmt, 4, weekday);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
LOGGER_ERR("error inserting data: %s", sqlite3_errmsg(global_database));
return 1;
}
sqlite3_finalize(stmt);
return 0;
}

View file

@ -62,16 +62,18 @@ junction_relay_schedule_insert_weekdays(int relay_id, int *schedule_ids)
sqlite3_bind_int(stmt, i * 3 + 3, relay_id);
}
free(query);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
LOGGER_ERR("error inserting data: %s", sqlite3_errmsg(global_database));
return false;
return 1;
}
sqlite3_finalize(stmt);
return true;
return 0;
}
int

View file

@ -105,6 +105,8 @@ junction_tag_insert_list(int *tag_ids, int relay_id, int schedule_id, int count)
}
}
free(query);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{

View file

@ -7,7 +7,7 @@
#include <logger.h>
#include <database.h>
#include <models/macro.h>
#include <models/junction_macro.h>
#include <models/macro_action.h>
#include <models/schedule.h>
#include <models/tag.h>
@ -28,6 +28,7 @@ db_update_insert(macro_t *macro, sqlite3_stmt *stmt)
return rc != SQLITE_DONE;
}
static macro_t*
macro_db_select_mapper(sqlite3_stmt *stmt)
{
@ -95,16 +96,17 @@ macro_db_select(sqlite3_stmt *stmt)
int
macro_save(macro_t *macro)
{
int opened_transaction = database_transaction_begin();
database_transaction_lock lock;
database_transaction_begin(&lock);
sqlite3_stmt *stmt;
if(macro->id)
{
sqlite3_prepare_v2(global_database, "UPDATE macros SET uid = ?2, name = ?3, periods = ?4 WHERE id=?1;", -1, &stmt, NULL);
sqlite3_prepare_v2(global_database, "UPDATE macros SET uid = ?2, name = ?3 WHERE id=?1;", -1, &stmt, NULL);
}
else
{
sqlite3_prepare_v2(global_database, "INSERT INTO macros(uid, name, periods) values (?2, ?3, ?4);", -1, &stmt, NULL);
sqlite3_prepare_v2(global_database, "INSERT INTO macros(uid, name) values (?2, ?3);", -1, &stmt, NULL);
}
int result = db_update_insert(macro, stmt);
@ -120,10 +122,7 @@ macro_save(macro_t *macro)
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
}
if(opened_transaction)
{
database_transaction_rollback();
}
database_transaction_rollback(&lock);
}
else
{
@ -132,10 +131,7 @@ macro_save(macro_t *macro)
macro->id = sqlite3_last_insert_rowid(global_database);
}
if(opened_transaction)
{
database_transaction_commit();
}
database_transaction_commit(&lock);
}
cache_invalidate_macro(macro->id);
@ -236,31 +232,42 @@ macro_to_json(macro_t *macro)
}
cJSON_AddItemToObject(json, "id", json_id);
cJSON *json_tags = cJSON_CreateArray();
int *tags_ids = NULL; //junction_tag_get_tags_for_macro_id(macro->id);
if(tags_ids != NULL)
cJSON *json_actions = cJSON_CreateArray();
macro_action_t **macro_actions = macro_action_get_for_macro(macro->id);
for(int i = 0; macro_actions[i] != NULL; ++i)
{
for(int i = 0; tags_ids[i] != 0; ++i)
{
char *tag = tag_get_tag(tags_ids[i]);
if(tag == NULL)
{
continue;
}
cJSON *json_action = cJSON_CreateObject();
cJSON *json_tag = cJSON_CreateString(tag);
if (json_tag == NULL)
{
LOGGER_DEBUG("failed to add tag from string '%s'\n", tag);
free(tag);
continue;
}
cJSON_AddItemToArray(json_tags, json_tag);
free(tag);
cJSON *json_action_weekday = cJSON_CreateNumber(macro_actions[i]->weekday);
if(json_action_weekday == NULL)
{
LOGGER_DEBUG("failed to create weekday from number %d\n", macro_actions[i]->weekday);
continue;
}
free(tags_ids);
relay_t *relay = relay_get_by_id(macro_actions[i]->relay_id);
if(!relay)
{
LOGGER_DEBUG("failed to get relay\n");
continue;
}
schedule_t *schedule = schedule_get_by_id(macro_actions[i]->schedule_id);
if(!schedule)
{
LOGGER_DEBUG("failed to get schedule\n");
relay_free(relay);
continue;
}
cJSON_AddItemToObject(json_action, "weekday", json_action_weekday);
cJSON_AddItemToObject(json_action, "relay", relay_to_json(relay, 0));
cJSON_AddItemToObject(json_action, "schedule", schedule_to_json(schedule));
cJSON_AddItemToArray(json_actions, json_action);
}
cJSON_AddItemToObject(json, "tags", json_tags);
cJSON_AddItemToObject(json, "actions", json_actions);
macro_action_free_list(macro_actions);
char *json_str = cJSON_Print(json);
cache_put_json_macro(macro->id, json_str);
@ -271,31 +278,6 @@ macro_to_json(macro_t *macro)
return json;
}
macro_t*
macro_get_by_id_or_off(int id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT * FROM macros WHERE id = ?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, id);
macro_t **sql_result = macro_db_select(stmt);
macro_t *result = sql_result[0];
free(sql_result);
if(result)
{
return result;
}
uuid_t tmp_uuid;
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "off", 3);
return macro_get_by_uid(tmp_uuid);
}
macro_t*
macro_get_by_id(int id)
{
@ -313,34 +295,6 @@ macro_get_by_id(int id)
return result;
}
macro_t*
macro_get_by_uid_or_off(uuid_t uid)
{
char uuid_str[UUID_STR_LEN];
uuid_unparse(uid, uuid_str);
LOGGER_DEBUG("getting macro [uid=%s] or off from database\n", uuid_str);
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT * FROM macros WHERE uid = ?1;", -1, &stmt, NULL);
sqlite3_bind_blob(stmt, 1, uid, sizeof(uuid_t), SQLITE_STATIC);
macro_t **sql_result = macro_db_select(stmt);
macro_t *result = sql_result[0];
free(sql_result);
if(result)
{
return result;
}
uuid_t tmp_uuid;
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "off", 3);
return macro_get_by_uid(tmp_uuid);
}
macro_t*
macro_get_by_uid(uuid_t uid)
{

139
src/models/macro_action.c Normal file
View file

@ -0,0 +1,139 @@
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <models/macro_action.h>
#include <logger.h>
#include <cache.h>
#include <database.h>
static macro_action_t*
macro_action_db_select_mapper(sqlite3_stmt *stmt)
{
macro_action_t *new_macro_action = malloc(sizeof(macro_action_t));
for(int i = 0; i < sqlite3_column_count(stmt); i++)
{
const char *name = sqlite3_column_name(stmt, i);
switch(name[0])
{
case 'm': // macro_id
new_macro_action->macro_id = sqlite3_column_int(stmt, i);
break;
case 'r': // relay_id
new_macro_action->relay_id = sqlite3_column_int(stmt, i);
break;
case 's': // schedule_id
new_macro_action->schedule_id = sqlite3_column_int(stmt, i);
break;
case 'w': // weekday
new_macro_action->weekday = (uint8_t)sqlite3_column_int(stmt, i);
break;
default: // ignore columns not implemented
break;
}
}
return new_macro_action;
}
static macro_action_t**
macro_action_db_select(sqlite3_stmt *stmt)
{
macro_action_t **all_macro_actions = malloc(sizeof(macro_action_t*));
int row = 0;
for(;;)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
macro_action_t *new_macro_action = macro_action_db_select_mapper(stmt);
row++;
all_macro_actions = (macro_action_t**)realloc(all_macro_actions, sizeof(macro_action_t*) * (row + 1));
all_macro_actions[row - 1] = new_macro_action;
}
else
{
if(s == SQLITE_DONE)
{
break;
}
else
{
LOGGER_ERR("error selecting macro_actions from database: %s\n", sqlite3_errstr(s));
break;
}
}
}
sqlite3_finalize(stmt);
all_macro_actions[row] = NULL;
return all_macro_actions;
}
int
macro_action_insert(macro_action_t *macro_action)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "INSERT INTO macro_actions(macro_id, relay_id, schedule_id, weekday) values (?1, ?2, ?3, ?4);", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, macro_action->macro_id);
sqlite3_bind_int(stmt, 2, macro_action->relay_id);
sqlite3_bind_int(stmt, 3, macro_action->schedule_id);
sqlite3_bind_int(stmt, 4, macro_action->weekday);
int rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
cache_invalidate_macro(macro_action->macro_id);
return rc != SQLITE_DONE;
}
macro_action_t**
macro_action_get_for_macro(int macro_id)
{
LOGGER_DEBUG("getting macro_actions [macro_id=%d] from database\n", macro_id);
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT * FROM macro_actions WHERE macro_id=?", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, macro_id);
return macro_action_db_select(stmt);
}
void
macro_action_free_list(macro_action_t **macro_actions)
{
for(int i = 0; macro_actions[i] != NULL; ++i)
{
free(macro_actions[i]);
}
free(macro_actions);
}
int*
macro_action_get_macro_ids_with_schedule(int schedule_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT macro_id FROM macro_actions WHERE schedule_id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, schedule_id);
return database_helper_get_ids(stmt);
}
int*
macro_action_get_macro_ids_with_relay(int relay_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT macro_id FROM macro_actions WHERE relay_id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, relay_id);
return database_helper_get_ids(stmt);
}

View file

@ -115,7 +115,8 @@ relay_db_select(sqlite3_stmt *stmt)
int
relay_save(relay_t *relay)
{
int opened_transaction = database_transaction_begin();
database_transaction_lock lock;
database_transaction_begin(&lock);
sqlite3_stmt *stmt;
if(relay->id)
@ -140,10 +141,7 @@ relay_save(relay_t *relay)
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
}
if(opened_transaction)
{
database_transaction_rollback();
}
database_transaction_rollback(&lock);
}
else
{
@ -163,10 +161,7 @@ relay_save(relay_t *relay)
}
junction_relay_schedule_insert_weekdays(relay->id, schedule_ids);
if(opened_transaction)
{
database_transaction_commit();
}
database_transaction_commit(&lock);
}
cache_invalidate_relay(relay->id, -1);

View file

@ -112,7 +112,8 @@ schedule_db_select(sqlite3_stmt *stmt)
int
schedule_save(schedule_t *schedule)
{
int opened_transaction = database_transaction_begin();
database_transaction_lock lock;
database_transaction_begin(&lock);
sqlite3_stmt *stmt;
if(schedule->id)
@ -137,10 +138,7 @@ schedule_save(schedule_t *schedule)
LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database));
}
if(opened_transaction)
{
database_transaction_rollback();
}
database_transaction_rollback(&lock);
}
else
{
@ -149,10 +147,7 @@ schedule_save(schedule_t *schedule)
schedule->id = sqlite3_last_insert_rowid(global_database);
}
if(opened_transaction)
{
database_transaction_commit();
}
database_transaction_commit(&lock);
}
cache_invalidate_schedule(schedule->id);