fix: database types
This commit is contained in:
parent
beea18f70b
commit
b3f75f4004
10 changed files with 76 additions and 55 deletions
|
@ -14,7 +14,7 @@ endif()
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -latomic")
|
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -g -latomic")
|
||||||
|
|
||||||
##########
|
##########
|
||||||
# If you include the drogon source code locally in your project, use this method to add drogon
|
# If you include the drogon source code locally in your project, use this method to add drogon
|
||||||
|
|
|
@ -93,7 +93,7 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||||
|
|
||||||
Json::Value body = *req->getJsonObject();
|
Json::Value body = *req->getJsonObject();
|
||||||
uuid_t active_schedule_id;
|
uuid_t active_schedule_id;
|
||||||
if(schedule_dbo::parse_id(body["active_schedule"].asCString(), active_schedule_id))
|
if(schedule_dbo::parse_uid(body["active_schedule"].asCString(), active_schedule_id))
|
||||||
{
|
{
|
||||||
LOG_DEBUG << "bad active_schedule uuid";
|
LOG_DEBUG << "bad active_schedule uuid";
|
||||||
auto resp = HttpResponse::newHttpResponse();
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
|
@ -103,13 +103,17 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num);
|
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num);
|
||||||
|
schedule_dbo **schedule = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
|
||||||
bool db_action_result;
|
bool db_action_result;
|
||||||
|
|
||||||
if(relay)
|
if(relay)
|
||||||
{
|
{
|
||||||
strncpy(relay->name, body["name"].asCString(), 127);
|
strncpy(relay->name, body["name"].asCString(), 127);
|
||||||
uuid_copy(relay->active_schedule_id, active_schedule_id);
|
relay->active_schedule_id = schedule[0]->id;
|
||||||
|
uuid_copy(relay->controller_id, controller_id);
|
||||||
|
|
||||||
|
relay->reload_active_schedule();
|
||||||
|
|
||||||
db_action_result = relay->update();
|
db_action_result = relay->update();
|
||||||
}
|
}
|
||||||
|
@ -119,7 +123,7 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||||
relay->number = relay_num;
|
relay->number = relay_num;
|
||||||
strncpy(relay->name, body["name"].asCString(), 127);
|
strncpy(relay->name, body["name"].asCString(), 127);
|
||||||
|
|
||||||
uuid_copy(relay->active_schedule_id, active_schedule_id);
|
relay->active_schedule_id = schedule[0]->id;
|
||||||
uuid_copy(relay->controller_id, controller_id);
|
uuid_copy(relay->controller_id, controller_id);
|
||||||
|
|
||||||
relay->reload_active_schedule();
|
relay->reload_active_schedule();
|
||||||
|
@ -135,7 +139,7 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto schedules = schedule_dbo::get_by_simple("id", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
auto schedules = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
auto controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
auto controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
|
||||||
uint16_t *periods = schedules[0]->periods->to_blob();
|
uint16_t *periods = schedules[0]->periods->to_blob();
|
||||||
|
@ -144,7 +148,7 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||||
|
|
||||||
binn_map_set_uint8(map, COMMAND_MAPPING_CODE, config::command_code_set_schedule);
|
binn_map_set_uint8(map, COMMAND_MAPPING_CODE, config::command_code_set_schedule);
|
||||||
binn_map_set_uint8(map, COMMAND_MAPPING_RELAY_NUM, relay_num);
|
binn_map_set_uint8(map, COMMAND_MAPPING_RELAY_NUM, relay_num);
|
||||||
binn_map_set_blob(map, COMMAND_MAPPING_SCHEDULE_ID, schedules[0]->id, sizeof(uuid_t));
|
binn_map_set_blob(map, COMMAND_MAPPING_SCHEDULE_ID, schedules[0]->uid, sizeof(uuid_t));
|
||||||
binn_map_set_uint16(map, COMMAND_MAPPING_PERIODS_COUNT, periods_count);
|
binn_map_set_uint16(map, COMMAND_MAPPING_PERIODS_COUNT, periods_count);
|
||||||
|
|
||||||
// periods + 1 to skip length in periods[0]
|
// periods + 1 to skip length in periods[0]
|
||||||
|
|
|
@ -28,7 +28,7 @@ void
|
||||||
schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id_str)
|
schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id_str)
|
||||||
{
|
{
|
||||||
uuid_t schedule_id;
|
uuid_t schedule_id;
|
||||||
if(schedule_dbo::parse_id(schedule_id_str.c_str(), schedule_id))
|
if(schedule_dbo::parse_uid(schedule_id_str.c_str(), schedule_id))
|
||||||
{
|
{
|
||||||
auto resp = HttpResponse::newHttpResponse();
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
resp->setStatusCode(k400BadRequest);
|
resp->setStatusCode(k400BadRequest);
|
||||||
|
@ -36,7 +36,7 @@ schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
|
||||||
if(schedules[0])
|
if(schedules[0])
|
||||||
{
|
{
|
||||||
|
@ -67,14 +67,14 @@ schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const
|
||||||
}
|
}
|
||||||
|
|
||||||
uuid_t schedule_id;
|
uuid_t schedule_id;
|
||||||
if(schedule_dbo::parse_id(schedule_id_str.c_str(), schedule_id))
|
if(schedule_dbo::parse_uid(schedule_id_str.c_str(), schedule_id))
|
||||||
{
|
{
|
||||||
auto resp = HttpResponse::newHttpResponse();
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
resp->setStatusCode(k400BadRequest);
|
resp->setStatusCode(k400BadRequest);
|
||||||
callback(resp);
|
callback(resp);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
|
||||||
if(schedules[0])
|
if(schedules[0])
|
||||||
{
|
{
|
||||||
|
@ -105,8 +105,8 @@ schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResp
|
||||||
|
|
||||||
strncpy(new_schedule.name, body["name"].asCString(), 127);
|
strncpy(new_schedule.name, body["name"].asCString(), 127);
|
||||||
new_schedule.name[127] = '\0';
|
new_schedule.name[127] = '\0';
|
||||||
uuid_generate(new_schedule.id);
|
uuid_generate(new_schedule.uid);
|
||||||
new_schedule.id[32] = '\0';
|
new_schedule.uid[32] = '\0';
|
||||||
new_schedule.periods = helpers::parse_periods(body["periods"]);
|
new_schedule.periods = helpers::parse_periods(body["periods"]);
|
||||||
|
|
||||||
if(!new_schedule.insert())
|
if(!new_schedule.insert())
|
||||||
|
@ -127,14 +127,14 @@ schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
|
||||||
const std::string &schedule_id_str)
|
const std::string &schedule_id_str)
|
||||||
{
|
{
|
||||||
uuid_t schedule_id;
|
uuid_t schedule_id;
|
||||||
if(schedule_dbo::parse_id(schedule_id_str.c_str(), schedule_id))
|
if(schedule_dbo::parse_uid(schedule_id_str.c_str(), schedule_id))
|
||||||
{
|
{
|
||||||
auto resp = HttpResponse::newHttpResponse();
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
resp->setStatusCode(k400BadRequest);
|
resp->setStatusCode(k400BadRequest);
|
||||||
callback(resp);
|
callback(resp);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
|
||||||
Json::Value body = *req->jsonObject();
|
Json::Value body = *req->jsonObject();
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,9 @@ void valid_json::doFilter(const HttpRequestPtr &req,
|
||||||
is_valid &= body["active_schedule"].type() == Json::ValueType::stringValue;
|
is_valid &= body["active_schedule"].type() == Json::ValueType::stringValue;
|
||||||
|
|
||||||
uuid_t active_schedule_id;
|
uuid_t active_schedule_id;
|
||||||
if(schedule_dbo::parse_id(body["active_schedule"].asCString(), active_schedule_id))
|
if(schedule_dbo::parse_uid(body["active_schedule"].asCString(), active_schedule_id))
|
||||||
{
|
{
|
||||||
|
LOG_DEBUG << "parse_uid failed";
|
||||||
auto res = drogon::HttpResponse::newHttpResponse();
|
auto res = drogon::HttpResponse::newHttpResponse();
|
||||||
res->setStatusCode(k400BadRequest);
|
res->setStatusCode(k400BadRequest);
|
||||||
fcb(res);
|
fcb(res);
|
||||||
|
@ -31,7 +32,7 @@ void valid_json::doFilter(const HttpRequestPtr &req,
|
||||||
|
|
||||||
if(is_valid)
|
if(is_valid)
|
||||||
{
|
{
|
||||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
bool schedule_found = schedules[0] != nullptr;
|
bool schedule_found = schedules[0] != nullptr;
|
||||||
schedule_dbo::free_list(schedules);
|
schedule_dbo::free_list(schedules);
|
||||||
if(schedule_found)
|
if(schedule_found)
|
||||||
|
@ -42,6 +43,7 @@ void valid_json::doFilter(const HttpRequestPtr &req,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Check failed
|
//Check failed
|
||||||
|
LOG_DEBUG << "schedule not found";
|
||||||
auto res = drogon::HttpResponse::newHttpResponse();
|
auto res = drogon::HttpResponse::newHttpResponse();
|
||||||
res->setStatusCode(k400BadRequest);
|
res->setStatusCode(k400BadRequest);
|
||||||
fcb(res);
|
fcb(res);
|
||||||
|
|
|
@ -37,7 +37,7 @@ helpers::create_sql_filtered_query(const char *sql, sql_filter_builder **filters
|
||||||
|
|
||||||
if(filter->bind_func == (intptr_t)&sqlite3_bind_int)
|
if(filter->bind_func == (intptr_t)&sqlite3_bind_int)
|
||||||
{
|
{
|
||||||
sqlite3_bind_int(stmt, i + 1, (int)(intptr_t)filter->value);
|
sqlite3_bind_int(stmt, i + 1, *((int*)filter->value));
|
||||||
}
|
}
|
||||||
if(filter->bind_func == (intptr_t)&sqlite3_bind_text)
|
if(filter->bind_func == (intptr_t)&sqlite3_bind_text)
|
||||||
{
|
{
|
||||||
|
|
|
@ -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, 1, relay->id);
|
||||||
sqlite3_bind_int(stmt, 2, relay->number);
|
sqlite3_bind_int(stmt, 2, relay->number);
|
||||||
sqlite3_bind_text(stmt, 3, relay->name, -1, SQLITE_STATIC);
|
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_blob(stmt, 5, relay->controller_id, sizeof(uuid_t), SQLITE_STATIC);
|
||||||
sqlite3_bind_text(stmt, 6, relay->tag, -1, 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);
|
const char *name = sqlite3_column_name(stmt, i);
|
||||||
switch(name[0])
|
switch(name[0])
|
||||||
{
|
{
|
||||||
case 'a': // active_schedule_id
|
case 'a': // active_schedule_dbid
|
||||||
uuid_copy(new_relay->active_schedule_id, (const unsigned char*)sqlite3_column_blob(stmt, i));
|
new_relay->active_schedule_id = sqlite3_column_int(stmt, i);
|
||||||
break;
|
break;
|
||||||
case 'c': // controller_id
|
case 'c': // controller_id
|
||||||
uuid_copy(new_relay->controller_id, (const unsigned char*)sqlite3_column_blob(stmt, i));
|
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
|
void
|
||||||
relay_dbo::reload_active_schedule()
|
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])
|
if(!schedules[0])
|
||||||
{
|
{
|
||||||
|
@ -131,8 +131,11 @@ relay_dbo::reload_active_schedule()
|
||||||
uuid_t off_uuid;
|
uuid_t off_uuid;
|
||||||
memset(off_uuid, 0, sizeof(uuid_t));
|
memset(off_uuid, 0, sizeof(uuid_t));
|
||||||
memcpy(off_uuid, "off", 3);
|
memcpy(off_uuid, "off", 3);
|
||||||
schedules = schedule_dbo::get_by_simple("id", off_uuid, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
schedules = schedule_dbo::get_by_simple("uid", off_uuid, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
uuid_copy(this->active_schedule_id, off_uuid);
|
if(schedules[0])
|
||||||
|
{
|
||||||
|
this->active_schedule_id = schedules[0]->id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this->active_schedule = schedules[0];
|
this->active_schedule = schedules[0];
|
||||||
|
@ -180,12 +183,12 @@ relay_dbo::to_json()
|
||||||
{
|
{
|
||||||
char controller_id_str[37];
|
char controller_id_str[37];
|
||||||
uuid_unparse(this->controller_id, controller_id_str);
|
uuid_unparse(this->controller_id, controller_id_str);
|
||||||
char active_schedule_id_str[37];
|
char active_schedule_uid_str[37];
|
||||||
uuid_unparse(this->active_schedule_id, active_schedule_id_str);
|
schedule_dbo::unparse_uid(this->active_schedule->uid, active_schedule_uid_str);
|
||||||
Json::Value relay_json;
|
Json::Value relay_json;
|
||||||
relay_json["name"] = this->name;
|
relay_json["name"] = this->name;
|
||||||
relay_json["number"] = this->number;
|
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["controller_id"] = controller_id_str;
|
||||||
relay_json["active_schedule"] = this->active_schedule->to_json();
|
relay_json["active_schedule"] = this->active_schedule->to_json();
|
||||||
relay_json["tag"] = this->tag;
|
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 *filters[2];
|
||||||
helpers::sql_filter_builder filter(
|
helpers::sql_filter_builder filter(
|
||||||
"number",
|
"number",
|
||||||
(void*)(intptr_t)relay_num,
|
&relay_num,
|
||||||
(intptr_t)&sqlite3_bind_int,
|
(intptr_t)&sqlite3_bind_int,
|
||||||
0,
|
0,
|
||||||
"AND"
|
"AND"
|
||||||
|
|
|
@ -16,7 +16,7 @@ public:
|
||||||
char name[128];
|
char name[128];
|
||||||
int number;
|
int number;
|
||||||
uuid_t controller_id;
|
uuid_t controller_id;
|
||||||
uuid_t active_schedule_id;
|
int active_schedule_id;
|
||||||
char tag[64];
|
char tag[64];
|
||||||
schedule_dbo *active_schedule;
|
schedule_dbo *active_schedule;
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,10 @@ static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt
|
||||||
uint16_t *periods_blob = schedule->periods->to_blob();
|
uint16_t *periods_blob = schedule->periods->to_blob();
|
||||||
int blob_size = (int)sizeof(uint16_t) * ((periods_blob[0] * 2) + 1);
|
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_int(stmt, 1, schedule->id);
|
||||||
sqlite3_bind_text(stmt, 2, schedule->name, -1, SQLITE_STATIC);
|
sqlite3_bind_blob(stmt, 2, schedule->uid, sizeof(uuid_t), SQLITE_STATIC);
|
||||||
sqlite3_bind_blob(stmt, 3, periods_blob, blob_size, 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);
|
rc = sqlite3_step(stmt);
|
||||||
|
|
||||||
|
@ -39,7 +40,7 @@ schedule_db_select_mapper(sqlite3_stmt *stmt)
|
||||||
switch(name[0])
|
switch(name[0])
|
||||||
{
|
{
|
||||||
case 'i': // id
|
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;
|
break;
|
||||||
case 'n': // name
|
case 'n': // name
|
||||||
strncpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), 127);
|
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
|
case 'p': // periods
|
||||||
new_schedule->periods = new period_list((const uint16_t*)sqlite3_column_blob(stmt, i));
|
new_schedule->periods = new period_list((const uint16_t*)sqlite3_column_blob(stmt, i));
|
||||||
break;
|
break;
|
||||||
|
case 'u': // uid
|
||||||
|
uuid_copy(new_schedule->uid, (const unsigned char*)sqlite3_column_blob(stmt, i));
|
||||||
|
break;
|
||||||
default: // ignore columns not implemented
|
default: // ignore columns not implemented
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -101,7 +105,7 @@ schedule_dbo::update()
|
||||||
{
|
{
|
||||||
sqlite3_stmt *stmt;
|
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);
|
return schedule_db_update_insert(this, stmt);
|
||||||
}
|
}
|
||||||
|
@ -111,7 +115,7 @@ schedule_dbo::insert()
|
||||||
{
|
{
|
||||||
sqlite3_stmt *stmt;
|
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);
|
return schedule_db_update_insert(this, stmt);
|
||||||
}
|
}
|
||||||
|
@ -123,7 +127,7 @@ schedule_dbo::remove()
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
sqlite3_prepare_v2(globals::db, "DELETE FROM schedules WHERE id=?1;", -1, &stmt, nullptr);
|
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);
|
rc = sqlite3_step(stmt);
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
|
@ -141,7 +145,7 @@ Json::Value
|
||||||
schedule_dbo::to_json()
|
schedule_dbo::to_json()
|
||||||
{
|
{
|
||||||
char id_str[37];
|
char id_str[37];
|
||||||
schedule_dbo::unparse_id(this->id, id_str);
|
schedule_dbo::unparse_uid(this->uid, id_str);
|
||||||
Json::Value schedule_json;
|
Json::Value schedule_json;
|
||||||
schedule_json["name"] = this->name;
|
schedule_json["name"] = this->name;
|
||||||
schedule_json["id"] = id_str;
|
schedule_json["id"] = id_str;
|
||||||
|
@ -198,22 +202,22 @@ schedule_dbo::free_list(schedule_dbo **schedules_list)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
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));
|
memset(result, 0, sizeof(uuid_t));
|
||||||
memcpy(result, "off", 3);
|
memcpy(result, "off", 3);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(strcmp("on", id_str) == 0)
|
if(strcmp("on", uid_str) == 0)
|
||||||
{
|
{
|
||||||
memset(result, 0, sizeof(uuid_t));
|
memset(result, 0, sizeof(uuid_t));
|
||||||
memcpy(result, "on", 2);
|
memcpy(result, "on", 2);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(uuid_parse(id_str, result))
|
if(uuid_parse(uid_str, result))
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -221,13 +225,13 @@ schedule_dbo::parse_id(const char *id_str, uuid_t result)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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;
|
uuid_t tmp_uuid;
|
||||||
|
|
||||||
memset(tmp_uuid, 0, sizeof(uuid_t));
|
memset(tmp_uuid, 0, sizeof(uuid_t));
|
||||||
memcpy(tmp_uuid, "off", 3);
|
memcpy(tmp_uuid, "off", 3);
|
||||||
if(uuid_compare(id, tmp_uuid) == 0)
|
if(uuid_compare(uid, tmp_uuid) == 0)
|
||||||
{
|
{
|
||||||
strcpy(result, "off");
|
strcpy(result, "off");
|
||||||
return;
|
return;
|
||||||
|
@ -235,11 +239,11 @@ schedule_dbo::unparse_id(const uuid_t id, char *result)
|
||||||
|
|
||||||
memset(tmp_uuid, 0, sizeof(uuid_t));
|
memset(tmp_uuid, 0, sizeof(uuid_t));
|
||||||
memcpy(tmp_uuid, "on", 2);
|
memcpy(tmp_uuid, "on", 2);
|
||||||
if(uuid_compare(id, tmp_uuid) == 0)
|
if(uuid_compare(uid, tmp_uuid) == 0)
|
||||||
{
|
{
|
||||||
strcpy(result, "on");
|
strcpy(result, "on");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uuid_unparse(id, result);
|
uuid_unparse(uid, result);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,8 @@ class schedule_dbo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
uuid_t id;
|
int id;
|
||||||
|
uuid_t uid;
|
||||||
char name[128];
|
char name[128];
|
||||||
period_list *periods;
|
period_list *periods;
|
||||||
|
|
||||||
|
@ -44,10 +45,10 @@ public:
|
||||||
get_all();
|
get_all();
|
||||||
|
|
||||||
static int
|
static int
|
||||||
parse_id(const char *id_str, uuid_t result);
|
parse_uid(const char *uid_str, uuid_t result);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
unparse_id(const uuid_t id, char *result);
|
unparse_uid(const uuid_t uid, char *result);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,27 +18,34 @@ create table controllers
|
||||||
|
|
||||||
create table relays
|
create table relays
|
||||||
(
|
(
|
||||||
id INTEGER not null
|
id INTEGER
|
||||||
|
not null
|
||||||
primary key
|
primary key
|
||||||
unique,
|
unique,
|
||||||
name VARCHAR(128),
|
name VARCHAR(128),
|
||||||
number INTEGER not null,
|
number INTEGER
|
||||||
controller_id VARCHAR(33) not null
|
not null,
|
||||||
|
controller_id VARCHAR(33)
|
||||||
|
not null
|
||||||
references controllers (id),
|
references controllers (id),
|
||||||
active_schedule_id VARCHAR(33)
|
active_schedule_id int
|
||||||
references schedules,
|
references schedules (id),
|
||||||
tag vARCHAR(64)
|
tag vARCHAR(64)
|
||||||
);
|
);
|
||||||
|
|
||||||
create table schedules
|
create table schedules
|
||||||
(
|
(
|
||||||
id VARCHAR(33) not null
|
id INTEGER
|
||||||
|
not null
|
||||||
primary key
|
primary key
|
||||||
unique,
|
unique,
|
||||||
|
uid VARCHAR(33)
|
||||||
|
not null
|
||||||
|
unique,
|
||||||
name VARCHAR(128),
|
name VARCHAR(128),
|
||||||
periods BLOB,
|
periods BLOB,
|
||||||
tag vARCHAR(64)
|
tag vARCHAR(64)
|
||||||
);
|
);
|
||||||
|
|
||||||
INSERT INTO schedules (id, name, periods) VALUES (x'6f666600000000000000000000000000', 'off', x'00');
|
INSERT INTO schedules (uid, name, periods) VALUES (x'6f666600000000000000000000000000', 'off', x'00');
|
||||||
INSERT INTO schedules (id, name, periods) VALUES (x'6f6e0000000000000000000000000000', 'on', x'010000009F05');
|
INSERT INTO schedules (uid, name, periods) VALUES (x'6f6e0000000000000000000000000000', 'on', x'010000009F05');
|
||||||
|
|
Loading…
Reference in a new issue