remwharead  0.8.6
sqlite.hpp
1 /* This file is part of remwharead.
2  * Copyright © 2019 tastytea <tastytea@tastytea.de>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef REMWHAREAD_SQLITE_HPP
18 #define REMWHAREAD_SQLITE_HPP
19 
20 #include <Poco/Data/Session.h>
21 #include <chrono>
22 #include <experimental/filesystem>
23 #include <list>
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 namespace remwharead
29 {
30 namespace fs = std::experimental::filesystem;
31 using std::string;
32 using std::vector;
33 using std::chrono::system_clock;
34 using time_point = system_clock::time_point;
35 using std::list;
36 using Poco::Data::Session;
37 
45 class Database
46 {
47 public:
55  struct entry
56  {
57  string uri;
58  string archive_uri;
59  time_point datetime;
60  vector<string> tags;
61  string title;
62  string description;
63  string fulltext;
64 
70  friend bool operator ==(const Database::entry &a,
71  const Database::entry &b);
72 
78  [[nodiscard]]
79  string fulltext_oneline() const;
80  };
81 
87  Database();
88 
94  explicit operator bool() const;
95 
101  void store(const entry &data) const;
102 
108  [[nodiscard]]
109  list<entry> retrieve(const time_point &start = time_point(),
110  const time_point &end = system_clock::now()) const;
111 
119  size_t remove(const string &uri);
120 
126  [[nodiscard]]
127  static string tags_to_string(const vector<string> &tags);
128 
129 private:
130  fs::path _dbpath;
131  std::unique_ptr<Session> _session;
132  bool _connected;
133 
134  [[nodiscard]]
135  fs::path get_data_home() const;
136 };
137 } // namespace remwharead
138 
139 #endif // REMWHAREAD_SQLITE_HPP
list< entry > retrieve(const time_point &start=time_point(), const time_point &end=system_clock::now()) const
Retrieve a list of Database::entry from the database.
Definition: sqlite.cpp:104
string fulltext_oneline() const
The full text in one line.
Definition: sqlite.cpp:71
Store and retrieve files from/to SQLite.
Definition: sqlite.hpp:45
void store(const entry &data) const
Store a Database::entry in the database.
Definition: sqlite.cpp:82
Describes a database entry.
Definition: sqlite.hpp:55
friend bool operator==(const Database::entry &a, const Database::entry &b)
Returns true if date and time are equal.
Definition: sqlite.cpp:66
Database()
Connects to the database and creates it if necessary.
Definition: sqlite.cpp:35
static string tags_to_string(const vector< string > &tags)
Returns tags as comma separated string.
Definition: sqlite.cpp:169
size_t remove(const string &uri)
Remove all entries with this URI from database.
Definition: sqlite.cpp:160