Browse Source

added deletion of events

main
Stefan Pranger 3 years ago
parent
commit
5f2725b5c2
  1. 9
      db/FSWatcher.cpp
  2. 55
      db/update.cpp
  3. 1
      db/update.h

9
db/FSWatcher.cpp

@ -61,19 +61,22 @@ namespace db {
void FSWatcher::create_event(const std::string event_file) {
if(!is_ics(event_file)) return;
ical::IcalObject* object = new ical::IcalObject();
util::parse_main_component(object, util::parse_ics_file(event_file));
db::insert_events(m_db, object);
util::parse_main_component(object, util::parse_ics_file(event_file), event_file);
DEBUG << "Created Event " << event_file;
db::insert_events(m_db, object);
}
void FSWatcher::modify_event(const std::string event_file) {
if(!is_ics(event_file)) return;
ical::IcalObject* object = new ical::IcalObject();
util::parse_main_component(object, util::parse_ics_file(event_file), event_file);
DEBUG << "Modified Event " << event_file;
db::insert_events(m_db, object);
}
void FSWatcher::delete_event(const std::string event_file) {
if(!is_ics(event_file)) return;
DEBUG << "Removing Event " << event_file;
db::remove_events(m_db, event_file);
}
}

55
db/update.cpp

@ -1,31 +1,74 @@
#include <db/update.h>
#include <db/statements/builder/InsertStatementBuilder.h>
#include <db/statements/builder/SelectStatementBuilder.h>
#include <db/statements/builder/UpdateStatementBuilder.h>
#include <db/statements/builder/DeleteStatementBuilder.h>
namespace db {
uint32_t insert_events(sql::Database *db, const ical::IcalObject *object) {
DEBUG << *object;
uint32_t event_id;
for(auto const& event : object->get_events()) {
stmt::Statement insert_object = stmt::Statement::insert().table("events").columns("uid, summary, dtstart, dtend").values().row(event->get_db_row()).close();
DEBUG << insert_object.str();
stmt::Statement select_event_by_uid = stmt::Statement::select().columns("1").from_table("events").where("uid = \"" + event->get_uid() + "\"").exists();
uint32_t ret = db->execAndGet(select_event_by_uid.str());
if(ret > 0) {
DEBUG << "Updating " << event->print();
stmt::Statement update_event = stmt::Statement::update().table("events").set(event->update_row_string()).where("uid = \"" + event->get_uid() + "\"").close();
DEBUG << update_event.str();
db->exec(update_event.str());
stmt::Statement get_event_id = stmt::Statement::select().columns("id").from_table("events").where("uid = \"" + event->get_uid() + "\"").close();
uint32_t event_id = db->execAndGet(get_event_id.str());
stmt::Statement delete_alarms = stmt::Statement::remove().table("alarms").where("event_id = \"" + std::to_string(event_id) + "\"").close();
db->exec(delete_alarms.str());
for(auto const& alarm : event->get_alarms()) {
insert_alarm(db, &alarm, event_id);
}
} else {
DEBUG << "Inserting " << event->print();
stmt::Statement insert_object = stmt::Statement::insert().table("events").columns("uid, summary, dtstart, dtend, filename").values().row(event->get_db_row()).close();
DEBUG << insert_object.str() << std::endl;
db->exec(insert_object.str());
event_id = db->execAndGet("SELECT last_insert_rowid();");
DEBUG << event_id;
for(auto const& alarm : event->get_alarms()) {
insert_alarm(db, &alarm, event_id);
}
}
}
return 0;
}
uint32_t insert_alarm(sql::Database *db, const ical::Alarm *alarm, const uint32_t event_id) {
std::vector<std::string> row = alarm->get_db_row();
row.push_back(std::to_string(event_id));
stmt::Statement insert_alarm = stmt::Statement::insert().table("alarms").columns("action, description, absolute_trigger, eventID").values().row(row).close();
stmt::Statement insert_alarm = stmt::Statement::insert().table("alarms").columns("action, description, absolute_trigger, event_id").values().row(row).close();
DEBUG << insert_alarm.str();
try {
db->exec(insert_alarm.str());
} catch(sql::Exception &e) {
WARN << e.what() << std::endl;
}
return 0;
}
uint32_t remove_events(sql::Database *db, const std::string &filename) {
stmt::Statement delete_event = stmt::Statement::remove().table("events").where("filename = \"" + filename + "\"").close();
DEBUG << delete_event.str();
try {
db->exec(delete_event.str());
} catch(sql::Exception &e) {
WARN << e.what() << std::endl;
}
return 0;
}
//uint32_t update_alarm(sql::Database *db, const ical::Alarm *alarm) {
// std::vector<std::string> row = alarm->get_db_row();
// row.push_back(std::to_string(event_id));
// stmt::Statement insert_alarm = stmt::Statement::insert().table("alarms").columns("action, description, absolute_trigger, event_id").values().row(row).close();
// DEBUG << insert_alarm.str();
// db->exec(insert_alarm.str());
// return 0;
//}
}

1
db/update.h

@ -6,4 +6,5 @@
namespace db {
uint32_t insert_events(sql::Database *db, const ical::IcalObject *object);
uint32_t insert_alarm(sql::Database *db, const ical::Alarm *alarm, const uint32_t event_id);
uint32_t remove_events(sql::Database *db, const std::string &filename);
}
Loading…
Cancel
Save