Debian Codegraph @

src/bnetd/sql_odbc.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2005 CreepLord ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #ifdef WITH_SQL_ODBC
19 #include "common/setup_before.h"
20 #ifdef HAVE_WINDOWS_H
21 # include <windows.h>
22 #endif
23 #include <sqlext.h>
24 #include <cctype>
25 #include "common/eventlog.h"
26 #include "common/xalloc.h"
27 #include "common/xstring.h"
28 #include "storage_sql.h"
29 #include "sql_odbc.h"
30 #include "common/setup_after.h"
31
32 namespace pvpgn
33 {
34
35 namespace bnetd
36 {
37
38 /* t_sql_engine Interface methods. */
39 static int sql_odbc_init(const char *, const char *, const char *, const char *, const char *, const char *);
40 static int sql_odbc_close(void);
41 static t_sql_res * sql_odbc_query_res(const char *);
42 static int sql_odbc_query(const char *);
43 static t_sql_row * sql_odbc_fetch_row(t_sql_res *);
44 static void sql_odbc_free_result(t_sql_res *);
45 static unsigned int sql_odbc_num_rows(t_sql_res *);
46 static unsigned int sql_odbc_affected_rows(void);
47 static unsigned int sql_odbc_num_fields(t_sql_res *);
48 static t_sql_field * sql_odbc_fetch_fields(t_sql_res *);
49 static int sql_odbc_free_fields(t_sql_field *);
50 static void sql_odbc_escape_string(char *, const char *, int);
51
52 t_sql_engine sql_odbc = {
53 sql_odbc_init,
54 sql_odbc_close,
55 sql_odbc_query_res,
56 sql_odbc_query,
57 sql_odbc_fetch_row,
58 sql_odbc_free_result,
59 sql_odbc_num_rows,
60 sql_odbc_num_fields,
61 sql_odbc_affected_rows,
62 sql_odbc_fetch_fields,
63 sql_odbc_free_fields,
64 sql_odbc_escape_string
65 };
66
67 struct t_odbc_rowSet_{
68 t_sql_row *row;
69 struct t_odbc_rowSet_ *next;
70 };
71 typedef struct t_odbc_rowSet_ t_odbc_rowSet;
72
73 typedef struct{
74 HSTMT stmt;
75 t_odbc_rowSet *curRow;
76 t_odbc_rowSet *rowSet;
77 unsigned int rowCount;
78 } t_odbc_res;
79
80 static t_sql_row *odbc_alloc_row(t_odbc_res *result, SQLINTEGER *sizes);
81 static t_odbc_rowSet *odbc_alloc_rowSet();
82 static int odbc_Result(SQLRETURN retCode);
83 static void odbc_Error(SQLSMALLINT type, void *obj, t_eventlog_level level, const char *function);
84 static int odbc_Fail();
85
86 static HENV env = SQL_NULL_HENV;
87 static HDBC con = SQL_NULL_HDBC;
88
89 static SQLINTEGER ROWCOUNT = 0;
90
91 #ifndef RUNTIME_LIBS
92 #define p_SQLAllocEnv SQLAllocEnv
93 #define p_SQLAllocConnect SQLAllocConnect
94 #define p_SQLAllocStmt SQLAllocStmt
95 #define p_SQLBindCol SQLBindCol
96 #define p_SQLColAttribute SQLColAttributeA
97 #define p_SQLConnect SQLConnectA
98 #define p_SQLDisconnect SQLDisconnect
99 #define p_SQLExecDirect SQLExecDirectA
100 #define p_SQLFetch SQLFetch
101 #define p_SQLFreeHandle SQLFreeHandle
102 #define p_SQLRowCount SQLRowCount
103 #define p_SQLGetDiagRec SQLGetDiagRecA
104 #define p_SQLNumResultCols SQLNumResultCols
105 #else
106 /* RUNTIME_LIBS */
107 static int odbc_load_dll(void);
108
109 typedef SQLRETURN(SQL_API *f_SQLAllocEnv)(SQLHENV*);
110 typedef SQLRETURN(SQL_API *f_SQLAllocConnect)(SQLHENV, SQLHDBC*);
111 typedef SQLRETURN(SQL_API *f_SQLAllocStmt)(SQLHDBC, SQLHSTMT*);
112 typedef SQLRETURN(SQL_API *f_SQLBindCol)(SQLHSTMT, SQLUSMALLINT, SQLSMALLINT, SQLPOINTER, SQLLEN, SQLLEN*);
113 typedef SQLRETURN(SQL_API *f_SQLColAttribute)(SQLHSTMT, SQLUSMALLINT, SQLUSMALLINT, SQLPOINTER, SQLSMALLINT, SQLSMALLINT*, SQLPOINTER);
114 typedef SQLRETURN(SQL_API *f_SQLConnect)(SQLHDBC, SQLCHAR*, SQLSMALLINT, SQLCHAR*, SQLSMALLINT, SQLCHAR*, SQLSMALLINT);
115 typedef SQLRETURN(SQL_API *f_SQLDisconnect)(SQLHDBC);
116 typedef SQLRETURN(SQL_API *f_SQLExecDirect)(SQLHSTMT, SQLCHAR*, SQLINTEGER);
117 typedef SQLRETURN(SQL_API *f_SQLFetch)(SQLHSTMT);
118 typedef SQLRETURN(SQL_API *f_SQLFreeHandle)(SQLSMALLINT, SQLHANDLE);
119 typedef SQLRETURN(SQL_API *f_SQLRowCount)(SQLHSTMT, SQLLEN*);
120 typedef SQLRETURN(SQL_API *f_SQLGetDiagRec)(SQLSMALLINT, SQLHANDLE, SQLSMALLINT, SQLCHAR*, SQLINTEGER*, SQLCHAR*, SQLSMALLINT, SQLSMALLINT*);
121 typedef SQLRETURN(SQL_API *f_SQLNumResultCols)(SQLHSTMT, SQLSMALLINT*);
122
123 static f_SQLAllocEnv p_SQLAllocEnv;
124 static f_SQLAllocConnect p_SQLAllocConnect;
125 static f_SQLAllocStmt p_SQLAllocStmt;
126 static f_SQLBindCol p_SQLBindCol;
127 static f_SQLColAttribute p_SQLColAttribute;
128 static f_SQLConnect p_SQLConnect;
129 static f_SQLDisconnect p_SQLDisconnect;
130 static f_SQLExecDirect p_SQLExecDirect;
131 static f_SQLFetch p_SQLFetch;
132 static f_SQLFreeHandle p_SQLFreeHandle;
133 static f_SQLRowCount p_SQLRowCount;
134 static f_SQLGetDiagRec p_SQLGetDiagRec;
135 static f_SQLNumResultCols p_SQLNumResultCols;
136
137 #include "compat/runtime_libs.h" /* defines OpenLibrary(), GetFunction(), CloseLibrary() & ODBC_LIB */
138
139 static void * handle = NULL;
140
141 static int odbc_load_dll(void)
142 {
143 if ((handle = OpenLibrary(ODBC_LIB)) == NULL) return -1;
144
145 if (((p_SQLAllocEnv = (f_SQLAllocEnv)GetFunction(handle, "SQLAllocEnv")) == NULL) ||
146 ((p_SQLAllocConnect = (f_SQLAllocConnect)GetFunction(handle, "SQLAllocConnect")) == NULL) ||
147 ((p_SQLAllocStmt = (f_SQLAllocStmt)GetFunction(handle, "SQLAllocStmt")) == NULL) ||
148 ((p_SQLBindCol = (f_SQLBindCol)GetFunction(handle, "SQLBindCol")) == NULL) ||
149 ((p_SQLColAttribute = (f_SQLColAttribute)GetFunction(handle, "SQLColAttribute")) == NULL) ||
150 ((p_SQLConnect = (f_SQLConnect)GetFunction(handle, "SQLConnect")) == NULL) ||
151 ((p_SQLDisconnect = (f_SQLDisconnect)GetFunction(handle, "SQLDisconnect")) == NULL) ||
152 ((p_SQLExecDirect = (f_SQLExecDirect)GetFunction(handle, "SQLExecDirect")) == NULL) ||
153 ((p_SQLFetch = (f_SQLFetch)GetFunction(handle, "SQLFetch")) == NULL) ||
154 ((p_SQLFreeHandle = (f_SQLFreeHandle)GetFunction(handle, "SQLFreeHandle")) == NULL) ||
155 ((p_SQLRowCount = (f_SQLRowCount)GetFunction(handle, "SQLRowCount")) == NULL) ||
156 ((p_SQLGetDiagRec = (f_SQLGetDiagRec)GetFunction(handle, "SQLGetDiagRec")) == NULL) ||
157 ((p_SQLNumResultCols = (f_SQLNumResultCols)GetFunction(handle, "SQLNumResultCols")) == NULL))
158 {
159 CloseLibrary(handle);
160 handle = NULL;
161 return -1;
162 }
163
164 return 0;
165 }
166 #endif
167
168 /************************************************
169 t_sql_engine Interface methods.
170 ************************************************/
171
172 static int sql_odbc_init(const char *host, const char *port, const char *socket, const char *name, const char *user, const char *pass)
173 {
174 #ifdef RUNTIME_LIBS
175 if (odbc_load_dll()) {
176 eventlog(eventlog_level_error, __FUNCTION__, "error loading library file \"{}\"", ODBC_LIB);
177 return -1;
178 }
179 #endif
180 /* Create environment. */
181 if (odbc_Result(p_SQLAllocEnv(&env)) && odbc_Result(p_SQLAllocConnect(env, &con))) {
182 eventlog(eventlog_level_debug, __FUNCTION__, "Created ODBC environment.");
183 }
184 else {
185 eventlog(eventlog_level_error, __FUNCTION__, "Unable to allocate ODBC environment.");
186 return odbc_Fail();
187 }
188 if (odbc_Result(p_SQLConnect(con, (SQLCHAR*)name, SQL_NTS, (SQLCHAR*)user, SQL_NTS, (SQLCHAR*)pass, SQL_NTS))) {
189 eventlog(eventlog_level_debug, __FUNCTION__, "Connected to ODBC datasource \"{}\".", name);
190 }
191 else {
192 eventlog(eventlog_level_error, __FUNCTION__, "Unable to connect to ODBC datasource \"{}\".", name);
193 odbc_Error(SQL_HANDLE_DBC, con, eventlog_level_error, __FUNCTION__);
194 return odbc_Fail();
195 }
196 return 0;
197 }
198
199 static int sql_odbc_close(void)
200 {
201 if (con) {
202 p_SQLDisconnect(con);
203 p_SQLFreeHandle(SQL_HANDLE_DBC, con);
204 con = NULL;
205 }
206 if (env) {
207 p_SQLFreeHandle(SQL_HANDLE_ENV, env);
208 env = NULL;
209 }
210 #ifdef RUNTIME_LIBS
211 if (handle) {
212 CloseLibrary(handle);
213 handle = NULL;
214 }
215 #endif
216 eventlog(eventlog_level_debug, __FUNCTION__, "ODBC connection closed.");
217 return 0;
218 }
219
220 static t_sql_res* sql_odbc_query_res(const char *query)
221 {
222 t_odbc_res *res = NULL;
223 HSTMT stmt = SQL_NULL_HSTMT;
224 SQLRETURN result = 0;
225 t_odbc_rowSet *rowSet = NULL;
226 ROWCOUNT = 0;
227
228 /* Validate query. */
229 if (query == NULL) {
230 eventlog(eventlog_level_error, __FUNCTION__, "Got a NULL query!");
231 return NULL;
232 }
233 // eventlog(eventlog_level_trace, __FUNCTION__, "{}", query);
234
235 /* Run query and check for success. */
236 p_SQLAllocStmt(con, &stmt);
237 result = p_SQLExecDirect(stmt, (SQLCHAR*)query, SQL_NTS);
238 if (!odbc_Result(result)) {
239 // odbc_Error(SQL_HANDLE_STMT, stmt, eventlog_level_debug, __FUNCTION__);
240 p_SQLFreeHandle(SQL_HANDLE_STMT, stmt);
241 return NULL;
242 }
243
244 /* Create a result. */
245 res = (t_odbc_res *)xmalloc(sizeof *res);
246 res->stmt = stmt;
247 rowSet = odbc_alloc_rowSet();
248 res->rowSet = rowSet;
249 res->curRow = rowSet;
250 res->rowCount = 0;
251
252 /* Store rows. */
253 do {
254 SQLINTEGER *sizes = NULL;
255 t_sql_row *row = odbc_alloc_row(res, sizes);
256 if (!row) {
257 sql_odbc_free_result(res);
258 return NULL;
259 }
260 result = p_SQLFetch(stmt);
261 if (odbc_Result(result)) {
262 rowSet->row = row;
263 rowSet->next = odbc_alloc_rowSet();
264 rowSet = rowSet->next;
265 res->rowCount++;
266 }
267 else {
268 sql_odbc_free_fields(row);
269 }
270 if (sizes) xfree(sizes);
271 } while (odbc_Result(result));
272 ROWCOUNT = res->rowCount;
273 if (result == SQL_NO_DATA_FOUND) {
274 return res;
275 }
276 else if (!odbc_Result(result)) {
277 eventlog(eventlog_level_error, __FUNCTION__, "Unable to fetch row - ODBC error {}.", result);
278 odbc_Error(SQL_HANDLE_STMT, stmt, eventlog_level_error, __FUNCTION__);
279 sql_odbc_free_result(res);
280 return NULL;
281 }
282 return res;
283 }
284
285 static int sql_odbc_query(const char *query)
286 {
287 int result;
288 HSTMT stmt = SQL_NULL_HSTMT;
289
290 /* Validate query. */
291 if (query == NULL) {
292 eventlog(eventlog_level_error, __FUNCTION__, "Got a NULL query!");
293 return -1;
294 }
295 // eventlog(eventlog_level_trace, __FUNCTION__, "{}", query);
296
297 p_SQLAllocStmt(con, &stmt);
298 result = odbc_Result(p_SQLExecDirect(stmt, (SQLCHAR*)query, SQL_NTS));
299 if (result) {
300 p_SQLRowCount(stmt, &ROWCOUNT);
301 }
302 else {
303 // odbc_Error(SQL_HANDLE_STMT, stmt, eventlog_level_trace, __FUNCTION__);
304 }
305 p_SQLFreeHandle(SQL_HANDLE_STMT, stmt);
306 result = result == -1 ? 0 : -1;
307 return result;
308 }
309
310 static t_sql_row* sql_odbc_fetch_row(t_sql_res *result)
311 {
312 t_odbc_res *res;
313 t_sql_row *row;
314 if (!result) {
315 eventlog(eventlog_level_error, __FUNCTION__, "Got NULL result.");
316 return NULL;
317 }
318 res = (t_odbc_res*)result;
319 row = res->curRow->row;
320 if (row) {
321 res->curRow = res->curRow->next;
322 }
323 return row;
324 }
325
326 static void sql_odbc_free_result(t_sql_res *result)
327 {
328 if (result) {
329 t_odbc_res *res = (t_odbc_res*)result;
330 if (res->stmt) {
331 p_SQLFreeHandle(SQL_HANDLE_STMT, res->stmt);
332 res->stmt = NULL;
333 }
334 if (res->rowSet) {
335 t_odbc_rowSet *rowSet = res->rowSet;
336 while (rowSet) {
337 t_odbc_rowSet *temp = (rowSet->next);
338 sql_odbc_free_fields(rowSet->row);
339 xfree(rowSet);
340 rowSet = temp;
341 }
342 }
343 xfree(result);
344 }
345 }
346
347 static unsigned int sql_odbc_num_rows(t_sql_res *result)
348 {
349 if (!result) {
350 eventlog(eventlog_level_error, __FUNCTION__, "Got NULL result.");
351 return 0;
352 }
353 return ((t_odbc_res*)result)->rowCount;
354 }
355
356 static unsigned int sql_odbc_num_fields(t_sql_res *result)
357 {
358 SQLSMALLINT numCols = 0;
359 if (!result) {
360 eventlog(eventlog_level_error, __FUNCTION__, "Got NULL result.");
361 }
362 else {
363 p_SQLNumResultCols(((t_odbc_res*)result)->stmt, &numCols);
364 }
365 return numCols;
366 }
367
368 static unsigned int sql_odbc_affected_rows(void)
369 {
370 return ROWCOUNT;
371 }
372
373 static t_sql_field* sql_odbc_fetch_fields(t_sql_res *result)
374 {
375 int i;
376 t_odbc_res *res;
377 int fieldCount;
378 t_sql_field *fields;
379 if (!result) {
380 eventlog(eventlog_level_error, __FUNCTION__, "Got NULL result.");
381 return NULL;
382 }
383 res = (t_odbc_res *)result;
384 fieldCount = sql_odbc_num_fields(result);
385 fields = (t_sql_field *)xcalloc(sizeof *fields, fieldCount + 1);
386 if (!fields) {
387 return NULL;
388 }
389 fields[fieldCount] = NULL;
390 for (i = 0; i < fieldCount; i++)
391 {
392 SQLSMALLINT fNameSz;
393 p_SQLColAttribute(res->stmt, i + 1, SQL_DESC_NAME, NULL, 0, &fNameSz, NULL);
394 char* fName = (char*)xmalloc(fNameSz);
395 if (!fName)
396 {
397 return NULL;
398 }
399 p_SQLColAttribute(res->stmt, i + 1, SQL_DESC_NAME, fName, fNameSz, &fNameSz, NULL);
400 char* tmp = fName;
401 for (; *tmp; ++tmp)
402 {
403 *tmp = safe_toupper(*tmp);
404 }
405 fields[i] = fName;
406 }
407 return fields;
408 }
409
410 static int sql_odbc_free_fields(t_sql_field *fields)
411 {
412 if (fields) {
413 int i = 0;
414 while (fields[i]) {
415 xfree(fields[i]);
416 i++;
417 }
418 xfree(fields);
419 }
420 return 0;
421 }
422
423 static void sql_odbc_escape_string(char *escape, const char *from, int len)
424 {
425 int i;
426 for (i = 0; i < len && from[i] != 0; i++) {
427 if (from[i] == '\'') {
428 escape[i] = '"';
429 }
430 else {
431 escape[i] = from[i];
432 }
433 }
434 escape[i] = 0;
435 }
436
437 /************************************************
438 End t_sql_engine Interface methods.
439 ************************************************/
440
441 static t_sql_row* odbc_alloc_row(t_odbc_res *result, SQLINTEGER *sizes)
442 {
443 int i, fieldCount;
444 HSTMT stmt = result->stmt;
445 t_sql_row *row;
446
447 fieldCount = sql_odbc_num_fields(result);
448 /* Create a new row. */
449 row = (t_sql_row *)xcalloc(sizeof *row, fieldCount + 1);
450 if (!row) {
451 return NULL;
452 }
453 row[fieldCount] = NULL;
454 sizes = (SQLINTEGER *)xcalloc(sizeof *sizes, fieldCount);
455
456 for (i = 0; i < fieldCount; i++)
457 {
458 SQLLEN cellSz;
459 p_SQLColAttribute(stmt, i + 1, SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL, &cellSz);
460 char* cell = (char*)xcalloc(sizeof *cell, ++cellSz);
461 if (!cell)
462 {
463 return NULL;
464 }
465 p_SQLBindCol(stmt, i + 1, SQL_C_CHAR, cell, cellSz, &sizes[i]);
466
467 row[i] = cell;
468 }
469 return row;
470 }
471
472 static t_odbc_rowSet* odbc_alloc_rowSet()
473 {
474 t_odbc_rowSet *rowSet = (t_odbc_rowSet *)xmalloc(sizeof *rowSet);
475 rowSet->row = NULL;
476 rowSet->next = NULL;
477 return rowSet;
478 }
479
480 static void odbc_Error(SQLSMALLINT type, void *obj, t_eventlog_level level, const char *function)
481 {
482 SQLCHAR mState[6] = "\0";
483 long native = 0;
484 SQLSMALLINT mTextLen;
485 short i = 0;
486
487 while (p_SQLGetDiagRec(type, obj, ++i, NULL, NULL, NULL, 0, &mTextLen) != SQL_NO_DATA) {
488 SQLCHAR *mText = (SQLCHAR *)xcalloc(sizeof *mText, ++mTextLen);
489 p_SQLGetDiagRec(type, obj, i, mState, &native, mText, mTextLen, NULL);
490 eventlog(level, function, "ODBC Error: State {}, Native {}: {}", mState, native, mText);
491 xfree(mText);
492 }
493 }
494
495 static int odbc_Result(SQLRETURN retCode)
496 {
497 int result = 0;
498 if (retCode == SQL_SUCCESS || retCode == SQL_SUCCESS_WITH_INFO) {
499 result = -1;
500 }
501 return result;
502 }
503
504 static int odbc_Fail()
505 {
506 sql_odbc_close();
507 return -1;
508 }
509
510 }
511
512 }
513
514 #endif