droplet 1.3.0
A multipurpose Discord bot with the hacker in mind
Loading...
Searching...
No Matches
Database.hpp
Go to the documentation of this file.
1/*
2 * (c) Copyright erarnitox.de - All rights reserved
3 * Author: Erarnitox <david@erarnitox.de>
4 *
5 * License: MIT License
6 *
7 * Description: This class manages Database access
8 * and provides a high level API for internal use
9 *
10 * Documentation: https://droplet.erarnitox.de/doxygen/html/database
11 */
12
13#pragma once
14
15#include <iostream>
16#include <string>
17#include <vector>
18
19#include "DatabaseBackend.hpp"
20#include "RowDTOAdapter.hpp"
21
27class Database {
28 public:
29 Database() = delete;
30
31 [[nodiscard]] static bool connect(const std::string& db_name,
32 const std::string& user,
33 const std::string& password,
34 const std::string& host,
35 const std::string& port);
36
37 [[nodiscard]] static bool connect(const std::string& connection_string);
38
39 static bool hasConnection() noexcept;
40 static void reconnect() noexcept;
41 static void disconnect() noexcept;
42 static NativeDatabase::Connection* getConnection() noexcept;
43};
44
45namespace database {
46
54template <typename... Types>
55[[nodiscard]] bool execQuery(const std::string& query, Types&&... args) noexcept {
56 static int times = 0;
57 try {
59
60 // perform the database transaction
61 const NativeDatabase::ParameterList params{std::forward<Types>(args)...};
62 const NativeDatabase::Result result = txn.exec(query, params);
63
64 txn.commit();
65
66 times = 0;
67 return true;
69 ++times;
70 if (times > 10) {
71 times = 0;
72 return false;
73 }
75 return execQuery(query, args...);
76 } catch (const std::exception& e) {
77 std::cerr << "Error: " << e.what() << "\nInvalid exec for query:\n" << query << std::endl;
78 return false;
79 }
80}
81
89template <typename... Types>
90[[nodiscard]] RowDTOAdapter execSelect(const std::string& query, Types&&... args) noexcept {
91 static int times = 0;
92 try {
94
95 // perform the database transaction
96 const NativeDatabase::ParameterList params{std::forward<Types>(args)...};
97 const NativeDatabase::Result result = txn.exec(query, params);
98 txn.commit();
99
100 times = 0;
101
102 return {result[0]};
104 ++times;
105 if (times > 10) {
106 times = 0;
107 return {NativeDatabase::Row()};
108 }
110 return execSelect(query, args...);
111 } catch (const std::exception& e) {
112 std::cerr << "Error: " << e.what() << "\nInvalid selection for query:\n" << query << std::endl;
113 return {{}}; // return an empty selection
114 }
115}
116
124template <typename... Types>
125[[nodiscard]] std::vector<RowDTOAdapter> execSelectAll(const std::string& query, Types&&... args) noexcept {
126 static int times = 0;
127 try {
129
130 // perform the database transaction
131 const NativeDatabase::ParameterList params{std::forward<Types>(args)...};
132 const NativeDatabase::Result result = txn.exec(query, params);
133
134 txn.commit();
135
136 times = 0;
137
138 std::vector<RowDTOAdapter> selection;
139 for (int i{0}; i < result.size(); ++i) {
140 selection.emplace_back(result[i]);
141 }
142
143 return selection;
145 ++times;
146 if (times > 10) {
147 times = 0;
148 return {NativeDatabase::Row()};
149 }
151 return execSelectAll(query, args...);
152 } catch (const std::exception& e) {
153 std::cerr << "Error: " << e.what() << "\nInvalid selection for query:\n" << query << std::endl;
154 return {{}}; // return an empty selection
155 }
156}
157} // namespace database
static bool hasConnection() noexcept
checks if the database has an active connection
Definition Database.cpp:68
static NativeDatabase::Connection * getConnection() noexcept
returns the database connection. Reconnects if the Database has no active connection
Definition Database.cpp:107
Database()=delete
static void disconnect() noexcept
disconnect from a postgres database
Definition Database.cpp:60
static bool connect(const std::string &db_name, const std::string &user, const std::string &password, const std::string &host, const std::string &port)
connect to a postgres database
Definition Database.cpp:32
static void reconnect() noexcept
reconnects to the database server if the connection was lost
Definition Database.cpp:78
Definition RowDTOAdapter.hpp:20
Definition DatabaseBackend.hpp:30
pqxx::broken_connection BrokenConnectionException
Definition DatabaseBackend.hpp:33
pqxx::work Transaction
Definition DatabaseBackend.hpp:34
pqxx::row Row
Definition DatabaseBackend.hpp:31
pqxx::result Result
Definition DatabaseBackend.hpp:36
pqxx::params ParameterList
Definition DatabaseBackend.hpp:35
Definition Database.hpp:45
std::vector< RowDTOAdapter > execSelectAll(const std::string &query, Types &&... args) noexcept
executes a Select on the database
Definition Database.hpp:125
bool execQuery(const std::string &query, Types &&... args) noexcept
executes a query on the database
Definition Database.hpp:55
RowDTOAdapter execSelect(const std::string &query, Types &&... args) noexcept
executes a Select on the database
Definition Database.hpp:90