Mantis App v0.2.8
Loading...
Searching...
No Matches
database.h
Go to the documentation of this file.
1
6#ifndef DATABASE_H
7#define DATABASE_H
8
9#include <memory>
10#include <soci/soci.h>
11#include <nlohmann/json.hpp>
12#include "private-impl/soci_custom_types.hpp"
13
14#include "../app/app.h"
15#include "logging.h"
16
17// #define __file__ "core/tables/database.h"
18
19namespace mantis
20{
21 using json = nlohmann::json;
22
29 {
30 public:
33
40 bool connect(DbType backend, const std::string& conn_str);
41
45 void disconnect() const;
46
51 bool migrate() const;
52
57 [[nodiscard]] std::shared_ptr<soci::session> session() const;
58
63 [[nodiscard]] soci::connection_pool& connectionPool() const;
64
69 [[nodiscard]] bool isConnected() const;
70
71 const std::string __class_name__ = "mantis::DatabaseUnit";
72
73 private:
77 void writeCheckpoint() const;
78
79 std::unique_ptr<soci::connection_pool> m_connPool;
80 };
81
86 class MantisLoggerImpl : public soci::logger_impl
87 {
88 public:
93 void start_query(std::string const& query) override
94 {
95 logger_impl::start_query(query);
96 Log::trace("$ sql << {}", query);
97 // Log::trace("$ sql << {}\n\t└── Values ({})", query, params);
98 }
99
100 private:
105 logger_impl* do_clone() const override
106 {
107 return new MantisLoggerImpl();
108 }
109 };
110}
111
112#endif //DATABASE_H
Database Management Class.
Definition database.h:29
~DatabaseUnit()
Definition database.cpp:27
std::shared_ptr< soci::session > session() const
Get access to a session from the pool.
Definition database.cpp:209
DatabaseUnit()
Definition database.cpp:23
void disconnect() const
CLose all database connections and destroy connection pools.
Definition database.cpp:144
bool migrate() const
Run database migrations, creates the default system tables.
Definition database.cpp:162
const std::string __class_name__
Definition database.h:71
soci::connection_pool & connectionPool() const
Definition database.cpp:214
bool connect(DbType backend, const std::string &conn_str)
Initializes the connection pool & connects to specific database.
Definition database.cpp:32
bool isConnected() const
Check if the database is connected.
Definition database.cpp:219
static void trace(fmt::format_string< Args... > msg, Args &&... args)
Definition logging.h:45
Logger implementation for soci, allowing us to override the default logging behaviour with our own cu...
Definition database.h:87
void start_query(std::string const &query) override
Called before query is executed by soci, we can log the query here.
Definition database.h:93
Wrapper around spdlog's functionality.
nlohmann::json json
Definition mantis.h:35
router.h
Definition app.h:31
DbType
Enum for which database is currently selected.
Definition app.h:46