#include #include #include #include #include "relay_dbo.h" #include "globals.h" #include "junction_relay_schedule_dbo.h" #include "junction_tag_dbo.h" #include "tag_dbo.h" #include "controller_dbo.h" #include "schedule_dbo.h" static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt) { int rc; junction_relay_schedule_dbo::remove_for_relay(relay->id); for(int i = 0; i < 7; ++i) { junction_relay_schedule_dbo::insert(i, relay->id, relay->schedules[i]->id); } 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->controller_id, sizeof(uuid_t), SQLITE_STATIC); rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) { printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db)); return false; } sqlite3_finalize(stmt); return true; } static relay_dbo* relay_db_select_mapper(sqlite3_stmt *stmt) { auto *new_relay = new relay_dbo(); for(int i = 0; i < sqlite3_column_count(stmt); i++) { const char *name = sqlite3_column_name(stmt, i); switch(name[0]) { case 'c': // controller_id 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); break; case 'n': switch(name[1]) { case 'a': // name strncpy(new_relay->name, (const char*)sqlite3_column_text(stmt, i), 127); break; case 'u': // number new_relay->number = sqlite3_column_int(stmt, i); break; default: break; } break; default: // ignore columns not implemented break; } } for(int i = 0; i < 7; ++i) { int schedule_id = junction_relay_schedule_dbo::get_schedule_id(i, new_relay->id); new_relay->schedules[i] = schedule_dbo::get_by_id_or_off(schedule_id); } return new_relay; } static relay_dbo** relay_db_select(sqlite3_stmt *stmt) { auto **all_relays = (relay_dbo**)malloc(sizeof(relay_dbo*)); int row = 0; while(true) { int s; s = sqlite3_step(stmt); if (s == SQLITE_ROW) { relay_dbo *new_relay = relay_db_select_mapper(stmt); row++; all_relays = (relay_dbo**)realloc(all_relays, sizeof(relay_dbo*) * (row + 1)); all_relays[row - 1] = new_relay; } else { if (s == SQLITE_DONE) { break; } else { LOG_ERROR << "Error Selecting relays from database: " << sqlite3_errstr(s); break; } } } sqlite3_finalize(stmt); all_relays[row] = nullptr; return all_relays; } bool relay_dbo::save() { sqlite3_stmt *stmt; if(this->id) { sqlite3_prepare_v2(globals::db, "UPDATE relays set number = ?2, name = ?3, controller_id = ?4 WHERE id = ?1;", -1, &stmt, nullptr); } else { sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, controller_id) values (?2, ?3, ?4);", -1, &stmt, nullptr); } return relay_db_update_insert(this, stmt); } bool relay_dbo::remove() { sqlite3_stmt *stmt; int rc; sqlite3_prepare_v2(globals::db, "DELETE FROM relays WHERE id=?1;", -1, &stmt, nullptr); sqlite3_bind_int(stmt, 1, this->id); rc = sqlite3_step(stmt); sqlite3_finalize(stmt); return rc == SQLITE_DONE; } Json::Value relay_dbo::to_json() { this->active_schedule = this->schedules[helpers::get_day_of_week()]; char controller_id_str[37]; uuid_unparse(this->controller_id, controller_id_str); char active_schedule_uid_str[37]; schedule_dbo::unparse_uid(this->active_schedule->uid, active_schedule_uid_str); Json::Value schedules_json(Json::arrayValue); for(int i = 0; i < 7; ++i) { schedules_json.append(this->schedules[i]->to_json()); } Json::Value relay_json; relay_json["name"] = this->name; relay_json["number"] = this->number; 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["schedules"] = schedules_json; Json::Value tags_json(Json::arrayValue); int *tags_ids = junction_tag_dbo::get_tags_for_relay_id(this->id); if(tags_ids != nullptr) { int tags_count; for(tags_count = 0; tags_ids[tags_count] != 0; ++tags_count); char **tags = (char**)malloc(sizeof(char*) * tags_count); for(int i = 0; i < tags_count; ++i) { tags[i] = tag_dbo::get_tag(tags_ids[i]); tags_json[i] = tags[i]; } } relay_json["tags"] = tags_json; return relay_json; } relay_dbo** relay_dbo::get_all() { sqlite3_stmt *stmt; sqlite3_prepare_v2(globals::db, "SELECT * FROM relays;", -1, &stmt, nullptr); return relay_db_select(stmt); } relay_dbo** 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 { key, value, bind_func, bind_func_param, ";" }; filters[0] = &filter; sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM relays WHERE", filters); return relay_db_select(stmt); } relay_dbo** relay_dbo::get_by(helpers::sql_filter_builder **filters) { sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM relays WHERE", filters); return relay_db_select(stmt); } relay_dbo* relay_dbo::get_by_id(int id) { relay_dbo **relays = relay_dbo::get_by_simple("id", &id, (intptr_t)&sqlite3_bind_int, 0); relay_dbo *result = relays[0]; free(relays); return result; } relay_dbo* 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", &relay_num, (intptr_t)&sqlite3_bind_int, 0, "AND" ); helpers::sql_filter_builder filter2( "controller_id", (void*)controller_id, (intptr_t)sqlite3_bind_blob, sizeof(uuid_t), ";" ); filters[0] = &filter; filters[1] = &filter2; auto relays = relay_dbo::get_by(filters); relay_dbo *relay = relays[0]; free(relays); return relay; } bool 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_blob, sizeof(uuid_t)); bool valid_id_and_num = controllers[0] && controllers[0]->relay_count > relay_num; controller_dbo::free_list(controllers); return valid_id_and_num; } void relay_dbo::free_list(relay_dbo **relays_list) { for(int i = 0; relays_list[i] != nullptr; i++) { delete relays_list[i]; } free(relays_list); }