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.

111  {
112  try
113  {
114  const string query = "SELECT * FROM remwharead WHERE datetime "
115  "BETWEEN '" + timepoint_to_string(start, true)
116  + "' AND '" + timepoint_to_string(end, true)
117  + "' ORDER BY datetime DESC;";
118 
119  sqlite::query q(*_con, query);
120  sqlite::result_type res = q.get_result();
121  vector<entry> entries;
122 
123  while(res->next_row())
124  {
125  vector<string> tags;
126  const string strtags = res->get_string(3);
127  size_t pos = 0;
128  while (pos != std::string::npos)
129  {
130  const size_t newpos = strtags.find(',', pos);
131  tags.push_back(strtags.substr(pos, newpos - pos));
132  pos = newpos;
133  if (pos != std::string::npos)
134  {
135  ++pos;
136  }
137  }
138  entries.push_back
139  ({
140  res->get_string(0),
141  res->get_string(1),
142  string_to_timepoint(res->get_string(2), true),
143  tags,
144  res->get_string(4),
145  res->get_string(5),
146  res->get_string(6)
147  });
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.

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

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