Store and retrieve files from/to SQLite.
More...
#include <remwharead/sqlite.hpp>
Store and retrieve files from/to SQLite.
- Since
- 0.6.0
◆ 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.
114 const string query =
"SELECT * FROM remwharead WHERE datetime " 115 "BETWEEN '" + timepoint_to_string(start,
true)
116 +
"' AND '" + timepoint_to_string(end,
true)
117 +
"' ORDER BY datetime DESC;";
119 sqlite::query q(*_con, query);
120 sqlite::result_type res = q.get_result();
121 vector<entry> entries;
123 while(res->next_row())
126 const string strtags = res->get_string(3);
128 while (pos != std::string::npos)
130 const size_t newpos = strtags.find(
',', pos);
131 tags.push_back(strtags.substr(pos, newpos - pos));
133 if (pos != std::string::npos)
142 string_to_timepoint(res->get_string(2),
true),
152 catch (std::exception &e)
154 cerr <<
"Error in " << __func__ <<
": " << e.what() << endl;
◆ store()
void remwharead::Database::store |
( |
const entry & |
data | ) |
const |
Store a Database::entry in the database.
86 const string strdatetime = timepoint_to_string(data.datetime,
true);
88 for (
const string &tag : data.tags)
91 if (tag != *(data.tags.rbegin()))
97 sqlite::execute ins(*_con,
"INSERT INTO remwharead " 98 "VALUES(?, ?, ?, ?, ?, ?, ?);");
99 ins % data.uri % data.archive_uri % strdatetime % strtags
100 % data.title % data.description % data.fulltext;
103 catch (std::exception &e)
105 cerr <<
"Error in " << __func__ <<
": " << e.what() << endl;
The documentation for this class was generated from the following files: