RTS API Documentation  1.10.11
Functions | Variables
switch_odbc.c File Reference
#include <switch.h>
+ Include dependency graph for switch_odbc.c:

Go to the source code of this file.

Functions

void switch_odbc_skip_autocommit_flip (void)
 
switch_odbc_handle_tswitch_odbc_handle_new (const char *dsn, const char *username, const char *password)
 
void switch_odbc_set_num_retries (switch_odbc_handle_t *handle, int num_retries)
 
switch_odbc_status_t switch_odbc_handle_disconnect (switch_odbc_handle_t *handle)
 
switch_odbc_status_t switch_odbc_statement_handle_free (switch_odbc_statement_handle_t *stmt)
 
switch_odbc_status_t switch_odbc_handle_connect (switch_odbc_handle_t *handle)
 
switch_odbc_status_t switch_odbc_handle_exec_string (switch_odbc_handle_t *handle, const char *sql, char *resbuf, size_t len, char **err)
 
switch_odbc_status_t switch_odbc_handle_exec (switch_odbc_handle_t *handle, const char *sql, switch_odbc_statement_handle_t *rstmt, char **err)
 
switch_odbc_status_t switch_odbc_handle_callback_exec_detailed (const char *file, const char *func, int line, switch_odbc_handle_t *handle, const char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err)
 Execute the sql query and issue a callback for each row returned. More...
 
void switch_odbc_handle_destroy (switch_odbc_handle_t **handlep)
 
switch_odbc_state_t switch_odbc_handle_get_state (switch_odbc_handle_t *handle)
 
char * switch_odbc_handle_get_error (switch_odbc_handle_t *handle, switch_odbc_statement_handle_t stmt)
 
int switch_odbc_handle_affected_rows (switch_odbc_handle_t *handle)
 
switch_bool_t switch_odbc_available (void)
 
switch_odbc_status_t switch_odbc_SQLSetAutoCommitAttr (switch_odbc_handle_t *handle, switch_bool_t on)
 
switch_odbc_status_t switch_odbc_SQLEndTran (switch_odbc_handle_t *handle, switch_bool_t commit)
 

Variables

uint8_t skip_autocommit_flip = 0
 

Function Documentation

◆ switch_odbc_available()

switch_bool_t switch_odbc_available ( void  )

Definition at line 809 of file switch_odbc.c.

References SWITCH_FALSE, and SWITCH_TRUE.

Referenced by _switch_cache_db_get_db_handle(), and switch_database_available().

810 {
811 #ifdef SWITCH_HAVE_ODBC
812  return SWITCH_TRUE;
813 #else
814  return SWITCH_FALSE;
815 #endif
816 }

◆ switch_odbc_handle_affected_rows()

int switch_odbc_handle_affected_rows ( switch_odbc_handle_t handle)

Definition at line 800 of file switch_odbc.c.

Referenced by switch_cache_db_affected_rows().

801 {
802 #ifdef SWITCH_HAVE_ODBC
803  return handle->affected_rows;
804 #else
805  return 0;
806 #endif
807 }

◆ switch_odbc_handle_callback_exec_detailed()

switch_odbc_status_t switch_odbc_handle_callback_exec_detailed ( const char *  file,
const char *  func,
int  line,
switch_odbc_handle_t handle,
const char *  sql,
switch_core_db_callback_func_t  callback,
void *  pdata,
char **  err 
)

Execute the sql query and issue a callback for each row returned.

Parameters
filethe file from which this function is called
functhe function from which this function is called
linethe line from which this function is called
handlethe ODBC handle
sqlthe sql string to execute
callbackthe callback function to execute
pdatathe state data passed on each callback invocation
Returns
SWITCH_STATUS_SUCCESS if the operation was successful
Note
none

Definition at line 547 of file switch_odbc.c.

References memset(), switch_assert, SWITCH_CHANNEL_ID_LOG, SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_ODBC_FAIL, switch_odbc_handle_get_error(), SWITCH_ODBC_SUCCESS, switch_safe_free, switch_str_nil, and zstr.

551 {
552 #ifdef SWITCH_HAVE_ODBC
553  SQLHSTMT stmt = NULL;
554  SQLSMALLINT c = 0, x = 0;
555  SQLLEN m = 0;
556  char *x_err = NULL, *err_str = NULL;
557  int result;
558  int err_cnt = 0;
559  int done = 0;
560 
561  handle->affected_rows = 0;
562 
563  switch_assert(callback != NULL);
564 
565  if (!db_is_up(handle)) {
566  x_err = "DB is not up!";
567  goto error;
568  }
569 
570  if (SQLAllocHandle(SQL_HANDLE_STMT, handle->con, &stmt) != SQL_SUCCESS) {
571  x_err = "Unable to SQL allocate handle!";
572  goto error;
573  }
574 
575  if (SQLPrepare(stmt, (unsigned char *) sql, SQL_NTS) != SQL_SUCCESS) {
576  x_err = "Unable to prepare SQL statement!";
577  goto error;
578  }
579 
580  result = SQLExecute(stmt);
581 
582  if (result != SQL_SUCCESS && result != SQL_SUCCESS_WITH_INFO && result != SQL_NO_DATA) {
583  x_err = "execute error!";
584  goto error;
585  }
586 
587  SQLNumResultCols(stmt, &c);
588  SQLRowCount(stmt, &m);
589  handle->affected_rows = (int) m;
590 
591 
592  while (!done) {
593  int name_len = 256;
594  char **names;
595  char **vals;
596  int y = 0;
597 
598  result = SQLFetch(stmt);
599 
600  if (result != SQL_SUCCESS) {
601  if (result != SQL_NO_DATA) {
602  err_cnt++;
603  }
604  break;
605  }
606 
607  names = calloc(c, sizeof(*names));
608  vals = calloc(c, sizeof(*vals));
609 
610  switch_assert(names && vals);
611 
612  for (x = 1; x <= c; x++) {
613  SQLSMALLINT NameLength = 0, DataType = 0, DecimalDigits = 0, Nullable = 0;
614  SQLULEN ColumnSize = 0;
615  SQLLEN numRecs = 0;
616  SQLCHAR SqlState[6], Msg[SQL_MAX_MESSAGE_LENGTH];
617  SQLINTEGER NativeError;
618  SQLSMALLINT diagCount, MsgLen;
619  names[y] = malloc(name_len);
620  switch_assert(names[y]);
621  memset(names[y], 0, name_len);
622 
623  SQLDescribeCol(stmt, x, (SQLCHAR *) names[y], (SQLSMALLINT) name_len, &NameLength, &DataType, &ColumnSize, &DecimalDigits, &Nullable);
624 
625  if (ColumnSize <= 16383 || ColumnSize == 2147483647) {
626  SQLCHAR val[16384] = { 0 };
627  SQLLEN StrLen_or_IndPtr;
628  SQLRETURN rc;
629  ColumnSize = 16384;
630 
631  /* check diag record and see if we can get real size
632  * https://docs.microsoft.com/en-us/sql/odbc/reference/develop-app/using-sqlgetdiagrec-and-sqlgetdiagfield?view=sql-server-ver15
633  * szSqlState = "01004" and StrLen_or_IndPtr=15794
634  */
635  rc = SQLGetData(stmt, x, SQL_C_CHAR, val, ColumnSize, &StrLen_or_IndPtr);
636 
637  if (rc == SQL_SUCCESS_WITH_INFO) {
638  int truncated = 0;
639  diagCount = 1;
640 
641  SQLGetDiagField(SQL_HANDLE_STMT, stmt, 0, SQL_DIAG_NUMBER, &numRecs, 0, 0);
642 
643  while (diagCount <= numRecs) {
644  SQLGetDiagRec(SQL_HANDLE_STMT, stmt, diagCount, SqlState, &NativeError,Msg, sizeof(Msg), &MsgLen);
645  if (!strcmp((char*)SqlState,"01004")){
646  truncated = 1;
647  break;
648  }
649 
650  diagCount++;
651  }
652 
653  if (truncated) {
654  if (StrLen_or_IndPtr && StrLen_or_IndPtr <= 268435456) {
655  int ValLen = strlen((char*)val);
656  ColumnSize = StrLen_or_IndPtr + 1;
657  vals[y] = malloc(ColumnSize);
658  switch_assert(vals[y]);
659  memset(vals[y], 0, ColumnSize);
660  strcpy(vals[y], (char*)val);
661  rc = SQLGetData(stmt, x, SQL_C_CHAR, (SQLCHAR *)vals[y] + ValLen, ColumnSize - ValLen, NULL);
662  if (rc != SQL_SUCCESS
663 #if (ODBCVER >= 0x0300)
664  && rc != SQL_NO_DATA
665 #endif
666  ) {
667  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQLGetData was truncated and failed to complete.\n");
668  switch_safe_free(vals[y]);
669  }
670  } else {
671  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "sql data truncated - %s\n",SqlState);
672  vals[y] = NULL;
673  }
674  } else {
675  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQLGetData failed\n");
676  vals[y] = NULL;
677  }
678  } else if (rc == SQL_SUCCESS){
679  vals[y] = strdup((char *)val);
680  } else {
681  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQLGetData failed\n");
682  vals[y] = NULL;
683  }
684  } else {
685  ColumnSize++;
686 
687  vals[y] = malloc(ColumnSize);
688  switch_assert(vals[y]);
689  memset(vals[y], 0, ColumnSize);
690  SQLGetData(stmt, x, SQL_C_CHAR, (SQLCHAR *) vals[y], ColumnSize, NULL);
691  }
692  y++;
693  }
694 
695  if (callback(pdata, y, vals, names)) {
696  done = 1;
697  }
698 
699  for (x = 0; x < y; x++) {
700  free(names[x]);
701  switch_safe_free(vals[x]);
702  }
703  free(names);
704  free(vals);
705  }
706 
707  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
708  stmt = NULL; /* Make sure we don't try to free this handle again */
709 
710  if (!err_cnt) {
711  return SWITCH_ODBC_SUCCESS;
712  }
713 
714  error:
715 
716  if (stmt) {
717  err_str = switch_odbc_handle_get_error(handle, stmt);
718  }
719 
720  if (zstr(err_str) && !zstr(x_err)) {
721  err_str = strdup(x_err);
722  }
723 
724  if (err_str) {
725  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_ERROR, "ERR: [%s]\n[%s]\n", sql, switch_str_nil(err_str));
726  if (err) {
727  *err = err_str;
728  } else {
729  free(err_str);
730  }
731  }
732 
733  if (stmt) {
734  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
735  }
736 
737 
738 #endif
739  return SWITCH_ODBC_FAIL;
740 }
#define SWITCH_CHANNEL_LOG
#define zstr(x)
Definition: switch_utils.h:314
Definition: cJSON.c:68
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:993
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
char * switch_odbc_handle_get_error(switch_odbc_handle_t *handle, switch_odbc_statement_handle_t stmt)
Definition: switch_odbc.c:780
memset(buf, 0, buflen)

◆ switch_odbc_handle_connect()

switch_odbc_status_t switch_odbc_handle_connect ( switch_odbc_handle_t handle)

Definition at line 343 of file switch_odbc.c.

References FALSE, SWITCH_CHANNEL_LOG, SWITCH_FALSE, SWITCH_LOG_DEBUG1, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_ODBC_FAIL, switch_odbc_handle_disconnect(), switch_odbc_handle_get_error(), SWITCH_ODBC_STATE_CONNECTED, SWITCH_ODBC_SUCCESS, SWITCH_TRUE, and TRUE.

Referenced by _switch_cache_db_get_db_handle(), and switch_odbc_handle_disconnect().

344 {
345 #ifdef SWITCH_HAVE_ODBC
346  int result;
347  SQLINTEGER err;
348  int16_t mlen;
349  unsigned char msg[200] = "", stat[10] = "";
350  SQLSMALLINT valueLength = 0;
351  int i = 0;
352 
353  init_odbc_handles(handle, SWITCH_FALSE); /* Init ODBC handles, if they are already initialized, don't do it again */
354 
355  if (handle->state == SWITCH_ODBC_STATE_CONNECTED) {
357  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Re-connecting %s\n", handle->dsn);
358  }
359 
360  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Connecting %s\n", handle->dsn);
361 
362  if (!strstr(handle->dsn, "DRIVER")) {
363  result = SQLConnect(handle->con, (SQLCHAR *) handle->dsn, SQL_NTS, (SQLCHAR *) handle->username, SQL_NTS, (SQLCHAR *) handle->password, SQL_NTS);
364  } else {
365  SQLCHAR outstr[1024] = { 0 };
366  SQLSMALLINT outstrlen = 0;
367  result =
368  SQLDriverConnect(handle->con, NULL, (SQLCHAR *) handle->dsn, (SQLSMALLINT) strlen(handle->dsn), outstr, sizeof(outstr), &outstrlen,
369  SQL_DRIVER_NOPROMPT);
370  }
371 
372  if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
373  char *err_str;
374  if ((err_str = switch_odbc_handle_get_error(handle, NULL))) {
376  free(err_str);
377  } else {
378  SQLGetDiagRec(SQL_HANDLE_DBC, handle->con, 1, stat, &err, msg, sizeof(msg), &mlen);
379  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error SQLConnect=%d errno=%d [%s]\n", result, (int) err, msg);
380  }
381 
382  /* Deallocate handles again, more chanses to succeed when reconnecting */
383  init_odbc_handles(handle, SWITCH_TRUE); /* Reinit ODBC handles */
384  return SWITCH_ODBC_FAIL;
385  }
386 
387  result = SQLGetInfo(handle->con, SQL_DRIVER_NAME, (SQLCHAR *) handle->odbc_driver, 255, &valueLength);
388  if (result == SQL_SUCCESS || result == SQL_SUCCESS_WITH_INFO) {
389  for (i = 0; i < valueLength; ++i)
390  handle->odbc_driver[i] = (char) toupper(handle->odbc_driver[i]);
391  }
392 
393  if (strstr(handle->odbc_driver, "SQORA32.DLL") != 0 || strstr(handle->odbc_driver, "SQORA64.DLL") != 0) {
394  handle->is_firebird = FALSE;
395  handle->is_oracle = TRUE;
396  } else if (strstr(handle->odbc_driver, "FIREBIRD") != 0 || strstr(handle->odbc_driver, "FB32") != 0 || strstr(handle->odbc_driver, "FB64") != 0) {
397  handle->is_firebird = TRUE;
398  handle->is_oracle = FALSE;
399  } else {
400  handle->is_firebird = FALSE;
401  handle->is_oracle = FALSE;
402  }
403 
404  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Connected to [%s]\n", handle->dsn);
405  handle->state = SWITCH_ODBC_STATE_CONNECTED;
406  return SWITCH_ODBC_SUCCESS;
407 #else
408  return SWITCH_ODBC_FAIL;
409 #endif
410 }
switch_odbc_status_t switch_odbc_handle_disconnect(switch_odbc_handle_t *handle)
Definition: switch_odbc.c:126
#define SWITCH_CHANNEL_LOG
#define FALSE
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define TRUE
char * switch_odbc_handle_get_error(switch_odbc_handle_t *handle, switch_odbc_statement_handle_t stmt)
Definition: switch_odbc.c:780

◆ switch_odbc_handle_destroy()

void switch_odbc_handle_destroy ( switch_odbc_handle_t **  handlep)

Definition at line 742 of file switch_odbc.c.

References switch_odbc_handle_disconnect(), and switch_safe_free.

Referenced by _switch_cache_db_get_db_handle(), and sql_close().

743 {
744 #ifdef SWITCH_HAVE_ODBC
745 
746  switch_odbc_handle_t *handle = NULL;
747 
748  if (!handlep) {
749  return;
750  }
751  handle = *handlep;
752 
753  if (handle) {
755 
756  if (handle->env != SQL_NULL_HANDLE) {
757  SQLFreeHandle(SQL_HANDLE_DBC, handle->con);
758  SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
759  }
760  switch_safe_free(handle->dsn);
761  switch_safe_free(handle->username);
762  switch_safe_free(handle->password);
763  free(handle);
764  }
765  *handlep = NULL;
766 #else
767  return;
768 #endif
769 }
switch_odbc_status_t switch_odbc_handle_disconnect(switch_odbc_handle_t *handle)
Definition: switch_odbc.c:126
struct switch_odbc_handle switch_odbc_handle_t
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885

◆ switch_odbc_handle_disconnect()

switch_odbc_status_t switch_odbc_handle_disconnect ( switch_odbc_handle_t handle)

Definition at line 126 of file switch_odbc.c.

References DEFAULT_ODBC_RETRIES, SWITCH_CHANNEL_LOG, switch_event_add_header(), switch_event_create, switch_event_fire, SWITCH_EVENT_TRAP, SWITCH_LOG_CRIT, SWITCH_LOG_DEBUG10, SWITCH_LOG_ERROR, SWITCH_LOG_INFO, switch_log_printf(), SWITCH_ODBC_FAIL, switch_odbc_handle_connect(), switch_odbc_handle_get_error(), SWITCH_ODBC_STATE_CONNECTED, SWITCH_ODBC_STATE_DOWN, SWITCH_ODBC_SUCCESS, switch_safe_free, SWITCH_STACK_BOTTOM, SWITCH_STATUS_SUCCESS, switch_str_nil, SWITCH_TRUE, and switch_yield.

Referenced by switch_odbc_handle_connect(), and switch_odbc_handle_destroy().

127 {
128 #ifdef SWITCH_HAVE_ODBC
129 
130  int result;
131 
132  if (!handle) {
133  return SWITCH_ODBC_FAIL;
134  }
135 
136  if (handle->state == SWITCH_ODBC_STATE_CONNECTED) {
137  result = SQLDisconnect(handle->con);
138  if (result == SWITCH_ODBC_SUCCESS) {
139  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "Disconnected %d from [%s]\n", result, handle->dsn);
140  } else {
141  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Disconnecting [%s]\n", handle->dsn);
142  }
143  }
144 
145  handle->state = SWITCH_ODBC_STATE_DOWN;
146 
147  return SWITCH_ODBC_SUCCESS;
148 #else
149  return SWITCH_ODBC_FAIL;
150 #endif
151 }
#define SWITCH_CHANNEL_LOG
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.

◆ switch_odbc_handle_exec()

switch_odbc_status_t switch_odbc_handle_exec ( switch_odbc_handle_t handle,
const char *  sql,
switch_odbc_statement_handle_t rstmt,
char **  err 
)

Definition at line 456 of file switch_odbc.c.

References SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_ODBC_FAIL, switch_odbc_handle_get_error(), SWITCH_ODBC_SUCCESS, switch_str_nil, switch_stristr(), and zstr.

Referenced by switch_cache_db_execute_sql_real(), switch_cache_db_test_reactive_ex(), and switch_odbc_handle_exec_string().

458 {
459 #ifdef SWITCH_HAVE_ODBC
460  SQLHSTMT stmt = NULL;
461  int result;
462  char *err_str = NULL, *err2 = NULL;
463  SQLLEN m = 0;
464 
465  handle->affected_rows = 0;
466 
467  if (!db_is_up(handle)) {
468  goto error;
469  }
470 
471  if (SQLAllocHandle(SQL_HANDLE_STMT, handle->con, &stmt) != SQL_SUCCESS) {
472  err2 = "SQLAllocHandle failed.";
473  goto error;
474  }
475 
476  if (SQLPrepare(stmt, (unsigned char *) sql, SQL_NTS) != SQL_SUCCESS) {
477  err2 = "SQLPrepare failed.";
478  goto error;
479  }
480 
481  result = SQLExecute(stmt);
482 
483  switch (result) {
484  case SQL_SUCCESS:
485  case SQL_SUCCESS_WITH_INFO:
486  case SQL_NO_DATA:
487  break;
488  case SQL_ERROR:
489  err2 = "SQLExecute returned SQL_ERROR.";
490  goto error;
491  break;
492  case SQL_NEED_DATA:
493  err2 = "SQLExecute returned SQL_NEED_DATA.";
494  goto error;
495  break;
496  default:
497  err2 = "SQLExecute returned unknown result code.";
498  goto error;
499  }
500 
501  SQLRowCount(stmt, &m);
502  handle->affected_rows = (int) m;
503 
504  if (rstmt) {
505  *rstmt = stmt;
506  } else {
507  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
508  }
509 
510  return SWITCH_ODBC_SUCCESS;
511 
512  error:
513 
514 
515  if (stmt) {
516  err_str = switch_odbc_handle_get_error(handle, stmt);
517  }
518 
519  if (zstr(err_str)) {
520  if (err2) {
521  err_str = strdup(err2);
522  } else {
523  err_str = strdup((char *)"SQL ERROR!");
524  }
525  }
526 
527  if (err_str) {
528  if (!switch_stristr("already exists", err_str) && !switch_stristr("duplicate key name", err_str)) {
529  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERR: [%s]\n[%s]\n", sql, switch_str_nil(err_str));
530  }
531  if (err) {
532  *err = err_str;
533  } else {
534  free(err_str);
535  }
536  }
537 
538  if (rstmt) {
539  *rstmt = stmt;
540  } else if (stmt) {
541  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
542  }
543 #endif
544  return SWITCH_ODBC_FAIL;
545 }
#define SWITCH_CHANNEL_LOG
#define zstr(x)
Definition: switch_utils.h:314
Definition: cJSON.c:68
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:993
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_stristr(const char *instr, const char *str)
char * switch_odbc_handle_get_error(switch_odbc_handle_t *handle, switch_odbc_statement_handle_t stmt)
Definition: switch_odbc.c:780

◆ switch_odbc_handle_exec_string()

switch_odbc_status_t switch_odbc_handle_exec_string ( switch_odbc_handle_t handle,
const char *  sql,
char *  resbuf,
size_t  len,
char **  err 
)

Definition at line 412 of file switch_odbc.c.

References name, SWITCH_ODBC_FAIL, switch_odbc_handle_exec(), switch_odbc_statement_handle_free(), and SWITCH_ODBC_SUCCESS.

Referenced by switch_cache_db_execute_sql2str().

413 {
414 #ifdef SWITCH_HAVE_ODBC
416  switch_odbc_statement_handle_t stmt = NULL;
417  SQLCHAR name[1024];
418  SQLLEN m = 0;
419 
420  handle->affected_rows = 0;
421 
422  if (switch_odbc_handle_exec(handle, sql, &stmt, err) == SWITCH_ODBC_SUCCESS) {
423  SQLSMALLINT NameLength, DataType, DecimalDigits, Nullable;
424  SQLULEN ColumnSize;
425  int result;
426 
427  SQLRowCount(stmt, &m);
428  handle->affected_rows = (int) m;
429 
430  if (m == 0) {
431  goto done;
432  }
433 
434  result = SQLFetch(stmt);
435 
436  if (result != SQL_SUCCESS && result != SQL_SUCCESS_WITH_INFO && result != SQL_NO_DATA) {
437  goto done;
438  }
439 
440  SQLDescribeCol(stmt, 1, name, sizeof(name), &NameLength, &DataType, &ColumnSize, &DecimalDigits, &Nullable);
441  SQLGetData(stmt, 1, SQL_C_CHAR, (SQLCHAR *) resbuf, (SQLLEN) len, NULL);
442 
443  sstatus = SWITCH_ODBC_SUCCESS;
444  }
445 
446  done:
447 
449 
450  return sstatus;
451 #else
452  return SWITCH_ODBC_FAIL;
453 #endif
454 }
switch_odbc_status_t
Definition: switch_odbc.h:49
switch_odbc_status_t switch_odbc_handle_exec(switch_odbc_handle_t *handle, const char *sql, switch_odbc_statement_handle_t *rstmt, char **err)
Definition: switch_odbc.c:456
void * switch_odbc_statement_handle_t
Definition: switch_odbc.h:39
const char *const name
Definition: switch_cJSON.h:250
switch_odbc_status_t switch_odbc_statement_handle_free(switch_odbc_statement_handle_t *stmt)
Definition: switch_odbc.c:328

◆ switch_odbc_handle_get_error()

char* switch_odbc_handle_get_error ( switch_odbc_handle_t handle,
switch_odbc_statement_handle_t  stmt 
)

Definition at line 780 of file switch_odbc.c.

References buffer, length, and switch_mprintf().

Referenced by switch_odbc_handle_callback_exec_detailed(), switch_odbc_handle_connect(), switch_odbc_handle_disconnect(), and switch_odbc_handle_exec().

781 {
782 #ifdef SWITCH_HAVE_ODBC
783 
784  char buffer[SQL_MAX_MESSAGE_LENGTH + 1] = "";
785  char sqlstate[SQL_SQLSTATE_SIZE + 1] = "";
786  SQLINTEGER sqlcode;
787  SQLSMALLINT length;
788  char *ret = NULL;
789 
790  if (SQLError(handle->env, handle->con, stmt, (SQLCHAR *) sqlstate, &sqlcode, (SQLCHAR *) buffer, sizeof(buffer), &length) == SQL_SUCCESS) {
791  ret = switch_mprintf("STATE: %s CODE %ld ERROR: %s\n", sqlstate, sqlcode, buffer);
792  };
793 
794  return ret;
795 #else
796  return NULL;
797 #endif
798 }
char const int length
Definition: switch_cJSON.h:153
char * buffer
Definition: switch_cJSON.h:153
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)

◆ switch_odbc_handle_get_state()

switch_odbc_state_t switch_odbc_handle_get_state ( switch_odbc_handle_t handle)

Definition at line 771 of file switch_odbc.c.

References SWITCH_ODBC_STATE_ERROR, and SWITCH_ODBC_STATE_INIT.

772 {
773 #ifdef SWITCH_HAVE_ODBC
774  return handle ? handle->state : SWITCH_ODBC_STATE_INIT;
775 #else
777 #endif
778 }

◆ switch_odbc_handle_new()

switch_odbc_handle_t* switch_odbc_handle_new ( const char *  dsn,
const char *  username,
const char *  password 
)

Definition at line 72 of file switch_odbc.c.

References DEFAULT_ODBC_RETRIES, memset(), SWITCH_ODBC_STATE_INIT, and switch_safe_free.

Referenced by _switch_cache_db_get_db_handle().

73 {
74 #ifdef SWITCH_HAVE_ODBC
75  switch_odbc_handle_t *new_handle;
76 
77  if (!(new_handle = malloc(sizeof(*new_handle)))) {
78  goto err;
79  }
80 
81  memset(new_handle, 0, sizeof(*new_handle));
82 
83  if (!(new_handle->dsn = strdup(dsn))) {
84  goto err;
85  }
86 
87  if (username) {
88  if (!(new_handle->username = strdup(username))) {
89  goto err;
90  }
91  }
92 
93  if (password) {
94  if (!(new_handle->password = strdup(password))) {
95  goto err;
96  }
97  }
98 
99  new_handle->env = SQL_NULL_HANDLE;
100  new_handle->state = SWITCH_ODBC_STATE_INIT;
101  new_handle->affected_rows = 0;
102  new_handle->num_retries = DEFAULT_ODBC_RETRIES;
103 
104  return new_handle;
105 
106  err:
107  if (new_handle) {
108  switch_safe_free(new_handle->dsn);
109  switch_safe_free(new_handle->username);
110  switch_safe_free(new_handle->password);
111  switch_safe_free(new_handle);
112  }
113 #endif
114  return NULL;
115 }
struct switch_odbc_handle switch_odbc_handle_t
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
#define DEFAULT_ODBC_RETRIES
Definition: switch_odbc.h:37
memset(buf, 0, buflen)

◆ switch_odbc_set_num_retries()

void switch_odbc_set_num_retries ( switch_odbc_handle_t handle,
int  num_retries 
)

Definition at line 117 of file switch_odbc.c.

118 {
119 #ifdef SWITCH_HAVE_ODBC
120  if (handle) {
121  handle->num_retries = num_retries;
122  }
123 #endif
124 }

◆ switch_odbc_skip_autocommit_flip()

void switch_odbc_skip_autocommit_flip ( void  )

Definition at line 67 of file switch_odbc.c.

References skip_autocommit_flip.

Referenced by switch_load_core_config().

68 {
70 }
uint8_t skip_autocommit_flip
Definition: switch_odbc.c:65

◆ switch_odbc_SQLEndTran()

switch_odbc_status_t switch_odbc_SQLEndTran ( switch_odbc_handle_t handle,
switch_bool_t  commit 
)

Definition at line 835 of file switch_odbc.c.

References SWITCH_FALSE.

Referenced by do_trans(), switch_cache_db_persistant_execute_trans_full(), and switch_core_sqldb_start().

836 {
837 #ifdef SWITCH_HAVE_ODBC
838  if (commit) {
839  return SQLEndTran(SQL_HANDLE_DBC, handle->con, SQL_COMMIT);
840  } else {
841  return SQLEndTran(SQL_HANDLE_DBC, handle->con, SQL_ROLLBACK);
842  }
843 #else
845 #endif
846 }
switch_odbc_status_t
Definition: switch_odbc.h:49

◆ switch_odbc_SQLSetAutoCommitAttr()

switch_odbc_status_t switch_odbc_SQLSetAutoCommitAttr ( switch_odbc_handle_t handle,
switch_bool_t  on 
)

Definition at line 818 of file switch_odbc.c.

References skip_autocommit_flip, SWITCH_FALSE, and SWITCH_ODBC_SUCCESS.

Referenced by do_trans(), switch_cache_db_persistant_execute_trans_full(), and switch_core_sqldb_start().

819 {
820 #ifdef SWITCH_HAVE_ODBC
821  if (skip_autocommit_flip) {
822  return SWITCH_ODBC_SUCCESS;
823  }
824 
825  if (on) {
826  return SQLSetConnectAttr(handle->con, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER *) SQL_AUTOCOMMIT_ON, 0 );
827  } else {
828  return SQLSetConnectAttr(handle->con, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER *) SQL_AUTOCOMMIT_OFF, 0 );
829  }
830 #else
832 #endif
833 }
switch_odbc_status_t
Definition: switch_odbc.h:49
uint8_t skip_autocommit_flip
Definition: switch_odbc.c:65

◆ switch_odbc_statement_handle_free()

switch_odbc_status_t switch_odbc_statement_handle_free ( switch_odbc_statement_handle_t stmt)

Definition at line 328 of file switch_odbc.c.

References SWITCH_ODBC_FAIL, and SWITCH_ODBC_SUCCESS.

Referenced by switch_odbc_handle_exec_string().

329 {
330  if (!stmt || !*stmt) {
331  return SWITCH_ODBC_FAIL;
332  }
333 #ifdef SWITCH_HAVE_ODBC
334  SQLFreeHandle(SQL_HANDLE_STMT, *stmt);
335  *stmt = NULL;
336  return SWITCH_ODBC_SUCCESS;
337 #else
338  return SWITCH_ODBC_FAIL;
339 #endif
340 }

Variable Documentation

◆ skip_autocommit_flip

uint8_t skip_autocommit_flip = 0