From a399050892995d73671da71f1be66a895f4eac13 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Sun, 21 Jul 2019 00:29:05 +0200 Subject: [PATCH] add: device relay get one fix: stuff --- controllers/api_v1_devices_relays.cc | 24 +++++++++++++++++++++--- models/device_dbo.cc | 8 ++++++++ models/device_dbo.h | 4 ++++ models/relay_dbo.cc | 20 ++++++++++++++------ models/relay_dbo.h | 4 ++++ models/schedule_dbo.cc | 9 +++++++++ models/schedule_dbo.h | 4 ++++ 7 files changed, 64 insertions(+), 9 deletions(-) diff --git a/controllers/api_v1_devices_relays.cc b/controllers/api_v1_devices_relays.cc index 7c5ba80..0adbc70 100644 --- a/controllers/api_v1_devices_relays.cc +++ b/controllers/api_v1_devices_relays.cc @@ -1,5 +1,6 @@ #include #include +#include #include "api_v1_devices.h" using namespace api::v1; @@ -9,8 +10,7 @@ devices::get_relays_all(const HttpRequestPtr &req, std::function &&callback, const std::string& device_id, int relay_num) { - relay_dbo **relays = relay_dbo::get_by_simple("id", (void *) (intptr_t) relay_num, (intptr_t) &sqlite3_bind_int); + helpers::sql_filter_builder *filters[2]; + helpers::sql_filter_builder filter + { + "number", + (void*)(intptr_t)relay_num, + (intptr_t)&sqlite3_bind_int, + "AND" + }; + helpers::sql_filter_builder filter2 + { + "device_id", + (void*)device_id.c_str(), + (intptr_t)sqlite3_bind_text, + ";" + }; + filters[0] = &filter; + filters[1] = &filter2; + + auto relays = relay_dbo::get_by(filters); if(relays[0]) { diff --git a/models/device_dbo.cc b/models/device_dbo.cc index 43f8b13..6073691 100644 --- a/models/device_dbo.cc +++ b/models/device_dbo.cc @@ -187,6 +187,14 @@ device_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func return device_db_select(stmt); } +device_dbo** +device_dbo::get_by(helpers::sql_filter_builder **filters) +{ + sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM devices WHERE", filters); + + return device_db_select(stmt); +} + void device_dbo::free_list(device_dbo **devices_list) { diff --git a/models/device_dbo.h b/models/device_dbo.h index 9ed4133..89db361 100644 --- a/models/device_dbo.h +++ b/models/device_dbo.h @@ -4,6 +4,7 @@ #include #include #include +#include class device_dbo { @@ -34,6 +35,9 @@ public: static device_dbo** get_by_simple(const char *key, const void *value, intptr_t bind_func); + static device_dbo** + get_by(helpers::sql_filter_builder **filters); + static device_dbo** get_all(); }; diff --git a/models/relay_dbo.cc b/models/relay_dbo.cc index b523f7f..e9daacc 100644 --- a/models/relay_dbo.cc +++ b/models/relay_dbo.cc @@ -47,7 +47,7 @@ relay_db_select_mapper(sqlite3_stmt *stmt) new_relay->id = sqlite3_column_int(stmt, i); break; case 'n': - switch(name[0]) + switch(name[1]) { case 'a': // name strncpy(new_relay->name, (const char*)sqlite3_column_text(stmt, i), 127); @@ -93,9 +93,8 @@ relay_db_select(sqlite3_stmt *stmt) } else { - LOG_ERROR << "Error Selecting relays from database"; - sqlite3_finalize(stmt); - return nullptr; + LOG_ERROR << "Error Selecting relays from database: " << sqlite3_errstr(s); + break; } } } @@ -145,8 +144,11 @@ Json::Value relay_dbo::to_json() { Json::Value relay_json; + // relay_json["id"] = this->id; relay_json["name"] = this->name; - relay_json["id"] = this->id; + relay_json["number"] = this->number; + relay_json["active_schedule_id"] = this->active_schedule_id; + relay_json["device_id"] = this->device_id; return relay_json; } @@ -178,6 +180,13 @@ relay_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func) 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); +} + void relay_dbo::free_list(relay_dbo **relays_list) { @@ -187,4 +196,3 @@ relay_dbo::free_list(relay_dbo **relays_list) } free(relays_list); } - diff --git a/models/relay_dbo.h b/models/relay_dbo.h index 1c99b67..b8d1b71 100644 --- a/models/relay_dbo.h +++ b/models/relay_dbo.h @@ -4,6 +4,7 @@ #include #include #include +#include class relay_dbo { @@ -33,6 +34,9 @@ public: static relay_dbo** get_by_simple(const char *key, const void *value, intptr_t bind_func); + static relay_dbo** + get_by(helpers::sql_filter_builder **filters); + static relay_dbo** get_all(); }; diff --git a/models/schedule_dbo.cc b/models/schedule_dbo.cc index 1917f3f..9309433 100644 --- a/models/schedule_dbo.cc +++ b/models/schedule_dbo.cc @@ -177,6 +177,15 @@ schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_fu ";" }; filters[0] = &filter; + + sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM schedules WHERE", filters); + + return schedule_db_select(stmt); +} + +schedule_dbo** +schedule_dbo::get_by(helpers::sql_filter_builder **filters) +{ sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM schedules WHERE", filters); return schedule_db_select(stmt); diff --git a/models/schedule_dbo.h b/models/schedule_dbo.h index 1a6a886..9b9898c 100644 --- a/models/schedule_dbo.h +++ b/models/schedule_dbo.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "period.h" #include "period_list.h" @@ -35,6 +36,9 @@ public: static schedule_dbo** get_by_simple(const char *key, const void *value, intptr_t bind_func); + static schedule_dbo** + get_by(helpers::sql_filter_builder **filters); + static schedule_dbo** get_all(); };