remwharead  0.8.5
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
36  : _connected(false)
37 {
38  try
39  {
40  _dbpath = get_data_home();
41  if (!fs::exists(_dbpath))
42  {
43  fs::create_directories(_dbpath);
44  }
45  _dbpath /= "database.sqlite";
46 
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;
52 
53  _connected = true;
54  }
55  catch (std::exception &e)
56  {
57  cerr << "Error in " << __func__ << ": " << e.what() << endl;
58  }
59 }

Member Function Documentation

◆ operator bool()

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

Returns true if connected to the database.

Since
0.6.0
62 {
63  return _connected;
64 }

◆ 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
115 {
116  try
117  {
118  Database::entry entrybuf;
119  string datetime;
120  string strtags;
121  Statement select(*_session);
122 
123  // bind() copies the value.
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);
131 
132  list<entry> entries;
133 
134  while(!select.done() && select.execute() != 0)
135  {
136  entrybuf.datetime = string_to_timepoint(datetime, true);
137 
138  vector<string> tags;
139  size_t pos = 0;
140  while (pos != string::npos)
141  {
142  const size_t newpos = strtags.find(',', pos);
143  const string tag = strtags.substr(pos, newpos - pos);
144  if (!tag.empty())
145  {
146  tags.push_back(tag);
147  }
148  pos = newpos;
149  if (pos != string::npos)
150  {
151  ++pos;
152  }
153  }
154  entrybuf.tags = tags;
155 
156  entries.push_back(entrybuf);
157  }
158 
159  return entries;
160  }
161  catch (std::exception &e)
162  {
163  cerr << "Error in " << __func__ << ": " << e.what() << endl;
164  }
165 
166  return {};
167 }

◆ store()

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

Store a Database::entry in the database.

Since
0.6.0
83 {
84  try
85  {
86  const string strdatetime = timepoint_to_string(data.datetime, true);
87  string strtags;
88  Statement insert(*_session);
89 
90  for (const string &tag : data.tags)
91  {
92  strtags += tag;
93  if (tag != *(data.tags.rbegin()))
94  {
95  strtags += ",";
96  }
97  }
98 
99  // useRef() uses the const reference.
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);
105  insert.execute();
106  }
107  catch (std::exception &e)
108  {
109  cerr << "Error in " << __func__ << ": " << e.what() << endl;
110  }
111 }

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