fix: save id as blob, not as string

add: some progress to new discovery
add: some tests
remove: migrations. restarting for now
This commit is contained in:
Tobias Reisinger 2020-02-23 20:06:14 +01:00
parent b4eec336a2
commit 51ab1d7982
22 changed files with 345 additions and 121 deletions

View file

@ -3,6 +3,7 @@
#include <trantor/utils/Logger.h>
#include <sys/socket.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include <helpers.h>
#include "controller_dbo.h"
#include "globals.h"
@ -19,7 +20,7 @@ static bool controller_db_update_insert(controller_dbo *controller, sqlite3_stmt
{
int rc;
sqlite3_bind_text(stmt, 1, controller->id, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 1, controller->id, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, controller->name, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, controller->ip, -1, SQLITE_STATIC);
sqlite3_bind_int(stmt, 4, controller->active);
@ -56,7 +57,7 @@ controller_db_select_mapper(sqlite3_stmt *stmt)
switch(name[1])
{
case 'd': // id
strncpy(new_controller->id, (const char*)sqlite3_column_text(stmt, i), 32);
uuid_copy(new_controller->id, (const unsigned char*)sqlite3_column_blob(stmt, i));
break;
case 'p': // ip
strncpy(new_controller->ip, (const char*)sqlite3_column_text(stmt, i), 16);
@ -104,7 +105,7 @@ controller_db_select(sqlite3_stmt *stmt)
if (s == SQLITE_ROW)
{
controller_dbo *new_controller = controller_db_select_mapper(stmt);
new_controller->relays = relay_dbo::get_by_simple("controller_id", new_controller->id, (intptr_t)&sqlite3_bind_text);
new_controller->relays = relay_dbo::get_by_simple("controller_id", new_controller->id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
row++;
all_controllers = (controller_dbo**)realloc(all_controllers, sizeof(controller_dbo*) * (row + 1));
@ -158,7 +159,7 @@ controller_dbo::remove()
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM controllers WHERE id=?1;", -1, &stmt, nullptr);
sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 1, this->id, sizeof(uuid_t), SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
@ -169,9 +170,11 @@ controller_dbo::remove()
Json::Value
controller_dbo::to_json()
{
char id_str[37];
uuid_unparse(this->id, id_str);
Json::Value controller_json;
controller_json["name"] = this->name;
controller_json["id"] = this->id;
controller_json["id"] = id_str;
controller_json["ip"] = this->ip;
controller_json["relay_count"] = this->relay_count;
controller_json["active"] = this->active;
@ -199,7 +202,7 @@ controller_dbo::get_all()
}
controller_dbo**
controller_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func)
controller_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param)
{
helpers::sql_filter_builder *filters[1];
helpers::sql_filter_builder filter
@ -207,6 +210,7 @@ controller_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_
key,
value,
bind_func,
bind_func_param,
";"
};
filters[0] = &filter;

View file

@ -2,6 +2,7 @@
#define EMGAUWA_CORE_controller_DBO_H
#include <string>
#include <uuid/uuid.h>
#include <sqlite3.h>
#include <json/value.h>
#include <helpers.h>
@ -11,7 +12,7 @@ class controller_dbo
{
public:
char id[33];
uuid_t id;
char name[128];
char ip[17];
bool active;
@ -35,7 +36,7 @@ public:
to_json();
static controller_dbo**
get_by_simple(const char *key, const void *value, intptr_t bind_func);
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
static controller_dbo**
get_by(helpers::sql_filter_builder **filters);

View file

@ -14,8 +14,8 @@ static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt)
sqlite3_bind_int(stmt, 1, relay->id);
sqlite3_bind_int(stmt, 2, relay->number);
sqlite3_bind_text(stmt, 3, relay->name, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 4, relay->active_schedule_id, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 5, relay->controller_id, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 4, relay->active_schedule_id, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_blob(stmt, 5, relay->controller_id, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_text(stmt, 6, relay->tag, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
@ -41,10 +41,10 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
switch(name[0])
{
case 'a': // active_schedule_id
strncpy(new_relay->active_schedule_id, (const char*)sqlite3_column_text(stmt, i), 32);
uuid_copy(new_relay->active_schedule_id, (const unsigned char*)sqlite3_column_blob(stmt, i));
break;
case 'c': // controller_id
strncpy(new_relay->controller_id, (const char*)sqlite3_column_text(stmt, i), 32);
uuid_copy(new_relay->controller_id, (const unsigned char*)sqlite3_column_blob(stmt, i));
break;
case 'i':
new_relay->id = sqlite3_column_int(stmt, i);
@ -123,13 +123,16 @@ relay_db_select(sqlite3_stmt *stmt)
void
relay_dbo::reload_active_schedule()
{
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", this->active_schedule_id, (intptr_t)&sqlite3_bind_text);
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", this->active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
if(!schedules[0])
{
free(schedules);
schedules = schedule_dbo::get_by_simple("id", "off", (intptr_t)&sqlite3_bind_text);
strcpy(this->active_schedule_id, "off");
uuid_t off_uuid;
memset(off_uuid, 0, sizeof(uuid_t));
memcpy(off_uuid, "off", 3);
schedules = schedule_dbo::get_by_simple("id", off_uuid, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
uuid_copy(this->active_schedule_id, off_uuid);
}
this->active_schedule = schedules[0];
@ -175,11 +178,15 @@ relay_dbo::remove()
Json::Value
relay_dbo::to_json()
{
char controller_id_str[37];
uuid_unparse(this->controller_id, controller_id_str);
char active_schedule_id_str[37];
uuid_unparse(this->active_schedule_id, active_schedule_id_str);
Json::Value relay_json;
relay_json["name"] = this->name;
relay_json["number"] = this->number;
relay_json["active_schedule_id"] = this->active_schedule_id;
relay_json["controller_id"] = this->controller_id;
relay_json["active_schedule_id"] = active_schedule_id_str;
relay_json["controller_id"] = controller_id_str;
relay_json["active_schedule"] = this->active_schedule->to_json();
relay_json["tag"] = this->tag;
@ -197,7 +204,7 @@ relay_dbo::get_all()
}
relay_dbo**
relay_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func)
relay_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param)
{
helpers::sql_filter_builder *filters[1];
helpers::sql_filter_builder filter
@ -205,6 +212,7 @@ relay_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func)
key,
value,
bind_func,
bind_func_param,
";"
};
filters[0] = &filter;
@ -221,19 +229,21 @@ relay_dbo::get_by(helpers::sql_filter_builder **filters)
}
relay_dbo*
relay_dbo::get_relay_for_controller(const char *controller_id, int relay_num)
relay_dbo::get_relay_for_controller(uuid_t controller_id, int relay_num)
{
helpers::sql_filter_builder *filters[2];
helpers::sql_filter_builder filter(
"number",
(void*)(intptr_t)relay_num,
(intptr_t)&sqlite3_bind_int,
0,
"AND"
);
helpers::sql_filter_builder filter2(
"controller_id",
(void*)controller_id,
(intptr_t)sqlite3_bind_text,
(intptr_t)sqlite3_bind_blob,
sizeof(uuid_t),
";"
);
filters[0] = &filter;
@ -247,9 +257,9 @@ relay_dbo::get_relay_for_controller(const char *controller_id, int relay_num)
}
bool
relay_dbo::valid_num_for_controller(const char *search_controller_id, int relay_num)
relay_dbo::valid_num_for_controller(uuid_t search_controller_id, int relay_num)
{
controller_dbo **controllers = controller_dbo::get_by_simple("id", search_controller_id, (intptr_t)&sqlite3_bind_text);
controller_dbo **controllers = controller_dbo::get_by_simple("id", search_controller_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
bool valid_id_and_num = controllers[0] && controllers[0]->relay_count > relay_num;
controller_dbo::free_list(controllers);

View file

@ -4,6 +4,7 @@
#include <string>
#include <sqlite3.h>
#include <json/value.h>
#include <uuid/uuid.h>
#include <helpers.h>
#include "schedule_dbo.h"
@ -14,8 +15,8 @@ public:
int id;
char name[128];
int number;
char controller_id[33];
char active_schedule_id[33];
uuid_t controller_id;
uuid_t active_schedule_id;
char tag[64];
schedule_dbo *active_schedule;
@ -38,16 +39,16 @@ public:
free_list(relay_dbo **relays_list);
static relay_dbo**
get_by_simple(const char *key, const void *value, intptr_t bind_func);
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
static relay_dbo**
get_by(helpers::sql_filter_builder **filters);
static relay_dbo*
get_relay_for_controller(const char *controller_id, int relay_num);
get_relay_for_controller(uuid_t controller_id, int relay_num);
static bool
valid_num_for_controller(const char *search_controller_id, int relay_num);
valid_num_for_controller(uuid_t search_controller_id, int relay_num);
static relay_dbo**
get_all();

View file

@ -11,7 +11,7 @@ static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt
uint16_t *periods_blob = schedule->periods->to_db_blob();
int blob_size = (int)sizeof(uint16_t) * ((periods_blob[0] * 2) + 1);
sqlite3_bind_text(stmt, 1, schedule->id, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 1, schedule->id, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, schedule->name, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 3, periods_blob, blob_size, SQLITE_STATIC);
@ -39,8 +39,7 @@ schedule_db_select_mapper(sqlite3_stmt *stmt)
switch(name[0])
{
case 'i': // id
strncpy(new_schedule->id, (const char*)sqlite3_column_text(stmt, i), 32);
new_schedule->id[32] = '\0';
uuid_copy(new_schedule->id, (const unsigned char*)sqlite3_column_blob(stmt, i));
break;
case 'n': // name
strncpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), 127);
@ -124,7 +123,7 @@ schedule_dbo::remove()
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM schedules WHERE id=?1;", -1, &stmt, nullptr);
sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 1, this->id, sizeof(uuid_t), SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
@ -141,9 +140,11 @@ schedule_dbo::~schedule_dbo()
Json::Value
schedule_dbo::to_json()
{
char id_str[37];
schedule_dbo::unparse_id(this->id, id_str);
Json::Value schedule_json;
schedule_json["name"] = this->name;
schedule_json["id"] = this->id;
schedule_json["id"] = id_str;
schedule_json["periods"] = this->periods->to_json();
return schedule_json;
@ -160,7 +161,7 @@ schedule_dbo::get_all()
}
schedule_dbo**
schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func)
schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param)
{
helpers::sql_filter_builder *filters[1];
helpers::sql_filter_builder filter
@ -168,6 +169,7 @@ schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_fu
key,
value,
bind_func,
bind_func_param,
";"
};
filters[0] = &filter;
@ -195,3 +197,49 @@ schedule_dbo::free_list(schedule_dbo **schedules_list)
free(schedules_list);
}
int
schedule_dbo::parse_id(const char *id_str, uuid_t result)
{
if(strcmp("off", id_str) == 0)
{
memset(result, 0, sizeof(uuid_t));
memcpy(result, "off", 3);
return 0;
}
if(strcmp("on", id_str) == 0)
{
memset(result, 0, sizeof(uuid_t));
memcpy(result, "on", 2);
return 0;
}
if(uuid_parse(id_str, result))
{
return 1;
}
return 0;
}
void
schedule_dbo::unparse_id(const uuid_t id, char *result)
{
uuid_t tmp_uuid;
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "off", 3);
if(uuid_compare(id, tmp_uuid) == 0)
{
strcpy(result, "off");
return;
}
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "on", 2);
if(uuid_compare(id, tmp_uuid) == 0)
{
strcpy(result, "on");
return;
}
uuid_unparse(id, result);
}

View file

@ -3,6 +3,7 @@
#include <string>
#include <sqlite3.h>
#include <uuid/uuid.h>
#include <json/value.h>
#include <helpers.h>
#include "period.h"
@ -12,7 +13,7 @@ class schedule_dbo
{
public:
char id[33];
uuid_t id;
char name[128];
period_list *periods;
@ -34,13 +35,19 @@ public:
free_list(schedule_dbo **schedules_list);
static schedule_dbo**
get_by_simple(const char *key, const void *value, intptr_t bind_func);
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
static schedule_dbo**
get_by(helpers::sql_filter_builder **filters);
static schedule_dbo**
get_all();
static int
parse_id(const char *id_str, uuid_t result);
static void
unparse_id(const uuid_t id, char *result);
};