forked from mangoszero/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlPreparedStatement.cpp
More file actions
172 lines (151 loc) · 5.57 KB
/
SqlPreparedStatement.cpp
File metadata and controls
172 lines (151 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* MaNGOS is a full featured server for World of Warcraft, supporting
* the following clients: 1.12.x, 2.4.3, 3.3.5a, 4.3.4a and 5.4.8
*
* Copyright (C) 2005-2021 MaNGOS <https://getmangos.eu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* World of Warcraft, and all World of Warcraft or Warcraft art, images,
* and lore are copyrighted by Blizzard Entertainment, Inc.
*/
#include "DatabaseEnv.h"
SqlStmtParameters::SqlStmtParameters(uint32 nParams)
{
// reserve memory if needed
if (nParams > 0)
{
m_params.reserve(nParams);
}
}
void SqlStmtParameters::reset(const SqlStatement& stmt)
{
m_params.clear();
// reserve memory if needed
if (stmt.arguments() > 0)
{
m_params.reserve(stmt.arguments());
}
}
//////////////////////////////////////////////////////////////////////////
SqlStatement& SqlStatement::operator=(const SqlStatement& index)
{
if (this != &index)
{
m_index = index.m_index;
m_pDB = index.m_pDB;
delete m_pParams;
m_pParams = NULL;
if (index.m_pParams)
{
m_pParams = new SqlStmtParameters(*(index.m_pParams));
}
}
return *this;
}
bool SqlStatement::Execute()
{
SqlStmtParameters* args = detach();
// verify amount of bound parameters
if (args->boundParams() != arguments())
{
sLog.outError("SQL ERROR: wrong amount of parameters (%i instead of %i)", args->boundParams(), arguments());
sLog.outError("SQL ERROR: statement: %s", m_pDB->GetStmtString(ID()).c_str());
MANGOS_ASSERT(false);
return false;
}
return m_pDB->ExecuteStmt(m_index, args);
}
bool SqlStatement::DirectExecute()
{
SqlStmtParameters* args = detach();
// verify amount of bound parameters
if (args->boundParams() != arguments())
{
sLog.outError("SQL ERROR: wrong amount of parameters (%i instead of %i)", args->boundParams(), arguments());
sLog.outError("SQL ERROR: statement: %s", m_pDB->GetStmtString(ID()).c_str());
MANGOS_ASSERT(false);
return false;
}
return m_pDB->DirectExecuteStmt(m_index, args);
}
//////////////////////////////////////////////////////////////////////////
SqlPlainPreparedStatement::SqlPlainPreparedStatement(const std::string& fmt, SqlConnection& conn) : SqlPreparedStatement(fmt, conn)
{
m_bPrepared = true;
m_nParams = std::count(m_szFmt.begin(), m_szFmt.end(), '?');
m_bIsQuery = strnicmp(m_szFmt.c_str(), "select", 6) == 0;
}
void SqlPlainPreparedStatement::bind(const SqlStmtParameters& holder)
{
// verify if we bound all needed input parameters
if (m_nParams != holder.boundParams())
{
MANGOS_ASSERT(false);
return;
}
// reset resulting plain SQL request
m_szPlainRequest = m_szFmt;
size_t nLastPos = 0;
SqlStmtParameters::ParameterContainer const& _args = holder.params();
SqlStmtParameters::ParameterContainer::const_iterator iter_last = _args.end();
for (SqlStmtParameters::ParameterContainer::const_iterator iter = _args.begin(); iter != iter_last; ++iter)
{
// bind parameter
const SqlStmtFieldData& data = (*iter);
std::ostringstream fmt;
DataToString(data, fmt);
nLastPos = m_szPlainRequest.find('?', nLastPos);
if (nLastPos != std::string::npos)
{
std::string tmp = fmt.str();
m_szPlainRequest.replace(nLastPos, 1, tmp);
nLastPos += tmp.length();
}
}
}
bool SqlPlainPreparedStatement::execute()
{
if (m_szPlainRequest.empty())
{
return false;
}
return m_pConn.Execute(m_szPlainRequest.c_str());
}
void SqlPlainPreparedStatement::DataToString(const SqlStmtFieldData& data, std::ostringstream& fmt)
{
switch (data.type())
{
case FIELD_BOOL: fmt << "'" << uint32(data.toBool()) << "'"; break;
case FIELD_UI8: fmt << "'" << uint32(data.toUint8()) << "'"; break;
case FIELD_UI16: fmt << "'" << uint32(data.toUint16()) << "'"; break;
case FIELD_UI32: fmt << "'" << data.toUint32() << "'"; break;
case FIELD_UI64: fmt << "'" << data.toUint64() << "'"; break;
case FIELD_I8: fmt << "'" << int32(data.toInt8()) << "'"; break;
case FIELD_I16: fmt << "'" << int32(data.toInt16()) << "'"; break;
case FIELD_I32: fmt << "'" << data.toInt32() << "'"; break;
case FIELD_I64: fmt << "'" << data.toInt64() << "'"; break;
case FIELD_FLOAT: fmt << "'" << data.toFloat() << "'"; break;
case FIELD_DOUBLE: fmt << "'" << data.toDouble() << "'"; break;
case FIELD_STRING:
{
std::string tmp = data.toStr();
m_pConn.DB().escape_string(tmp);
fmt << "'" << tmp << "'";
break;
}
case FIELD_NONE: break;
}
}