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

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

#include <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 vector< entryretrieve (const time_point &start=time_point(), const time_point &end=system_clock::now()) const
 Retrieve a vector of Database::entry from the database. More...
 

Detailed Description

Store and retrieve files from/to SQLite.

Member Typedef Documentation

◆ entry

Describes a database entry.

Constructor & Destructor Documentation

◆ Database()

remwharead::Database::Database ( )

Connects to the database and creates it if necessary.

32  : _connected(false)
33  {
34  try
35  {
36  xdgHandle xdg;
37  xdgInitHandle(&xdg);
38  _dbpath = xdgDataHome(&xdg) / fs::path("remwharead");
39  xdgWipeHandle(&xdg);
40 
41  if (!fs::exists(_dbpath))
42  {
43  fs::create_directories(_dbpath);
44  }
45  _dbpath /= "database.sqlite";
46 
47  _con = std::make_unique<sqlite::connection>(_dbpath);
48  sqlite::execute(*_con, "CREATE TABLE IF NOT EXISTS remwharead("
49  "uri TEXT, archive_uri TEXT, datetime TEXT, "
50  "tags TEXT, title TEXT, description TEXT, "
51  "fulltext TEXT);", true);
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

Returns true if connected to the database.

62  {
63  return _connected;
64  }

◆ retrieve()

const vector< Database::entry > remwharead::Database::retrieve ( const time_point &  start = time_point(),
const time_point &  end = system_clock::now() 
) const

Retrieve a vector of Database::entry from the database.

116  {
117  try
118  {
119  const string query = "SELECT * FROM remwharead WHERE datetime "
120  "BETWEEN '" + timepoint_to_string(start, true)
121  + "' AND '" + timepoint_to_string(end, true)
122  + "' ORDER BY datetime DESC;";
123 
124  sqlite::query q(*_con, query);
125  sqlite::result_type res = q.get_result();
126  vector<entry> entries;
127 
128  while(res->next_row())
129  {
130  vector<string> tags;
131  const string strtags = res->get_string(3);
132  size_t pos = 0;
133  while (pos != std::string::npos)
134  {
135  const size_t newpos = strtags.find(',', pos);
136  tags.push_back(strtags.substr(pos, newpos - pos));
137  pos = newpos;
138  if (pos != std::string::npos)
139  {
140  ++pos;
141  }
142  }
143  entries.push_back
144  ({
145  res->get_string(0),
146  res->get_string(1),
147  string_to_timepoint(res->get_string(2), true),
148  tags,
149  res->get_string(4),
150  res->get_string(5),
151  res->get_string(6)
152  });
153  }
154 
155  return entries;
156  }
157  catch (std::exception &e)
158  {
159  cerr << "Error in " << __func__ << ": " << e.what() << endl;
160  }
161 
162  return {};
163  }

◆ store()

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

Store a Database::entry in the database.

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

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