TrinityCore
PreparedStatement.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef _PREPAREDSTATEMENT_H
19#define _PREPAREDSTATEMENT_H
20
21#include "DatabaseEnvFwd.h"
22#include "Define.h"
23#include <array>
24#include <string>
25#include <variant>
26#include <vector>
27
28class MySQLConnection;
29
31{
32 std::variant<
33 bool,
34 uint8,
35 uint16,
36 uint32,
37 uint64,
38 int8,
39 int16,
40 int32,
41 int64,
42 float,
43 double,
44 std::string,
45 std::vector<uint8>,
46 std::nullptr_t
48
49 template<typename T>
50 static std::string ToString(T value);
51
52 static std::string ToString(bool value);
53 static std::string ToString(uint8 value);
54 static std::string ToString(int8 value);
55 static std::string ToString(std::string const& value);
56 static std::string ToString(std::vector<uint8> const& value);
57 static std::string ToString(std::nullptr_t);
58};
59
60//- Upper-level class that is used in code
62{
64
65 public:
66 explicit PreparedStatementBase(uint32 index, uint8 capacity);
67 virtual ~PreparedStatementBase();
68
69 void setNull(const uint8 index);
70 void setBool(const uint8 index, const bool value);
71 void setUInt8(const uint8 index, const uint8 value);
72 void setUInt16(const uint8 index, const uint16 value);
73 void setUInt32(const uint8 index, const uint32 value);
74 void setUInt64(const uint8 index, const uint64 value);
75 void setInt8(const uint8 index, const int8 value);
76 void setInt16(const uint8 index, const int16 value);
77 void setInt32(const uint8 index, const int32 value);
78 void setInt64(const uint8 index, const int64 value);
79 void setFloat(const uint8 index, const float value);
80 void setDouble(const uint8 index, const double value);
81 void setString(const uint8 index, const std::string& value);
82 void setStringView(const uint8 index, const std::string_view value);
83 void setBinary(const uint8 index, const std::vector<uint8>& value);
84 template <size_t Size>
85 void setBinary(const uint8 index, std::array<uint8, Size> const& value)
86 {
87 std::vector<uint8> vec(value.begin(), value.end());
88 setBinary(index, vec);
89 }
90
91 uint32 GetIndex() const { return m_index; }
92 std::vector<PreparedStatementData> const& GetParameters() const { return statement_data; }
93
94 protected:
96
97 //- Buffer of parameters, not tied to MySQL in any way yet
98 std::vector<PreparedStatementData> statement_data;
99
102};
103
104template<typename T>
106{
107public:
108 explicit PreparedStatement(uint32 index, uint8 capacity) : PreparedStatementBase(index, capacity)
109 {
110 }
111
112private:
113 PreparedStatement(PreparedStatement const& right) = delete;
115};
116
117//- Lower-level class, enqueuable operation
119{
120public:
122 static bool Execute(MySQLConnection* conn, PreparedStatementBase* stmt);
123};
124
125#endif
std::shared_ptr< PreparedResultSet > PreparedQueryResult
uint8_t uint8
Definition: Define.h:144
#define TC_DATABASE_API
Definition: Define.h:111
int64_t int64
Definition: Define.h:137
int16_t int16
Definition: Define.h:139
int8_t int8
Definition: Define.h:140
int32_t int32
Definition: Define.h:138
uint64_t uint64
Definition: Define.h:141
uint16_t uint16
Definition: Define.h:143
uint32_t uint32
Definition: Define.h:142
PreparedStatementBase(PreparedStatementBase const &right)=delete
PreparedStatementBase & operator=(PreparedStatementBase const &right)=delete
void setBinary(const uint8 index, std::array< uint8, Size > const &value)
std::vector< PreparedStatementData > statement_data
std::vector< PreparedStatementData > const & GetParameters() const
PreparedStatement(uint32 index, uint8 capacity)
PreparedStatement(PreparedStatement const &right)=delete
PreparedStatement & operator=(PreparedStatement const &right)=delete
static std::string ToString(T value)
std::variant< bool, uint8, uint16, uint32, uint64, int8, int16, int32, int64, float, double, std::string, std::vector< uint8 >, std::nullptr_t > data