remwharead  0.9.1
Classes | Public Member Functions | Static 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...
 
size_t remove (const string &uri)
 Remove all entries with this URI from database. More...
 

Static Public Member Functions

static string tags_to_string (const vector< string > &tags)
 Returns tags as comma separated string. 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 }

◆ remove()

size_t remwharead::Database::remove ( const string &  uri)

Remove all entries with this URI from database.

Returns
Number of removed entries.
Since
0.9.0
161 {
162  Statement del(*_session);
163 
164  del << "DELETE FROM remwharead WHERE uri = ?;", bind(uri);
165 
166  return del.execute();
167 }

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

◆ 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 = tags_to_string(data.tags);
88  Statement insert(*_session);
89 
90  // useRef() uses the const reference.
91  insert << "INSERT INTO remwharead "
92  "VALUES(?, ?, ?, ?, ?, ?, ?);",
93  useRef(data.uri), useRef(data.archive_uri),
94  useRef(strdatetime), useRef(strtags), useRef(data.title),
95  useRef(data.description), useRef(data.fulltext);
96  insert.execute();
97  }
98  catch (std::exception &e)
99  {
100  cerr << "Error in " << __func__ << ": " << e.what() << endl;
101  }
102 }
static string tags_to_string(const vector< string > &tags)
Returns tags as comma separated string.
Definition: sqlite.cpp:169

◆ tags_to_string()

string remwharead::Database::tags_to_string ( const vector< string > &  tags)
static

Returns tags as comma separated string.

Since
0.9.0
170 {
171  string strtags;
172 
173  for (const string &tag : tags)
174  {
175  strtags += tag;
176  if (tag != *(tags.rbegin()))
177  {
178  strtags += ',';
179  }
180  }
181 
182  return strtags;
183 }

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