Store and retrieve files from/to SQLite.
More...
#include <remwharead/sqlite.hpp>
Store and retrieve files from/to SQLite.
- Since
- 0.6.0
◆ Database()
remwharead::Database::Database |
( |
| ) |
|
Connects to the database and creates it if necessary.
- Since
- 0.6.0
40 _dbpath = get_data_home();
41 if (!fs::exists(_dbpath))
43 fs::create_directories(_dbpath);
45 _dbpath /=
"database.sqlite";
47 Poco::Data::SQLite::Connector::registerConnector();
48 _session = std::make_unique<Session>(
"SQLite", _dbpath);
49 *_session <<
"CREATE TABLE IF NOT EXISTS remwharead(" 50 "uri TEXT, archive_uri TEXT, datetime TEXT, " 51 "tags TEXT, title TEXT, description TEXT, fulltext TEXT);", now;
55 catch (std::exception &e)
57 cerr <<
"Error in " << __func__ <<
": " << e.what() << endl;
◆ operator bool()
remwharead::Database::operator bool |
( |
| ) |
const |
|
explicit |
Returns true if connected to the database.
- Since
- 0.6.0
◆ retrieve()
list< Database::entry > remwharead::Database::retrieve |
( |
const time_point & |
start = time_point() , |
|
|
const time_point & |
end = system_clock::now() |
|
) |
| const |
Retrieve a list of Database::entry from the database.
- Since
- 0.6.0
118 Database::entry entrybuf;
121 Statement select(*_session);
124 select <<
"SELECT * FROM remwharead WHERE datetime " 125 "BETWEEN ? AND ? ORDER BY datetime DESC;",
126 bind(timepoint_to_string(start,
true)),
127 bind(timepoint_to_string(end,
true)),
128 into(entrybuf.uri), into(entrybuf.archive_uri), into(datetime),
129 into(strtags), into(entrybuf.title), into(entrybuf.description),
130 into(entrybuf.fulltext), range(0, 1);
134 while(!select.done() && select.execute() != 0)
136 entrybuf.datetime = string_to_timepoint(datetime,
true);
140 while (pos != string::npos)
142 const size_t newpos = strtags.find(
',', pos);
143 const string tag = strtags.substr(pos, newpos - pos);
149 if (pos != string::npos)
154 entrybuf.tags = tags;
156 entries.push_back(entrybuf);
161 catch (std::exception &e)
163 cerr <<
"Error in " << __func__ <<
": " << e.what() << endl;
◆ store()
void remwharead::Database::store |
( |
const entry & |
data | ) |
const |
Store a Database::entry in the database.
- Since
- 0.6.0
86 const string strdatetime = timepoint_to_string(data.datetime,
true);
88 Statement insert(*_session);
90 for (
const string &tag : data.tags)
93 if (tag != *(data.tags.rbegin()))
100 insert <<
"INSERT INTO remwharead " 101 "VALUES(?, ?, ?, ?, ?, ?, ?);",
102 useRef(data.uri), useRef(data.archive_uri),
103 useRef(strdatetime), useRef(strtags), useRef(data.title),
104 useRef(data.description), useRef(data.fulltext);
107 catch (std::exception &e)
109 cerr <<
"Error in " << __func__ <<
": " << e.what() << endl;
The documentation for this class was generated from the following files: