Store and retrieve files from/to SQLite.
More...
#include <sqlite.hpp>
Store and retrieve files from/to SQLite.
◆ entry
Describes a database entry.
◆ Database()
remwharead::Database::Database |
( |
| ) |
|
Connects to the database and creates it if necessary.
38 _dbpath = xdgDataHome(&xdg) / fs::path(
"remwharead");
41 if (!fs::exists(_dbpath))
43 fs::create_directories(_dbpath);
45 _dbpath /=
"database.sqlite";
47 _con = std::make_unique<sqlite::connection>(_dbpath);
48 sqlite::execute(*_con,
"CREATE TABLE IF NOT EXISTS remwharead(" 49 "uri TEXT, archive_uri TEXT, datetime TEXT, " 50 "tags TEXT, title TEXT, description TEXT, " 51 "fulltext TEXT);",
true);
55 catch (std::exception &e)
57 cerr <<
"Error in " << __func__ <<
": " << e.what() << endl;
◆ operator bool()
remwharead::Database::operator bool |
( |
| ) |
const |
Returns true if connected to the database.
◆ retrieve()
const vector< Database::entry > remwharead::Database::retrieve |
( |
const time_point & |
start = time_point() , |
|
|
const time_point & |
end = system_clock::now() |
|
) |
| const |
Retrieve a vector of Database::entry from the database.
119 const string query =
"SELECT * FROM remwharead WHERE datetime " 120 "BETWEEN '" + timepoint_to_string(start,
true)
121 +
"' AND '" + timepoint_to_string(end,
true)
122 +
"' ORDER BY datetime DESC;";
124 sqlite::query q(*_con, query);
125 sqlite::result_type res = q.get_result();
126 vector<entry> entries;
128 while(res->next_row())
131 const string strtags = res->get_string(3);
133 while (pos != std::string::npos)
135 const size_t newpos = strtags.find(
',', pos);
136 tags.push_back(strtags.substr(pos, newpos - pos));
138 if (pos != std::string::npos)
147 string_to_timepoint(res->get_string(2),
true),
157 catch (std::exception &e)
159 cerr <<
"Error in " << __func__ <<
": " << e.what() << endl;
◆ store()
void remwharead::Database::store |
( |
const entry & |
data | ) |
const |
Store a Database::entry in the database.
91 const string strdatetime = timepoint_to_string(data.datetime,
true);
93 for (
const string &tag : data.tags)
96 if (tag != *(data.tags.rbegin()))
102 sqlite::execute ins(*_con,
"INSERT INTO remwharead " 103 "VALUES(?, ?, ?, ?, ?, ?, ?);");
104 ins % data.uri % data.archive_uri % strdatetime % strtags
105 % data.title % data.description % data.fulltext;
108 catch (std::exception &e)
110 cerr <<
"Error in " << __func__ <<
": " << e.what() << endl;
The documentation for this class was generated from the following files: