remwharead  0.6.3
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 <experimental/filesystem>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 #include <chrono>
25 #include <sqlite/connection.hpp>
26 
27 namespace remwharead
28 {
29  namespace fs = std::experimental::filesystem;
30  using std::string;
31  using std::vector;
32  using std::chrono::system_clock;
33  using time_point = system_clock::time_point;
34 
36  class Database
37  {
38  public:
40  typedef struct entry
41  {
42  string uri;
43  string archive_uri;
44  time_point datetime;
45  vector<string> tags;
46  string title;
47  string description;
48  string fulltext;
49 
51  friend bool operator ==(const Database::entry &a,
52  const Database::entry &b);
54  const string fulltext_oneline() const;
55  } entry;
56 
58  Database();
59 
61  operator bool() const;
62 
64  void store(const entry &data) const;
65 
67  const vector<entry> retrieve(
68  const time_point &start = time_point(),
69  const time_point &end = system_clock::now()) const;
70 
71  private:
72  fs::path _dbpath;
73  std::unique_ptr<sqlite::connection> _con;
74  bool _connected;
75  };
76 
77  using DB = Database;
78 }
79 
80 #endif // REMWHAREAD_SQLITE_HPP
struct remwharead::Database::entry entry
Describes a database entry.
Store and retrieve files from/to SQLite.
Definition: sqlite.hpp:36
void store(const entry &data) const
Store a Database::entry in the database.
Definition: sqlite.cpp:87
Describes a database entry.
Definition: sqlite.hpp:40
Definition: search.cpp:23
friend bool operator==(const Database::entry &a, const Database::entry &b)
Returns true if date and time are equal.
Definition: sqlite.cpp:66
const string fulltext_oneline() const
The full text in one line.
Definition: sqlite.cpp:76
const vector< entry > retrieve(const time_point &start=time_point(), const time_point &end=system_clock::now()) const
Retrieve a vector of Database::entry from the database.
Definition: sqlite.cpp:114
Database()
Connects to the database and creates it if necessary.
Definition: sqlite.cpp:31