-
Notifications
You must be signed in to change notification settings - Fork 9
/
databasepgsql.cpp
215 lines (179 loc) · 5.2 KB
/
databasepgsql.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
////////////////////////////////////////////////////////////////////////
// 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_PGSQL__
#include <iostream>
#include "database.h"
#include "databasepgsql.h"
#include "configmanager.h"
extern ConfigManager g_config;
DatabasePgSQL::DatabasePgSQL() :
m_handle(NULL)
{
std::stringstream dns;
dns << "host='" << g_config.getString(ConfigManager::SQL_HOST) << "' dbname='" << g_config.getString(ConfigManager::SQL_DB) << "' user='" << g_config.getString(ConfigManager::SQL_USER) << "' password='" << g_config.getString(ConfigManager::SQL_PASS) << "' port='" << g_config.getNumber(ConfigManager::SQL_PORT) << "'";
m_handle = PQconnectdb(dns.str().c_str());
if(PQstatus(m_handle) != CONNECTION_OK)
{
std::clog << "Failed to estabilish PostgreSQL database connection: " << PQerrorMessage(m_handle) << std::endl;
PQfinish(m_handle);
}
else
m_connected = true;
}
std::string DatabasePgSQL::_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 DatabasePgSQL::query(std::string query)
{
if(!m_connected)
return false;
// executes query
PGresult* res = PQexec(m_handle, _parse(query).c_str());
ExecStatusType stat = PQresultStatus(res);
if(stat != PGRES_COMMAND_OK && stat != PGRES_TUPLES_OK)
{
std::clog << "PQexec(): " << query << ": " << PQresultErrorMessage(res) << std::endl;
PQclear(res);
return false;
}
// everything went fine
PQclear(res);
return true;
}
DBResult* DatabasePgSQL::storeQuery(std::string query)
{
if(!m_connected)
return NULL;
// executes query
PGresult* res = PQexec(m_handle, _parse(query).c_str());
ExecStatusType stat = PQresultStatus(res);
if(stat != PGRES_COMMAND_OK && stat != PGRES_TUPLES_OK)
{
std::clog << "PQexec(): " << query << ": " << PQresultErrorMessage(res) << std::endl;
PQclear(res);
return false;
}
// everything went fine
DBResult* result = new PgSQLResult(res);
return verifyResult(result);
}
std::string DatabasePgSQL::escapeString(std::string s)
{
// remember to quote even empty string!
if(!m_connected || !s.size())
return std::string("''");
// the worst case is 2n + 1
int32_t error;
char* output = new char[(s.length() * 2) + 1];
// quotes escaped string and frees temporary buffer
PQescapeStringConn(m_handle, output, s.c_str(), s.length(), reinterpret_cast<int32_t*>(&error));
std::string r = std::string("'");
r += output;
r += "'";
delete[] output;
return r;
}
std::string DatabasePgSQL::escapeBlob(const char *s, uint32_t length)
{
// remember to quote even empty stream!
if(!m_connected || !s)
return std::string("''");
// quotes escaped string and frees temporary buffer
size_t len;
char* output = (char*)PQescapeByteaConn(m_handle, (uint8_t*)s, length, &len);
std::string r = std::string("E'");
r += output;
r += "'";
PQfreemem(output);
return r;
}
uint64_t DatabasePgSQL::getLastInsertId()
{
if(!m_connected)
return 0;
PGresult* res = PQexec(m_handle, "SELECT LASTVAL() as last;");
ExecStatusType stat = PQresultStatus(res);
if(stat != PGRES_COMMAND_OK && stat != PGRES_TUPLES_OK)
{
std::clog << "PQexec(): \"SELECT LASTVAL() as last\": " << PQresultErrorMessage(res) << std::endl;
PQclear(res);
return 0;
}
const uint64_t id = atoll(PQgetvalue(res, 0, PQfnumber(res, "last")));
PQclear(res);
return id;
}
const char* PgSQLResult::getDataStream(const std::string& s, uint64_t& size)
{
std::string buf = PQgetvalue(m_handle, m_cursor, PQfnumber(m_handle, s.c_str()));
uint8_t* temp = PQunescapeBytea( (const uint8_t*)buf.c_str(), (size_t*)&size);
char* value = new char[buf.size()];
strcpy(value, (char*)temp);
PQfreemem(temp);
return value;
}
void PgSQLResult::free()
{
if(!m_handle)
{
std::clog << "[Critical - PgSQLResult::free] Trying to free already freed result!!!" << std::endl;
return;
}
PQclear(m_handle);
m_handle = NULL;
delete this;
}
bool PgSQLResult::next()
{
if(m_cursor >= m_rows)
return false;
m_cursor++;
return true;
}
PgSQLResult::~PgSQLResult()
{
if(m_handle)
PQclear(m_handle);
}
PgSQLResult::PgSQLResult(PGresult* result)
{
if(!result)
return;
m_handle = result;
m_rows = PQntuples(m_handle) - 1;
m_cursor = -1;
}
#endif