2019-05-14 20:45:48 +02:00
|
|
|
/* This file is part of remwharead.
|
|
|
|
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
#include "sqlite.hpp"
|
|
|
|
#include "time.hpp"
|
|
|
|
#include <Poco/Data/SQLite/Connector.h>
|
|
|
|
#include <Poco/Data/Session.h>
|
2019-05-14 22:57:51 +02:00
|
|
|
#include <algorithm>
|
2019-08-05 19:11:37 +02:00
|
|
|
#include <basedir.h>
|
2019-09-30 13:20:36 +02:00
|
|
|
#include <exception>
|
|
|
|
#include <iostream>
|
2019-05-14 20:45:48 +02:00
|
|
|
|
2019-07-27 09:59:43 +02:00
|
|
|
namespace remwharead
|
2019-05-14 20:45:48 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
using namespace Poco::Data::Keywords;
|
|
|
|
using Poco::Data::Statement;
|
2019-05-14 20:45:48 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
Database::Database()
|
|
|
|
: _connected(false)
|
|
|
|
{
|
|
|
|
try
|
2019-07-27 09:59:43 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
xdgHandle xdg;
|
|
|
|
xdgInitHandle(&xdg);
|
|
|
|
_dbpath = xdgDataHome(&xdg) / fs::path("remwharead");
|
|
|
|
xdgWipeHandle(&xdg);
|
2019-05-14 20:45:48 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
if (!fs::exists(_dbpath))
|
2019-07-27 09:59:43 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
fs::create_directories(_dbpath);
|
2019-07-27 09:59:43 +02:00
|
|
|
}
|
2019-09-30 13:20:36 +02:00
|
|
|
_dbpath /= "database.sqlite";
|
2019-07-27 09:59:43 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
Poco::Data::SQLite::Connector::registerConnector();
|
|
|
|
_session = std::make_unique<Session>("SQLite", _dbpath);
|
|
|
|
*_session << "CREATE TABLE IF NOT EXISTS remwharead("
|
|
|
|
"uri TEXT, archive_uri TEXT, datetime TEXT, "
|
|
|
|
"tags TEXT, title TEXT, description TEXT, fulltext TEXT);", now;
|
2019-05-14 20:45:48 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
_connected = true;
|
|
|
|
}
|
|
|
|
catch (std::exception &e)
|
2019-05-19 12:47:38 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
cerr << "Error in " << __func__ << ": " << e.what() << endl;
|
2019-07-27 09:59:43 +02:00
|
|
|
}
|
2019-09-30 13:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Database::operator bool() const
|
|
|
|
{
|
|
|
|
return _connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator ==(const Database::entry &a, const Database::entry &b)
|
|
|
|
{
|
|
|
|
return (a.datetime == b.datetime);
|
|
|
|
}
|
2019-05-19 12:47:38 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
string Database::entry::fulltext_oneline() const
|
|
|
|
{
|
|
|
|
string oneline = fulltext;
|
|
|
|
size_t pos = 0;
|
|
|
|
while ((pos = oneline.find('\n', pos)) != string::npos)
|
2019-05-20 10:50:49 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
oneline.replace(pos, 1, "\\n");
|
2019-05-20 10:50:49 +02:00
|
|
|
}
|
2019-09-30 13:20:36 +02:00
|
|
|
return oneline;
|
|
|
|
}
|
2019-05-16 03:56:17 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
void Database::store(const Database::entry &data) const
|
|
|
|
{
|
|
|
|
try
|
2019-05-14 20:45:48 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
const string strdatetime = timepoint_to_string(data.datetime, true);
|
|
|
|
string strtags;
|
|
|
|
Statement insert(*_session);
|
2019-08-07 19:41:58 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
for (const string &tag : data.tags)
|
|
|
|
{
|
|
|
|
strtags += tag;
|
|
|
|
if (tag != *(data.tags.rbegin()))
|
2019-05-14 22:57:51 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
strtags += ",";
|
2019-05-14 22:57:51 +02:00
|
|
|
}
|
2019-07-27 09:59:43 +02:00
|
|
|
}
|
2019-09-30 13:20:36 +02:00
|
|
|
|
|
|
|
// useRef() uses the const reference.
|
|
|
|
insert << "INSERT INTO remwharead "
|
|
|
|
"VALUES(?, ?, ?, ?, ?, ?, ?);",
|
|
|
|
useRef(data.uri), useRef(data.archive_uri),
|
|
|
|
useRef(strdatetime), useRef(strtags), useRef(data.title),
|
|
|
|
useRef(data.description), useRef(data.fulltext);
|
|
|
|
insert.execute();
|
2019-05-14 20:45:48 +02:00
|
|
|
}
|
2019-09-30 13:20:36 +02:00
|
|
|
catch (std::exception &e)
|
|
|
|
{
|
|
|
|
cerr << "Error in " << __func__ << ": " << e.what() << endl;
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 00:05:18 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
list<Database::entry> Database::retrieve(const time_point &start,
|
|
|
|
const time_point &end) const
|
|
|
|
{
|
|
|
|
try
|
2019-05-16 00:05:18 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
Database::entry entrybuf;
|
|
|
|
string datetime;
|
|
|
|
string strtags;
|
|
|
|
Statement select(*_session);
|
|
|
|
|
|
|
|
// bind() copies the value.
|
|
|
|
select << "SELECT * FROM remwharead WHERE datetime "
|
|
|
|
"BETWEEN ? AND ? ORDER BY datetime DESC;",
|
|
|
|
bind(timepoint_to_string(start, true)),
|
|
|
|
bind(timepoint_to_string(end, true)),
|
|
|
|
into(entrybuf.uri), into(entrybuf.archive_uri), into(datetime),
|
|
|
|
into(strtags), into(entrybuf.title), into(entrybuf.description),
|
|
|
|
into(entrybuf.fulltext), range(0, 1);
|
|
|
|
|
|
|
|
list<entry> entries;
|
|
|
|
|
|
|
|
while(!select.done() && select.execute() != 0)
|
2019-07-27 09:59:43 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
entrybuf.datetime = string_to_timepoint(datetime, true);
|
2019-08-07 19:41:58 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
vector<string> tags;
|
|
|
|
size_t pos = 0;
|
|
|
|
while (pos != string::npos)
|
|
|
|
{
|
|
|
|
const size_t newpos = strtags.find(',', pos);
|
|
|
|
const string tag = strtags.substr(pos, newpos - pos);
|
|
|
|
if (!tag.empty())
|
2019-05-16 00:05:18 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
tags.push_back(tag);
|
|
|
|
}
|
|
|
|
pos = newpos;
|
|
|
|
if (pos != string::npos)
|
|
|
|
{
|
|
|
|
++pos;
|
2019-05-16 00:05:18 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-30 13:20:36 +02:00
|
|
|
entrybuf.tags = tags;
|
2019-07-27 09:59:43 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
entries.push_back(entrybuf);
|
2019-05-16 00:05:18 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
return entries;
|
2019-05-16 00:05:18 +02:00
|
|
|
}
|
2019-09-30 13:20:36 +02:00
|
|
|
catch (std::exception &e)
|
|
|
|
{
|
|
|
|
cerr << "Error in " << __func__ << ": " << e.what() << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2019-09-25 03:58:29 +02:00
|
|
|
} // namespace remwharead
|