implemented new tdata format
[public/netxms.git] / src / db / libnxdb / session.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Database Abstraction Library
4 ** Copyright (C) 2003-2016 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation; either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: session.cpp
21 **
22 **/
23
24 #include "libnxdb.h"
25
26 /**
27 * Check if statement handle is valid
28 */
29 #define IS_VALID_STATEMENT_HANDLE(s) ((s != NULL) && (s->m_connection != NULL))
30
31 /**
32 * Performance counters
33 */
34 static UINT64 s_perfSelectQueries = 0;
35 static UINT64 s_perfNonSelectQueries = 0;
36 static UINT64 s_perfTotalQueries = 0;
37 static UINT64 s_perfLongRunningQueries = 0;
38 static UINT64 s_perfFailedQueries = 0;
39
40 /**
41 * Session init callback
42 */
43 static void (*s_sessionInitCb)(DB_HANDLE session) = NULL;
44
45 /**
46 * Invalidate all prepared statements on connection
47 */
48 static void InvalidatePreparedStatements(DB_HANDLE hConn)
49 {
50 for(int i = 0; i < hConn->m_preparedStatements->size(); i++)
51 {
52 db_statement_t *stmt = hConn->m_preparedStatements->get(i);
53 hConn->m_driver->m_fpDrvFreeStatement(stmt->m_statement);
54 stmt->m_statement = NULL;
55 stmt->m_connection = NULL;
56 }
57 hConn->m_preparedStatements->clear();
58 }
59
60 /**
61 * Connect to database
62 */
63 DB_HANDLE LIBNXDB_EXPORTABLE DBConnect(DB_DRIVER driver, const TCHAR *server, const TCHAR *dbName,
64 const TCHAR *login, const TCHAR *password, const TCHAR *schema, TCHAR *errorText)
65 {
66 DBDRV_CONNECTION hDrvConn;
67 DB_HANDLE hConn = NULL;
68
69 nxlog_debug(8, _T("DBConnect: server=%s db=%s login=%s schema=%s"), CHECK_NULL(server), CHECK_NULL(dbName), CHECK_NULL(login), CHECK_NULL(schema));
70 #ifdef UNICODE
71 char *mbServer = (server == NULL) ? NULL : MBStringFromWideString(server);
72 char *mbDatabase = (dbName == NULL) ? NULL : MBStringFromWideString(dbName);
73 char *mbLogin = (login == NULL) ? NULL : MBStringFromWideString(login);
74 char *mbPassword = (password == NULL) ? NULL : MBStringFromWideString(password);
75 char *mbSchema = (schema == NULL) ? NULL : MBStringFromWideString(schema);
76 errorText[0] = 0;
77 hDrvConn = driver->m_fpDrvConnect(mbServer, mbLogin, mbPassword, mbDatabase, mbSchema, errorText);
78 #else
79 WCHAR wcErrorText[DBDRV_MAX_ERROR_TEXT] = L"";
80 hDrvConn = driver->m_fpDrvConnect(server, login, password, dbName, schema, wcErrorText);
81 WideCharToMultiByte(CP_ACP, WC_DEFAULTCHAR | WC_COMPOSITECHECK, wcErrorText, -1, errorText, DBDRV_MAX_ERROR_TEXT, NULL, NULL);
82 errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
83 #endif
84 if (hDrvConn != NULL)
85 {
86 hConn = (DB_HANDLE)malloc(sizeof(struct db_handle_t));
87 if (hConn != NULL)
88 {
89 hConn->m_driver = driver;
90 hConn->m_dumpSql = driver->m_dumpSql;
91 hConn->m_reconnectEnabled = true;
92 hConn->m_connection = hDrvConn;
93 hConn->m_mutexTransLock = MutexCreateRecursive();
94 hConn->m_transactionLevel = 0;
95 hConn->m_preparedStatements = new ObjectArray<db_statement_t>(4, 4, false);
96 #ifdef UNICODE
97 hConn->m_dbName = mbDatabase;
98 hConn->m_login = mbLogin;
99 hConn->m_password = mbPassword;
100 hConn->m_server = mbServer;
101 hConn->m_schema = mbSchema;
102 #else
103 hConn->m_dbName = (dbName == NULL) ? NULL : _tcsdup(dbName);
104 hConn->m_login = (login == NULL) ? NULL : _tcsdup(login);
105 hConn->m_password = (password == NULL) ? NULL : _tcsdup(password);
106 hConn->m_server = (server == NULL) ? NULL : _tcsdup(server);
107 hConn->m_schema = (schema == NULL) ? NULL : _tcsdup(schema);
108 #endif
109 if (driver->m_fpDrvSetPrefetchLimit != NULL)
110 driver->m_fpDrvSetPrefetchLimit(hDrvConn, driver->m_defaultPrefetchLimit);
111 nxlog_debug(4, _T("New DB connection opened: handle=%p"), hConn);
112 if (s_sessionInitCb != NULL)
113 s_sessionInitCb(hConn);
114 }
115 else
116 {
117 driver->m_fpDrvDisconnect(hDrvConn);
118 }
119 }
120 #ifdef UNICODE
121 if (hConn == NULL)
122 {
123 safe_free(mbServer);
124 safe_free(mbDatabase);
125 safe_free(mbLogin);
126 safe_free(mbPassword);
127 safe_free(mbSchema);
128 }
129 #endif
130 return hConn;
131 }
132
133 /**
134 * Disconnect from database
135 */
136 void LIBNXDB_EXPORTABLE DBDisconnect(DB_HANDLE hConn)
137 {
138 if (hConn == NULL)
139 return;
140
141 nxlog_debug(4, _T("DB connection %p closed"), hConn);
142
143 InvalidatePreparedStatements(hConn);
144
145 hConn->m_driver->m_fpDrvDisconnect(hConn->m_connection);
146 MutexDestroy(hConn->m_mutexTransLock);
147 safe_free(hConn->m_dbName);
148 safe_free(hConn->m_login);
149 safe_free(hConn->m_password);
150 safe_free(hConn->m_server);
151 safe_free(hConn->m_schema);
152 delete hConn->m_preparedStatements;
153 free(hConn);
154 }
155
156 /**
157 * Enable/disable reconnect
158 */
159 void LIBNXDB_EXPORTABLE DBEnableReconnect(DB_HANDLE hConn, bool enabled)
160 {
161 if (hConn != NULL)
162 {
163 hConn->m_reconnectEnabled = enabled;
164 }
165 }
166
167 /**
168 * Reconnect to database
169 */
170 static void DBReconnect(DB_HANDLE hConn)
171 {
172 int nCount;
173 WCHAR errorText[DBDRV_MAX_ERROR_TEXT];
174
175 nxlog_debug(4, _T("DB reconnect: handle=%p"), hConn);
176
177 InvalidatePreparedStatements(hConn);
178 hConn->m_driver->m_fpDrvDisconnect(hConn->m_connection);
179 for(nCount = 0; ; nCount++)
180 {
181 hConn->m_connection = hConn->m_driver->m_fpDrvConnect(hConn->m_server, hConn->m_login,
182 hConn->m_password, hConn->m_dbName, hConn->m_schema, errorText);
183 if (hConn->m_connection != NULL)
184 {
185 if (hConn->m_driver->m_fpDrvSetPrefetchLimit != NULL)
186 hConn->m_driver->m_fpDrvSetPrefetchLimit(hConn->m_connection, hConn->m_driver->m_defaultPrefetchLimit);
187 if (s_sessionInitCb != NULL)
188 s_sessionInitCb(hConn);
189 break;
190 }
191 if (nCount == 0)
192 {
193 MutexLock(hConn->m_driver->m_mutexReconnect);
194 if ((hConn->m_driver->m_reconnect == 0) && (hConn->m_driver->m_fpEventHandler != NULL))
195 hConn->m_driver->m_fpEventHandler(DBEVENT_CONNECTION_LOST, NULL, NULL, true, hConn->m_driver->m_userArg);
196 hConn->m_driver->m_reconnect++;
197 MutexUnlock(hConn->m_driver->m_mutexReconnect);
198 }
199 ThreadSleepMs(1000);
200 }
201 if (nCount > 0)
202 {
203 MutexLock(hConn->m_driver->m_mutexReconnect);
204 hConn->m_driver->m_reconnect--;
205 if ((hConn->m_driver->m_reconnect == 0) && (hConn->m_driver->m_fpEventHandler != NULL))
206 hConn->m_driver->m_fpEventHandler(DBEVENT_CONNECTION_RESTORED, NULL, NULL, false, hConn->m_driver->m_userArg);
207 MutexUnlock(hConn->m_driver->m_mutexReconnect);
208 }
209 }
210
211 /**
212 * Set default prefetch limit
213 */
214 void LIBNXDB_EXPORTABLE DBSetDefaultPrefetchLimit(DB_DRIVER driver, int limit)
215 {
216 driver->m_defaultPrefetchLimit = limit;
217 }
218
219 /**
220 * Set prefetch limit
221 */
222 bool LIBNXDB_EXPORTABLE DBSetPrefetchLimit(DB_HANDLE hConn, int limit)
223 {
224 if (hConn->m_driver->m_fpDrvSetPrefetchLimit == NULL)
225 return false; // Not supported by driver
226 return hConn->m_driver->m_fpDrvSetPrefetchLimit(hConn->m_connection, limit);
227 }
228
229 /**
230 * Set session initialization callback
231 */
232 void LIBNXDB_EXPORTABLE DBSetSessionInitCallback(void (*cb)(DB_HANDLE))
233 {
234 s_sessionInitCb = cb;
235 }
236
237 /**
238 * Perform a non-SELECT SQL query
239 */
240 bool LIBNXDB_EXPORTABLE DBQueryEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText)
241 {
242 DWORD dwResult;
243 #ifdef UNICODE
244 #define pwszQuery szQuery
245 #define wcErrorText errorText
246 #else
247 WCHAR *pwszQuery = WideStringFromMBString(szQuery);
248 WCHAR wcErrorText[DBDRV_MAX_ERROR_TEXT] = L"";
249 #endif
250
251 MutexLock(hConn->m_mutexTransLock);
252 INT64 ms = GetCurrentTimeMs();
253
254 dwResult = hConn->m_driver->m_fpDrvQuery(hConn->m_connection, pwszQuery, wcErrorText);
255 if ((dwResult == DBERR_CONNECTION_LOST) && hConn->m_reconnectEnabled)
256 {
257 DBReconnect(hConn);
258 dwResult = hConn->m_driver->m_fpDrvQuery(hConn->m_connection, pwszQuery, wcErrorText);
259 }
260
261 s_perfNonSelectQueries++;
262 s_perfTotalQueries++;
263
264 ms = GetCurrentTimeMs() - ms;
265 if (hConn->m_driver->m_dumpSql)
266 {
267 nxlog_debug(9, _T("%s sync query: \"%s\" [%d ms]"), (dwResult == DBERR_SUCCESS) ? _T("Successful") : _T("Failed"), szQuery, ms);
268 }
269 if ((dwResult == DBERR_SUCCESS) && ((UINT32)ms > g_sqlQueryExecTimeThreshold))
270 {
271 nxlog_debug(3, _T("Long running query: \"%s\" [%d ms]"), szQuery, (int)ms);
272 s_perfLongRunningQueries++;
273 }
274
275 MutexUnlock(hConn->m_mutexTransLock);
276
277 #ifndef UNICODE
278 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wcErrorText, -1, errorText, DBDRV_MAX_ERROR_TEXT, NULL, NULL);
279 errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
280 #endif
281
282 if (dwResult != DBERR_SUCCESS)
283 {
284 s_perfFailedQueries++;
285 if (hConn->m_driver->m_logSqlErrors)
286 nxlog_write(g_sqlErrorMsgCode, EVENTLOG_ERROR_TYPE, "ss", szQuery, errorText);
287 if (hConn->m_driver->m_fpEventHandler != NULL)
288 hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, dwResult == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
289 }
290
291 #ifndef UNICODE
292 free(pwszQuery);
293 #endif
294
295 return dwResult == DBERR_SUCCESS;
296 #undef pwszQuery
297 #undef wcErrorText
298 }
299
300 bool LIBNXDB_EXPORTABLE DBQuery(DB_HANDLE hConn, const TCHAR *query)
301 {
302 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
303 return DBQueryEx(hConn, query, errorText);
304 }
305
306 /**
307 * Perform SELECT query
308 */
309 DB_RESULT LIBNXDB_EXPORTABLE DBSelectEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText)
310 {
311 DBDRV_RESULT hResult;
312 DB_RESULT result = NULL;
313 DWORD dwError = DBERR_OTHER_ERROR;
314 #ifdef UNICODE
315 #define pwszQuery szQuery
316 #define wcErrorText errorText
317 #else
318 WCHAR *pwszQuery = WideStringFromMBString(szQuery);
319 WCHAR wcErrorText[DBDRV_MAX_ERROR_TEXT] = L"";
320 #endif
321
322 MutexLock(hConn->m_mutexTransLock);
323 INT64 ms = GetCurrentTimeMs();
324
325 s_perfSelectQueries++;
326 s_perfTotalQueries++;
327
328 hResult = hConn->m_driver->m_fpDrvSelect(hConn->m_connection, pwszQuery, &dwError, wcErrorText);
329 if ((hResult == NULL) && (dwError == DBERR_CONNECTION_LOST) && hConn->m_reconnectEnabled)
330 {
331 DBReconnect(hConn);
332 hResult = hConn->m_driver->m_fpDrvSelect(hConn->m_connection, pwszQuery, &dwError, wcErrorText);
333 }
334
335 ms = GetCurrentTimeMs() - ms;
336 if (hConn->m_driver->m_dumpSql)
337 {
338 nxlog_debug(9, _T("%s sync query: \"%s\" [%d ms]"), (hResult != NULL) ? _T("Successful") : _T("Failed"), szQuery, (int)ms);
339 }
340 if ((hResult != NULL) && ((UINT32)ms > g_sqlQueryExecTimeThreshold))
341 {
342 nxlog_debug(3, _T("Long running query: \"%s\" [%d ms]"), szQuery, (int)ms);
343 s_perfLongRunningQueries++;
344 }
345 MutexUnlock(hConn->m_mutexTransLock);
346
347 #ifndef UNICODE
348 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wcErrorText, -1, errorText, DBDRV_MAX_ERROR_TEXT, NULL, NULL);
349 errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
350 #endif
351
352 if (hResult == NULL)
353 {
354 s_perfFailedQueries++;
355 if (hConn->m_driver->m_logSqlErrors)
356 nxlog_write(g_sqlErrorMsgCode, EVENTLOG_ERROR_TYPE, "ss", szQuery, errorText);
357 if (hConn->m_driver->m_fpEventHandler != NULL)
358 hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, dwError == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
359 }
360
361 #ifndef UNICODE
362 free(pwszQuery);
363 #endif
364
365 if (hResult != NULL)
366 {
367 result = (DB_RESULT)malloc(sizeof(db_result_t));
368 result->m_driver = hConn->m_driver;
369 result->m_connection = hConn;
370 result->m_data = hResult;
371 }
372
373 return result;
374 #undef pwszQuery
375 #undef wcErrorText
376 }
377
378 DB_RESULT LIBNXDB_EXPORTABLE DBSelect(DB_HANDLE hConn, const TCHAR *query)
379 {
380 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
381
382 return DBSelectEx(hConn, query, errorText);
383 }
384
385 /**
386 * Get number of columns
387 */
388 int LIBNXDB_EXPORTABLE DBGetColumnCount(DB_RESULT hResult)
389 {
390 return hResult->m_driver->m_fpDrvGetColumnCount(hResult->m_data);
391 }
392
393 /**
394 * Get column name
395 */
396 bool LIBNXDB_EXPORTABLE DBGetColumnName(DB_RESULT hResult, int column, TCHAR *buffer, int bufSize)
397 {
398 const char *name;
399
400 name = hResult->m_driver->m_fpDrvGetColumnName(hResult->m_data, column);
401 if (name != NULL)
402 {
403 #ifdef UNICODE
404 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, buffer, bufSize);
405 buffer[bufSize - 1] = 0;
406 #else
407 nx_strncpy(buffer, name, bufSize);
408 #endif
409 }
410 return name != NULL;
411 }
412
413 /**
414 * Get column count for unbuffered result set
415 */
416 int LIBNXDB_EXPORTABLE DBGetColumnCount(DB_UNBUFFERED_RESULT hResult)
417 {
418 return hResult->m_driver->m_fpDrvGetColumnCountUnbuffered(hResult->m_data);
419 }
420
421 /**
422 * Get column name for unbuffered result set
423 */
424 bool LIBNXDB_EXPORTABLE DBGetColumnName(DB_UNBUFFERED_RESULT hResult, int column, TCHAR *buffer, int bufSize)
425 {
426 const char *name;
427
428 name = hResult->m_driver->m_fpDrvGetColumnNameUnbuffered(hResult->m_data, column);
429 if (name != NULL)
430 {
431 #ifdef UNICODE
432 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, buffer, bufSize);
433 buffer[bufSize - 1] = 0;
434 #else
435 nx_strncpy(buffer, name, bufSize);
436 #endif
437 }
438 return name != NULL;
439 }
440
441 /**
442 * Get field's value. If buffer is NULL, dynamically allocated string will be returned.
443 * Caller is responsible for destroying it by calling free().
444 */
445 TCHAR LIBNXDB_EXPORTABLE *DBGetField(DB_RESULT hResult, int iRow, int iColumn, TCHAR *pszBuffer, int nBufLen)
446 {
447 #ifdef UNICODE
448 if (pszBuffer != NULL)
449 {
450 *pszBuffer = 0;
451 return hResult->m_driver->m_fpDrvGetField(hResult->m_data, iRow, iColumn, pszBuffer, nBufLen);
452 }
453 else
454 {
455 WCHAR *pszTemp;
456 LONG nLen = hResult->m_driver->m_fpDrvGetFieldLength(hResult->m_data, iRow, iColumn);
457 if (nLen == -1)
458 {
459 pszTemp = NULL;
460 }
461 else
462 {
463 nLen++;
464 pszTemp = (WCHAR *)malloc(nLen * sizeof(WCHAR));
465 hResult->m_driver->m_fpDrvGetField(hResult->m_data, iRow, iColumn, pszTemp, nLen);
466 }
467 return pszTemp;
468 }
469 #else
470 return DBGetFieldA(hResult, iRow, iColumn, pszBuffer, nBufLen);
471 #endif
472 }
473
474 /**
475 * Get field's value as UTF8 string. If buffer is NULL, dynamically allocated string will be returned.
476 * Caller is responsible for destroying it by calling free().
477 */
478 char LIBNXDB_EXPORTABLE *DBGetFieldUTF8(DB_RESULT hResult, int iRow, int iColumn, char *pszBuffer, int nBufLen)
479 {
480 if (hResult->m_driver->m_fpDrvGetFieldUTF8 != NULL)
481 {
482 if (pszBuffer != NULL)
483 {
484 *pszBuffer = 0;
485 return hResult->m_driver->m_fpDrvGetFieldUTF8(hResult->m_data, iRow, iColumn, pszBuffer, nBufLen);
486 }
487 else
488 {
489 char *pszTemp;
490 LONG nLen = hResult->m_driver->m_fpDrvGetFieldLength(hResult->m_data, iRow, iColumn);
491 if (nLen == -1)
492 {
493 pszTemp = NULL;
494 }
495 else
496 {
497 nLen = nLen * 2 + 1; // increase buffer size because driver may return field length in characters
498 pszTemp = (char *)malloc(nLen);
499 hResult->m_driver->m_fpDrvGetFieldUTF8(hResult->m_data, iRow, iColumn, pszTemp, nLen);
500 }
501 return pszTemp;
502 }
503 }
504 else
505 {
506 LONG nLen = hResult->m_driver->m_fpDrvGetFieldLength(hResult->m_data, iRow, iColumn);
507 if (nLen == -1)
508 return NULL;
509 nLen = nLen * 2 + 1; // increase buffer size because driver may return field length in characters
510
511 WCHAR *wtemp = (WCHAR *)malloc(nLen * sizeof(WCHAR));
512 hResult->m_driver->m_fpDrvGetField(hResult->m_data, iRow, iColumn, wtemp, nLen);
513 char *value = (pszBuffer != NULL) ? pszBuffer : (char *)malloc(nLen);
514 WideCharToMultiByte(CP_UTF8, 0, wtemp, -1, value, (pszBuffer != NULL) ? nBufLen : nLen, NULL, NULL);
515 free(wtemp);
516 return value;
517 }
518 }
519
520 /**
521 * Get field's value as multibyte string. If buffer is NULL, dynamically allocated string will be returned.
522 * Caller is responsible for destroying it by calling free().
523 */
524 char LIBNXDB_EXPORTABLE *DBGetFieldA(DB_RESULT hResult, int iRow, int iColumn, char *pszBuffer, int nBufLen)
525 {
526 WCHAR *pwszData, *pwszBuffer;
527 char *pszRet;
528 int nLen;
529
530 if (pszBuffer != NULL)
531 {
532 *pszBuffer = 0;
533 pwszBuffer = (WCHAR *)malloc(nBufLen * sizeof(WCHAR));
534 pwszData = hResult->m_driver->m_fpDrvGetField(hResult->m_data, iRow, iColumn, pwszBuffer, nBufLen);
535 if (pwszData != NULL)
536 {
537 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, pwszData, -1, pszBuffer, nBufLen, NULL, NULL);
538 pszRet = pszBuffer;
539 }
540 else
541 {
542 pszRet = NULL;
543 }
544 free(pwszBuffer);
545 }
546 else
547 {
548 nLen = hResult->m_driver->m_fpDrvGetFieldLength(hResult->m_data, iRow, iColumn);
549 if (nLen == -1)
550 {
551 pszRet = NULL;
552 }
553 else
554 {
555 nLen++;
556 pwszBuffer = (WCHAR *)malloc(nLen * sizeof(WCHAR));
557 pwszData = hResult->m_driver->m_fpDrvGetField(hResult->m_data, iRow, iColumn, pwszBuffer, nLen);
558 if (pwszData != NULL)
559 {
560 nLen = (int)wcslen(pwszData) + 1;
561 pszRet = (char *)malloc(nLen);
562 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, pwszData, -1, pszRet, nLen, NULL, NULL);
563 }
564 else
565 {
566 pszRet = NULL;
567 }
568 free(pwszBuffer);
569 }
570 }
571 return pszRet;
572 }
573
574 /**
575 * Get text field and escape it for XML document. Returned string
576 * always dynamically allocated and must be destroyed by caller.
577 */
578 TCHAR LIBNXDB_EXPORTABLE *DBGetFieldForXML(DB_RESULT hResult, int row, int col)
579 {
580 TCHAR *value = DBGetField(hResult, row, col, NULL, 0);
581 TCHAR *xmlString = EscapeStringForXML(value, -1);
582 safe_free(value);
583 return xmlString;
584 }
585
586 /**
587 * Get field's value as unsigned long
588 */
589 UINT32 LIBNXDB_EXPORTABLE DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn)
590 {
591 INT32 iVal;
592 UINT32 dwVal;
593 TCHAR *pszVal, szBuffer[256];
594
595 pszVal = DBGetField(hResult, iRow, iColumn, szBuffer, 256);
596 if (pszVal == NULL)
597 return 0;
598 StrStrip(pszVal);
599 if (*pszVal == _T('-'))
600 {
601 iVal = _tcstol(pszVal, NULL, 10);
602 memcpy(&dwVal, &iVal, sizeof(INT32)); // To prevent possible conversion
603 }
604 else
605 {
606 dwVal = _tcstoul(pszVal, NULL, 10);
607 }
608 return dwVal;
609 }
610
611 /**
612 * Get field's value as unsigned 64-bit int
613 */
614 UINT64 LIBNXDB_EXPORTABLE DBGetFieldUInt64(DB_RESULT hResult, int iRow, int iColumn)
615 {
616 INT64 iVal;
617 UINT64 qwVal;
618 TCHAR *pszVal, szBuffer[256];
619
620 pszVal = DBGetField(hResult, iRow, iColumn, szBuffer, 256);
621 if (pszVal == NULL)
622 return 0;
623 StrStrip(pszVal);
624 if (*pszVal == _T('-'))
625 {
626 iVal = _tcstoll(pszVal, NULL, 10);
627 memcpy(&qwVal, &iVal, sizeof(INT64)); // To prevent possible conversion
628 }
629 else
630 {
631 qwVal = _tcstoull(pszVal, NULL, 10);
632 }
633 return qwVal;
634 }
635
636 /**
637 * Get field's value as signed long
638 */
639 INT32 LIBNXDB_EXPORTABLE DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn)
640 {
641 TCHAR *pszVal, szBuffer[256];
642
643 pszVal = DBGetField(hResult, iRow, iColumn, szBuffer, 256);
644 return pszVal == NULL ? 0 : _tcstol(pszVal, NULL, 10);
645 }
646
647 /**
648 * Get field's value as signed 64-bit int
649 */
650 INT64 LIBNXDB_EXPORTABLE DBGetFieldInt64(DB_RESULT hResult, int iRow, int iColumn)
651 {
652 TCHAR *pszVal, szBuffer[256];
653
654 pszVal = DBGetField(hResult, iRow, iColumn, szBuffer, 256);
655 return pszVal == NULL ? 0 : _tcstoll(pszVal, NULL, 10);
656 }
657
658 /**
659 * Get field's value as double
660 */
661 double LIBNXDB_EXPORTABLE DBGetFieldDouble(DB_RESULT hResult, int iRow, int iColumn)
662 {
663 TCHAR *pszVal, szBuffer[256];
664
665 pszVal = DBGetField(hResult, iRow, iColumn, szBuffer, 256);
666 return pszVal == NULL ? 0 : _tcstod(pszVal, NULL);
667 }
668
669 /**
670 * Get field's value as IPv4 address
671 */
672 UINT32 LIBNXDB_EXPORTABLE DBGetFieldIPAddr(DB_RESULT hResult, int iRow, int iColumn)
673 {
674 TCHAR *pszVal, szBuffer[256];
675
676 pszVal = DBGetField(hResult, iRow, iColumn, szBuffer, 256);
677 return pszVal == NULL ? 0 : ntohl(_t_inet_addr(pszVal));
678 }
679
680 /**
681 * Get field's value as IP address
682 */
683 InetAddress LIBNXDB_EXPORTABLE DBGetFieldInetAddr(DB_RESULT hResult, int iRow, int iColumn)
684 {
685 TCHAR *pszVal, szBuffer[256];
686
687 pszVal = DBGetField(hResult, iRow, iColumn, szBuffer, 256);
688 return pszVal == NULL ? InetAddress() : InetAddress::parse(pszVal);
689 }
690
691 /**
692 * Get field's value as integer array from byte array encoded in hex
693 */
694 bool LIBNXDB_EXPORTABLE DBGetFieldByteArray(DB_RESULT hResult, int iRow, int iColumn,
695 int *pnArray, int nSize, int nDefault)
696 {
697 char pbBytes[128];
698 bool bResult;
699 int i, nLen;
700 TCHAR *pszVal, szBuffer[256];
701
702 pszVal = DBGetField(hResult, iRow, iColumn, szBuffer, 256);
703 if (pszVal != NULL)
704 {
705 StrToBin(pszVal, (BYTE *)pbBytes, 128);
706 nLen = (int)_tcslen(pszVal) / 2;
707 for(i = 0; (i < nSize) && (i < nLen); i++)
708 pnArray[i] = pbBytes[i];
709 for(; i < nSize; i++)
710 pnArray[i] = nDefault;
711 bResult = true;
712 }
713 else
714 {
715 for(i = 0; i < nSize; i++)
716 pnArray[i] = nDefault;
717 bResult = false;
718 }
719 return bResult;
720 }
721
722 bool LIBNXDB_EXPORTABLE DBGetFieldByteArray2(DB_RESULT hResult, int iRow, int iColumn,
723 BYTE *data, int nSize, int nDefault)
724 {
725 bool bResult;
726 TCHAR *pszVal, szBuffer[256];
727
728 pszVal = DBGetField(hResult, iRow, iColumn, szBuffer, 256);
729 if (pszVal != NULL)
730 {
731 int bytes = (int)StrToBin(pszVal, data, nSize);
732 if (bytes < nSize)
733 memset(&data[bytes], 0, nSize - bytes);
734 bResult = true;
735 }
736 else
737 {
738 memset(data, nDefault, nSize);
739 bResult = false;
740 }
741 return bResult;
742 }
743
744 /**
745 * Get field's value as GUID
746 */
747 uuid LIBNXDB_EXPORTABLE DBGetFieldGUID(DB_RESULT hResult, int iRow, int iColumn)
748 {
749 TCHAR buffer[256];
750 TCHAR *value = DBGetField(hResult, iRow, iColumn, buffer, 256);
751 return (value == NULL) ? uuid::NULL_UUID : uuid::parse(value);
752 }
753
754 /**
755 * Get number of rows in result
756 */
757 int LIBNXDB_EXPORTABLE DBGetNumRows(DB_RESULT hResult)
758 {
759 if (hResult == NULL)
760 return 0;
761 return hResult->m_driver->m_fpDrvGetNumRows(hResult->m_data);
762 }
763
764 /**
765 * Free result
766 */
767 void LIBNXDB_EXPORTABLE DBFreeResult(DB_RESULT hResult)
768 {
769 if (hResult != NULL)
770 {
771 hResult->m_driver->m_fpDrvFreeResult(hResult->m_data);
772 free(hResult);
773 }
774 }
775
776 /**
777 * Unbuffered SELECT query
778 */
779 DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectUnbufferedEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText)
780 {
781 DBDRV_UNBUFFERED_RESULT hResult;
782 DB_UNBUFFERED_RESULT result = NULL;
783 DWORD dwError = DBERR_OTHER_ERROR;
784 #ifdef UNICODE
785 #define pwszQuery szQuery
786 #define wcErrorText errorText
787 #else
788 WCHAR *pwszQuery = WideStringFromMBString(szQuery);
789 WCHAR wcErrorText[DBDRV_MAX_ERROR_TEXT] = L"";
790 #endif
791
792 MutexLock(hConn->m_mutexTransLock);
793 INT64 ms = GetCurrentTimeMs();
794
795 s_perfSelectQueries++;
796 s_perfTotalQueries++;
797
798 hResult = hConn->m_driver->m_fpDrvSelectUnbuffered(hConn->m_connection, pwszQuery, &dwError, wcErrorText);
799 if ((hResult == NULL) && (dwError == DBERR_CONNECTION_LOST) && hConn->m_reconnectEnabled)
800 {
801 DBReconnect(hConn);
802 hResult = hConn->m_driver->m_fpDrvSelectUnbuffered(hConn->m_connection, pwszQuery, &dwError, wcErrorText);
803 }
804
805 ms = GetCurrentTimeMs() - ms;
806 if (hConn->m_driver->m_dumpSql)
807 {
808 nxlog_debug(9, _T("%s unbuffered query: \"%s\" [%d ms]"), (hResult != NULL) ? _T("Successful") : _T("Failed"), szQuery, (int)ms);
809 }
810 if ((hResult != NULL) && ((UINT32)ms > g_sqlQueryExecTimeThreshold))
811 {
812 nxlog_debug(3, _T("Long running query: \"%s\" [%d ms]"), szQuery, (int)ms);
813 s_perfLongRunningQueries++;
814 }
815 if (hResult == NULL)
816 {
817 s_perfFailedQueries++;
818 MutexUnlock(hConn->m_mutexTransLock);
819
820 #ifndef UNICODE
821 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wcErrorText, -1, errorText, DBDRV_MAX_ERROR_TEXT, NULL, NULL);
822 errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
823 #endif
824
825 if (hConn->m_driver->m_logSqlErrors)
826 nxlog_write(g_sqlErrorMsgCode, EVENTLOG_ERROR_TYPE, "ss", szQuery, errorText);
827 if (hConn->m_driver->m_fpEventHandler != NULL)
828 hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, dwError == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
829 }
830
831 #ifndef UNICODE
832 free(pwszQuery);
833 #endif
834
835 if (hResult != NULL)
836 {
837 result = (DB_UNBUFFERED_RESULT)malloc(sizeof(db_unbuffered_result_t));
838 result->m_driver = hConn->m_driver;
839 result->m_connection = hConn;
840 result->m_data = hResult;
841 }
842
843 return result;
844 #undef pwszQuery
845 #undef wcErrorText
846 }
847
848 DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectUnbuffered(DB_HANDLE hConn, const TCHAR *query)
849 {
850 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
851
852 return DBSelectUnbufferedEx(hConn, query, errorText);
853 }
854
855 /**
856 * Fetch next row from unbuffered SELECT result
857 */
858 bool LIBNXDB_EXPORTABLE DBFetch(DB_UNBUFFERED_RESULT hResult)
859 {
860 return hResult->m_driver->m_fpDrvFetch(hResult->m_data);
861 }
862
863 /**
864 * Get field's value from unbuffered SELECT result
865 */
866 TCHAR LIBNXDB_EXPORTABLE *DBGetField(DB_UNBUFFERED_RESULT hResult, int iColumn, TCHAR *pBuffer, int iBufSize)
867 {
868 #ifdef UNICODE
869 if (pBuffer != NULL)
870 {
871 return hResult->m_driver->m_fpDrvGetFieldUnbuffered(hResult->m_data, iColumn, pBuffer, iBufSize);
872 }
873 else
874 {
875 INT32 nLen;
876 WCHAR *pszTemp;
877
878 nLen = hResult->m_driver->m_fpDrvGetFieldLengthUnbuffered(hResult->m_data, iColumn);
879 if (nLen == -1)
880 {
881 pszTemp = NULL;
882 }
883 else
884 {
885 nLen++;
886 pszTemp = (WCHAR *)malloc(nLen * sizeof(WCHAR));
887 hResult->m_driver->m_fpDrvGetFieldUnbuffered(hResult->m_data, iColumn, pszTemp, nLen);
888 }
889 return pszTemp;
890 }
891 #else
892 WCHAR *pwszData, *pwszBuffer;
893 char *pszRet;
894 int nLen;
895
896 if (pBuffer != NULL)
897 {
898 pwszBuffer = (WCHAR *)malloc(iBufSize * sizeof(WCHAR));
899 if (hResult->m_driver->m_fpDrvGetFieldUnbuffered(hResult->m_data, iColumn, pwszBuffer, iBufSize) != NULL)
900 {
901 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR,
902 pwszBuffer, -1, pBuffer, iBufSize, NULL, NULL);
903 pszRet = pBuffer;
904 }
905 else
906 {
907 pszRet = NULL;
908 }
909 free(pwszBuffer);
910 }
911 else
912 {
913 nLen = hResult->m_driver->m_fpDrvGetFieldLengthUnbuffered(hResult->m_data, iColumn);
914 if (nLen == -1)
915 {
916 pszRet = NULL;
917 }
918 else
919 {
920 nLen++;
921 pwszBuffer = (WCHAR *)malloc(nLen * sizeof(WCHAR));
922 pwszData = hResult->m_driver->m_fpDrvGetFieldUnbuffered(hResult->m_data, iColumn, pwszBuffer, nLen);
923 if (pwszData != NULL)
924 {
925 nLen = (int)wcslen(pwszData) + 1;
926 pszRet = (char *)malloc(nLen);
927 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR,
928 pwszData, -1, pszRet, nLen, NULL, NULL);
929 }
930 else
931 {
932 pszRet = NULL;
933 }
934 free(pwszBuffer);
935 }
936 }
937 return pszRet;
938 #endif
939 }
940
941 /**
942 * Get field's value from unbuffered SELECT result as UTF-8 string
943 */
944 char LIBNXDB_EXPORTABLE *DBGetFieldUTF8(DB_UNBUFFERED_RESULT hResult, int iColumn, char *pBuffer, int iBufSize)
945 {
946 /* FIXME: add appropriate call to DB drivers */
947 TCHAR *tmp = DBGetField(hResult, iColumn, NULL, 0);
948 if (tmp == NULL)
949 return NULL;
950
951 char *s;
952 #ifdef UNICODE
953 if (pBuffer != NULL)
954 {
955 s = pBuffer;
956 WideCharToMultiByte(CP_UTF8, 0, tmp, -1, s, iBufSize, NULL, NULL);
957 s[iBufSize - 1] = 0;
958 }
959 else
960 {
961 s = UTF8StringFromWideString(tmp);
962 }
963 #else
964 if (pBuffer != NULL)
965 {
966 s = pBuffer;
967 mb_to_utf8(tmp, -1, s, iBufSize);
968 s[iBufSize - 1] = 0;
969 }
970 else
971 {
972 s = UTF8StringFromMBString(tmp);
973 }
974 #endif
975 free(tmp);
976 return s;
977 }
978
979 /**
980 * Get field's value as unsigned long from unbuffered SELECT result
981 */
982 UINT32 LIBNXDB_EXPORTABLE DBGetFieldULong(DB_UNBUFFERED_RESULT hResult, int iColumn)
983 {
984 INT32 iVal;
985 UINT32 dwVal;
986 TCHAR szBuffer[64];
987
988 if (DBGetField(hResult, iColumn, szBuffer, 64) == NULL)
989 return 0;
990 StrStrip(szBuffer);
991 if (szBuffer[0] == _T('-'))
992 {
993 iVal = _tcstol(szBuffer, NULL, 10);
994 memcpy(&dwVal, &iVal, sizeof(INT32)); // To prevent possible conversion
995 }
996 else
997 {
998 dwVal = _tcstoul(szBuffer, NULL, 10);
999 }
1000 return dwVal;
1001 }
1002
1003 /**
1004 * Get field's value as unsigned 64-bit int from unbuffered SELECT result
1005 */
1006 UINT64 LIBNXDB_EXPORTABLE DBGetFieldUInt64(DB_UNBUFFERED_RESULT hResult, int iColumn)
1007 {
1008 INT64 iVal;
1009 UINT64 qwVal;
1010 TCHAR szBuffer[64];
1011
1012 if (DBGetField(hResult, iColumn, szBuffer, 64) == NULL)
1013 return 0;
1014 StrStrip(szBuffer);
1015 if (szBuffer[0] == _T('-'))
1016 {
1017 iVal = _tcstoll(szBuffer, NULL, 10);
1018 memcpy(&qwVal, &iVal, sizeof(INT64)); // To prevent possible conversion
1019 }
1020 else
1021 {
1022 qwVal = _tcstoull(szBuffer, NULL, 10);
1023 }
1024 return qwVal;
1025 }
1026
1027 /**
1028 * Get field's value as signed long from unbuffered SELECT result
1029 */
1030 INT32 LIBNXDB_EXPORTABLE DBGetFieldLong(DB_UNBUFFERED_RESULT hResult, int iColumn)
1031 {
1032 TCHAR szBuffer[64];
1033 return DBGetField(hResult, iColumn, szBuffer, 64) == NULL ? 0 : _tcstol(szBuffer, NULL, 10);
1034 }
1035
1036 /**
1037 * Get field's value as signed 64-bit int from unbuffered SELECT result
1038 */
1039 INT64 LIBNXDB_EXPORTABLE DBGetFieldInt64(DB_UNBUFFERED_RESULT hResult, int iColumn)
1040 {
1041 TCHAR szBuffer[64];
1042 return DBGetField(hResult, iColumn, szBuffer, 64) == NULL ? 0 : _tcstoll(szBuffer, NULL, 10);
1043 }
1044
1045 /**
1046 * Get field's value as signed long from unbuffered SELECT result
1047 */
1048 double LIBNXDB_EXPORTABLE DBGetFieldDouble(DB_UNBUFFERED_RESULT hResult, int iColumn)
1049 {
1050 TCHAR szBuffer[64];
1051 return DBGetField(hResult, iColumn, szBuffer, 64) == NULL ? 0 : _tcstod(szBuffer, NULL);
1052 }
1053
1054 /**
1055 * Get field's value as IPv4 address from unbuffered SELECT result
1056 */
1057 UINT32 LIBNXDB_EXPORTABLE DBGetFieldIPAddr(DB_UNBUFFERED_RESULT hResult, int iColumn)
1058 {
1059 TCHAR buffer[64];
1060 return (DBGetField(hResult, iColumn, buffer, 64) == NULL) ? INADDR_NONE : ntohl(_t_inet_addr(buffer));
1061 }
1062
1063 /**
1064 * Get field's value as IP address from unbuffered SELECT result
1065 */
1066 InetAddress LIBNXDB_EXPORTABLE DBGetFieldInetAddr(DB_UNBUFFERED_RESULT hResult, int iColumn)
1067 {
1068 TCHAR buffer[64];
1069 return (DBGetField(hResult, iColumn, buffer, 64) == NULL) ? InetAddress() : InetAddress::parse(buffer);
1070 }
1071
1072 /**
1073 * Get field's value as GUID from unbuffered SELECT result
1074 */
1075 uuid LIBNXDB_EXPORTABLE DBGetFieldGUID(DB_UNBUFFERED_RESULT hResult, int iColumn)
1076 {
1077 TCHAR buffer[64];
1078 return (DBGetField(hResult, iColumn, buffer, 64) == NULL) ? uuid::NULL_UUID : uuid::parse(buffer);
1079 }
1080
1081 /**
1082 * Free unbuffered SELECT result
1083 */
1084 void LIBNXDB_EXPORTABLE DBFreeResult(DB_UNBUFFERED_RESULT hResult)
1085 {
1086 hResult->m_driver->m_fpDrvFreeUnbufferedResult(hResult->m_data);
1087 MutexUnlock(hResult->m_connection->m_mutexTransLock);
1088 free(hResult);
1089 }
1090
1091 /**
1092 * Prepare statement
1093 */
1094 DB_STATEMENT LIBNXDB_EXPORTABLE DBPrepareEx(DB_HANDLE hConn, const TCHAR *query, TCHAR *errorText)
1095 {
1096 DB_STATEMENT result = NULL;
1097 INT64 ms;
1098
1099 #ifdef UNICODE
1100 #define pwszQuery query
1101 #define wcErrorText errorText
1102 #else
1103 WCHAR *pwszQuery = WideStringFromMBString(query);
1104 WCHAR wcErrorText[DBDRV_MAX_ERROR_TEXT] = L"";
1105 #endif
1106
1107 MutexLock(hConn->m_mutexTransLock);
1108
1109 if (hConn->m_driver->m_dumpSql)
1110 ms = GetCurrentTimeMs();
1111
1112 DWORD errorCode;
1113 DBDRV_STATEMENT stmt = hConn->m_driver->m_fpDrvPrepare(hConn->m_connection, pwszQuery, &errorCode, wcErrorText);
1114 if ((stmt == NULL) && (errorCode == DBERR_CONNECTION_LOST) && hConn->m_reconnectEnabled)
1115 {
1116 DBReconnect(hConn);
1117 stmt = hConn->m_driver->m_fpDrvPrepare(hConn->m_connection, pwszQuery, &errorCode, wcErrorText);
1118 }
1119 MutexUnlock(hConn->m_mutexTransLock);
1120
1121 if (stmt != NULL)
1122 {
1123 result = (DB_STATEMENT)malloc(sizeof(db_statement_t));
1124 result->m_driver = hConn->m_driver;
1125 result->m_connection = hConn;
1126 result->m_statement = stmt;
1127 result->m_query = _tcsdup(query);
1128 }
1129 else
1130 {
1131 #ifndef UNICODE
1132 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wcErrorText, -1, errorText, DBDRV_MAX_ERROR_TEXT, NULL, NULL);
1133 errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
1134 #endif
1135
1136 if (hConn->m_driver->m_logSqlErrors)
1137 nxlog_write(g_sqlErrorMsgCode, EVENTLOG_ERROR_TYPE, "ss", query, errorText);
1138 if (hConn->m_driver->m_fpEventHandler != NULL)
1139 hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, errorCode == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
1140
1141 s_perfFailedQueries++;
1142 s_perfTotalQueries++;
1143 }
1144
1145 if (hConn->m_driver->m_dumpSql)
1146 {
1147 ms = GetCurrentTimeMs() - ms;
1148 nxlog_debug(9, _T("{%p} %s prepare: \"%s\" [%d ms]"), result, (result != NULL) ? _T("Successful") : _T("Failed"), query, ms);
1149 }
1150
1151 #ifndef UNICODE
1152 free(pwszQuery);
1153 #endif
1154
1155 if (result != NULL)
1156 {
1157 hConn->m_preparedStatements->add(result);
1158 }
1159
1160 return result;
1161 #undef pwszQuery
1162 #undef wcErrorText
1163 }
1164
1165 /**
1166 * Prepare statement
1167 */
1168 DB_STATEMENT LIBNXDB_EXPORTABLE DBPrepare(DB_HANDLE hConn, const TCHAR *query)
1169 {
1170 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
1171 return DBPrepareEx(hConn, query, errorText);
1172 }
1173
1174 /**
1175 * Destroy prepared statement
1176 */
1177 void LIBNXDB_EXPORTABLE DBFreeStatement(DB_STATEMENT hStmt)
1178 {
1179 if (hStmt == NULL)
1180 return;
1181
1182 if (hStmt->m_connection != NULL)
1183 {
1184 hStmt->m_connection->m_preparedStatements->remove(hStmt);
1185 }
1186 hStmt->m_driver->m_fpDrvFreeStatement(hStmt->m_statement);
1187 free(hStmt->m_query);
1188 free(hStmt);
1189 }
1190
1191 /**
1192 * Get source query for prepared statement
1193 */
1194 const TCHAR LIBNXDB_EXPORTABLE *DBGetStatementSource(DB_STATEMENT hStmt)
1195 {
1196 return hStmt->m_query;
1197 }
1198
1199 /**
1200 * Open batch
1201 */
1202 bool LIBNXDB_EXPORTABLE DBOpenBatch(DB_STATEMENT hStmt)
1203 {
1204 if (!IS_VALID_STATEMENT_HANDLE(hStmt) || (hStmt->m_driver->m_fpDrvOpenBatch == NULL))
1205 return false;
1206 return hStmt->m_driver->m_fpDrvOpenBatch(hStmt->m_statement);
1207 }
1208
1209 /**
1210 * Start next batch row batch
1211 */
1212 void LIBNXDB_EXPORTABLE DBNextBatchRow(DB_STATEMENT hStmt)
1213 {
1214 if (IS_VALID_STATEMENT_HANDLE(hStmt) && (hStmt->m_driver->m_fpDrvNextBatchRow != NULL))
1215 hStmt->m_driver->m_fpDrvNextBatchRow(hStmt->m_statement);
1216 }
1217
1218 /**
1219 * Bind parameter (generic)
1220 */
1221 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, int cType, void *buffer, int allocType)
1222 {
1223 if ((pos <= 0) || !IS_VALID_STATEMENT_HANDLE(hStmt))
1224 return;
1225
1226 if (hStmt->m_connection->m_driver->m_dumpSql)
1227 {
1228 if (cType == DB_CTYPE_STRING)
1229 {
1230 nxlog_debug(9, _T("{%p} bind at pos %d: \"%s\""), hStmt, pos, buffer);
1231 }
1232 else if (cType == DB_CTYPE_UTF8_STRING)
1233 {
1234 nxlog_debug(9, _T("{%p} bind at pos %d (UTF-8): \"%hs\""), hStmt, pos, buffer);
1235 }
1236 else
1237 {
1238 TCHAR text[64];
1239 switch(cType)
1240 {
1241 case DB_CTYPE_INT32:
1242 _sntprintf(text, 64, _T("%d"), *((INT32 *)buffer));
1243 break;
1244 case DB_CTYPE_UINT32:
1245 _sntprintf(text, 64, _T("%u"), *((UINT32 *)buffer));
1246 break;
1247 case DB_CTYPE_INT64:
1248 _sntprintf(text, 64, INT64_FMT, *((INT64 *)buffer));
1249 break;
1250 case DB_CTYPE_UINT64:
1251 _sntprintf(text, 64, UINT64_FMT, *((UINT64 *)buffer));
1252 break;
1253 case DB_CTYPE_DOUBLE:
1254 _sntprintf(text, 64, _T("%f"), *((double *)buffer));
1255 break;
1256 }
1257 nxlog_debug(9, _T("{%p} bind at pos %d: \"%s\""), hStmt, pos, text);
1258 }
1259 }
1260
1261 #ifdef UNICODE
1262 #define wBuffer buffer
1263 #define realAllocType allocType
1264 #else
1265 void *wBuffer;
1266 int realAllocType = allocType;
1267 if (cType == DB_CTYPE_STRING)
1268 {
1269 wBuffer = (void *)WideStringFromMBString((char *)buffer);
1270 if (allocType == DB_BIND_DYNAMIC)
1271 free(buffer);
1272 realAllocType = DB_BIND_DYNAMIC;
1273 }
1274 else
1275 {
1276 wBuffer = buffer;
1277 }
1278 #endif
1279 hStmt->m_driver->m_fpDrvBind(hStmt->m_statement, pos, sqlType, cType, wBuffer, realAllocType);
1280 #undef wBuffer
1281 #undef realAllocType
1282 }
1283
1284 /**
1285 * Bind string parameter
1286 */
1287 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const TCHAR *value, int allocType)
1288 {
1289 if (value != NULL)
1290 DBBind(hStmt, pos, sqlType, DB_CTYPE_STRING, (void *)value, allocType);
1291 else
1292 DBBind(hStmt, pos, sqlType, DB_CTYPE_STRING, (void *)_T(""), DB_BIND_STATIC);
1293 }
1294
1295 /**
1296 * Bind string parameter with length validation
1297 */
1298 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const TCHAR *value, int allocType, int maxLen)
1299 {
1300 if (value != NULL)
1301 {
1302 if ((int)_tcslen(value) <= maxLen)
1303 {
1304 DBBind(hStmt, pos, sqlType, DB_CTYPE_STRING, (void *)value, allocType);
1305 }
1306 else
1307 {
1308 if (allocType == DB_BIND_DYNAMIC)
1309 {
1310 ((TCHAR *)value)[maxLen] = 0;
1311 DBBind(hStmt, pos, sqlType, DB_CTYPE_STRING, (void *)value, DB_BIND_DYNAMIC);
1312 }
1313 else
1314 {
1315 TCHAR *temp = (TCHAR *)nx_memdup(value, sizeof(TCHAR) * (maxLen + 1));
1316 temp[maxLen] = 0;
1317 DBBind(hStmt, pos, sqlType, DB_CTYPE_STRING, temp, DB_BIND_DYNAMIC);
1318 }
1319 }
1320 }
1321 else
1322 {
1323 DBBind(hStmt, pos, sqlType, DB_CTYPE_STRING, (void *)_T(""), DB_BIND_STATIC);
1324 }
1325 }
1326
1327 /**
1328 * Bind 32 bit integer parameter
1329 */
1330 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, INT32 value)
1331 {
1332 DBBind(hStmt, pos, sqlType, DB_CTYPE_INT32, &value, DB_BIND_TRANSIENT);
1333 }
1334
1335 /**
1336 * Bind 32 bit unsigned integer parameter
1337 */
1338 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, UINT32 value)
1339 {
1340 // C type intentionally set to INT32 - unsigned numbers will be written as negatives
1341 // and correctly parsed on read by DBGetFieldULong
1342 // Setting it to UINT32 may cause INSERT/UPDATE failures on some databases
1343 DBBind(hStmt, pos, sqlType, DB_CTYPE_INT32, &value, DB_BIND_TRANSIENT);
1344 }
1345
1346 /**
1347 * Bind 64 bit integer parameter
1348 */
1349 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, INT64 value)
1350 {
1351 DBBind(hStmt, pos, sqlType, DB_CTYPE_INT64, &value, DB_BIND_TRANSIENT);
1352 }
1353
1354 /**
1355 * Bind 64 bit unsigned integer parameter
1356 */
1357 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, UINT64 value)
1358 {
1359 DBBind(hStmt, pos, sqlType, DB_CTYPE_UINT64, &value, DB_BIND_TRANSIENT);
1360 }
1361
1362 /**
1363 * Bind floating point parameter
1364 */
1365 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, double value)
1366 {
1367 DBBind(hStmt, pos, sqlType, DB_CTYPE_DOUBLE, &value, DB_BIND_TRANSIENT);
1368 }
1369
1370 /**
1371 * Bind UUID parameter
1372 */
1373 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const uuid& value)
1374 {
1375 TCHAR buffer[64];
1376 DBBind(hStmt, pos, sqlType, DB_CTYPE_STRING, value.toString(buffer), DB_BIND_TRANSIENT);
1377 }
1378
1379 /**
1380 * Execute prepared statement (non-SELECT)
1381 */
1382 bool LIBNXDB_EXPORTABLE DBExecuteEx(DB_STATEMENT hStmt, TCHAR *errorText)
1383 {
1384 if (!IS_VALID_STATEMENT_HANDLE(hStmt))
1385 {
1386 _tcscpy(errorText, _T("Invalid statement handle"));
1387 return false;
1388 }
1389
1390 #ifdef UNICODE
1391 #define wcErrorText errorText
1392 #else
1393 WCHAR wcErrorText[DBDRV_MAX_ERROR_TEXT] = L"";
1394 #endif
1395
1396 DB_HANDLE hConn = hStmt->m_connection;
1397 MutexLock(hConn->m_mutexTransLock);
1398 UINT64 ms = GetCurrentTimeMs();
1399
1400 s_perfNonSelectQueries++;
1401 s_perfTotalQueries++;
1402
1403 DWORD dwResult = hConn->m_driver->m_fpDrvExecute(hConn->m_connection, hStmt->m_statement, wcErrorText);
1404 ms = GetCurrentTimeMs() - ms;
1405 if (hConn->m_driver->m_dumpSql)
1406 {
1407 nxlog_debug(9, _T("%s prepared sync query: \"%s\" [%d ms]"), (dwResult == DBERR_SUCCESS) ? _T("Successful") : _T("Failed"), hStmt->m_query, (int)ms);
1408 }
1409 if ((dwResult == DBERR_SUCCESS) && ((UINT32)ms > g_sqlQueryExecTimeThreshold))
1410 {
1411 nxlog_debug(3, _T("Long running query: \"%s\" [%d ms]"), hStmt->m_query, (int)ms);
1412 s_perfLongRunningQueries++;
1413 }
1414
1415 // Do reconnect if needed, but don't retry statement execution
1416 // because it will fail anyway
1417 if ((dwResult == DBERR_CONNECTION_LOST) && hConn->m_reconnectEnabled)
1418 {
1419 DBReconnect(hConn);
1420 }
1421
1422 MutexUnlock(hConn->m_mutexTransLock);
1423
1424 #ifndef UNICODE
1425 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wcErrorText, -1, errorText, DBDRV_MAX_ERROR_TEXT, NULL, NULL);
1426 errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
1427 #endif
1428
1429 if (dwResult != DBERR_SUCCESS)
1430 {
1431 if (hConn->m_driver->m_logSqlErrors)
1432 nxlog_write(g_sqlErrorMsgCode, EVENTLOG_ERROR_TYPE, "ss", hStmt->m_query, errorText);
1433 if (hConn->m_driver->m_fpEventHandler != NULL)
1434 {
1435 #ifdef UNICODE
1436 hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, hStmt->m_query, wcErrorText, dwResult == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
1437 #else
1438 WCHAR *query = WideStringFromMBString(hStmt->m_query);
1439 hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, query, wcErrorText, dwResult == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
1440 free(query);
1441 #endif
1442 }
1443 s_perfFailedQueries++;
1444 }
1445
1446 return dwResult == DBERR_SUCCESS;
1447 #undef wcErrorText
1448 }
1449
1450 /**
1451 * Execute prepared statement (non-SELECT)
1452 */
1453 bool LIBNXDB_EXPORTABLE DBExecute(DB_STATEMENT hStmt)
1454 {
1455 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
1456 return DBExecuteEx(hStmt, errorText);
1457 }
1458
1459 /**
1460 * Execute prepared SELECT statement
1461 */
1462 DB_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedEx(DB_STATEMENT hStmt, TCHAR *errorText)
1463 {
1464 if (!IS_VALID_STATEMENT_HANDLE(hStmt))
1465 {
1466 _tcscpy(errorText, _T("Invalid statement handle"));
1467 return NULL;
1468 }
1469
1470 DB_RESULT result = NULL;
1471 #ifdef UNICODE
1472 #define wcErrorText errorText
1473 #else
1474 WCHAR wcErrorText[DBDRV_MAX_ERROR_TEXT] = L"";
1475 #endif
1476
1477 DB_HANDLE hConn = hStmt->m_connection;
1478 MutexLock(hConn->m_mutexTransLock);
1479
1480 s_perfSelectQueries++;
1481 s_perfTotalQueries++;
1482
1483 INT64 ms = GetCurrentTimeMs();
1484 DWORD dwError = DBERR_OTHER_ERROR;
1485 DBDRV_RESULT hResult = hConn->m_driver->m_fpDrvSelectPrepared(hConn->m_connection, hStmt->m_statement, &dwError, wcErrorText);
1486
1487 ms = GetCurrentTimeMs() - ms;
1488 if (hConn->m_driver->m_dumpSql)
1489 {
1490 nxlog_debug(9, _T("%s prepared sync query: \"%s\" [%d ms]"),
1491 (hResult != NULL) ? _T("Successful") : _T("Failed"), hStmt->m_query, (int)ms);
1492 }
1493 if ((hResult != NULL) && ((UINT32)ms > g_sqlQueryExecTimeThreshold))
1494 {
1495 nxlog_debug(3, _T("Long running query: \"%s\" [%d ms]"), hStmt->m_query, (int)ms);
1496 s_perfLongRunningQueries++;
1497 }
1498
1499 // Do reconnect if needed, but don't retry statement execution
1500 // because it will fail anyway
1501 if ((hResult == NULL) && (dwError == DBERR_CONNECTION_LOST) && hConn->m_reconnectEnabled)
1502 {
1503 DBReconnect(hConn);
1504 }
1505
1506 MutexUnlock(hConn->m_mutexTransLock);
1507
1508 #ifndef UNICODE
1509 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wcErrorText, -1, errorText, DBDRV_MAX_ERROR_TEXT, NULL, NULL);
1510 errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
1511 #endif
1512
1513 if (hResult == NULL)
1514 {
1515 if (hConn->m_driver->m_logSqlErrors)
1516 nxlog_write(g_sqlErrorMsgCode, EVENTLOG_ERROR_TYPE, "ss", hStmt->m_query, errorText);
1517 if (hConn->m_driver->m_fpEventHandler != NULL)
1518 {
1519 #ifdef UNICODE
1520 hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, hStmt->m_query, wcErrorText, dwError == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
1521 #else
1522 WCHAR *query = WideStringFromMBString(hStmt->m_query);
1523 hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, query, wcErrorText, dwError == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
1524 free(query);
1525 #endif
1526 }
1527 s_perfFailedQueries++;
1528 }
1529
1530 if (hResult != NULL)
1531 {
1532 result = (DB_RESULT)malloc(sizeof(db_result_t));
1533 result->m_driver = hConn->m_driver;
1534 result->m_connection = hConn;
1535 result->m_data = hResult;
1536 }
1537
1538 return result;
1539 #undef wcErrorText
1540 }
1541
1542 /**
1543 * Execute prepared SELECT statement
1544 */
1545 DB_RESULT LIBNXDB_EXPORTABLE DBSelectPrepared(DB_STATEMENT hStmt)
1546 {
1547 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
1548 return DBSelectPreparedEx(hStmt, errorText);
1549 }
1550
1551 /**
1552 * Execute prepared SELECT statement without caching results
1553 */
1554 DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedUnbufferedEx(DB_STATEMENT hStmt, TCHAR *errorText)
1555 {
1556 if (!IS_VALID_STATEMENT_HANDLE(hStmt))
1557 {
1558 _tcscpy(errorText, _T("Invalid statement handle"));
1559 return NULL;
1560 }
1561
1562 DB_UNBUFFERED_RESULT result = NULL;
1563 #ifdef UNICODE
1564 #define wcErrorText errorText
1565 #else
1566 WCHAR wcErrorText[DBDRV_MAX_ERROR_TEXT] = L"";
1567 #endif
1568
1569 DB_HANDLE hConn = hStmt->m_connection;
1570 MutexLock(hConn->m_mutexTransLock);
1571
1572 s_perfSelectQueries++;
1573 s_perfTotalQueries++;
1574
1575 INT64 ms = GetCurrentTimeMs();
1576 DWORD dwError = DBERR_OTHER_ERROR;
1577 DBDRV_UNBUFFERED_RESULT hResult = hConn->m_driver->m_fpDrvSelectPreparedUnbuffered(hConn->m_connection, hStmt->m_statement, &dwError, wcErrorText);
1578
1579 ms = GetCurrentTimeMs() - ms;
1580 if (hConn->m_driver->m_dumpSql)
1581 {
1582 nxlog_debug(9, _T("%s prepared sync query: \"%s\" [%d ms]"),
1583 (hResult != NULL) ? _T("Successful") : _T("Failed"), hStmt->m_query, (int)ms);
1584 }
1585 if ((hResult != NULL) && ((UINT32)ms > g_sqlQueryExecTimeThreshold))
1586 {
1587 nxlog_debug(3, _T("Long running query: \"%s\" [%d ms]"), hStmt->m_query, (int)ms);
1588 s_perfLongRunningQueries++;
1589 }
1590
1591 // Do reconnect if needed, but don't retry statement execution
1592 // because it will fail anyway
1593 if ((hResult == NULL) && (dwError == DBERR_CONNECTION_LOST) && hConn->m_reconnectEnabled)
1594 {
1595 DBReconnect(hConn);
1596 }
1597
1598 #ifndef UNICODE
1599 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wcErrorText, -1, errorText, DBDRV_MAX_ERROR_TEXT, NULL, NULL);
1600 errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
1601 #endif
1602
1603 if (hResult == NULL)
1604 {
1605 MutexUnlock(hConn->m_mutexTransLock);
1606
1607 if (hConn->m_driver->m_logSqlErrors)
1608 nxlog_write(g_sqlErrorMsgCode, EVENTLOG_ERROR_TYPE, "ss", hStmt->m_query, errorText);
1609 if (hConn->m_driver->m_fpEventHandler != NULL)
1610 {
1611 #ifdef UNICODE
1612 hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, hStmt->m_query, wcErrorText, dwError == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
1613 #else
1614 WCHAR *query = WideStringFromMBString(hStmt->m_query);
1615 hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, query, wcErrorText, dwError == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
1616 free(query);
1617 #endif
1618 }
1619 s_perfFailedQueries++;
1620 }
1621
1622 if (hResult != NULL)
1623 {
1624 result = (DB_UNBUFFERED_RESULT)malloc(sizeof(db_unbuffered_result_t));
1625 result->m_driver = hConn->m_driver;
1626 result->m_connection = hConn;
1627 result->m_data = hResult;
1628 }
1629
1630 return result;
1631 #undef wcErrorText
1632 }
1633
1634 /**
1635 * Execute prepared SELECT statement
1636 */
1637 DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedUnbuffered(DB_STATEMENT hStmt)
1638 {
1639 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
1640 return DBSelectPreparedUnbufferedEx(hStmt, errorText);
1641 }
1642
1643 /**
1644 * Begin transaction
1645 */
1646 bool LIBNXDB_EXPORTABLE DBBegin(DB_HANDLE hConn)
1647 {
1648 DWORD dwResult;
1649 bool bRet = false;
1650
1651 MutexLock(hConn->m_mutexTransLock);
1652 if (hConn->m_transactionLevel == 0)
1653 {
1654 dwResult = hConn->m_driver->m_fpDrvBegin(hConn->m_connection);
1655 if ((dwResult == DBERR_CONNECTION_LOST) && hConn->m_reconnectEnabled)
1656 {
1657 DBReconnect(hConn);
1658 dwResult = hConn->m_driver->m_fpDrvBegin(hConn->m_connection);
1659 }
1660 if (dwResult == DBERR_SUCCESS)
1661 {
1662 hConn->m_transactionLevel++;
1663 bRet = true;
1664 nxlog_debug(9, _T("BEGIN TRANSACTION successful (level %d)"), hConn->m_transactionLevel);
1665 }
1666 else
1667 {
1668 MutexUnlock(hConn->m_mutexTransLock);
1669 nxlog_debug(9, _T("BEGIN TRANSACTION failed"), hConn->m_transactionLevel);
1670 }
1671 }
1672 else
1673 {
1674 hConn->m_transactionLevel++;
1675 bRet = true;
1676 nxlog_debug(9, _T("BEGIN TRANSACTION successful (level %d)"), hConn->m_transactionLevel);
1677 }
1678 return bRet;
1679 }
1680
1681 /**
1682 * Commit transaction
1683 */
1684 bool LIBNXDB_EXPORTABLE DBCommit(DB_HANDLE hConn)
1685 {
1686 bool bRet = false;
1687
1688 MutexLock(hConn->m_mutexTransLock);
1689 if (hConn->m_transactionLevel > 0)
1690 {
1691 hConn->m_transactionLevel--;
1692 if (hConn->m_transactionLevel == 0)
1693 bRet = (hConn->m_driver->m_fpDrvCommit(hConn->m_connection) == DBERR_SUCCESS);
1694 else
1695 bRet = true;
1696 nxlog_debug(9, _T("COMMIT TRANSACTION %s (level %d)"), bRet ? _T("successful") : _T("failed"), hConn->m_transactionLevel);
1697 MutexUnlock(hConn->m_mutexTransLock);
1698 }
1699 MutexUnlock(hConn->m_mutexTransLock);
1700 return bRet;
1701 }
1702
1703 /**
1704 * Rollback transaction
1705 */
1706 bool LIBNXDB_EXPORTABLE DBRollback(DB_HANDLE hConn)
1707 {
1708 bool bRet = false;
1709
1710 MutexLock(hConn->m_mutexTransLock);
1711 if (hConn->m_transactionLevel > 0)
1712 {
1713 hConn->m_transactionLevel--;
1714 if (hConn->m_transactionLevel == 0)
1715 bRet = (hConn->m_driver->m_fpDrvRollback(hConn->m_connection) == DBERR_SUCCESS);
1716 else
1717 bRet = true;
1718 nxlog_debug(9, _T("ROLLBACK TRANSACTION %s (level %d)"), bRet ? _T("successful") : _T("failed"), hConn->m_transactionLevel);
1719 MutexUnlock(hConn->m_mutexTransLock);
1720 }
1721 MutexUnlock(hConn->m_mutexTransLock);
1722 return bRet;
1723 }
1724
1725 /**
1726 * Prepare string for using in SQL statement
1727 */
1728 String LIBNXDB_EXPORTABLE DBPrepareString(DB_HANDLE conn, const TCHAR *str, int maxSize)
1729 {
1730 return DBPrepareString(conn->m_driver, str, maxSize);
1731 }
1732
1733 /**
1734 * Prepare string for using in SQL statement
1735 */
1736 String LIBNXDB_EXPORTABLE DBPrepareString(DB_DRIVER drv, const TCHAR *str, int maxSize)
1737 {
1738 String out;
1739 if ((maxSize > 0) && (str != NULL) && (maxSize < (int)_tcslen(str)))
1740 {
1741 TCHAR *temp = (TCHAR *)malloc((maxSize + 1) * sizeof(TCHAR));
1742 nx_strncpy(temp, str, maxSize + 1);
1743 #ifdef UNICODE
1744 out.setBuffer(drv->m_fpDrvPrepareStringW(temp));
1745 #else
1746 out.setBuffer(drv->m_fpDrvPrepareStringA(temp));
1747 #endif
1748 free(temp);
1749 }
1750 else
1751 {
1752 #ifdef UNICODE
1753 out.setBuffer(drv->m_fpDrvPrepareStringW(CHECK_NULL_EX(str)));
1754 #else
1755 out.setBuffer(drv->m_fpDrvPrepareStringA(CHECK_NULL_EX(str)));
1756 #endif
1757 }
1758 return out;
1759 }
1760
1761 /**
1762 * Prepare string for using in SQL statement (multi-byte string version)
1763 */
1764 #ifdef UNICODE
1765
1766 String LIBNXDB_EXPORTABLE DBPrepareStringA(DB_HANDLE conn, const char *str, int maxSize)
1767 {
1768 WCHAR *wcs = WideStringFromMBString(str);
1769 String s = DBPrepareString(conn, wcs, maxSize);
1770 free(wcs);
1771 return s;
1772 }
1773
1774 String LIBNXDB_EXPORTABLE DBPrepareStringA(DB_DRIVER drv, const char *str, int maxSize)
1775 {
1776 WCHAR *wcs = WideStringFromMBString(str);
1777 String s = DBPrepareString(drv, wcs, maxSize);
1778 free(wcs);
1779 return s;
1780 }
1781
1782 #endif
1783
1784 /**
1785 * Check if given table exist
1786 */
1787 int LIBNXDB_EXPORTABLE DBIsTableExist(DB_HANDLE conn, const TCHAR *table)
1788 {
1789 #ifdef UNICODE
1790 return conn->m_driver->m_fpDrvIsTableExist(conn->m_connection, table);
1791 #else
1792 WCHAR wname[256];
1793 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, table, -1, wname, 256);
1794 return conn->m_driver->m_fpDrvIsTableExist(conn->m_connection, wname);
1795 #endif
1796 }
1797
1798 /**
1799 * Get performance counters
1800 */
1801 void LIBNXDB_EXPORTABLE DBGetPerfCounters(LIBNXDB_PERF_COUNTERS *counters)
1802 {
1803 counters->failedQueries = s_perfFailedQueries;
1804 counters->longRunningQueries = s_perfLongRunningQueries;
1805 counters->nonSelectQueries = s_perfNonSelectQueries;
1806 counters->selectQueries = s_perfSelectQueries;
1807 counters->totalQueries = s_perfTotalQueries;
1808 }