TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PreparedStatement.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
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 <future>
22 #include "SQLOperation.h"
23 
24 #ifdef __APPLE__
25 #undef TYPE_BOOL
26 #endif
27 
28 //- Union for data buffer (upper-level bind -> queue -> lower-level bind)
30 {
31  bool boolean;
40  float f;
41  double d;
42 };
43 
44 //- This enum helps us differ data held in above union
46 {
61 };
62 
64 {
67  std::vector<uint8> binary;
68 };
69 
70 //- Forward declare
72 
73 //- Upper-level class that is used in code
75 {
76  friend class PreparedStatementTask;
77  friend class MySQLPreparedStatement;
78  friend class MySQLConnection;
79 
80  public:
81  explicit PreparedStatement(uint32 index);
83 
84  void setBool(const uint8 index, const bool value);
85  void setUInt8(const uint8 index, const uint8 value);
86  void setUInt16(const uint8 index, const uint16 value);
87  void setUInt32(const uint8 index, const uint32 value);
88  void setUInt64(const uint8 index, const uint64 value);
89  void setInt8(const uint8 index, const int8 value);
90  void setInt16(const uint8 index, const int16 value);
91  void setInt32(const uint8 index, const int32 value);
92  void setInt64(const uint8 index, const int64 value);
93  void setFloat(const uint8 index, const float value);
94  void setDouble(const uint8 index, const double value);
95  void setString(const uint8 index, const std::string& value);
96  void setBinary(const uint8 index, const std::vector<uint8>& value);
97  void setNull(const uint8 index);
98 
99  protected:
100  void BindParameters();
101 
102  protected:
105  std::vector<PreparedStatementData> statement_data; //- Buffer of parameters, not tied to MySQL in any way yet
106 
107  PreparedStatement(PreparedStatement const& right) = delete;
108  PreparedStatement& operator=(PreparedStatement const& right) = delete;
109 };
110 
111 //- Class of which the instances are unique per MySQLConnection
112 //- access to these class objects is only done when a prepared statement task
113 //- is executed.
115 {
116  friend class MySQLConnection;
117  friend class PreparedStatement;
118 
119  public:
120  MySQLPreparedStatement(MYSQL_STMT* stmt);
122 
123  void setBool(const uint8 index, const bool value);
124  void setUInt8(const uint8 index, const uint8 value);
125  void setUInt16(const uint8 index, const uint16 value);
126  void setUInt32(const uint8 index, const uint32 value);
127  void setUInt64(const uint8 index, const uint64 value);
128  void setInt8(const uint8 index, const int8 value);
129  void setInt16(const uint8 index, const int16 value);
130  void setInt32(const uint8 index, const int32 value);
131  void setInt64(const uint8 index, const int64 value);
132  void setFloat(const uint8 index, const float value);
133  void setDouble(const uint8 index, const double value);
134  void setBinary(const uint8 index, const std::vector<uint8>& value, bool isString);
135  void setNull(const uint8 index);
136 
137  protected:
138  MYSQL_STMT* GetSTMT() { return m_Mstmt; }
139  MYSQL_BIND* GetBind() { return m_bind; }
141  void ClearParameters();
142  bool CheckValidIndex(uint8 index);
143  std::string getQueryString(std::string const& sqlPattern) const;
144 
145  private:
146  void setValue(MYSQL_BIND* param, enum_field_types type, const void* value, uint32 len, bool isUnsigned);
147 
148  private:
149  MYSQL_STMT* m_Mstmt;
151  std::vector<bool> m_paramsSet;
152  MYSQL_BIND* m_bind;
153 
154  MySQLPreparedStatement(MySQLPreparedStatement const& right) = delete;
156 };
157 
158 typedef std::future<PreparedQueryResult> PreparedQueryResultFuture;
159 typedef std::promise<PreparedQueryResult> PreparedQueryResultPromise;
160 
161 //- Lower-level class, enqueuable operation
163 {
164  public:
165  PreparedStatementTask(PreparedStatement* stmt, bool async = false);
167 
168  bool Execute() override;
169  PreparedQueryResultFuture GetFuture() { return m_result->get_future(); }
170 
171  protected:
175 };
176 #endif
#define TC_DATABASE_API
Definition: Define.h:122
PreparedQueryResultFuture GetFuture()
Definition: PreparedStatement.h:169
Definition: PreparedStatement.h:55
PreparedStatement * m_stmt
Definition: PreparedStatement.h:172
PreparedStatementValueType
Definition: PreparedStatement.h:45
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
PreparedStatementDataUnion data
Definition: PreparedStatement.h:65
int8_t int8
Definition: Define.h:148
Definition: PreparedStatement.h:51
uint16 ui16
Definition: PreparedStatement.h:34
std::future< PreparedQueryResult > PreparedQueryResultFuture
Definition: PreparedStatement.h:158
PreparedStatementValueType type
Definition: PreparedStatement.h:66
MYSQL_STMT * GetSTMT()
Definition: PreparedStatement.h:138
int64_t int64
Definition: Define.h:145
void setBinary(const uint8 index, const std::vector< uint8 > &value)
Definition: PreparedStatement.cpp:197
int16 i16
Definition: PreparedStatement.h:35
void setInt16(const uint8 index, const int16 value)
Definition: PreparedStatement.cpp:142
void setUInt8(const uint8 index, const uint8 value)
Definition: PreparedStatement.cpp:97
Definition: PreparedStatement.h:54
Definition: PreparedStatement.h:47
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
virtual bool Execute()=0
Definition: PreparedStatement.h:63
PreparedStatement * m_stmt
Definition: PreparedStatement.h:140
Definition: PreparedStatement.h:56
Definition: PreparedStatement.h:58
Definition: PreparedStatement.h:60
Definition: PreparedStatement.h:74
void setBool(const uint8 index, const bool value)
Definition: PreparedStatement.cpp:88
Definition: PreparedStatement.h:52
uint8 ui8
Definition: PreparedStatement.h:32
uint64 ui64
Definition: PreparedStatement.h:38
int8 i8
Definition: PreparedStatement.h:33
std::promise< PreparedQueryResult > PreparedQueryResultPromise
Definition: Callback.h:27
bool m_has_result
Definition: PreparedStatement.h:173
void setUInt16(const uint8 index, const uint16 value)
Definition: PreparedStatement.cpp:106
std::vector< PreparedStatementData > statement_data
Definition: PreparedStatement.h:105
Vector2int16 & operator=(const Any &a)
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
uint64_t uint64
Definition: Define.h:149
Definition: PreparedStatement.h:114
uint16_t uint16
Definition: Define.h:151
void setInt64(const uint8 index, const int64 value)
Definition: PreparedStatement.cpp:160
Definition: PreparedStatement.h:49
PreparedQueryResultPromise * m_result
Definition: PreparedStatement.h:174
Definition: PreparedStatement.h:53
void setInt32(const uint8 index, const int32 value)
Definition: PreparedStatement.cpp:151
void setUInt32(const uint8 index, const uint32 value)
Definition: PreparedStatement.cpp:115
bool boolean
Definition: PreparedStatement.h:31
uint32 ui32
Definition: PreparedStatement.h:36
float f
Definition: PreparedStatement.h:40
uint32 m_paramCount
Definition: PreparedStatement.h:150
void setInt8(const uint8 index, const int8 value)
Definition: PreparedStatement.cpp:133
std::future< PreparedQueryResult > PreparedQueryResultFuture
Definition: Callback.h:26
double d
Definition: PreparedStatement.h:41
std::vector< uint8 > binary
Definition: PreparedStatement.h:67
Definition: PreparedStatement.h:48
Definition: PreparedStatement.h:162
void setNull(const uint8 index)
Definition: PreparedStatement.cpp:206
uint32 m_index
Definition: PreparedStatement.h:104
Definition: PreparedStatement.h:50
void setFloat(const uint8 index, const float value)
Definition: PreparedStatement.cpp:169
void setDouble(const uint8 index, const double value)
Definition: PreparedStatement.cpp:178
void setUInt64(const uint8 index, const uint64 value)
Definition: PreparedStatement.cpp:124
uint8_t uint8
Definition: Define.h:152
MYSQL_STMT * m_Mstmt
Definition: PreparedStatement.h:149
Definition: SQLOperation.h:56
std::promise< PreparedQueryResult > PreparedQueryResultPromise
Definition: PreparedStatement.h:159
Definition: PreparedStatement.h:29
const FieldDescriptor value
Definition: descriptor.h:1522
int16_t int16
Definition: Define.h:147
MYSQL_BIND * GetBind()
Definition: PreparedStatement.h:139
Definition: PreparedStatement.h:57
Definition: PreparedStatement.h:59
MySQLPreparedStatement * m_stmt
Definition: PreparedStatement.h:103
int64 i64
Definition: PreparedStatement.h:39
int32 i32
Definition: PreparedStatement.h:37
friend class MySQLPreparedStatement
Definition: PreparedStatement.h:77