remwharead/src/lib/sqlite.cpp

160 lines
4.8 KiB
C++
Raw Normal View History

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/>.
*/
#include <exception>
#include <iostream>
#include <algorithm>
#include <basedir.h>
2019-05-14 20:45:48 +02:00
#include <sqlite/execute.hpp>
#include <sqlite/query.hpp>
#include "time.hpp"
2019-05-14 20:45:48 +02:00
#include "sqlite.hpp"
2019-07-27 09:59:43 +02:00
namespace remwharead
2019-05-14 20:45:48 +02:00
{
2019-07-27 09:59:43 +02:00
using std::cerr;
using std::endl;
2019-05-14 20:45:48 +02:00
2019-07-27 09:59:43 +02:00
Database::Database()
: _connected(false)
{
try
2019-05-14 20:45:48 +02:00
{
xdgHandle xdg;
xdgInitHandle(&xdg);
_dbpath = xdgDataHome(&xdg) / fs::path("remwharead");
xdgWipeHandle(&xdg);
2019-05-14 20:45:48 +02:00
2019-07-27 09:59:43 +02:00
if (!fs::exists(_dbpath))
{
fs::create_directories(_dbpath);
}
_dbpath /= "database.sqlite";
2019-05-14 20:45:48 +02:00
2019-07-27 09:59:43 +02:00
_con = std::make_unique<sqlite::connection>(_dbpath);
sqlite::execute(*_con, "CREATE TABLE IF NOT EXISTS remwharead("
"uri TEXT, archive_uri TEXT, datetime TEXT, "
"tags TEXT, title TEXT, description TEXT, "
"fulltext TEXT);", true);
_connected = true;
}
catch (std::exception &e)
{
cerr << "Error in " << __func__ << ": " << e.what() << endl;
}
2019-05-14 20:45:48 +02:00
}
2019-07-27 09:59:43 +02:00
Database::operator bool() const
2019-05-14 20:45:48 +02:00
{
2019-07-27 09:59:43 +02:00
return _connected;
2019-05-14 20:45:48 +02:00
}
2019-07-27 09:59:43 +02:00
bool operator ==(const Database::entry &a, const Database::entry &b)
2019-05-19 12:47:38 +02:00
{
return (a.datetime == b.datetime);
2019-07-27 09:59:43 +02:00
}
2019-05-19 12:47:38 +02:00
2019-07-27 09:59:43 +02:00
const string Database::entry::fulltext_oneline() const
{
2019-07-27 09:59:43 +02:00
string oneline = fulltext;
size_t pos = 0;
while ((pos = oneline.find('\n', pos)) != std::string::npos)
{
oneline.replace(pos, 1, "\\n");
}
return oneline;
}
2019-05-16 03:56:17 +02:00
2019-07-27 09:59:43 +02:00
void Database::store(const Database::entry &data) const
2019-05-14 20:45:48 +02:00
{
2019-07-27 09:59:43 +02:00
try
{
2019-07-27 09:59:43 +02:00
const string strdatetime = timepoint_to_string(data.datetime, true);
string strtags;
for (const string &tag : data.tags)
{
2019-07-27 09:59:43 +02:00
strtags += tag;
if (tag != *(data.tags.rbegin()))
{
strtags += ",";
}
}
2019-07-27 09:59:43 +02:00
sqlite::execute ins(*_con, "INSERT INTO remwharead "
"VALUES(?, ?, ?, ?, ?, ?, ?);");
ins % data.uri % data.archive_uri % strdatetime % strtags
% data.title % data.description % data.fulltext;
ins();
}
catch (std::exception &e)
{
cerr << "Error in " << __func__ << ": " << e.what() << endl;
}
2019-05-14 20:45:48 +02:00
}
2019-05-16 00:05:18 +02:00
2019-07-27 09:59:43 +02:00
const vector<Database::entry> Database::retrieve(
const time_point &start, const time_point &end) const
2019-05-16 00:05:18 +02:00
{
2019-07-27 09:59:43 +02:00
try
{
const string query = "SELECT * FROM remwharead WHERE datetime "
"BETWEEN '" + timepoint_to_string(start, true)
+ "' AND '" + timepoint_to_string(end, true)
+ "' ORDER BY datetime DESC;";
2019-05-16 00:05:18 +02:00
2019-07-27 09:59:43 +02:00
sqlite::query q(*_con, query);
sqlite::result_type res = q.get_result();
vector<entry> entries;
2019-05-16 00:05:18 +02:00
2019-07-27 09:59:43 +02:00
while(res->next_row())
2019-05-16 00:05:18 +02:00
{
2019-07-27 09:59:43 +02:00
vector<string> tags;
const string strtags = res->get_string(3);
size_t pos = 0;
while (pos != std::string::npos)
2019-05-16 00:05:18 +02:00
{
2019-07-27 09:59:43 +02:00
const size_t newpos = strtags.find(',', pos);
tags.push_back(strtags.substr(pos, newpos - pos));
pos = newpos;
if (pos != std::string::npos)
{
++pos;
}
2019-05-16 00:05:18 +02:00
}
2019-07-27 09:59:43 +02:00
entries.push_back
({
res->get_string(0),
res->get_string(1),
string_to_timepoint(res->get_string(2), true),
tags,
res->get_string(4),
res->get_string(5),
res->get_string(6)
});
2019-05-16 00:05:18 +02:00
}
2019-07-27 09:59:43 +02:00
return entries;
}
catch (std::exception &e)
{
cerr << "Error in " << __func__ << ": " << e.what() << endl;
2019-05-16 00:05:18 +02:00
}
2019-07-27 09:59:43 +02:00
return {};
2019-05-16 00:05:18 +02:00
}
}