Rewrote database interface.

It is a class now and has a saner return type, a struct.
develop
tastytea 2019-01-12 18:18:49 +01:00
parent 165e9039a7
commit 54919796d7
No known key found for this signature in database
GPG Key ID: CFC39497F1B26E07
2 changed files with 62 additions and 77 deletions

View File

@ -28,20 +28,27 @@
using std::cerr; using std::cerr;
namespace fs = std::experimental::filesystem; namespace fs = std::experimental::filesystem;
const string get_filepath() Database::Database() {};
Database::data::operator bool() const
{ {
string filepath; return !user.empty();
}
const string Database::get_filepath() const
{
fs::path filepath;
xdgHandle xdg; xdgHandle xdg;
xdgInitHandle(&xdg); xdgInitHandle(&xdg);
filepath = xdgDataHome(&xdg); filepath = xdgDataHome(&xdg);
xdgWipeHandle(&xdg); xdgWipeHandle(&xdg);
filepath += "/whyblocked"; filepath /= "whyblocked";
if (!fs::exists(filepath)) if (!fs::exists(filepath))
{ {
fs::create_directories(filepath); fs::create_directories(filepath);
} }
filepath += "/database.sqlite"; filepath /= "database.sqlite";
if (!fs::exists(filepath)) if (!fs::exists(filepath))
{ {
sqlite::connection con(filepath); sqlite::connection con(filepath);
@ -53,7 +60,7 @@ const string get_filepath()
return filepath; return filepath;
} }
bool database::add_block(const string &user, const int blocked, bool Database::add_user(const string &user, const int blocked,
const string &reason) const string &reason)
{ {
try try
@ -72,7 +79,7 @@ bool database::add_block(const string &user, const int blocked,
return true; return true;
} }
bool database::add_receipt(const string &user, const string &receipt) bool Database::add_receipt(const string &user, const string &receipt)
{ {
try try
{ {
@ -90,7 +97,7 @@ bool database::add_receipt(const string &user, const string &receipt)
return true; return true;
} }
bool database::remove(const string &user) bool Database::remove(const string &user)
{ {
try try
{ {
@ -111,78 +118,44 @@ bool database::remove(const string &user)
return true; return true;
} }
bool database::view(result_view &result, const string &sql_query) const vector<Database::data> Database::query(const string &sql_query) const
{
try
{
string query;
if (sql_query.empty())
{
query = "SELECT * FROM blocks;";
}
else
{
query = sql_query;
}
sqlite::connection con(get_filepath());
sqlite::query q(con, query);
sqlite::result_type res = q.get_result();
while(res->next_row())
{
result.push_back(
{
res->get_string(0),
res->get_int(1),
res->get_string(2)
});
}
}
catch (const std::exception &e)
{
cerr << "An error occurred: " << e.what() << std::endl;
return false;
}
return true;
}
bool database::details(const string &user, result_details &result)
{ {
try try
{ {
sqlite::connection con(get_filepath()); sqlite::connection con(get_filepath());
sqlite::query q_blocks(con, sqlite::query q_blocks(con, sql_query);
"SELECT * FROM blocks WHERE user = \'" + user + "\';");
sqlite::result_type res_blocks = q_blocks.get_result(); sqlite::result_type res_blocks = q_blocks.get_result();
std::vector<data> result;
while(res_blocks->next_row())
{
const string user = res_blocks->get_string(0);
const int blocked = res_blocks->get_int(1);
const string reason = res_blocks->get_string(2);
sqlite::query q_urls(con, sqlite::query q_urls(con,
"SELECT * FROM urls WHERE user = \'" + user + "\';"); "SELECT * FROM urls WHERE user = \'" + user + "\';");
sqlite::result_type res_urls = q_urls.get_result(); sqlite::result_type res_urls = q_urls.get_result();
vector<string> receipts;
if (!res_blocks->next_row()) while(res_urls->next_row())
{ {
cerr << user << " is not in the database.\n"; receipts.push_back(res_urls->get_string(1));
return false;
} }
std::vector<string> urls; result.push_back(
while (res_urls->next_row())
{ {
urls.push_back(res_urls->get_string(1)); user,
blocked,
reason,
receipts
});
} }
result = return result;
{
res_blocks->get_int(1),
res_blocks->get_string(2),
urls
};
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
cerr << "An error occurred: " << e.what() << std::endl; cerr << "An error occurred: " << e.what() << std::endl;
return false; return {};
} }
return true;
} }

View File

@ -22,18 +22,30 @@
#include <string> #include <string>
using std::string; using std::string;
using result_view = std::vector<std::tuple<string, int, string>>; using std::vector;
using result_details = std::tuple<int, string, std::vector<string>>;
const string get_filepath(); class Database
namespace database
{ {
bool add_block(const string &user, const int blocked, public:
const string &reason); struct data
{
const string user;
const int blocked;
const string reason;
const vector<string> receipts;
explicit operator bool() const;
};
Database();
bool add_user(const string &user, const int blocked, const string &reason);
bool add_receipt(const string &user, const string &receipt); bool add_receipt(const string &user, const string &receipt);
bool remove(const string &user); bool remove(const string &user);
bool view(result_view &result, const string &sql_query = ""); const vector<data> query(const string &sql_query = "SELECT * FROM blocks;")
bool details(const string &user, result_details &result); const;
}
private:
const string get_filepath() const;
};
#endif // WHYBLOCKED_HPP #endif // WHYBLOCKED_HPP