fix: database types

This commit is contained in:
Tobias Reisinger 2020-04-19 02:44:35 +02:00
parent beea18f70b
commit b3f75f4004
10 changed files with 76 additions and 55 deletions

View file

@ -14,7 +14,7 @@ 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_blob(stmt, 4, relay->active_schedule_id, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_int(stmt, 4, relay->active_schedule_id);
sqlite3_bind_blob(stmt, 5, relay->controller_id, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_text(stmt, 6, relay->tag, -1, SQLITE_STATIC);
@ -40,8 +40,8 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
const char *name = sqlite3_column_name(stmt, i);
switch(name[0])
{
case 'a': // active_schedule_id
uuid_copy(new_relay->active_schedule_id, (const unsigned char*)sqlite3_column_blob(stmt, i));
case 'a': // active_schedule_dbid
new_relay->active_schedule_id = sqlite3_column_int(stmt, i);
break;
case 'c': // controller_id
uuid_copy(new_relay->controller_id, (const unsigned char*)sqlite3_column_blob(stmt, i));
@ -123,7 +123,7 @@ 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_blob, sizeof(uuid_t));
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", &this->active_schedule_id, (intptr_t)&sqlite3_bind_int, 0);
if(!schedules[0])
{
@ -131,8 +131,11 @@ relay_dbo::reload_active_schedule()
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);
schedules = schedule_dbo::get_by_simple("uid", off_uuid, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
if(schedules[0])
{
this->active_schedule_id = schedules[0]->id;
}
}
this->active_schedule = schedules[0];
@ -180,12 +183,12 @@ 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);
char active_schedule_uid_str[37];
schedule_dbo::unparse_uid(this->active_schedule->uid, active_schedule_uid_str);
Json::Value relay_json;
relay_json["name"] = this->name;
relay_json["number"] = this->number;
relay_json["active_schedule_id"] = active_schedule_id_str;
relay_json["active_schedule_id"] = active_schedule_uid_str;
relay_json["controller_id"] = controller_id_str;
relay_json["active_schedule"] = this->active_schedule->to_json();
relay_json["tag"] = this->tag;
@ -234,7 +237,7 @@ 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,
&relay_num,
(intptr_t)&sqlite3_bind_int,
0,
"AND"

View file

@ -16,7 +16,7 @@ public:
char name[128];
int number;
uuid_t controller_id;
uuid_t active_schedule_id;
int active_schedule_id;
char tag[64];
schedule_dbo *active_schedule;

View file

@ -11,9 +11,10 @@ static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt
uint16_t *periods_blob = schedule->periods->to_blob();
int blob_size = (int)sizeof(uint16_t) * ((periods_blob[0] * 2) + 1);
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);
sqlite3_bind_int(stmt, 1, schedule->id);
sqlite3_bind_blob(stmt, 2, schedule->uid, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, schedule->name, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 4, periods_blob, blob_size, SQLITE_STATIC);
rc = sqlite3_step(stmt);
@ -39,7 +40,7 @@ schedule_db_select_mapper(sqlite3_stmt *stmt)
switch(name[0])
{
case 'i': // id
uuid_copy(new_schedule->id, (const unsigned char*)sqlite3_column_blob(stmt, i));
new_schedule->id = sqlite3_column_int(stmt, i);
break;
case 'n': // name
strncpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), 127);
@ -48,6 +49,9 @@ schedule_db_select_mapper(sqlite3_stmt *stmt)
case 'p': // periods
new_schedule->periods = new period_list((const uint16_t*)sqlite3_column_blob(stmt, i));
break;
case 'u': // uid
uuid_copy(new_schedule->uid, (const unsigned char*)sqlite3_column_blob(stmt, i));
break;
default: // ignore columns not implemented
break;
}
@ -101,7 +105,7 @@ schedule_dbo::update()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "UPDATE schedules SET name = ?2, periods = ?3 WHERE id=?1;", -1, &stmt, nullptr);
sqlite3_prepare_v2(globals::db, "UPDATE schedules SET uid = ?2, name = ?3, periods = ?4 WHERE id=?1;", -1, &stmt, nullptr);
return schedule_db_update_insert(this, stmt);
}
@ -111,7 +115,7 @@ schedule_dbo::insert()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "INSERT INTO schedules(id, name, periods) values (?1, ?2, ?3);", -1, &stmt, nullptr);
sqlite3_prepare_v2(globals::db, "INSERT INTO schedules(uid, name, periods) values (?2, ?3, ?4);", -1, &stmt, nullptr);
return schedule_db_update_insert(this, stmt);
}
@ -123,7 +127,7 @@ schedule_dbo::remove()
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM schedules WHERE id=?1;", -1, &stmt, nullptr);
sqlite3_bind_blob(stmt, 1, this->id, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_int(stmt, 1, this->id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
@ -141,7 +145,7 @@ Json::Value
schedule_dbo::to_json()
{
char id_str[37];
schedule_dbo::unparse_id(this->id, id_str);
schedule_dbo::unparse_uid(this->uid, id_str);
Json::Value schedule_json;
schedule_json["name"] = this->name;
schedule_json["id"] = id_str;
@ -198,22 +202,22 @@ schedule_dbo::free_list(schedule_dbo **schedules_list)
}
int
schedule_dbo::parse_id(const char *id_str, uuid_t result)
schedule_dbo::parse_uid(const char *uid_str, uuid_t result)
{
if(strcmp("off", id_str) == 0)
if(strcmp("off", uid_str) == 0)
{
memset(result, 0, sizeof(uuid_t));
memcpy(result, "off", 3);
return 0;
}
if(strcmp("on", id_str) == 0)
if(strcmp("on", uid_str) == 0)
{
memset(result, 0, sizeof(uuid_t));
memcpy(result, "on", 2);
return 0;
}
if(uuid_parse(id_str, result))
if(uuid_parse(uid_str, result))
{
return 1;
}
@ -221,13 +225,13 @@ schedule_dbo::parse_id(const char *id_str, uuid_t result)
}
void
schedule_dbo::unparse_id(const uuid_t id, char *result)
schedule_dbo::unparse_uid(const uuid_t uid, 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)
if(uuid_compare(uid, tmp_uuid) == 0)
{
strcpy(result, "off");
return;
@ -235,11 +239,11 @@ schedule_dbo::unparse_id(const uuid_t id, char *result)
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "on", 2);
if(uuid_compare(id, tmp_uuid) == 0)
if(uuid_compare(uid, tmp_uuid) == 0)
{
strcpy(result, "on");
return;
}
uuid_unparse(id, result);
uuid_unparse(uid, result);
}

View file

@ -13,7 +13,8 @@ class schedule_dbo
{
public:
uuid_t id;
int id;
uuid_t uid;
char name[128];
period_list *periods;
@ -44,10 +45,10 @@ public:
get_all();
static int
parse_id(const char *id_str, uuid_t result);
parse_uid(const char *uid_str, uuid_t result);
static void
unparse_id(const uuid_t id, char *result);
unparse_uid(const uuid_t uid, char *result);
};