-
Notifications
You must be signed in to change notification settings - Fork 9
/
databasesqlite.cpp
254 lines (214 loc) · 6.52 KB
/
databasesqlite.cpp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
////////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
////////////////////////////////////////////////////////////////////////
// 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 3 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, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#include "otpch.h"
#ifdef __USE_SQLITE__
#include <iostream>
#include <boost/regex.hpp>
#include "database.h"
#include "databasesqlite.h"
#include "tools.h"
#include "configmanager.h"
extern ConfigManager g_config;
#if SQLITE_VERSION_NUMBER < 3003009
#define sqlite3_prepare_v2 sqlite3_prepare
#endif
DatabaseSQLite::DatabaseSQLite() :
m_handle(NULL)
{
// test for existence of database file;
// sqlite3_open will create a new one if it isn't there (what we don't want)
if(!fileExists(g_config.getString(ConfigManager::SQL_FILE).c_str()))
return;
// Initialize sqlite
if(sqlite3_open(g_config.getString(ConfigManager::SQL_FILE).c_str(), &m_handle) != SQLITE_OK)
{
std::clog << "Failed to initialize SQLite connection: " << sqlite3_errmsg(m_handle) << " (" << sqlite3_errcode(m_handle) << ")" << std::endl;
sqlite3_close(m_handle);
}
else
m_connected = true;
}
std::string DatabaseSQLite::_parse(const std::string& s)
{
std::string query = "";
query.reserve(s.size());
bool inString = false;
for(uint32_t i = 0; i < s.length(); ++i)
{
uint8_t ch = s[i];
if(ch == '\'')
{
if(inString && s[i + 1] != '\'')
inString = false;
else
inString = true;
}
if(ch == '`' && !inString)
ch = '"';
query += ch;
}
return query;
}
bool DatabaseSQLite::query(std::string query)
{
boost::recursive_mutex::scoped_lock lockClass(sqliteLock);
if(!m_connected)
return false;
std::string buf = _parse(query);
#ifdef __SQL_QUERY_DEBUG__
std::clog << "SQLLITE DEBUG, query: " << buf << std::endl;
#endif
sqlite3_stmt* stmt;
// prepares statement
if(sqlite3_prepare_v2(m_handle, buf.c_str(), buf.length(), &stmt, NULL) != SQLITE_OK)
{
sqlite3_finalize(stmt);
std::clog << "sqlite3_prepare_v2(): SQLITE ERROR: " << sqlite3_errmsg(m_handle) << " (" << buf << ")" << std::endl;
return false;
}
// executes it once
int32_t ret = sqlite3_step(stmt);
if(ret != SQLITE_OK && ret != SQLITE_DONE && ret != SQLITE_ROW)
{
sqlite3_finalize(stmt);
std::clog << "sqlite3_step(): SQLITE ERROR: " << sqlite3_errmsg(m_handle) << std::endl;
return false;
}
// closes statement
// at all not sure if it should be debugged - query was executed correctly...
sqlite3_finalize(stmt);
return true;
}
DBResult* DatabaseSQLite::storeQuery(std::string query)
{
boost::recursive_mutex::scoped_lock lockClass(sqliteLock);
if(!m_connected)
return NULL;
std::string buf = _parse(query);
#ifdef __SQL_QUERY_DEBUG__
std::clog << "SQLLITE DEBUG, storeQuery: " << buf << std::endl;
#endif
sqlite3_stmt* stmt;
// prepares statement
if(sqlite3_prepare_v2(m_handle, buf.c_str(), buf.length(), &stmt, NULL) != SQLITE_OK)
{
sqlite3_finalize(stmt);
std::clog << "sqlite3_prepare_v2(): SQLITE ERROR: " << sqlite3_errmsg(m_handle) << " (" << buf << ")" << std::endl;
return NULL;
}
DBResult* result = new SQLiteResult(stmt);
return verifyResult(result);
}
std::string DatabaseSQLite::escapeString(std::string s)
{
// remember about quoiting even an empty string!
if(!s.size())
return std::string("''");
// the worst case is 2n + 3
char* output = new char[(s.length() << 1) + 3];
// quotes escaped string and frees temporary buffer
sqlite3_snprintf((s.length() << 1) + 1, output, "%Q", s.c_str());
std::string r(output);
delete[] output;
//escape % and _ because we are using LIKE operator.
r = boost::regex_replace(r, boost::regex("%"), "\\%");
r = boost::regex_replace(r, boost::regex("_"), "\\_");
if(r[r.length() - 1] != '\'')
r += "'";
return r;
}
std::string DatabaseSQLite::escapeBlob(const char* s, uint32_t length)
{
std::string buf = "x'";
char* hex = new char[2 + 1]; //need one extra byte for null-character
for(uint32_t i = 0; i < length; ++i)
{
sprintf(hex, "%02x", ((uint8_t)s[i]));
buf += hex;
}
delete[] hex;
buf += "'";
return buf;
}
int32_t SQLiteResult::getDataInt(const std::string& s)
{
listNames_t::iterator it = m_listNames.find(s);
if(it != m_listNames.end())
return sqlite3_column_int(m_handle, it->second);
std::clog << "Error during getDataInt(" << s << ")." << std::endl;
return 0; // Failed
}
int64_t SQLiteResult::getDataLong(const std::string& s)
{
listNames_t::iterator it = m_listNames.find(s);
if(it != m_listNames.end())
return sqlite3_column_int64(m_handle, it->second);
std::clog << "Error during getDataLong(" << s << ")." << std::endl;
return 0; // Failed
}
std::string SQLiteResult::getDataString(const std::string& s)
{
listNames_t::iterator it = m_listNames.find(s);
if(it != m_listNames.end() )
{
std::string value = (const char*)sqlite3_column_text(m_handle, it->second);
return value;
}
std::clog << "Error during getDataString(" << s << ")." << std::endl;
return std::string(""); // Failed
}
const char* SQLiteResult::getDataStream(const std::string& s, uint64_t& size)
{
listNames_t::iterator it = m_listNames.find(s);
if(it != m_listNames.end())
{
const char* value = (const char*)sqlite3_column_blob(m_handle, it->second);
size = sqlite3_column_bytes(m_handle, it->second);
return value;
}
std::clog << "Error during getDataStream(" << s << ")." << std::endl;
return NULL; // Failed
}
void SQLiteResult::free()
{
if(!m_handle)
{
std::clog << "[Critical - SQLiteResult::free] Trying to free already freed result!!!" << std::endl;
return;
}
sqlite3_finalize(m_handle);
m_handle = NULL;
m_listNames.clear();
delete this;
}
SQLiteResult::~SQLiteResult()
{
if(!m_handle)
return;
sqlite3_finalize(m_handle);
m_listNames.clear();
}
SQLiteResult::SQLiteResult(sqlite3_stmt* stmt)
{
if(!stmt)
return;
m_handle = stmt;
int32_t fields = sqlite3_column_count(m_handle);
for(int32_t i = 0; i < fields; ++i)
m_listNames[sqlite3_column_name(m_handle, i)] = i;
}
#endif