StormByte C++ Library: Database module 0.0.9999
StormByte-Database is a StormByte library module for handling database connections
Loading...
Searching...
No Matches
prepared_stmt.hxx
1#pragma once
2
3#include <StormByte/database/rows.hxx>
4
9namespace StormByte::Database {
14 class STORMBYTE_DATABASE_PUBLIC PreparedSTMT {
15 public:
21 PreparedSTMT(const std::string& name, const std::string& query) noexcept:
22 m_name(name), m_query(query) {}
23
29 PreparedSTMT(std::string&& name, std::string&& query) noexcept:
30 m_name(std::move(name)), m_query(std::move(query)) {}
31
36 PreparedSTMT(const PreparedSTMT& other) = delete;
37
42 PreparedSTMT(PreparedSTMT&& other) = default;
43
47 virtual ~PreparedSTMT() = default;
48
54 PreparedSTMT& operator=(const PreparedSTMT& other) = delete;
55
61 PreparedSTMT& operator=(PreparedSTMT&& other) = default;
62
69 template<typename... Args>
70 ExpectedRows Execute(Args&&... args) {
71 Reset();
72 // Bind implementations expect a zero-based index and add +1 internally
73 std::size_t idx = 0;
74 (void)( ( Bind(static_cast<int>(idx++), std::forward<Args>(args)) ), ... );
75 ExpectedRows result = DoExecute();
76 Reset();
77 return result;
78 }
79
84 inline const std::string& Name() const noexcept {
85 return m_name;
86 }
87
92 inline const std::string& Query() const noexcept {
93 return m_query;
94 }
95
96 protected:
97 std::string m_name;
98 std::string m_query;
99
100 private:
106 virtual void Bind(const int& index, const std::nullptr_t& value) noexcept = 0;
107
113 virtual void Bind(const int& index, const int& value) noexcept = 0;
114
120 virtual void Bind(const int& index, const unsigned int& value) noexcept = 0;
121
127 virtual void Bind(const int& index, const int64_t& value) noexcept = 0;
128
134 virtual void Bind(const int& index, const uint64_t& value) noexcept = 0;
135
141 virtual void Bind(const int& index, const double& value) noexcept = 0;
142
148 virtual void Bind(const int& index, bool value) noexcept = 0;
149
155 virtual void Bind(const int& index, const std::string& value) noexcept = 0;
156
160 virtual void Bind(const int& index, const std::vector<std::byte>& value) noexcept = 0;
161
167 inline void Bind(const int& index, const char* value) noexcept {
168 Bind(index, std::string(value));
169 }
170
174 virtual void Reset() noexcept = 0;
175
180 virtual ExpectedRows DoExecute() = 0;
181 };
182}
Prepared statement for databases.
Definition prepared_stmt.hxx:14
const std::string & Name() const noexcept
Definition prepared_stmt.hxx:84
PreparedSTMT & operator=(const PreparedSTMT &other)=delete
const std::string & Query() const noexcept
Definition prepared_stmt.hxx:92
std::string m_query
Query to prepare.
Definition prepared_stmt.hxx:98
std::string m_name
Name of the prepared statement.
Definition prepared_stmt.hxx:97
PreparedSTMT(std::string &&name, std::string &&query) noexcept
Definition prepared_stmt.hxx:29
PreparedSTMT(PreparedSTMT &&other)=default
PreparedSTMT & operator=(PreparedSTMT &&other)=default
ExpectedRows Execute(Args &&... args)
Definition prepared_stmt.hxx:70
PreparedSTMT(const std::string &name, const std::string &query) noexcept
Definition prepared_stmt.hxx:21
PreparedSTMT(const PreparedSTMT &other)=delete