Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

switch_odbc.c File Reference


Data Structures

struct  switch_odbc_handle

Functions

switch_odbc_handle_tswitch_odbc_handle_new (char *dsn, char *username, char *password)
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)
switch_odbc_status_t switch_odbc_handle_exec (switch_odbc_handle_t *handle, char *sql, SQLHSTMT *rstmt)
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)
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, SQLHSTMT stmt)


Function Documentation

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
 

00317 {
00318         SQLHSTMT stmt = NULL;
00319         SQLSMALLINT c = 0, x = 0;
00320         SQLLEN m = 0, t = 0;
00321         int result;
00322 
00323         switch_assert(callback != NULL);
00324 
00325         if (!db_is_up(handle)) {
00326                 goto error;
00327         }
00328 
00329         if (SQLAllocHandle(SQL_HANDLE_STMT, handle->con, &stmt) != SQL_SUCCESS) {
00330                 goto error;
00331         }
00332 
00333         if (SQLPrepare(stmt, (unsigned char *) sql, SQL_NTS) != SQL_SUCCESS) {
00334                 goto error;
00335         }
00336 
00337         result = SQLExecute(stmt);
00338 
00339         if (result != SQL_SUCCESS && result != SQL_SUCCESS_WITH_INFO) {
00340                 goto error;
00341         }
00342 
00343         SQLNumResultCols(stmt, &c);
00344         SQLRowCount(stmt, &m);
00345 
00346         if (m > 0) {
00347                 for (t = 0; t < m; t++) {
00348                         int name_len = 256;
00349                         char **names;
00350                         char **vals;
00351                         int y = 0;
00352                 
00353                         if (!(result = SQLFetch(stmt)) == SQL_SUCCESS) {
00354                                 goto error;
00355                         }
00356                 
00357                         names = calloc(c, sizeof(*names));
00358                         vals = calloc(c, sizeof(*vals));
00359                 
00360                         switch_assert(names && vals);
00361 
00362                         for (x = 1; x <= c; x++) {
00363                                 SQLSMALLINT NameLength, DataType, DecimalDigits, Nullable;
00364                                 SQLULEN ColumnSize;
00365                                 names[y] = malloc(name_len);
00366                                 memset(names[y], 0, name_len);
00367 
00368                                 SQLDescribeCol(stmt, x, (SQLCHAR *) names[y], (SQLSMALLINT)name_len, &NameLength, &DataType, &ColumnSize, &DecimalDigits, &Nullable);
00369                                 ColumnSize++;
00370 
00371                                 vals[y] = malloc(ColumnSize);
00372                                 memset(vals[y], 0, ColumnSize);
00373                                 SQLGetData(stmt, x, SQL_C_CHAR, (SQLCHAR *) vals[y], ColumnSize, NULL);
00374                                 y++;
00375                         }
00376                 
00377                         if (callback(pdata, y, vals, names)) {
00378                                 break;
00379                         }
00380 
00381                         for (x = 0; x < y; x++) {
00382                                 free(names[x]);
00383                                 free(vals[x]);
00384                         }
00385                         free(names);
00386                         free(vals);
00387                 }
00388         }
00389 
00390         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00391 
00392         return SWITCH_ODBC_SUCCESS;
00393 
00394   error:
00395 
00396         if (stmt) {
00397                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00398         }
00399 
00400         return SWITCH_ODBC_FAIL;
00401 }

switch_odbc_status_t switch_odbc_handle_connect switch_odbc_handle_t handle  ) 
 

00107 {
00108         int result;
00109         SQLINTEGER err;
00110         int16_t mlen;
00111         unsigned char msg[200], stat[10];
00112         SQLSMALLINT valueLength = 0;
00113         int i = 0;
00114 
00115         if (handle->env == SQL_NULL_HANDLE) {
00116                 result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &handle->env);
00117 
00118                 if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
00119                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error AllocHandle\n");
00120                         return SWITCH_ODBC_FAIL;
00121                 }
00122 
00123                 result = SQLSetEnvAttr(handle->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
00124 
00125                 if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
00126                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error SetEnv\n");
00127                         SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
00128                         return SWITCH_ODBC_FAIL;
00129                 }
00130 
00131                 result = SQLAllocHandle(SQL_HANDLE_DBC, handle->env, &handle->con);
00132 
00133                 if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
00134                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error AllocHDB %d\n", result);
00135                         SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
00136                         return SWITCH_ODBC_FAIL;
00137                 }
00138                 SQLSetConnectAttr(handle->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
00139         }
00140         if (handle->state == SWITCH_ODBC_STATE_CONNECTED) {
00141                 switch_odbc_handle_disconnect(handle);
00142                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Re-connecting %s\n", handle->dsn);
00143         }
00144 
00145         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connecting %s\n", handle->dsn);
00146         
00147         if(!strstr(handle->dsn, "DRIVER")) { 
00148                 result = SQLConnect(handle->con, (SQLCHAR *) handle->dsn, SQL_NTS, (SQLCHAR *) handle->username, SQL_NTS, (SQLCHAR *) handle->password, SQL_NTS); 
00149         } else { 
00150                 SQLCHAR outstr[1024] = {0}; 
00151                 SQLSMALLINT outstrlen = 0; 
00152                 result = SQLDriverConnect(handle->con, NULL, (SQLCHAR *) handle->dsn, (SQLSMALLINT)strlen(handle->dsn), outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT); 
00153         } 
00154 
00155         if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
00156                 char *err_str;
00157                 if ((err_str = switch_odbc_handle_get_error(handle, NULL))) {
00158                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s\n",err_str);
00159                         free(err_str);
00160                 } else {
00161                         SQLGetDiagRec(SQL_HANDLE_DBC, handle->con, 1, stat, &err, msg, 100, &mlen);
00162                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error SQLConnect=%d errno=%d %s\n", result, (int) err, msg);
00163                 }
00164                 SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
00165                 return SWITCH_ODBC_FAIL;
00166         }
00167 
00168         result = SQLGetInfo(handle->con, SQL_DRIVER_NAME, (SQLCHAR*)handle->odbc_driver, 255, &valueLength);
00169         if ( result == SQL_SUCCESS || result == SQL_SUCCESS_WITH_INFO) {
00170                 for (i = 0; i < valueLength; ++i)
00171                         handle->odbc_driver[i] = (char)toupper(handle->odbc_driver[i]);
00172         }
00173 
00174         if (strstr(handle->odbc_driver, "FIREBIRD") != 0 || strstr(handle->odbc_driver, "FB32") != 0 || strstr(handle->odbc_driver, "FB64") != 0) {
00175                 handle->is_firebird = TRUE;
00176         } else {
00177                 handle->is_firebird = FALSE;
00178         }
00179 
00180         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connected to [%s]\n", handle->dsn);
00181         handle->state = SWITCH_ODBC_STATE_CONNECTED;
00182         return SWITCH_ODBC_SUCCESS;
00183 }

Here is the call graph for this function:

void switch_odbc_handle_destroy switch_odbc_handle_t **  handlep  ) 
 

00404 {
00405         switch_odbc_handle_t *handle = NULL;
00406 
00407         if (!handlep) {
00408                 return;
00409         }
00410         handle = *handlep;
00411 
00412         if (handle) {
00413                 switch_odbc_handle_disconnect(handle);
00414 
00415                 SQLFreeHandle(SQL_HANDLE_DBC, handle->con);
00416                 SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
00417                 switch_safe_free(handle->dsn);
00418                 switch_safe_free(handle->username);
00419                 switch_safe_free(handle->password);
00420                 free(handle);
00421         }
00422         *handlep = NULL;
00423 }

Here is the call graph for this function:

switch_odbc_status_t switch_odbc_handle_disconnect switch_odbc_handle_t handle  ) 
 

00089 {
00090         int result;
00091 
00092         if (handle->state == SWITCH_ODBC_STATE_CONNECTED) {
00093                 result = SQLDisconnect(handle->con);
00094                 if (result == SWITCH_ODBC_SUCCESS) {
00095                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Disconnected %d from [%s]\n", result, handle->dsn);
00096                 } else {
00097                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Disconnectiong [%s]\n", handle->dsn);
00098                 }
00099         } 
00100 
00101         handle->state = SWITCH_ODBC_STATE_DOWN;
00102 
00103         return SWITCH_ODBC_SUCCESS;
00104 }

Here is the call graph for this function:

switch_odbc_status_t switch_odbc_handle_exec switch_odbc_handle_t handle,
char *  sql,
SQLHSTMT *  rstmt
 

00276 {
00277         SQLHSTMT stmt = NULL;
00278         int result;
00279 
00280         if (!db_is_up(handle)) {
00281                 goto error;
00282         }
00283         
00284         if (SQLAllocHandle(SQL_HANDLE_STMT, handle->con, &stmt) != SQL_SUCCESS) {
00285                 goto error;
00286         }
00287 
00288         if (SQLPrepare(stmt, (unsigned char *) sql, SQL_NTS) != SQL_SUCCESS) {
00289                 goto error;
00290         }
00291 
00292         result = SQLExecute(stmt);
00293 
00294         if (result != SQL_SUCCESS && result != SQL_SUCCESS_WITH_INFO) {
00295                 goto error;
00296         }
00297 
00298         if (rstmt) {
00299                 *rstmt = stmt;
00300         } else {
00301                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00302         }
00303 
00304         return SWITCH_ODBC_SUCCESS;
00305 
00306  error:
00307         if (rstmt) {
00308         *rstmt = stmt;
00309     } else if (stmt) {
00310                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00311         }
00312         return SWITCH_ODBC_FAIL;        
00313 }

char* switch_odbc_handle_get_error switch_odbc_handle_t handle,
SQLHSTMT  stmt
 

00431 {
00432         char buffer[SQL_MAX_MESSAGE_LENGTH + 1] = "";
00433         char sqlstate[SQL_SQLSTATE_SIZE + 1] = "";
00434         SQLINTEGER sqlcode;
00435         SQLSMALLINT length;
00436         char *ret = NULL;
00437 
00438         if (SQLError(handle->env, handle->con, stmt, (SQLCHAR *)sqlstate, &sqlcode, (SQLCHAR *)buffer, sizeof(buffer), &length) == SQL_SUCCESS) {
00439                 ret = switch_mprintf("STATE: %s CODE %ld ERROR: %s\n", sqlstate,  sqlcode, buffer);
00440         };
00441 
00442         return ret;
00443 }

Here is the call graph for this function:

switch_odbc_state_t switch_odbc_handle_get_state switch_odbc_handle_t handle  ) 
 

00426 {
00427         return handle ? handle->state : SWITCH_ODBC_STATE_INIT;
00428 }

switch_odbc_handle_t* switch_odbc_handle_new char *  dsn,
char *  username,
char *  password
 

00047 {
00048         switch_odbc_handle_t *new_handle;
00049 
00050         if (!(new_handle = malloc(sizeof(*new_handle)))) {
00051                 goto err;
00052         }
00053         
00054         memset(new_handle, 0, sizeof(*new_handle));
00055 
00056         if (!(new_handle->dsn = strdup(dsn))) {
00057                 goto err;
00058         }
00059 
00060         if (username) {
00061                 if (!(new_handle->username = strdup(username))) {
00062                         goto err;
00063                 }
00064         }
00065 
00066         if (password) {
00067                 if (!(new_handle->password = strdup(password))) {
00068                         goto err;
00069                 }
00070         }
00071 
00072         new_handle->env = SQL_NULL_HANDLE;
00073         new_handle->state = SWITCH_ODBC_STATE_INIT;
00074 
00075         return new_handle;
00076 
00077   err:
00078         if (new_handle) {
00079                 switch_safe_free(new_handle->dsn);
00080                 switch_safe_free(new_handle->username);
00081                 switch_safe_free(new_handle->password);
00082                 switch_safe_free(new_handle);
00083         }
00084 
00085         return NULL;
00086 }


Generated on Mon May 26 22:06:52 2008 for FreeSWITCH by  doxygen 1.3.9.1