remwharead  0.8.4
Classes | Public Member Functions | List of all members
remwharead::Database Class Reference

Store and retrieve files from/to SQLite. More...

#include <remwharead/sqlite.hpp>

Classes

struct  entry
 Describes a database entry. More...
 

Public Member Functions

 Database ()
 Connects to the database and creates it if necessary. More...
 
 operator bool () const
 Returns true if connected to the database. More...
 
void store (const entry &data) const
 Store a Database::entry in the database. More...
 
list< entryretrieve (const time_point &start=time_point(), const time_point &end=system_clock::now()) const
 Retrieve a list of Database::entry from the database. More...
 

Detailed Description

Store and retrieve files from/to SQLite.

Since
0.6.0

Constructor & Destructor Documentation

◆ Database()

remwharead::Database::Database ( )

Connects to the database and creates it if necessary.

Since
0.6.0
34  : _connected(false)
35 {
36  try
37  {
38  xdgHandle xdg;
39  xdgInitHandle(&xdg);
40  _dbpath = xdgDataHome(&xdg) / fs::path("remwharead");
41  xdgWipeHandle(&xdg);
42 
43  if (!fs::exists(_dbpath))
44  {
45  fs::create_directories(_dbpath);
46  }
47  _dbpath /= "database.sqlite";
48 
49  Poco::Data::SQLite::Connector::registerConnector();
50  _session = std::make_unique<Session>("SQLite", _dbpath);
51  *_session << "CREATE TABLE IF NOT EXISTS remwharead("
52  "uri TEXT, archive_uri TEXT, datetime TEXT, "
53  "tags TEXT, title TEXT, description TEXT, fulltext TEXT);", now;
54 
55  _connected = true;
56  }
57  catch (std::exception &e)
58  {
59  cerr << "Error in " << __func__ << ": " << e.what() << endl;
60  }
61 }

Member Function Documentation

◆ operator bool()

remwharead::Database::operator bool ( ) const
explicit

Returns true if connected to the database.

Since
0.6.0
64 {
65  return _connected;
66 }

◆ 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
117 {
118  try
119  {
120  Database::entry entrybuf;
121  string datetime;
122  string strtags;
123  Statement select(*_session);
124 
125  // bind() copies the value.
126  select << "SELECT * FROM remwharead WHERE datetime "
127  "BETWEEN ? AND ? ORDER BY datetime DESC;",
128  bind(timepoint_to_string(start, true)),
129  bind(timepoint_to_string(end, true)),
130  into(entrybuf.uri), into(entrybuf.archive_uri), into(datetime),
131  into(strtags), into(entrybuf.title), into(entrybuf.description),
132  into(entrybuf.fulltext), range(0, 1);
133 
134  list<entry> entries;
135 
136  while(!select.done() && select.execute() != 0)
137  {
138  entrybuf.datetime = string_to_timepoint(datetime, true);
139 
140  vector<string> tags;
141  size_t pos = 0;
142  while (pos != string::npos)
143  {
144  const size_t newpos = strtags.find(',', pos);
145  const string tag = strtags.substr(pos, newpos - pos);
146  if (!tag.empty())
147  {
148  tags.push_back(tag);
149  }
150  pos = newpos;
151  if (pos != string::npos)
152  {
153  ++pos;
154  }
155  }
156  entrybuf.tags = tags;
157 
158  entries.push_back(entrybuf);
159  }
160 
161  return entries;
162  }
163  catch (std::exception &e)
164  {
165  cerr << "Error in " << __func__ << ": " << e.what() << endl;
166  }
167 
168  return {};
169 }

◆ store()

void remwharead::Database::store ( const entry data) const

Store a Database::entry in the database.

Since
0.6.0
85 {
86  try
87  {
88  const string strdatetime = timepoint_to_string(data.datetime, true);
89  string strtags;
90  Statement insert(*_session);
91 
92  for (const string &tag : data.tags)
93  {
94  strtags += tag;
95  if (tag != *(data.tags.rbegin()))
96  {
97  strtags += ",";
98  }
99  }
100 
101  // useRef() uses the const reference.
102  insert << "INSERT INTO remwharead "
103  "VALUES(?, ?, ?, ?, ?, ?, ?);",
104  useRef(data.uri), useRef(data.archive_uri),
105  useRef(strdatetime), useRef(strtags), useRef(data.title),
106  useRef(data.description), useRef(data.fulltext);
107  insert.execute();
108  }
109  catch (std::exception &e)
110  {
111  cerr << "Error in " << __func__ << ": " << e.what() << endl;
112  }
113 }

The documentation for this class was generated from the following files: