195 lines
4.7 KiB
C++
195 lines
4.7 KiB
C++
//
|
|
// Created by tobias on 08/07/19.
|
|
//
|
|
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <string.h>
|
|
#include <trantor/utils/Logger.h>
|
|
#include "device_dbo.h"
|
|
#include "globals.h"
|
|
|
|
static bool device_db_update_insert(device_dbo *device, sqlite3_stmt *stmt)
|
|
{
|
|
int rc;
|
|
|
|
sqlite3_bind_text(stmt, 1, device->id, -1, SQLITE_STATIC);
|
|
sqlite3_bind_text(stmt, 2, device->name, -1, SQLITE_STATIC);
|
|
sqlite3_bind_text(stmt, 3, device->ip, -1, SQLITE_STATIC);
|
|
sqlite3_bind_int(stmt, 4, device->active);
|
|
sqlite3_bind_int(stmt, 5, device->port);
|
|
sqlite3_bind_int(stmt, 6, device->relay_count);
|
|
|
|
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 device_dbo*
|
|
device_db_select_mapper(sqlite3_stmt *stmt)
|
|
{
|
|
auto *new_device = (device_dbo*)malloc(sizeof(device_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
|
|
new_device->active = sqlite3_column_int(stmt, i);
|
|
break;
|
|
case 'i':
|
|
switch(name[1])
|
|
{
|
|
case 'd': // id
|
|
strncpy(new_device->id, (const char*)sqlite3_column_text(stmt, i), 32);
|
|
break;
|
|
case 'p': // ip
|
|
strncpy(new_device->ip, (const char*)sqlite3_column_text(stmt, i), 16);
|
|
break;
|
|
}
|
|
break;
|
|
case 'n': // name
|
|
strncpy(new_device->name, (const char*)sqlite3_column_text(stmt, i), 127);
|
|
break;
|
|
case 'p': // port
|
|
new_device->port = sqlite3_column_int(stmt, i);
|
|
break;
|
|
case 'r': // relay_count
|
|
new_device->relay_count = sqlite3_column_int(stmt, i);
|
|
break;
|
|
}
|
|
}
|
|
return new_device;
|
|
}
|
|
|
|
static device_dbo**
|
|
device_db_select(sqlite3_stmt *stmt)
|
|
{
|
|
auto **all_devices = (device_dbo**)malloc(sizeof(device_dbo*));
|
|
|
|
int row = 0;
|
|
|
|
while(true)
|
|
{
|
|
int s;
|
|
|
|
s = sqlite3_step(stmt);
|
|
if (s == SQLITE_ROW)
|
|
{
|
|
device_dbo *new_device = device_db_select_mapper(stmt);
|
|
row++;
|
|
|
|
all_devices = (device_dbo**)realloc(all_devices, sizeof(device_dbo*) * (row + 1));
|
|
all_devices[row - 1] = new_device;
|
|
}
|
|
else
|
|
{
|
|
if (s == SQLITE_DONE)
|
|
{
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR << "Error Selecting devices from database";
|
|
sqlite3_finalize(stmt);
|
|
return nullptr;
|
|
}
|
|
}
|
|
}
|
|
|
|
sqlite3_finalize(stmt);
|
|
all_devices[row] = nullptr;
|
|
|
|
return all_devices;
|
|
}
|
|
|
|
bool
|
|
device_dbo::update()
|
|
{
|
|
sqlite3_stmt *stmt;
|
|
|
|
sqlite3_prepare_v2(globals::db, "UPDATE devices set name = ?2, ip = ?3, active = ?4, port = ?5, relay_count = ?6 WHERE id = ?1;", -1, &stmt, nullptr);
|
|
|
|
return device_db_update_insert(this, stmt);
|
|
}
|
|
|
|
bool
|
|
device_dbo::insert()
|
|
{
|
|
sqlite3_stmt *stmt;
|
|
|
|
sqlite3_prepare_v2(globals::db, "INSERT INTO devices(id, name, ip, active, port, relay_count) values (?1, ?2, ?3, ?4, ?5, ?6);", -1, &stmt, nullptr);
|
|
|
|
return device_db_update_insert(this, stmt);
|
|
}
|
|
|
|
bool
|
|
device_dbo::remove()
|
|
{
|
|
sqlite3_stmt *stmt;
|
|
int rc;
|
|
|
|
sqlite3_prepare_v2(globals::db, "DELETE FROM devices WHERE id=?1;", -1, &stmt, nullptr);
|
|
sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC);
|
|
rc = sqlite3_step(stmt);
|
|
sqlite3_finalize(stmt);
|
|
|
|
return rc == SQLITE_DONE;
|
|
|
|
}
|
|
|
|
Json::Value
|
|
device_dbo::to_json()
|
|
{
|
|
Json::Value device_json;
|
|
device_json["name"] = this->name;
|
|
device_json["id"] = this->id;
|
|
device_json["ip"] = this->ip;
|
|
device_json["port"] = this->port;
|
|
device_json["relay_count"] = this->relay_count;
|
|
device_json["active"] = this->active;
|
|
|
|
return device_json;
|
|
}
|
|
|
|
device_dbo**
|
|
device_dbo::get_all()
|
|
{
|
|
sqlite3_stmt *stmt;
|
|
|
|
sqlite3_prepare_v2(globals::db, "SELECT * FROM devices;", -1, &stmt, nullptr);
|
|
|
|
return device_db_select(stmt);
|
|
}
|
|
|
|
device_dbo*
|
|
device_dbo::get_one_by(const char *key, const char *value)
|
|
{
|
|
sqlite3_stmt *stmt;
|
|
char* sql;
|
|
|
|
asprintf(&sql, "SELECT * FROM devices WHERE %s=?1;", key);
|
|
|
|
sqlite3_prepare_v2(globals::db, sql, -1, &stmt, nullptr);
|
|
sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
|
|
|
|
return device_db_select(stmt)[0];
|
|
}
|
|
|
|
void
|
|
device_dbo::free_list(device_dbo **devices_list)
|
|
{
|
|
for(int i = 0; devices_list[i] != nullptr; i++)
|
|
{
|
|
free(devices_list[i]);
|
|
}
|
|
free(devices_list);
|
|
}
|
|
|