From 673d442aed889c8ca52d571ce80ef8436088c14f Mon Sep 17 00:00:00 2001 From: Raymond Chandler Date: Thu, 27 Nov 2008 02:03:21 +0000 Subject: [PATCH] FSCORE-236 git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@10552 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_odbc.h | 29 ++++++++++++++++++++++++++++- src/switch_odbc.c | 19 +++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/include/switch_odbc.h b/src/include/switch_odbc.h index 1ec96ab489..4bbc15471f 100644 --- a/src/include/switch_odbc.h +++ b/src/include/switch_odbc.h @@ -64,8 +64,35 @@ SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_connect(switch_odbc_hand SWITCH_DECLARE(void) switch_odbc_handle_destroy(switch_odbc_handle_t **handlep); SWITCH_DECLARE(switch_odbc_state_t) switch_odbc_handle_get_state(switch_odbc_handle_t *handle); SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_exec(switch_odbc_handle_t *handle, char *sql, SQLHSTMT * rstmt); -SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_callback_exec(switch_odbc_handle_t *handle, + +/*! + \brief Execute the sql query and issue a callback for each row returned + \param file the file from which this function is called + \param func the function from which this function is called + \param line the line from which this function is called + \param handle the ODBC handle + \param sql the sql string to execute + \param callback the callback function to execute + \param pdata the state data passed on each callback invocation + \return SWITCH_STATUS_SUCCESS if the operation was successful + \note none +*/ +SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_callback_exec_detailed(const char *file, const char *func, int line, switch_odbc_handle_t *handle, char *sql, switch_core_db_callback_func_t callback, void *pdata); +/*! + \brief Execute the sql query and issue a callback for each row returned + \param handle the ODBC handle + \param sql the sql string to execute + \param callback the callback function to execute + \param pdata the state data passed on each callback invocation + \return SWITCH_STATUS_SUCCESS if the operation was successful + \note none +*/ +#define switch_odbc_handle_callback_exec(handle, sql, callback, pdata) \ + switch_odbc_handle_callback_exec_detailed(__FILE__, (char * )__SWITCH_FUNC__, __LINE__, \ + handle, sql, callback, pdata) + + SWITCH_DECLARE(char *) switch_odbc_handle_get_error(switch_odbc_handle_t *handle, SQLHSTMT stmt); SWITCH_END_EXTERN_C #endif diff --git a/src/switch_odbc.c b/src/switch_odbc.c index 19cff2d7d0..41628415e9 100644 --- a/src/switch_odbc.c +++ b/src/switch_odbc.c @@ -322,12 +322,14 @@ SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_exec(switch_odbc_handle_ return SWITCH_ODBC_FAIL; } -SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_callback_exec(switch_odbc_handle_t *handle, - char *sql, switch_core_db_callback_func_t callback, void *pdata) +SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_callback_exec_detailed(const char *file, const char *func, int line, + switch_odbc_handle_t *handle, + char *sql, switch_core_db_callback_func_t callback, void *pdata) { SQLHSTMT stmt = NULL; SQLSMALLINT c = 0, x = 0; SQLLEN m = 0, t = 0; + char *err_str = NULL; int result; switch_assert(callback != NULL); @@ -337,10 +339,12 @@ SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_callback_exec(switch_odb } if (SQLAllocHandle(SQL_HANDLE_STMT, handle->con, &stmt) != SQL_SUCCESS) { + err_str = "Unable to SQL allocate handle."; goto error; } if (SQLPrepare(stmt, (unsigned char *) sql, SQL_NTS) != SQL_SUCCESS) { + err_str = "Unable to prepare SQL statement."; goto error; } @@ -407,7 +411,18 @@ SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_callback_exec(switch_odb error: + /* err_str is already defined for some error cases */ + if (err_str != NULL) { + 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)); + err_str = NULL; + } + if (stmt) { + err_str = switch_odbc_handle_get_error(handle, stmt); + if (!switch_strlen_zero(err_str)) { + 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)); + } + switch_safe_free(err_str); SQLFreeHandle(SQL_HANDLE_STMT, stmt); }