add: tagging
This commit is contained in:
parent
5bc849ab40
commit
42c168e627
16 changed files with 749 additions and 161 deletions
174
models/junction_tag_dbo.cc
Normal file
174
models/junction_tag_dbo.cc
Normal file
|
@ -0,0 +1,174 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include <helpers.h>
|
||||
#include "junction_tag_dbo.h"
|
||||
#include "globals.h"
|
||||
|
||||
bool
|
||||
junction_tag_dbo::insert(int tag_id, int relay_id, int schedule_id)
|
||||
{
|
||||
int rc;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "INSERT INTO junction_tag(tag_id, schedule_id, relay_id) values (?1, ?2, ?3);", -1, &stmt, nullptr);
|
||||
|
||||
sqlite3_bind_int(stmt, 1, tag_id);
|
||||
sqlite3_bind_int(stmt, 2, schedule_id);
|
||||
sqlite3_bind_int(stmt, 3, relay_id);
|
||||
|
||||
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 int*
|
||||
get_ids(sqlite3_stmt *stmt)
|
||||
{
|
||||
auto *ids = (int*)malloc(sizeof(int));
|
||||
int new_id;
|
||||
|
||||
int row = 0;
|
||||
|
||||
while(true)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = sqlite3_step(stmt);
|
||||
if (s == SQLITE_ROW)
|
||||
{
|
||||
new_id = sqlite3_column_int(stmt, 0);
|
||||
row++;
|
||||
|
||||
ids = (int*)realloc(ids, sizeof(int) * (row + 1));
|
||||
ids[row - 1] = new_id;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s == SQLITE_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR << "Error Selecting relays from database: " << sqlite3_errstr(s);
|
||||
sqlite3_finalize(stmt);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
ids[row] = 0;
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
int*
|
||||
junction_tag_dbo::get_relays_for_tag_id(int tag_id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "SELECT relay_id FROM junction_tag WHERE tag_id=?1;", -1, &stmt, nullptr);
|
||||
sqlite3_bind_int(stmt, 1, tag_id);
|
||||
|
||||
return get_ids(stmt);
|
||||
}
|
||||
|
||||
int*
|
||||
junction_tag_dbo::get_schedules_for_tag_id(int tag_id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "SELECT schedule_id FROM junction_tag WHERE tag_id=?1;", -1, &stmt, nullptr);
|
||||
sqlite3_bind_int(stmt, 1, tag_id);
|
||||
|
||||
return get_ids(stmt);
|
||||
}
|
||||
|
||||
int*
|
||||
junction_tag_dbo::get_tags_for_relay_id(int relay_id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "SELECT tag_id FROM junction_tag WHERE relay_id=?1;", -1, &stmt, nullptr);
|
||||
sqlite3_bind_int(stmt, 1, relay_id);
|
||||
|
||||
return get_ids(stmt);
|
||||
}
|
||||
|
||||
int*
|
||||
junction_tag_dbo::get_tags_for_schedule_id(int schedule_id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "SELECT tag_id FROM junction_tag WHERE schedule_id=?1;", -1, &stmt, nullptr);
|
||||
sqlite3_bind_int(stmt, 1, schedule_id);
|
||||
|
||||
return get_ids(stmt);
|
||||
}
|
||||
|
||||
bool
|
||||
junction_tag_dbo::remove(int tag_id, int relay_id, int schedule_id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "DELETE FROM junction_tag WHERE tag_id=?1 AND schedule_id=?2 AND relay_id=?3;", -1, &stmt, nullptr);
|
||||
sqlite3_bind_int(stmt, 1, tag_id);
|
||||
sqlite3_bind_int(stmt, 2, schedule_id);
|
||||
sqlite3_bind_int(stmt, 3, relay_id);
|
||||
rc = sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return rc == SQLITE_DONE;
|
||||
}
|
||||
|
||||
bool
|
||||
junction_tag_dbo::remove_for_tag(int tag_id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "DELETE FROM junction_tag WHERE tag_id=?1;", -1, &stmt, nullptr);
|
||||
sqlite3_bind_int(stmt, 1, tag_id);
|
||||
rc = sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return rc == SQLITE_DONE;
|
||||
}
|
||||
|
||||
bool
|
||||
junction_tag_dbo::remove_for_relay(int relay_id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "DELETE FROM junction_tag WHERE relay_id=?1;", -1, &stmt, nullptr);
|
||||
sqlite3_bind_int(stmt, 1, relay_id);
|
||||
rc = sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return rc == SQLITE_DONE;
|
||||
}
|
||||
|
||||
bool
|
||||
junction_tag_dbo::remove_for_schedule(int schedule_id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "DELETE FROM junction_tag WHERE schedule_id=?1;", -1, &stmt, nullptr);
|
||||
sqlite3_bind_int(stmt, 1, schedule_id);
|
||||
rc = sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return rc == SQLITE_DONE;
|
||||
}
|
41
models/junction_tag_dbo.h
Normal file
41
models/junction_tag_dbo.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
#ifndef EMGAUWA_CORE_JUNCTION_TAG_DBO_H
|
||||
#define EMGAUWA_CORE_JUNCTION_TAG_DBO_H
|
||||
|
||||
#include <string>
|
||||
#include <sqlite3.h>
|
||||
#include <json/value.h>
|
||||
#include <uuid/uuid.h>
|
||||
#include <helpers.h>
|
||||
|
||||
namespace junction_tag_dbo
|
||||
{
|
||||
int*
|
||||
get_relays_for_tag_id(int tag_id);
|
||||
|
||||
int*
|
||||
get_schedules_for_tag_id(int tag_id);
|
||||
|
||||
int*
|
||||
get_tags_for_relay_id(int relay_id);
|
||||
|
||||
int*
|
||||
get_tags_for_schedule_id(int schedule_id);
|
||||
|
||||
bool
|
||||
insert(int tag_id, int relay_id, int schedule_id);
|
||||
|
||||
bool
|
||||
remove(int tag_id, int relay_id, int schedule_id);
|
||||
|
||||
bool
|
||||
remove_for_tag(int tag_id);
|
||||
|
||||
bool
|
||||
remove_for_relay(int relay_id);
|
||||
|
||||
bool
|
||||
remove_for_schedule(int schedule_id);
|
||||
};
|
||||
|
||||
|
||||
#endif //EMGAUWA_CORE_JUNCTION_TAG_DBO_H
|
|
@ -5,6 +5,8 @@
|
|||
#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"
|
||||
|
||||
|
@ -117,21 +119,17 @@ relay_db_select(sqlite3_stmt *stmt)
|
|||
}
|
||||
|
||||
bool
|
||||
relay_dbo::update()
|
||||
relay_dbo::save()
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "UPDATE relays set number = ?2, name = ?3, controller_id = ?4 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(number, name, controller_id) values (?2, ?3, ?4);", -1, &stmt, nullptr);
|
||||
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);
|
||||
}
|
||||
|
@ -175,6 +173,24 @@ relay_dbo::to_json()
|
|||
relay_json["active_schedule"] = this->active_schedule->to_json();
|
||||
relay_json["schedules"] = schedules_json;
|
||||
|
||||
int *tags_ids = junction_tag_dbo::get_tags_for_relay_id(this->id);
|
||||
if(tags_ids != nullptr)
|
||||
{
|
||||
Json::Value tags_json(Json::arrayValue);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -213,6 +229,17 @@ relay_dbo::get_by(helpers::sql_filter_builder **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)
|
||||
{
|
||||
|
|
|
@ -24,10 +24,7 @@ public:
|
|||
reload_active_schedule();
|
||||
|
||||
bool
|
||||
update();
|
||||
|
||||
bool
|
||||
insert();
|
||||
save();
|
||||
|
||||
bool
|
||||
remove();
|
||||
|
@ -42,7 +39,7 @@ public:
|
|||
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
|
||||
|
||||
static relay_dbo*
|
||||
get_by_id_or_off(const int id);
|
||||
get_by_id(int id);
|
||||
|
||||
static relay_dbo**
|
||||
get_by(helpers::sql_filter_builder **filters);
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
#include <trantor/utils/Logger.h>
|
||||
#include <helpers.h>
|
||||
#include "schedule_dbo.h"
|
||||
#include "junction_tag_dbo.h"
|
||||
#include "tag_dbo.h"
|
||||
#include "globals.h"
|
||||
#include "period.h"
|
||||
|
||||
|
@ -151,6 +153,24 @@ schedule_dbo::to_json()
|
|||
schedule_json["id"] = id_str;
|
||||
schedule_json["periods"] = this->periods->to_json();
|
||||
|
||||
int *tags_ids = junction_tag_dbo::get_tags_for_schedule_id(this->id);
|
||||
if(tags_ids != nullptr)
|
||||
{
|
||||
Json::Value tags_json(Json::arrayValue);
|
||||
|
||||
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];
|
||||
}
|
||||
schedule_json["tags"] = tags_json;
|
||||
}
|
||||
|
||||
return schedule_json;
|
||||
}
|
||||
|
||||
|
@ -184,7 +204,18 @@ schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_fu
|
|||
}
|
||||
|
||||
schedule_dbo*
|
||||
schedule_dbo::get_by_id_or_off(const int id)
|
||||
schedule_dbo::get_by_id(int id)
|
||||
{
|
||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", &id, (intptr_t)&sqlite3_bind_int, 0);
|
||||
schedule_dbo *result = schedules[0];
|
||||
|
||||
free(schedules);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
schedule_dbo*
|
||||
schedule_dbo::get_by_id_or_off(int id)
|
||||
{
|
||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", &id, (intptr_t)&sqlite3_bind_int, 0);
|
||||
|
||||
|
|
|
@ -39,7 +39,10 @@ public:
|
|||
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
|
||||
|
||||
static schedule_dbo*
|
||||
get_by_id_or_off(const int id);
|
||||
get_by_id_or_off(int id);
|
||||
|
||||
static schedule_dbo*
|
||||
get_by_id(int id);
|
||||
|
||||
static schedule_dbo**
|
||||
get_by(helpers::sql_filter_builder **filters);
|
||||
|
|
131
models/tag_dbo.cc
Normal file
131
models/tag_dbo.cc
Normal file
|
@ -0,0 +1,131 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include <helpers.h>
|
||||
#include "tag_dbo.h"
|
||||
#include "globals.h"
|
||||
|
||||
bool
|
||||
tag_dbo::save(int id, const char *tag)
|
||||
{
|
||||
int rc;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
if(id)
|
||||
{
|
||||
sqlite3_prepare_v2(globals::db, "UPDATE tags SET tag = ?2 WHERE id = ?1;", -1, &stmt, nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlite3_prepare_v2(globals::db, "INSERT INTO tags(tag) values (?2);", -1, &stmt, nullptr);
|
||||
}
|
||||
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
sqlite3_bind_text(stmt, 2, tag, -1, SQLITE_STATIC);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE)
|
||||
{
|
||||
printf("ERROR saving tag: %s\n", sqlite3_errmsg(globals::db));
|
||||
return false;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
char*
|
||||
tag_dbo::get_tag(int id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "SELECT tag FROM tags WHERE id=?1;", -1, &stmt, nullptr);
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
|
||||
char *result = nullptr;
|
||||
|
||||
while(true)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = sqlite3_step(stmt);
|
||||
if (s == SQLITE_ROW)
|
||||
{
|
||||
const char *found_tag = (const char *)sqlite3_column_text(stmt, 0);
|
||||
result = (char*)malloc(sizeof(char) * (strlen(found_tag) + 1));
|
||||
strcpy(result, found_tag);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s == SQLITE_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR << "Error Selecting tags from database: " << sqlite3_errstr(s);
|
||||
sqlite3_finalize(stmt);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int
|
||||
tag_dbo::get_id(const char *tag)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "SELECT id FROM tags WHERE tag=?1;", -1, &stmt, nullptr);
|
||||
sqlite3_bind_text(stmt, 1, tag, -1, SQLITE_STATIC);
|
||||
|
||||
int result = 0;
|
||||
|
||||
while(true)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = sqlite3_step(stmt);
|
||||
if (s == SQLITE_ROW)
|
||||
{
|
||||
result = sqlite3_column_int(stmt, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s == SQLITE_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR << "Error Selecting tags from database: " << sqlite3_errstr(s);
|
||||
sqlite3_finalize(stmt);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool
|
||||
tag_dbo::remove(int id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "DELETE FROM tags WHERE id=?1;", -1, &stmt, nullptr);
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return rc == SQLITE_DONE;
|
||||
}
|
23
models/tag_dbo.h
Normal file
23
models/tag_dbo.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef EMGAUWA_CORE_TAG_DBO_H
|
||||
#define EMGAUWA_CORE_TAG_DBO_H
|
||||
|
||||
#include <string>
|
||||
#include <sqlite3.h>
|
||||
|
||||
namespace tag_dbo
|
||||
{
|
||||
bool
|
||||
save(int id, const char *tag);
|
||||
|
||||
bool
|
||||
remove(int id);
|
||||
|
||||
char*
|
||||
get_tag(int id);
|
||||
|
||||
int
|
||||
get_id(const char* tag);
|
||||
};
|
||||
|
||||
|
||||
#endif //EMGAUWA_CORE_TAG_DBO_H
|
Loading…
Add table
Add a link
Reference in a new issue