From a1e3c57da206c88319ffce377d1b25d4e41d7e7b Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 14 May 2019 22:57:51 +0200 Subject: [PATCH] Storing URL & time & tags in the database works. --- CMakeLists.txt | 2 +- src/main.cpp | 16 +++++++++++++--- src/sqlite.cpp | 21 +++++++++++++++++---- src/sqlite.hpp | 7 ++++++- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a5a112..4a97374 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project(remwharead - VERSION 0.0.0 + VERSION 0.0.1 LANGUAGES CXX ) diff --git a/src/main.cpp b/src/main.cpp index 6b1ea15..8862c36 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,7 @@ #include #include +#include #include "sqlite.hpp" #include "parse_options.hpp" @@ -23,6 +24,7 @@ using std::cout; using std::cerr; using std::endl; using std::string; +using std::chrono::system_clock; int main(const int argc, const char *argv[]) { @@ -33,10 +35,18 @@ int main(const int argc, const char *argv[]) } Database db; - db ? cout << "success." : cout << "failure."; - cout << endl; - db.store("a", "b", "c", "d", "e", "f", "g"); + if (!db) + { + cerr << "Error: Database connection failed.\n"; + return 2; + } + + if (!opts.url.empty()) + { + db.store(opts.url, "archive", system_clock::now(), opts.tags, + "title", "description", "fulltext"); + } return 0; } diff --git a/src/sqlite.cpp b/src/sqlite.cpp index 53cfe3b..83b0ca8 100644 --- a/src/sqlite.cpp +++ b/src/sqlite.cpp @@ -16,9 +16,11 @@ #include #include +#include #include #include #include +#include "time.hpp" #include "sqlite.hpp" using std::cerr; @@ -59,15 +61,26 @@ Database::operator bool() const } 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) + const system_clock::time_point &datetime, + const vector &tags, const string &title, + const string &description, const string &fulltext) { try { + const string strdatetime = timepoint_to_string(datetime); + string strtags; + for (const string &tag : tags) + { + strtags += tag; + if (tag != *(tags.rbegin())) + { + strtags += ","; + } + } + sqlite::execute ins(*_con, "INSERT INTO remwharead " "VALUES(?, ?, ?, ?, ?, ?, ?);"); - ins % uri % archive_uri % datetime % tags + ins % uri % archive_uri % strdatetime % strtags % title % description % fulltext; ins(); } diff --git a/src/sqlite.hpp b/src/sqlite.hpp index 8cc3a67..21f1805 100644 --- a/src/sqlite.hpp +++ b/src/sqlite.hpp @@ -20,10 +20,14 @@ #include #include #include +#include +#include #include namespace fs = std::experimental::filesystem; using std::string; +using std::vector; +using std::chrono::system_clock; class Database { @@ -32,7 +36,8 @@ public: operator bool() const; void store(const string &uri, const string &archive_uri, - const string &datetime, const string &tags, const string &title, + const system_clock::time_point &datetime, + const vector &tags, const string &title, const string &description, const string &fulltext); private: