-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdb_core.cc
More file actions
315 lines (222 loc) · 7.38 KB
/
db_core.cc
File metadata and controls
315 lines (222 loc) · 7.38 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
This file is part of STFC.
Copyright 2006-2007 by Michael Krauss (info@stfc2.de) and Tobias Gafner
STFC is based on STGC,
Copyright 2003-2007 by Florian Brede (florian_brede@hotmail.com) and Philipp Schmidt
STFC 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.
STFC 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 "defines.hh"
#include "db_core.hh"
// TODO: mehr Fehlerbehandlung!
c_db_result::c_db_result(MYSQL_RES* in_result_id) {
this->result_id = in_result_id;
}
c_db_result::~c_db_result() {
mysql_free_result(this->result_id);
}
bool c_db_result::fetch_row() {
this->row = mysql_fetch_row(this->result_id);
return (this->row != NULL);
}
my_ulonglong c_db_result::num_rows() {
return mysql_num_rows(this->result_id);
}
c_db_core::c_db_core() {
this->state = DBSTATE_UNCONNECTED;
mysql_init(&this->mysql);
this->db_host = this->db_name = this->db_user = this->db_passwd = NULL;
this->last_result = NULL;
}
c_db_core::~c_db_core() {
if(this->state == DBSTATE_CONNECTED) this->end();
safe_delete_array(this->db_host);
safe_delete_array(this->db_name);
safe_delete_array(this->db_user);
safe_delete_array(this->db_passwd);
}
bool c_db_core::raise_error(char* sql) {
if(sql == NULL) {
DEBUG_LOG("c_db_core: (%i) %s\n", mysql_errno(&this->mysql), mysql_error(&this->mysql));
}
else {
DEBUG_LOG("c_db_core->query: at \"%s\" raised:\n(%i) %s\n", sql, mysql_errno(&this->mysql), mysql_error(&this->mysql));
}
return false;
}
bool c_db_core::init_by_str(char* in_host, char* in_name, char* in_user, char* in_passwd) {
this->db_host = new char[strlen(in_host) + 1];
strcpy(this->db_host, in_host);
if(in_name != NULL) {
this->db_name = new char[strlen(in_name) + 1];
strcpy(this->db_name, in_name);
}
this->db_user = new char[strlen(in_user) + 1];
strcpy(this->db_user, in_user);
if(in_passwd != NULL) {
this->db_passwd = new char[strlen(in_passwd) + 1];
strcpy(this->db_passwd, in_passwd);
}
return true;
}
/*
bool c_db_core::init_by_lua(c_lua_base* lua_base) {
const char* config_str;
if(!(config_str = lua_base->get_global_string("db_host"))) {
LOG_ERROR("c_db_core->init_by_lua: could not find valid host variable\n");
return false;
}
this->db_host = new char[strlen(config_str) + 1];
strcpy(this->db_host, config_str);
if((config_str = lua_base->get_global_string("db_name"))) {
this->db_name = new char[strlen(config_str) + 1];
strcpy(this->db_name, config_str);
}
if(!(config_str = lua_base->get_global_string("db_user"))) {
LOG_ERROR("c_db_core->init_by_lua: could not find valid user variable\n");
return false;
}
this->db_user = new char[strlen(config_str) + 1];
strcpy(this->db_user, config_str);
if((config_str = lua_base->get_global_string("db_passwd"))) {
this->db_passwd = new char[strlen(config_str) + 1];
strcpy(this->db_passwd, config_str);
}
return true;
}
bool c_db_core::init_by_xml(TiXmlHandle* config_handle) {
TiXmlElement* db_section = config_handle->FirstChild("database").Element();
if(!db_section) {
LOG_ERROR("c_db_core->init_by_xml: could not find database config section\n");
return false;
}
TiXmlElement* config_element;
const char* config_str;
if(!(config_element = db_section->FirstChildElement("host"))) {
LOG_ERROR("c_db_core->init_by_xml: could not find host element in database config section\n");
return false;
}
if(!(config_str = config_element->GetText())) {
LOG_ERROR("c_db_core->init_by_xml: empty host element in database config section\n");
return false;
}
this->db_host = new char[strlen(config_str) + 1];
strcpy(this->db_host, config_str);
if((config_element = db_section->FirstChildElement("name"))) {
if((config_str = config_element->GetText())) {
this->db_name = new char[strlen(config_str) + 1];
strcpy(this->db_name, config_str);
}
}
if(!(config_element = db_section->FirstChildElement("user"))) {
LOG_ERROR("c_db_core->init_by_xml: could not find user element in database config section\n");
return false;
}
if(!(config_str = config_element->GetText())) {
LOG_ERROR("c_db_core->init_by_xml: empty user element in database config section\n");
return false;
}
this->db_user = new char[strlen(config_str) + 1];
strcpy(this->db_user, config_str);
if((config_element = db_section->FirstChildElement("passwd"))) {
if((config_str = config_element->GetText())) {
this->db_passwd = new char[strlen(config_str) + 1];
strcpy(this->db_passwd, config_str);
}
}
return true;
}
*/
bool c_db_core::open() {
if(this->state == DBSTATE_CONNECTED) return true;
if(mysql_real_connect(&this->mysql, this->db_host, this->db_user, this->db_passwd, this->db_name, 0, NULL, 0) != NULL) {
this->state = DBSTATE_CONNECTED;
return true;
}
else {
return this->raise_error(NULL);
}
}
void c_db_core::end() {
if(this->state == DBSTATE_UNCONNECTED) return;
mysql_close(&this->mysql);
}
bool c_db_core::query(const char* sql_line) {
if(this->state == DBSTATE_UNCONNECTED) {
if(!this->open()) return false;
}
if(mysql_real_query(&this->mysql, sql_line, strlen(sql_line)) != 0) {
return this->raise_error((char*) sql_line);
}
return true;
}
bool c_db_core::query(char* sql_line, ...) {
if(this->state == DBSTATE_UNCONNECTED) {
if(!this->open()) return false;
}
int n_chars, buf_len = 128;
char* buf = NULL;
va_list arg_pointer;
while(1) {
buf = new char[buf_len];
va_start(arg_pointer, sql_line);
n_chars = vsnprintf(buf, buf_len, sql_line, arg_pointer);
va_end(arg_pointer);
if( (n_chars > -1) && (n_chars < buf_len) ) break;
if(n_chars > -1) buf_len = n_chars + 1;
else buf_len *= 2;
safe_delete_array(buf);
}
if(mysql_real_query(&this->mysql, buf, strlen(buf)) != 0) {
return this->raise_error(buf);
}
safe_delete_array(buf);
return true;
}
bool c_db_core::query(c_db_result** in_result_var, char* sql_line, ...) {
if(this->state == DBSTATE_UNCONNECTED) {
if(!this->open()) return false;
}
int n_chars, buf_len = 128;
char* buf = NULL;
va_list arg_pointer;
while(1) {
buf = new char[buf_len];
va_start(arg_pointer, sql_line);
n_chars = vsnprintf(buf, buf_len, sql_line, arg_pointer);
va_end(arg_pointer);
if( (n_chars > -1) && (n_chars < buf_len) ) break;
if(n_chars > -1) buf_len = n_chars + 1;
else buf_len *= 2;
safe_delete_array(buf);
}
if(mysql_real_query(&this->mysql, buf, strlen(buf)) != 0) {
return this->raise_error(buf);
}
safe_delete_array(buf);
MYSQL_RES* result = mysql_store_result(&this->mysql);
if(result == NULL) {
return this->raise_error(NULL);
}
*in_result_var = new c_db_result(result);
return true;
}
void c_db_core::ping() {
if(this->state == DBSTATE_CONNECTED) {
mysql_ping(&this->mysql);
}
}
int c_db_core::affected_rows() {
return mysql_affected_rows(&this->mysql);
}
int c_db_core::last_insert_id() {
return mysql_insert_id(&this->mysql);
}