FreeSWITCH API Documentation  1.7.0
Macros | Typedefs | Enumerations | Functions
switch_odbc.h File Reference
#include <switch.h>
+ Include dependency graph for switch_odbc.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DEFAULT_ODBC_RETRIES   120
 
#define switch_odbc_handle_callback_exec(handle,sql,callback, pdata, err)
 Execute the sql query and issue a callback for each row returned. More...
 

Typedefs

typedef void * switch_odbc_statement_handle_t
 

Enumerations

enum  switch_odbc_state_t { SWITCH_ODBC_STATE_INIT, SWITCH_ODBC_STATE_DOWN, SWITCH_ODBC_STATE_CONNECTED, SWITCH_ODBC_STATE_ERROR }
 
enum  switch_odbc_status_t { SWITCH_ODBC_SUCCESS = 0, SWITCH_ODBC_FAIL = -1 }
 

Functions

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_handle_connect (switch_odbc_handle_t *handle)
 
void switch_odbc_handle_destroy (switch_odbc_handle_t **handlep)
 
switch_odbc_state_t switch_odbc_handle_get_state (switch_odbc_handle_t *handle)
 
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_exec_string (switch_odbc_handle_t *handle, const char *sql, char *resbuf, size_t len, char **err)
 
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)
 
switch_odbc_status_t switch_odbc_statement_handle_free (switch_odbc_statement_handle_t *stmt)
 
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...
 
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)
 

Macro Definition Documentation

#define DEFAULT_ODBC_RETRIES   120

Definition at line 37 of file switch_odbc.h.

Referenced by switch_odbc_handle_new().

#define switch_odbc_handle_callback_exec (   handle,
  sql,
  callback,
  pdata,
  err 
)
Value:
handle, sql, callback, pdata, err)
#define __SWITCH_FUNC__
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.
Definition: switch_odbc.c:540

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

Parameters
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 92 of file switch_odbc.h.

Referenced by switch_cache_db_execute_sql_callback(), switch_cache_db_execute_sql_callback_err(), switch_cache_db_execute_sql_event_callback(), and switch_cache_db_execute_sql_event_callback_err().

Typedef Documentation

Definition at line 39 of file switch_odbc.h.

Enumeration Type Documentation

Enumerator
SWITCH_ODBC_STATE_INIT 
SWITCH_ODBC_STATE_DOWN 
SWITCH_ODBC_STATE_CONNECTED 
SWITCH_ODBC_STATE_ERROR 

Definition at line 42 of file switch_odbc.h.

Enumerator
SWITCH_ODBC_SUCCESS 
SWITCH_ODBC_FAIL 

Definition at line 49 of file switch_odbc.h.

Function Documentation

switch_bool_t switch_odbc_available ( void  )

Definition at line 738 of file switch_odbc.c.

References SWITCH_FALSE, and SWITCH_TRUE.

Referenced by _switch_cache_db_get_db_handle(), and switch_load_core_config().

739 {
740 #ifdef SWITCH_HAVE_ODBC
741  return SWITCH_TRUE;
742 #else
743  return SWITCH_FALSE;
744 #endif
745 }
int switch_odbc_handle_affected_rows ( switch_odbc_handle_t handle)

Definition at line 729 of file switch_odbc.c.

Referenced by switch_cache_db_affected_rows().

730 {
731 #ifdef SWITCH_HAVE_ODBC
732  return handle->affected_rows;
733 #else
734  return 0;
735 #endif
736 }
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 540 of file switch_odbc.c.

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

544 {
545 #ifdef SWITCH_HAVE_ODBC
546  SQLHSTMT stmt = NULL;
547  SQLSMALLINT c = 0, x = 0;
548  SQLLEN m = 0;
549  char *x_err = NULL, *err_str = NULL;
550  int result;
551  int err_cnt = 0;
552  int done = 0;
553 
554  handle->affected_rows = 0;
555 
556  switch_assert(callback != NULL);
557 
558  if (!db_is_up(handle)) {
559  x_err = "DB is not up!";
560  goto error;
561  }
562 
563  if (SQLAllocHandle(SQL_HANDLE_STMT, handle->con, &stmt) != SQL_SUCCESS) {
564  x_err = "Unable to SQL allocate handle!";
565  goto error;
566  }
567 
568  if (SQLPrepare(stmt, (unsigned char *) sql, SQL_NTS) != SQL_SUCCESS) {
569  x_err = "Unable to prepare SQL statement!";
570  goto error;
571  }
572 
573  result = SQLExecute(stmt);
574 
575  if (result != SQL_SUCCESS && result != SQL_SUCCESS_WITH_INFO && result != SQL_NO_DATA) {
576  x_err = "execute error!";
577  goto error;
578  }
579 
580  SQLNumResultCols(stmt, &c);
581  SQLRowCount(stmt, &m);
582  handle->affected_rows = (int) m;
583 
584 
585  while (!done) {
586  int name_len = 256;
587  char **names;
588  char **vals;
589  int y = 0;
590 
591  result = SQLFetch(stmt);
592 
593  if (result != SQL_SUCCESS) {
594  if (result != SQL_NO_DATA) {
595  err_cnt++;
596  }
597  break;
598  }
599 
600  names = calloc(c, sizeof(*names));
601  vals = calloc(c, sizeof(*vals));
602 
603  switch_assert(names && vals);
604 
605  for (x = 1; x <= c; x++) {
606  SQLSMALLINT NameLength = 0, DataType = 0, DecimalDigits = 0, Nullable = 0;
607  SQLULEN ColumnSize = 0;
608  names[y] = malloc(name_len);
609  memset(names[y], 0, name_len);
610 
611  SQLDescribeCol(stmt, x, (SQLCHAR *) names[y], (SQLSMALLINT) name_len, &NameLength, &DataType, &ColumnSize, &DecimalDigits, &Nullable);
612 
613  if (!ColumnSize) {
614  ColumnSize = 255;
615  }
616  ColumnSize++;
617 
618  vals[y] = malloc(ColumnSize);
619  memset(vals[y], 0, ColumnSize);
620  SQLGetData(stmt, x, SQL_C_CHAR, (SQLCHAR *) vals[y], ColumnSize, NULL);
621  y++;
622  }
623 
624  if (callback(pdata, y, vals, names)) {
625  done = 1;
626  }
627 
628  for (x = 0; x < y; x++) {
629  free(names[x]);
630  free(vals[x]);
631  }
632  free(names);
633  free(vals);
634  }
635 
636  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
637  stmt = NULL; /* Make sure we don't try to free this handle again */
638 
639  if (!err_cnt) {
640  return SWITCH_ODBC_SUCCESS;
641  }
642 
643  error:
644 
645  if (stmt) {
646  err_str = switch_odbc_handle_get_error(handle, stmt);
647  }
648 
649  if (zstr(err_str) && !zstr(x_err)) {
650  err_str = strdup(x_err);
651  }
652 
653  if (err_str) {
654  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));
655  if (err) {
656  *err = err_str;
657  } else {
658  free(err_str);
659  }
660  }
661 
662  if (stmt) {
663  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
664  }
665 
666 
667 #endif
668  return SWITCH_ODBC_FAIL;
669 }
#define zstr(x)
Definition: switch_utils.h:281
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:903
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:709
memset(buf, 0, buflen)
switch_odbc_status_t switch_odbc_handle_connect ( switch_odbc_handle_t handle)

Definition at line 336 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().

337 {
338 #ifdef SWITCH_HAVE_ODBC
339  int result;
340  SQLINTEGER err;
341  int16_t mlen;
342  unsigned char msg[200] = "", stat[10] = "";
343  SQLSMALLINT valueLength = 0;
344  int i = 0;
345 
346  init_odbc_handles(handle, SWITCH_FALSE); /* Init ODBC handles, if they are already initialized, don't do it again */
347 
348  if (handle->state == SWITCH_ODBC_STATE_CONNECTED) {
350  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Re-connecting %s\n", handle->dsn);
351  }
352 
353  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Connecting %s\n", handle->dsn);
354 
355  if (!strstr(handle->dsn, "DRIVER")) {
356  result = SQLConnect(handle->con, (SQLCHAR *) handle->dsn, SQL_NTS, (SQLCHAR *) handle->username, SQL_NTS, (SQLCHAR *) handle->password, SQL_NTS);
357  } else {
358  SQLCHAR outstr[1024] = { 0 };
359  SQLSMALLINT outstrlen = 0;
360  result =
361  SQLDriverConnect(handle->con, NULL, (SQLCHAR *) handle->dsn, (SQLSMALLINT) strlen(handle->dsn), outstr, sizeof(outstr), &outstrlen,
362  SQL_DRIVER_NOPROMPT);
363  }
364 
365  if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
366  char *err_str;
367  if ((err_str = switch_odbc_handle_get_error(handle, NULL))) {
369  free(err_str);
370  } else {
371  SQLGetDiagRec(SQL_HANDLE_DBC, handle->con, 1, stat, &err, msg, sizeof(msg), &mlen);
372  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error SQLConnect=%d errno=%d [%s]\n", result, (int) err, msg);
373  }
374 
375  /* Deallocate handles again, more chanses to succeed when reconnecting */
376  init_odbc_handles(handle, SWITCH_TRUE); /* Reinit ODBC handles */
377  return SWITCH_ODBC_FAIL;
378  }
379 
380  result = SQLGetInfo(handle->con, SQL_DRIVER_NAME, (SQLCHAR *) handle->odbc_driver, 255, &valueLength);
381  if (result == SQL_SUCCESS || result == SQL_SUCCESS_WITH_INFO) {
382  for (i = 0; i < valueLength; ++i)
383  handle->odbc_driver[i] = (char) toupper(handle->odbc_driver[i]);
384  }
385 
386  if (strstr(handle->odbc_driver, "SQORA32.DLL") != 0 || strstr(handle->odbc_driver, "SQORA64.DLL") != 0) {
387  handle->is_firebird = FALSE;
388  handle->is_oracle = TRUE;
389  } else if (strstr(handle->odbc_driver, "FIREBIRD") != 0 || strstr(handle->odbc_driver, "FB32") != 0 || strstr(handle->odbc_driver, "FB64") != 0) {
390  handle->is_firebird = TRUE;
391  handle->is_oracle = FALSE;
392  } else {
393  handle->is_firebird = FALSE;
394  handle->is_oracle = FALSE;
395  }
396 
397  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Connected to [%s]\n", handle->dsn);
398  handle->state = SWITCH_ODBC_STATE_CONNECTED;
399  return SWITCH_ODBC_SUCCESS;
400 #else
401  return SWITCH_ODBC_FAIL;
402 #endif
403 }
switch_odbc_status_t switch_odbc_handle_disconnect(switch_odbc_handle_t *handle)
Definition: switch_odbc.c:119
#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:709
void switch_odbc_handle_destroy ( switch_odbc_handle_t **  handlep)

Definition at line 671 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().

672 {
673 #ifdef SWITCH_HAVE_ODBC
674 
675  switch_odbc_handle_t *handle = NULL;
676 
677  if (!handlep) {
678  return;
679  }
680  handle = *handlep;
681 
682  if (handle) {
684 
685  if (handle->env != SQL_NULL_HANDLE) {
686  SQLFreeHandle(SQL_HANDLE_DBC, handle->con);
687  SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
688  }
689  switch_safe_free(handle->dsn);
690  switch_safe_free(handle->username);
691  switch_safe_free(handle->password);
692  free(handle);
693  }
694  *handlep = NULL;
695 #else
696  return;
697 #endif
698 }
switch_odbc_status_t switch_odbc_handle_disconnect(switch_odbc_handle_t *handle)
Definition: switch_odbc.c:119
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:789
switch_odbc_status_t switch_odbc_handle_disconnect ( switch_odbc_handle_t handle)

Definition at line 119 of file switch_odbc.c.

References SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_ODBC_FAIL, SWITCH_ODBC_STATE_CONNECTED, SWITCH_ODBC_STATE_DOWN, and SWITCH_ODBC_SUCCESS.

Referenced by switch_odbc_handle_connect(), and switch_odbc_handle_destroy().

120 {
121 #ifdef SWITCH_HAVE_ODBC
122 
123  int result;
124 
125  if (!handle) {
126  return SWITCH_ODBC_FAIL;
127  }
128 
129  if (handle->state == SWITCH_ODBC_STATE_CONNECTED) {
130  result = SQLDisconnect(handle->con);
131  if (result == SWITCH_ODBC_SUCCESS) {
132  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "Disconnected %d from [%s]\n", result, handle->dsn);
133  } else {
134  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Disconnecting [%s]\n", handle->dsn);
135  }
136  }
137 
138  handle->state = SWITCH_ODBC_STATE_DOWN;
139 
140  return SWITCH_ODBC_SUCCESS;
141 #else
142  return SWITCH_ODBC_FAIL;
143 #endif
144 }
#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_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 449 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(), and switch_odbc_handle_exec_string().

451 {
452 #ifdef SWITCH_HAVE_ODBC
453  SQLHSTMT stmt = NULL;
454  int result;
455  char *err_str = NULL, *err2 = NULL;
456  SQLLEN m = 0;
457 
458  handle->affected_rows = 0;
459 
460  if (!db_is_up(handle)) {
461  goto error;
462  }
463 
464  if (SQLAllocHandle(SQL_HANDLE_STMT, handle->con, &stmt) != SQL_SUCCESS) {
465  err2 = "SQLAllocHandle failed.";
466  goto error;
467  }
468 
469  if (SQLPrepare(stmt, (unsigned char *) sql, SQL_NTS) != SQL_SUCCESS) {
470  err2 = "SQLPrepare failed.";
471  goto error;
472  }
473 
474  result = SQLExecute(stmt);
475 
476  switch (result) {
477  case SQL_SUCCESS:
478  case SQL_SUCCESS_WITH_INFO:
479  case SQL_NO_DATA:
480  break;
481  case SQL_ERROR:
482  err2 = "SQLExecute returned SQL_ERROR.";
483  goto error;
484  break;
485  case SQL_NEED_DATA:
486  err2 = "SQLExecute returned SQL_NEED_DATA.";
487  goto error;
488  break;
489  default:
490  err2 = "SQLExecute returned unknown result code.";
491  goto error;
492  }
493 
494  SQLRowCount(stmt, &m);
495  handle->affected_rows = (int) m;
496 
497  if (rstmt) {
498  *rstmt = stmt;
499  } else {
500  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
501  }
502 
503  return SWITCH_ODBC_SUCCESS;
504 
505  error:
506 
507 
508  if (stmt) {
509  err_str = switch_odbc_handle_get_error(handle, stmt);
510  }
511 
512  if (zstr(err_str)) {
513  if (err2) {
514  err_str = strdup(err2);
515  } else {
516  err_str = strdup((char *)"SQL ERROR!");
517  }
518  }
519 
520  if (err_str) {
521  if (!switch_stristr("already exists", err_str) && !switch_stristr("duplicate key name", err_str)) {
522  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERR: [%s]\n[%s]\n", sql, switch_str_nil(err_str));
523  }
524  if (err) {
525  *err = err_str;
526  } else {
527  free(err_str);
528  }
529  }
530 
531  if (rstmt) {
532  *rstmt = stmt;
533  } else if (stmt) {
534  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
535  }
536 #endif
537  return SWITCH_ODBC_FAIL;
538 }
#define SWITCH_CHANNEL_LOG
#define zstr(x)
Definition: switch_utils.h:281
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:903
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:709
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 405 of file switch_odbc.c.

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

Referenced by switch_cache_db_execute_sql2str().

406 {
407 #ifdef SWITCH_HAVE_ODBC
409  switch_odbc_statement_handle_t stmt = NULL;
410  SQLCHAR name[1024];
411  SQLLEN m = 0;
412 
413  handle->affected_rows = 0;
414 
415  if (switch_odbc_handle_exec(handle, sql, &stmt, err) == SWITCH_ODBC_SUCCESS) {
416  SQLSMALLINT NameLength, DataType, DecimalDigits, Nullable;
417  SQLULEN ColumnSize;
418  int result;
419 
420  SQLRowCount(stmt, &m);
421  handle->affected_rows = (int) m;
422 
423  if (m == 0) {
424  goto done;
425  }
426 
427  result = SQLFetch(stmt);
428 
429  if (result != SQL_SUCCESS && result != SQL_SUCCESS_WITH_INFO && result != SQL_NO_DATA) {
430  goto done;
431  }
432 
433  SQLDescribeCol(stmt, 1, name, sizeof(name), &NameLength, &DataType, &ColumnSize, &DecimalDigits, &Nullable);
434  SQLGetData(stmt, 1, SQL_C_CHAR, (SQLCHAR *) resbuf, (SQLLEN) len, NULL);
435 
436  sstatus = SWITCH_ODBC_SUCCESS;
437  }
438 
439  done:
440 
442 
443  return sstatus;
444 #else
445  return SWITCH_ODBC_FAIL;
446 #endif
447 }
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:449
void * switch_odbc_statement_handle_t
Definition: switch_odbc.h:39
switch_odbc_status_t switch_odbc_statement_handle_free(switch_odbc_statement_handle_t *stmt)
Definition: switch_odbc.c:321
char* switch_odbc_handle_get_error ( switch_odbc_handle_t handle,
switch_odbc_statement_handle_t  stmt 
)

Definition at line 709 of file switch_odbc.c.

References switch_mprintf().

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

710 {
711 #ifdef SWITCH_HAVE_ODBC
712 
713  char buffer[SQL_MAX_MESSAGE_LENGTH + 1] = "";
714  char sqlstate[SQL_SQLSTATE_SIZE + 1] = "";
715  SQLINTEGER sqlcode;
716  SQLSMALLINT length;
717  char *ret = NULL;
718 
719  if (SQLError(handle->env, handle->con, stmt, (SQLCHAR *) sqlstate, &sqlcode, (SQLCHAR *) buffer, sizeof(buffer), &length) == SQL_SUCCESS) {
720  ret = switch_mprintf("STATE: %s CODE %ld ERROR: %s\n", sqlstate, sqlcode, buffer);
721  };
722 
723  return ret;
724 #else
725  return NULL;
726 #endif
727 }
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
switch_odbc_state_t switch_odbc_handle_get_state ( switch_odbc_handle_t handle)

Definition at line 700 of file switch_odbc.c.

References SWITCH_ODBC_STATE_ERROR, and SWITCH_ODBC_STATE_INIT.

701 {
702 #ifdef SWITCH_HAVE_ODBC
703  return handle ? handle->state : SWITCH_ODBC_STATE_INIT;
704 #else
706 #endif
707 }
switch_odbc_handle_t* switch_odbc_handle_new ( const char *  dsn,
const char *  username,
const char *  password 
)

Definition at line 65 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().

66 {
67 #ifdef SWITCH_HAVE_ODBC
68  switch_odbc_handle_t *new_handle;
69 
70  if (!(new_handle = malloc(sizeof(*new_handle)))) {
71  goto err;
72  }
73 
74  memset(new_handle, 0, sizeof(*new_handle));
75 
76  if (!(new_handle->dsn = strdup(dsn))) {
77  goto err;
78  }
79 
80  if (username) {
81  if (!(new_handle->username = strdup(username))) {
82  goto err;
83  }
84  }
85 
86  if (password) {
87  if (!(new_handle->password = strdup(password))) {
88  goto err;
89  }
90  }
91 
92  new_handle->env = SQL_NULL_HANDLE;
93  new_handle->state = SWITCH_ODBC_STATE_INIT;
94  new_handle->affected_rows = 0;
95  new_handle->num_retries = DEFAULT_ODBC_RETRIES;
96 
97  return new_handle;
98 
99  err:
100  if (new_handle) {
101  switch_safe_free(new_handle->dsn);
102  switch_safe_free(new_handle->username);
103  switch_safe_free(new_handle->password);
104  switch_safe_free(new_handle);
105  }
106 #endif
107  return NULL;
108 }
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:789
#define DEFAULT_ODBC_RETRIES
Definition: switch_odbc.h:37
memset(buf, 0, buflen)
void switch_odbc_set_num_retries ( switch_odbc_handle_t handle,
int  num_retries 
)

Definition at line 110 of file switch_odbc.c.

111 {
112 #ifdef SWITCH_HAVE_ODBC
113  if (handle) {
114  handle->num_retries = num_retries;
115  }
116 #endif
117 }
switch_odbc_status_t switch_odbc_SQLEndTran ( switch_odbc_handle_t handle,
switch_bool_t  commit 
)

Definition at line 760 of file switch_odbc.c.

References SWITCH_FALSE.

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

761 {
762 #ifdef SWITCH_HAVE_ODBC
763  if (commit) {
764  return SQLEndTran(SQL_HANDLE_DBC, handle->con, SQL_COMMIT);
765  } else {
766  return SQLEndTran(SQL_HANDLE_DBC, handle->con, SQL_ROLLBACK);
767  }
768 #else
770 #endif
771 }
switch_odbc_status_t
Definition: switch_odbc.h:49
switch_odbc_status_t switch_odbc_SQLSetAutoCommitAttr ( switch_odbc_handle_t handle,
switch_bool_t  on 
)

Definition at line 747 of file switch_odbc.c.

References SWITCH_FALSE.

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

748 {
749 #ifdef SWITCH_HAVE_ODBC
750  if (on) {
751  return SQLSetConnectAttr(handle->con, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER *) SQL_AUTOCOMMIT_ON, 0 );
752  } else {
753  return SQLSetConnectAttr(handle->con, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER *) SQL_AUTOCOMMIT_OFF, 0 );
754  }
755 #else
757 #endif
758 }
switch_odbc_status_t
Definition: switch_odbc.h:49
switch_odbc_status_t switch_odbc_statement_handle_free ( switch_odbc_statement_handle_t stmt)

Definition at line 321 of file switch_odbc.c.

References SWITCH_ODBC_FAIL, and SWITCH_ODBC_SUCCESS.

Referenced by switch_odbc_handle_exec_string().

322 {
323  if (!stmt || !*stmt) {
324  return SWITCH_ODBC_FAIL;
325  }
326 #ifdef SWITCH_HAVE_ODBC
327  SQLFreeHandle(SQL_HANDLE_STMT, *stmt);
328  *stmt = NULL;
329  return SWITCH_ODBC_SUCCESS;
330 #else
331  return SWITCH_ODBC_FAIL;
332 #endif
333 }