StormByte-Database 2.0.0
C++26 database module of the StormByte suite
 
Loading...
Searching...
No Matches
prepared_stmt.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
48#include <StormByte/size.hxx>
49
50#include <memory>
51#include <string>
52#include <string_view>
53#include <utility>
54
59namespace StormByte {
64 namespace Database {
70 public:
77 PreparedSTMT(std::string_view name, std::string_view query, const StormByte::Shared<Logger::Log>& logger);
78
86 PreparedSTMT(std::string_view name, std::string_view query, const StormByte::Shared<Logger::Log>& logger,
87 const StormByte::Shared<Telemetry>& telemetry);
88
92 PreparedSTMT(const PreparedSTMT &other) = delete;
93
97 PreparedSTMT(PreparedSTMT &&other) noexcept;
98
102 virtual ~PreparedSTMT() noexcept;
103
107 PreparedSTMT &operator=(const PreparedSTMT &other) = delete;
108
112 PreparedSTMT &operator=(PreparedSTMT &&other) noexcept;
113
120 template <typename... Args>
121 ExpectedRows Execute(Args &&...args) {
122 Reset();
123 StormByte::Size idx{};
124 (void)((Bind(idx++, std::forward<Args>(args))), ...);
125 ExpectedRows result = DoExecute();
126 Reset();
127 return result;
128 }
129
134 inline std::string_view Name() const noexcept {
135 return m_name;
136 }
137
142 inline std::string_view Query() const noexcept {
143 return m_query;
144 }
145
146 protected:
149
154 void RecordBackendEvent(BackendEvent event) noexcept {
155 if (m_telemetry)
156 m_telemetry->RecordEvent(event);
157 }
158
165 template <typename T>
166 void Bind(StormByte::Size index, T &&value) noexcept {
167 Binder(index, Value(std::forward<T>(value)));
168 }
169
174 void Bind(StormByte::Size index, std::nullptr_t) noexcept {
175 Binder(index, Value());
176 }
177
178 private:
179 std::string m_name;
180 std::string m_query;
186 virtual void Binder(StormByte::Size index, Value &&value) noexcept = 0;
187
191 virtual void Reset() noexcept = 0;
192
197 virtual ExpectedRows DoExecute() = 0;
198 };
199 }
200}
Abstract prepared statement.
Definition prepared_stmt.hxx:69
PreparedSTMT(std::string_view name, std::string_view query, const StormByte::Shared< Logger::Log > &logger, const StormByte::Shared< Telemetry > &telemetry)
Copy statement metadata and share connection telemetry.
std::string_view Name() const noexcept
Statement name.
Definition prepared_stmt.hxx:134
PreparedSTMT(PreparedSTMT &&other) noexcept
Move constructor.
virtual ~PreparedSTMT() noexcept
Destructor.
PreparedSTMT(std::string_view name, std::string_view query, const StormByte::Shared< Logger::Log > &logger)
Copy name, query and logger.
void RecordBackendEvent(BackendEvent event) noexcept
Record a backend event against the owning connection.
Definition prepared_stmt.hxx:154
StormByte::Shared< Logger::Log > m_logger
Shared logger, safe across the DLL boundary.
Definition prepared_stmt.hxx:147
StormByte::Shared< Telemetry > m_telemetry
Shared connection telemetry.
Definition prepared_stmt.hxx:148
std::string_view Query() const noexcept
SQL text.
Definition prepared_stmt.hxx:142
void Bind(StormByte::Size index, T &&value) noexcept
Bind a value at index.
Definition prepared_stmt.hxx:166
void Bind(StormByte::Size index, std::nullptr_t) noexcept
Bind SQL NULL at index.
Definition prepared_stmt.hxx:174
PreparedSTMT(const PreparedSTMT &other)=delete
Copy constructor (deleted).
Type-erased SQL value (NULL, integers, double, text, blob, bool).
Definition value.hxx:68
BackendEvent
Shared error and warning categories for backend-specific telemetry.
Definition telemetry.hxx:54
Expected< Rows, QueryException > ExpectedRows
Query result: Rows or QueryException.
Definition typedefs.hxx:82
Root namespace of the StormByte C++ suite.
#define STORMBYTE_DATABASE_PUBLIC
Definition visibility.h:54