core-legacy/models/relay_dbo.cc

191 lines
4.5 KiB
C++
Raw Normal View History

#include <cstdio>
#include <cstring>
#include <cstring>
#include <trantor/utils/Logger.h>
#include <helpers.h>
#include "relay_dbo.h"
#include "globals.h"
static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt)
{
int rc;
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->device_id, -1, 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 = (relay_dbo*)malloc(sizeof(relay_dbo));
for(int i = 0; i < sqlite3_column_count(stmt); i++)
{
const char *name = sqlite3_column_name(stmt, i);
switch(name[0])
{
case 'a': // active_schedule_id
strncpy(new_relay->active_schedule_id, (const char*)sqlite3_column_text(stmt, i), 33);
break;
case 'd': // device_id
strncpy(new_relay->device_id, (const char*)sqlite3_column_text(stmt, i), 33);
break;
case 'i':
new_relay->id = sqlite3_column_int(stmt, i);
break;
case 'n':
switch(name[0])
{
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;
}
default: // ignore columns not implemented
break;
}
}
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_finalize(stmt);
return nullptr;
}
}
}
sqlite3_finalize(stmt);
all_relays[row] = nullptr;
return all_relays;
}
bool
relay_dbo::update()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "UPDATE relays set number = ?2, name = ?3, active_schedule_id = ?4, device_id = ?5 WHERE id = ?1;", -1, &stmt, nullptr);
return relay_db_update_insert(this, stmt);
}
bool
relay_dbo::insert()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "INSERT INTO relays(id, number, name, active_schedule_id, device_id) values (?1, ?2, ?3, ?4, ?5);", -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()
{
Json::Value relay_json;
relay_json["name"] = this->name;
relay_json["id"] = this->id;
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)
{
helpers::sql_filter_builder *filters[1];
helpers::sql_filter_builder filter
{
key,
value,
bind_func,
";"
};
filters[0] = &filter;
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)
{
for(int i = 0; relays_list[i] != nullptr; i++)
{
free(relays_list[i]);
}
free(relays_list);
}