56#include <unordered_map>
119 bool Connect() noexcept;
125 void Disconnect() noexcept;
131 bool IsConnected() const noexcept {
132 std::lock_guard<std::recursive_mutex> lock(*m_operation_mutex);
141 std::lock_guard<std::recursive_mutex> lock(*m_operation_mutex);
150 std::lock_guard<std::recursive_mutex> lock(*m_operation_mutex);
161 template <
typename... Args>
163 auto telemetry = TrackOperation(Operation::PreparedStatement);
164 std::lock_guard<std::recursive_mutex> lock(*m_operation_mutex);
167 return Unexpected<UnknownSTMT>(name);
169 telemetry.Complete(result.has_value(), result ?
static_cast<std::uint64_t
>(result->Count()) : std::uint64_t{0});
220 m_telemetry->RecordEvent(event);
251 virtual
void DoPreConnect() noexcept {}
262 virtual
void DoPostConnect() noexcept {}
277 virtual
void DoPostDisconnect() noexcept {}
294 void PrepareSTMT(std::string_view name, std::string_view query)
noexcept;
317 std::unordered_map<std::string, StormByte::Unique<PreparedSTMT>> m_prepared_stmts;
Abstract backend.
Definition database.hxx:76
virtual ~Database() noexcept
Destructor.
Database & operator=(const Database &)=delete
Copy assignment (deleted).
Expected< Transaction, TransactionError > BeginTransaction(IsolationLevel level=IsolationLevel::Default)
Begin a transaction.
virtual void DoPreDisconnect() noexcept
Pre-disconnect hook.
Definition database.hxx:267
void ClearPreparedSTMTs() noexcept
Destroy all registered statements inside the Database module.
Database(Database &&other) noexcept
Move constructor.
virtual void DoBeginTransaction(IsolationLevel level)=0
Backend BEGIN with isolation.
Telemetry::OperationScope TrackOperation(Operation operation) const noexcept
Start timing a Database operation without invoking user callbacks.
Definition database.hxx:228
void SetTelemetry(StormByte::Shared< Telemetry > telemetry) noexcept
Replace the telemetry implementation, normally in a concrete backend constructor.
StormByte::Shared< std::recursive_mutex > m_operation_mutex
DLL-safe owner of the connection mutex.
Definition database.hxx:206
void DoPrepareSTMT(std::string_view name, std::string_view query) noexcept
Same as PrepareSTMT; kept for hook symmetry.
void RecordBackendEvent(BackendEvent event) noexcept
Record a categorized event reported by the active backend.
Definition database.hxx:219
virtual void DoDisconnect() noexcept=0
Backend disconnect.
void SetSslMode(SslMode mode) noexcept
TLS policy for the next Connect().
Definition database.hxx:140
virtual ExpectedRows Query(std::string_view query)=0
Execute a query that returns rows.
virtual bool SilentQuery(std::string_view query) noexcept=0
Execute a query that does not return rows.
void PrepareSTMT(std::string_view name, std::string_view query) noexcept
Register a prepared statement under name.
void CommitTransaction()
Commit the current transaction.
void RollbackTransaction()
Roll back the current transaction.
StormByte::Shared< Telemetry > m_telemetry
Shared cumulative counters for this connection.
Definition database.hxx:207
Database & operator=(Database &&other) noexcept
Move assignment.
ExpectedRows ExecuteSTMT(std::string_view name, Args &&...args)
Execute a prepared statement by name.
Definition database.hxx:162
virtual StormByte::Unique< PreparedSTMT > CreatePreparedSTMT(std::string_view name, std::string_view query) noexcept=0
Create a backend prepared statement.
SslMode m_ssl_mode
TLS policy for network backends.
Definition database.hxx:233
virtual bool DoSilentQuery(std::string_view query) noexcept=0
Backend silent query.
StormByte::Shared< Logger::Log > m_logger
Shared logger, safe across the DLL boundary.
Definition database.hxx:235
bool m_connected
Connection state.
Definition database.hxx:232
Database(const Database &)=delete
Copy constructor (deleted).
SslMode GetSslMode() const noexcept
Current TLS policy.
Definition database.hxx:149
Database(const StormByte::Shared< Logger::Log > &logger)
Construct with an optional logger.
virtual bool DoConnect() noexcept=0
Backend connect.
Abstract prepared statement.
Definition prepared_stmt.hxx:69
ExpectedRows Execute(Args &&...args)
Bind arguments and execute.
Definition prepared_stmt.hxx:121
Records one operation and its elapsed time on destruction.
Definition telemetry.hxx:102
Thread-safe cumulative telemetry for one database connection.
Definition telemetry.hxx:96
RAII transaction.
Definition transaction.hxx:67
BackendEvent
Shared error and warning categories for backend-specific telemetry.
Definition telemetry.hxx:54
SslMode
TLS policy for MariaDB / PostgreSQL.
Definition typedefs.hxx:88
Operation
Database operation categories measured by telemetry.
Definition telemetry.hxx:37
Expected< Rows, QueryException > ExpectedRows
Query result: Rows or QueryException.
Definition typedefs.hxx:82
IsolationLevel
Isolation for BeginTransaction().
Definition typedefs.hxx:99
Root namespace of the StormByte C++ suite.
std::conditional_t< Type::Reference< T >, std::expected< std::reference_wrapper< std::remove_reference_t< T > >, Shared< E > >, std::expected< T, Shared< E > > > Expected
#define STORMBYTE_DATABASE_PUBLIC
Definition visibility.h:54