Mantis App v0.1.13
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 public:
31
38 bool connect(DbType backend, const std::string& conn_str);
39
43 void disconnect() const;
44
48 void migrate() const;
49
54 [[nodiscard]] std::shared_ptr<soci::session> session() const;
55
60 [[nodiscard]] soci::connection_pool& connectionPool() const;
61
66 [[nodiscard]] bool isConnected() const;
67
73 static std::string tmToISODate(const std::tm& t);
74
75 const std::string __class_name__ = "mantis::DatabaseUnit";
76 private:
77 std::unique_ptr<soci::connection_pool> m_connPool;
78 };
79
84 class MantisLoggerImpl : public soci::logger_impl
85 {
86 public:
91 void start_query(std::string const & query) override
92 {
93 logger_impl::start_query(query);
94 Log::trace("$ sql << {}", query);
95 }
96
97 private:
102 logger_impl* do_clone() const override
103 {
104 return new MantisLoggerImpl();
105 }
106 };
107}
108
109#endif //DATABASE_H
Database Management Class.
Definition database.h:28
std::shared_ptr< soci::session > session() const
Get access to a session from the pool.
Definition database.cpp:141
DatabaseUnit()
Definition database.cpp:17
void disconnect() const
CLose all database connections and destroy connection pools.
Definition database.cpp:82
const std::string __class_name__
Definition database.h:75
soci::connection_pool & connectionPool() const
Definition database.cpp:146
void migrate() const
Run database migrations, creates the default system tables.
Definition database.cpp:97
bool connect(DbType backend, const std::string &conn_str)
Initializes the connection pool & connects to specific database.
Definition database.cpp:19
static std::string tmToISODate(const std::tm &t)
Convert c++ std::tm date/time value to ISO formatted string.
Definition database.cpp:159
bool isConnected() const
Check if the database is connected.
Definition database.cpp:151
static void trace(fmt::format_string< Args... > msg, Args &&... args)
Definition logging.h:47
Logger implementation for soci, allowing us to override the default logging behaviour with our own cu...
Definition database.h:85
void start_query(std::string const &query) override
Called before query is executed by soci, we can log the query here.
Definition database.h:91
Wrapper around spdlog's functionality.
nlohmann::json json
Definition mantis.h:35
router.h
Definition app.h:30
DbType
Enum for which database is currently selected.
Definition app.h:44