From 76548146c083cc9734442c52739e2b4f02e69b41 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 14 May 2019 20:45:48 +0200 Subject: [PATCH] Added rudimentary SQLite support. --- CMakeLists.txt | 2 +- README.adoc | 5 +-- remwharead.1.adoc | 7 +++-- src/main.cpp | 6 ++++ src/sqlite.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++ src/sqlite.hpp | 44 ++++++++++++++++++++++++++ 6 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 src/sqlite.cpp create mode 100644 src/sqlite.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b460659..9a5a112 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,7 +34,7 @@ link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS}) link_directories(${SQLITE3_LIBRARY_DIRS}) set(COMMON_LIBRARIES - ${LIBXDG_BASEDIR_LIBRARIES} vsqlitepp) + ${LIBXDG_BASEDIR_LIBRARIES} vsqlitepp stdc++fs) # Write version in header configure_file( diff --git a/README.adoc b/README.adoc index c9ff1cb..f16aaf3 100644 --- a/README.adoc +++ b/README.adoc @@ -4,8 +4,9 @@ This program is not usable at the moment. Most of the described functions are not yet available. -*remwharead* saves URLs of things you read in a database along with the current - date and time, title, description, the full text of the page and optional tags. +*remwharead* saves URLs of things you read in a database along with an URL to + the archived version, the current date and time, title, description, the full + text of the page and optional tags. == Usage diff --git a/remwharead.1.adoc b/remwharead.1.adoc index 1460509..94b00e3 100644 --- a/remwharead.1.adoc +++ b/remwharead.1.adoc @@ -2,7 +2,7 @@ :doctype: manpage :Author: tastytea :Email: tastytea@tastytea.de -:Date: 2019-05-12 +:Date: 2019-05-14 :Revision: 0.0.0 :man source: remwharead :man manual: General Commands Manual @@ -19,8 +19,9 @@ remwharead - Remember what you read, and when == DESCRIPTION -*remwharead* saves URLs of things you read in a database along with the current - date and time, title, description, the full text of the page and optional tags. +*remwharead* saves URLs of things you read in a database along with an URL to + the archived version, the current date and time, title, description, the full + text of the page and optional tags. == OPTIONS diff --git a/src/main.cpp b/src/main.cpp index 540842b..6b1ea15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,5 +32,11 @@ int main(const int argc, const char *argv[]) return opts.status_code; } + Database db; + db ? cout << "success." : cout << "failure."; + cout << endl; + + db.store("a", "b", "c", "d", "e", "f", "g"); + return 0; } diff --git a/src/sqlite.cpp b/src/sqlite.cpp new file mode 100644 index 0000000..53cfe3b --- /dev/null +++ b/src/sqlite.cpp @@ -0,0 +1,78 @@ +/* This file is part of remwharead. + * Copyright © 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include "sqlite.hpp" + +using std::cerr; +using std::endl; + +Database::Database() + : _connected(false) +{ + try + { + xdgHandle xdg; + xdgInitHandle(&xdg); + _dbpath = xdgDataHome(&xdg) / fs::path("remwharead"); + xdgWipeHandle(&xdg); + + if (!fs::exists(_dbpath)) + { + fs::create_directories(_dbpath); + } + _dbpath /= "database.sqlite"; + + _con = std::make_unique(_dbpath); + sqlite::execute(*_con, "CREATE TABLE IF NOT EXISTS remwharead(" + "uri TEXT, archive_uri TEXT, datetime TEXT, tags TEXT, " + "title TEXT, description TEXT, fulltext TEXT);", true); + + _connected = true; + } + catch (std::exception &e) + { + cerr << "Error: " << e.what() << endl; + } +} + +Database::operator bool() const +{ + return _connected; +} + +void Database::store(const string &uri, const string &archive_uri, + const string &datetime, const string &tags, + const string &title, const string &description, + const string &fulltext) +{ + try + { + sqlite::execute ins(*_con, "INSERT INTO remwharead " + "VALUES(?, ?, ?, ?, ?, ?, ?);"); + ins % uri % archive_uri % datetime % tags + % title % description % fulltext; + ins(); + } + catch (std::exception &e) + { + cerr << "Error: " << e.what() << endl; + } +} diff --git a/src/sqlite.hpp b/src/sqlite.hpp new file mode 100644 index 0000000..8cc3a67 --- /dev/null +++ b/src/sqlite.hpp @@ -0,0 +1,44 @@ +/* This file is part of remwharead. + * Copyright © 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef REMWHAREAD_SQLITE_HPP +#define REMWHAREAD_SQLITE_HPP + +#include +#include +#include +#include + +namespace fs = std::experimental::filesystem; +using std::string; + +class Database +{ +public: + Database(); + operator bool() const; + + void store(const string &uri, const string &archive_uri, + const string &datetime, const string &tags, const string &title, + const string &description, const string &fulltext); + +private: + fs::path _dbpath; + std::unique_ptr _con; + bool _connected; +}; + +#endif // REMWHAREAD_SQLITE_HPP