remwharead  0.8.4
Classes | Public Types | 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 Types

typedef struct remwharead::Database::entry 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...
 
const 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

Member Typedef Documentation

◆ entry

Describes a database entry.

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

Returns true if connected to the database.

Since
0.6.0
64  {
65  return _connected;
66  }

◆ retrieve()

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

◆ 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: