From 3fe1e78af45ea3abebce83ab940bb4fd80df5ca3 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 28 Nov 2019 00:49:28 +0100 Subject: [PATCH] Add `--delete`, to delete URIs from the database. Closes #5. --- include/sqlite.hpp | 9 +++++++++ man/remwharead.1.adoc | 7 ++++++- src/cli/parse_options.cpp | 15 ++++++++++++++- src/lib/sqlite.cpp | 9 +++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/include/sqlite.hpp b/include/sqlite.hpp index f91cea9..71f07ca 100644 --- a/include/sqlite.hpp +++ b/include/sqlite.hpp @@ -109,6 +109,15 @@ public: list retrieve(const time_point &start = time_point(), const time_point &end = system_clock::now()) const; + /*! + * @brief Remove all entries with this URI from database. + * + * @return Number of removed entries. + * + * @since 0.9.0 + */ + size_t remove(const string &uri); + private: fs::path _dbpath; std::unique_ptr _session; diff --git a/man/remwharead.1.adoc b/man/remwharead.1.adoc index ccffe66..f65263e 100644 --- a/man/remwharead.1.adoc +++ b/man/remwharead.1.adoc @@ -2,7 +2,7 @@ :doctype: manpage :Author: tastytea :Email: tastytea@tastytea.de -:Date: 2019-11-27 +:Date: 2019-11-28 :Revision: 0.0.0 :man source: remwharead :man manual: General Commands Manual @@ -17,6 +17,8 @@ remwharead - Saves URIs of things you want to remember in a database *remwharead* *-e*=_format_ [*-f*=_file_] [*-T*=_start_,_end_] [[*-s*|*-S*]=_expression_] [*-r*] +*remwharead* [*-d*=_URI_] + == DESCRIPTION *remwharead* saves URIs of things you want to remember in a database along with @@ -62,6 +64,9 @@ every tag is enclosed by _^_ and _$_. *-N*, *--no-archive*:: Do not archive URI. +*-d*=_URI_, *--delete*=_URI_:: +Remove all entries with this URI from the database. + *-h*, *--help*:: Show help message. diff --git a/src/cli/parse_options.cpp b/src/cli/parse_options.cpp index 735ed44..acc0bf5 100644 --- a/src/cli/parse_options.cpp +++ b/src/cli/parse_options.cpp @@ -15,6 +15,7 @@ */ #include "remwharead_cli.hpp" +#include "sqlite.hpp" #include "time.hpp" #include "version.hpp" #include @@ -81,6 +82,11 @@ void App::defineOptions(OptionSet& options) options.addOption( Option("no-archive", "N", "Do not archive URI.") .callback(OptionCallback(this, &App::handle_options))); + options.addOption( + Option("delete", "d", + "Remove all entries with this URI from database.") + .argument("URI") + .callback(OptionCallback(this, &App::handle_options))); } void App::handle_options(const std::string &name, const std::string &value) @@ -194,6 +200,12 @@ void App::handle_options(const std::string &name, const std::string &value) { _regex = true; } + else if (name == "delete") + { + Database db; + cout << "Deleted " << db.remove(value) << " entries.\n"; + _exit_requested = true; + } } void App::print_help(const string &option) @@ -207,7 +219,8 @@ void App::print_help(const string &option) helpFormatter->setCommand(commandName()); helpFormatter->setUsage("[-t tags] [-N] URI\n" "-e format [-f file] [-T start,end] " - "[[-s|-S] expression] [-r]"); + "[[-s|-S] expression] [-r]\n" + "-d URI"); } else { diff --git a/src/lib/sqlite.cpp b/src/lib/sqlite.cpp index 65b68e4..4da32af 100644 --- a/src/lib/sqlite.cpp +++ b/src/lib/sqlite.cpp @@ -166,6 +166,15 @@ list Database::retrieve(const time_point &start, return {}; } +size_t Database::remove(const string &uri) +{ + Statement del(*_session); + + del << "DELETE FROM remwharead WHERE uri = ?;", bind(uri); + + return del.execute(); +} + fs::path Database::get_data_home() const { fs::path path;