StormByte-Database 2.0.0
C++26 database module of the StormByte suite
 
Loading...
Searching...
No Matches
database.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Database.
5 *
6 * StormByte-Database original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Database source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, and in particular the bundled StormByte-Logger tree and
23 * the PostgreSQL, MariaDB and SQLite trees), which remain under their own
24 * licenses.
25 *
26 * Neither license grants any patent rights. Any patent licenses required
27 * to use this software or third-party components must be obtained separately
28 * from the patent holders.
29 *
30 * StormByte-Database is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU Lesser General Public License for more details.
34 *
35 * You should have received a copy of the GNU Lesser General Public License
36 * version 3 along with StormByte-Database. If not, see
37 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
38 *
39 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
40 */
41
42#pragma once
43
51
52#include <memory>
53#include <mutex>
54#include <string>
55#include <string_view>
56#include <unordered_map>
57
62namespace StormByte {
67 namespace Database {
77 public:
83
87 Database(const Database &) = delete;
88
92 Database(Database &&other) noexcept;
93
97 Database &operator=(const Database &) = delete;
98
102 Database &operator=(Database &&other) noexcept;
103
107 virtual ~Database() noexcept;
108
113 StormByte::Shared<Telemetry> GetTelemetry() const noexcept;
114
119 bool Connect() noexcept;
120
125 void Disconnect() noexcept;
126
131 bool IsConnected() const noexcept {
132 std::lock_guard<std::recursive_mutex> lock(*m_operation_mutex);
133 return m_connected;
134 }
135
140 void SetSslMode(SslMode mode) noexcept {
141 std::lock_guard<std::recursive_mutex> lock(*m_operation_mutex);
142 m_ssl_mode = mode;
143 }
144
149 SslMode GetSslMode() const noexcept {
150 std::lock_guard<std::recursive_mutex> lock(*m_operation_mutex);
151 return m_ssl_mode;
152 }
153
161 template <typename... Args>
162 ExpectedRows ExecuteSTMT(std::string_view name, Args &&...args) {
163 auto telemetry = TrackOperation(Operation::PreparedStatement);
164 std::lock_guard<std::recursive_mutex> lock(*m_operation_mutex);
165 PreparedSTMT *statement = FindPreparedSTMT(name);
166 if (!statement)
167 return Unexpected<UnknownSTMT>(name);
168 ExpectedRows result = statement->Execute(std::forward<Args>(args)...);
169 telemetry.Complete(result.has_value(), result ? static_cast<std::uint64_t>(result->Count()) : std::uint64_t{0});
170 return result;
171 }
172
178 virtual ExpectedRows Query(std::string_view query) = 0;
179
185 virtual bool SilentQuery(std::string_view query) noexcept = 0;
186
193
198
203
204 protected:
205 friend class Transaction;
208
214
219 void RecordBackendEvent(BackendEvent event) noexcept {
220 m_telemetry->RecordEvent(event);
221 }
222
229 return Telemetry::OperationScope{m_telemetry, operation};
230 }
231
234
236
240 void ClearPreparedSTMTs() noexcept;
241
251 virtual void DoPreConnect() noexcept {}
252
257 virtual bool DoConnect() noexcept = 0;
258
262 virtual void DoPostConnect() noexcept {}
263
267 virtual void DoPreDisconnect() noexcept {}
268
272 virtual void DoDisconnect() noexcept = 0;
273
277 virtual void DoPostDisconnect() noexcept {}
278
287 virtual StormByte::Unique<PreparedSTMT> CreatePreparedSTMT(std::string_view name, std::string_view query) noexcept = 0;
288
294 void PrepareSTMT(std::string_view name, std::string_view query) noexcept;
295
301 void DoPrepareSTMT(std::string_view name, std::string_view query) noexcept;
302
307 virtual void DoBeginTransaction(IsolationLevel level) = 0;
308
314 virtual bool DoSilentQuery(std::string_view query) noexcept = 0;
315
316 private:
317 std::unordered_map<std::string, StormByte::Unique<PreparedSTMT>> m_prepared_stmts;
318
324 PreparedSTMT *FindPreparedSTMT(std::string_view name);
325 };
326 }
327}
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