TrinityCore
PreparedStatement.cpp
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#include "PreparedStatement.h"
19#include "Errors.h"
20#include "MySQLConnection.h"
21#include "QueryResult.h"
22#include "StringFormat.h"
23
25 m_index(index), statement_data(capacity) { }
26
28
29//- Bind to buffer
30void PreparedStatementBase::setBool(const uint8 index, const bool value)
31{
32 ASSERT(index < statement_data.size());
33 statement_data[index].data = value;
34}
35
36void PreparedStatementBase::setUInt8(const uint8 index, const uint8 value)
37{
38 ASSERT(index < statement_data.size());
39 statement_data[index].data = value;
40}
41
42void PreparedStatementBase::setUInt16(const uint8 index, const uint16 value)
43{
44 ASSERT(index < statement_data.size());
45 statement_data[index].data = value;
46}
47
48void PreparedStatementBase::setUInt32(const uint8 index, const uint32 value)
49{
50 ASSERT(index < statement_data.size());
51 statement_data[index].data = value;
52}
53
54void PreparedStatementBase::setUInt64(const uint8 index, const uint64 value)
55{
56 ASSERT(index < statement_data.size());
57 statement_data[index].data = value;
58}
59
60void PreparedStatementBase::setInt8(const uint8 index, const int8 value)
61{
62 ASSERT(index < statement_data.size());
63 statement_data[index].data = value;
64}
65
66void PreparedStatementBase::setInt16(const uint8 index, const int16 value)
67{
68 ASSERT(index < statement_data.size());
69 statement_data[index].data = value;
70}
71
72void PreparedStatementBase::setInt32(const uint8 index, const int32 value)
73{
74 ASSERT(index < statement_data.size());
75 statement_data[index].data = value;
76}
77
78void PreparedStatementBase::setInt64(const uint8 index, const int64 value)
79{
80 ASSERT(index < statement_data.size());
81 statement_data[index].data = value;
82}
83
84void PreparedStatementBase::setFloat(const uint8 index, const float value)
85{
86 ASSERT(index < statement_data.size());
87 statement_data[index].data = value;
88}
89
90void PreparedStatementBase::setDouble(const uint8 index, const double value)
91{
92 ASSERT(index < statement_data.size());
93 statement_data[index].data = value;
94}
95
96void PreparedStatementBase::setString(const uint8 index, const std::string& value)
97{
98 ASSERT(index < statement_data.size());
99 statement_data[index].data = value;
100}
101
102void PreparedStatementBase::setStringView(const uint8 index, const std::string_view value)
103{
104 ASSERT(index < statement_data.size());
105 statement_data[index].data.emplace<std::string>(value);
106}
107
108void PreparedStatementBase::setBinary(const uint8 index, const std::vector<uint8>& value)
109{
110 ASSERT(index < statement_data.size());
111 statement_data[index].data = value;
112}
113
115{
116 ASSERT(index < statement_data.size());
117 statement_data[index].data = nullptr;
118}
119
120//- Execution
122{
123 PreparedResultSet* result = conn->Query(stmt);
124 if (!result || !result->GetRowCount())
125 {
126 delete result;
127 result = nullptr;
128 }
129
130 return PreparedQueryResult(result);
131}
132
134{
135 return conn->Execute(stmt);
136}
137
138template<typename T>
140{
141 return Trinity::StringFormat("{}", value);
142}
143
144std::string PreparedStatementData::ToString(bool value)
145{
146 return ToString<uint32>(value);
147}
148
150{
151 return ToString<uint32>(value);
152}
153
154template std::string PreparedStatementData::ToString<uint16>(uint16);
155template std::string PreparedStatementData::ToString<uint32>(uint32);
156template std::string PreparedStatementData::ToString<uint64>(uint64);
157
159{
160 return ToString<int32>(value);
161}
162
163template std::string PreparedStatementData::ToString<int16>(int16);
164template std::string PreparedStatementData::ToString<int32>(int32);
165template std::string PreparedStatementData::ToString<int64>(int64);
166template std::string PreparedStatementData::ToString<float>(float);
167template std::string PreparedStatementData::ToString<double>(double);
168
169std::string PreparedStatementData::ToString(std::string const& value)
170{
171 return Trinity::StringFormat("'{}'", value);
172}
173
174std::string PreparedStatementData::ToString(std::vector<uint8> const& /*value*/)
175{
176 return "BINARY";
177}
178
179std::string PreparedStatementData::ToString(std::nullptr_t)
180{
181 return "NULL";
182}
std::shared_ptr< PreparedResultSet > PreparedQueryResult
uint8_t uint8
Definition: Define.h:144
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
#define ASSERT
Definition: Errors.h:68
bool Execute(char const *sql)
ResultSet * Query(char const *sql)
uint64 GetRowCount() const
Definition: QueryResult.h:60
void setInt16(const uint8 index, const int16 value)
void setInt32(const uint8 index, const int32 value)
void setStringView(const uint8 index, const std::string_view value)
void setFloat(const uint8 index, const float value)
void setUInt8(const uint8 index, const uint8 value)
void setInt64(const uint8 index, const int64 value)
void setBool(const uint8 index, const bool value)
void setBinary(const uint8 index, const std::vector< uint8 > &value)
void setUInt32(const uint8 index, const uint32 value)
void setDouble(const uint8 index, const double value)
void setUInt16(const uint8 index, const uint16 value)
void setNull(const uint8 index)
void setString(const uint8 index, const std::string &value)
void setInt8(const uint8 index, const int8 value)
std::vector< PreparedStatementData > statement_data
void setUInt64(const uint8 index, const uint64 value)
PreparedStatementBase(uint32 index, uint8 capacity)
static PreparedQueryResult Query(MySQLConnection *conn, PreparedStatementBase *stmt)
static bool Execute(MySQLConnection *conn, PreparedStatementBase *stmt)
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
static std::string ToString(T value)