Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:49:16 2009

## Asterisk developer's documentation

# res_odbc.h File Reference

ODBC resource manager. More...

`#include <sql.h>`
`#include <sqlext.h>`
`#include <sqltypes.h>`

Include dependency graph for res_odbc.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Data Structures

struct | odbc_obj

## Typedefs

typedef struct odbc_obj | odbc_obj

## Enumerations

enum | odbc_status { ODBC_SUCCESS = 0, ODBC_FAIL = -1 }

## Functions

void | destroy_odbc_obj (odbc_obj **obj)
odbc_obj * | fetch_odbc_obj (const char *name, int check)
odbc_obj * | new_odbc_obj (char *name, char *dsn, char *username, char *password)
int | odbc_dump_fd (int fd, odbc_obj *obj)
odbc_status | odbc_obj_connect (odbc_obj *obj)
odbc_status | odbc_obj_disconnect (odbc_obj *obj)
SQLHSTMT | odbc_prepare_and_execute (odbc_obj *obj, SQLHSTMT(*prepare_cb)(odbc_obj *obj, void *data), void *data)
int | odbc_sanity_check (odbc_obj *obj)
int | odbc_smart_direct_execute (odbc_obj *obj, SQLHSTMT stmt, char *sql)
int | odbc_smart_execute (odbc_obj *obj, SQLHSTMT stmt)
int | register_odbc_obj (char *name, odbc_obj *obj)

## Detailed Description

ODBC resource manager.

Definition in file res_odbc.h.

## Typedef Documentation

typedef struct odbc_obj odbc_obj

Definition at line 32 of file res_odbc.h.

## Enumeration Type Documentation

enum odbc_status

Enumerator:

  ODBC_SUCCESS |
  ODBC_FAIL |

Definition at line 34 of file res_odbc.h.

    00034 { ODBC_SUCCESS=0,ODBC_FAIL=-1} odbc_status;

## Function Documentation

void destroy_odbc_obj | ( | odbc_obj ** | obj | ) |

Definition at line 462 of file res_odbc.c.

References ast_mutex_destroy(), ast_mutex_lock(), ast_mutex_unlock(), free, and odbc_obj_disconnect().

Referenced by odbc_destroy().

    00463 {
    00464    odbc_obj_disconnect(*obj);
    00465
    00466    ast_mutex_lock(&(*obj)->lock);
    00467    SQLFreeHandle(SQL_HANDLE_STMT, (*obj)->stmt);
    00468    SQLFreeHandle(SQL_HANDLE_DBC, (*obj)->con);
    00469    SQLFreeHandle(SQL_HANDLE_ENV, (*obj)->env);
    00470
    00471    free((*obj)->name);
    00472    free((*obj)->dsn);
    00473    if ((*obj)->username)
    00474       free((*obj)->username);
    00475    if ((*obj)->password)
    00476       free((*obj)->password);
    00477    ast_mutex_unlock(&(*obj)->lock);
    00478    ast_mutex_destroy(&(*obj)->lock);
    00479    free(*obj);
    00480 }

odbc_obj* fetch_odbc_obj | ( | const char * | name,
|  | int | check |
| ) |  |  |

Definition at line 411 of file res_odbc.c.

References odbc_read(), ODBC_REGISTRY, and odbc_sanity_check().

Referenced by config_odbc(), realtime_multi_odbc(), realtime_odbc(), and update_odbc().

    00412 {
    00413    odbc_obj *obj = NULL;
    00414    if((obj = (odbc_obj *) odbc_read(ODBC_REGISTRY, name))) {
    00415       if(check)
    00416          odbc_sanity_check(obj);
    00417    }
    00418    return obj;
    00419 }

odbc_obj* new_odbc_obj | ( | char * | name,
|  | char * | dsn,
|  | char * | username,
|  | char * | password |
| ) |  |  |

Definition at line 421 of file res_odbc.c.

References ast_mutex_init(), calloc, cleanup(), free, and malloc.

Referenced by load_odbc_config().

    00422 {
    00423    static odbc_obj *new;
    00424
    00425    if (!(new = calloc(1, sizeof(*new))) ||
    00426        !(new->name = malloc(strlen(name) + 1)) ||
    00427        !(new->dsn = malloc(strlen(dsn) + 1)))
    00428          goto cleanup;
    00429
    00430    if (username) {
    00431       if (!(new->username = malloc(strlen(username) + 1)))
    00432          goto cleanup;
    00433       strcpy(new->username, username);
    00434    }
    00435
    00436    if (password) {
    00437       if (!(new->password = malloc(strlen(password) + 1)))
    00438          goto cleanup;
    00439       strcpy(new->password, password);
    00440    }
    00441
    00442    strcpy(new->name, name);
    00443    strcpy(new->dsn, dsn);
    00444    new->env = SQL_NULL_HANDLE;
    00445    new->up = 0;
    00446    ast_mutex_init(&new->lock);
    00447    return new;
    00448
    00449 cleanup:
    00450    if (new) {
    00451       free(new->name);
    00452       free(new->dsn);
    00453       free(new->username);
    00454       free(new->password);
    00455
    00456       free(new);
    00457    }
    00458
    00459    return NULL;
    00460 }

int odbc_dump_fd | ( | int | fd,
|  | odbc_obj * | obj |
| ) |  |  |

Definition at line 303 of file res_odbc.c.

References ast_cli(), odbc_obj::dsn, odbc_obj::name, odbc_sanity_check(), and odbc_obj::up.

Referenced by odbc_show_command().

    00304 {
    00305    /* make sure the connection is up before we lie to our master.*/
    00306    odbc_sanity_check(obj);
    00307    ast_cli(fd, "Name: %s\nDSN: %s\nConnected: %s\n\n", obj->name, obj->dsn, obj->up ? "yes" : "no");
    00308    return 0;
    00309 }

odbc_status odbc_obj_connect | ( | odbc_obj * | obj | ) |

Definition at line 501 of file res_odbc.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), odbc_obj::con, odbc_obj::dsn, odbc_obj::env, odbc_obj::lock, LOG_NOTICE, LOG_WARNING, odbc_obj::name, ODBC_FAIL, odbc_obj_disconnect(), ODBC_SUCCESS, option_verbose, odbc_obj::password, odbc_obj::up, and odbc_obj::username.

Referenced by load_odbc_config(), odbc_connect_command(), odbc_prepare_and_execute(), odbc_sanity_check(), and odbc_smart_direct_execute().

    00502 {
    00503    int res;
    00504    SQLINTEGER err;
    00505    short int mlen;
    00506    unsigned char msg[200], stat[10];
    00507
    00508    ast_mutex_lock(&obj->lock);
    00509
    00510    if (obj->env == SQL_NULL_HANDLE) {
    00511       res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &obj->env);
    00512
    00513       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
    00514          if (option_verbose > 3)
    00515             ast_log(LOG_WARNING, "res_odbc: Error AllocHandle\n");
    00516          ast_mutex_unlock(&obj->lock);
    00517          return ODBC_FAIL;
    00518       }
    00519
    00520       res = SQLSetEnvAttr(obj->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
    00521
    00522       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
    00523          if (option_verbose > 3)
    00524             ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
    00525          SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
    00526          ast_mutex_unlock(&obj->lock);
    00527          return ODBC_FAIL;
    00528       }
    00529
    00530       res = SQLAllocHandle(SQL_HANDLE_DBC, obj->env, &obj->con);
    00531
    00532       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
    00533
    00534          if (option_verbose > 3)
    00535             ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
    00536          SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
    00537
    00538          ast_mutex_unlock(&obj->lock);
    00539          return ODBC_FAIL;
    00540       }
    00541       SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
    00542    }
    00543    if(obj->up) {
    00544       odbc_obj_disconnect(obj);
    00545       ast_log(LOG_NOTICE,"Re-connecting %s\n", obj->name);
    00546    }
    00547
    00548    ast_log(LOG_NOTICE, "Connecting %s\n", obj->name);
    00549
    00550    res = SQLConnect(obj->con,
    00551          (SQLCHAR *) obj->dsn, SQL_NTS,
    00552          (SQLCHAR *) obj->username, SQL_NTS,
    00553          (SQLCHAR *) obj->password, SQL_NTS);
    00554
    00555    if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
    00556       SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
    00557       SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
    00558       ast_mutex_unlock(&obj->lock);
    00559       ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
    00560       return ODBC_FAIL;
    00561    } else {
    00562
    00563       ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->name, obj->dsn);
    00564       obj->up = 1;
    00565    }
    00566
    00567    ast_mutex_unlock(&obj->lock);
    00568    return ODBC_SUCCESS;
    00569 }

odbc_status odbc_obj_disconnect | ( | odbc_obj * | obj | ) |

Definition at line 482 of file res_odbc.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), odbc_obj::con, odbc_obj::dsn, odbc_obj::lock, LOG_WARNING, odbc_obj::name, ODBC_SUCCESS, and odbc_obj::up.

Referenced by destroy_odbc_obj(), odbc_disconnect_command(), odbc_obj_connect(), odbc_prepare_and_execute(), odbc_sanity_check(), and odbc_smart_direct_execute().

    00483 {
    00484    int res;
    00485    ast_mutex_lock(&obj->lock);
    00486
    00487    res = SQLDisconnect(obj->con);
    00488
    00489
    00490    if (res == ODBC_SUCCESS) {
    00491       ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->name, obj->dsn);
    00492    } else {
    00493       ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",
    00494       obj->name, obj->dsn);
    00495    }
    00496    obj->up = 0;
    00497    ast_mutex_unlock(&obj->lock);
    00498    return ODBC_SUCCESS;
    00499 }

SQLHSTMT odbc_prepare_and_execute | ( | odbc_obj * | obj,
|  | SQLHSTMT(*)(odbc_obj *obj, void *data) | prepare_cb,
|  | void * | data |
| ) |  |  |

Definition at line 110 of file res_odbc.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), odbc_obj::lock, LOG_WARNING, odbc_obj_connect(), odbc_obj_disconnect(), and odbc_obj::up.

    00111 {
    00112    int res = 0, i, attempt;
    00113    SQLINTEGER nativeerror=0, numfields=0;
    00114    SQLSMALLINT diagbytes=0;
    00115    unsigned char state[10], diagnostic[256];
    00116    SQLHSTMT stmt;
    00117
    00118    for (attempt = 0; attempt < 2; attempt++) {
    00119       /* This prepare callback may do more than just prepare -- it may also
    00120        * bind parameters, bind results, etc.  The real key, here, is that
    00121        * when we disconnect, all handles become invalid for most databases.
    00122        * We must therefore redo everything when we establish a new
    00123        * connection. */
    00124       stmt = prepare_cb(obj, data);
    00125
    00126       if (stmt) {
    00127          res = SQLExecute(stmt);
    00128          if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
    00129             if (res == SQL_ERROR) {
    00130                SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
    00131                for (i=0; i< numfields + 1; i++) {
    00132                   SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
    00133                   ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
    00134                   if (i > 10) {
    00135                      ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
    00136                      break;
    00137                   }
    00138                }
    00139             }
    00140
    00141             ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
    00142             SQLFreeHandle(SQL_HANDLE_STMT, stmt);
    00143             stmt = NULL;
    00144
    00145             ast_mutex_lock(&obj->lock);
    00146             obj->up = 0;
    00147             ast_mutex_unlock(&obj->lock);
    00148             odbc_obj_disconnect(obj);
    00149             odbc_obj_connect(obj);
    00150             continue;
    00151          }
    00152          break;
    00153       }
    00154    }
    00155
    00156    return stmt;
    00157 }

int odbc_sanity_check | ( | odbc_obj * | obj | ) |

Definition at line 212 of file res_odbc.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), odbc_obj::con, odbc_obj::lock, LOG_WARNING, odbc_obj_connect(), odbc_obj_disconnect(), and odbc_obj::up.

Referenced by fetch_odbc_obj(), and odbc_dump_fd().

    00213 {
    00214    char *test_sql = "select 1";
    00215    SQLHSTMT stmt;
    00216    int res = 0;
    00217
    00218    ast_mutex_lock(&obj->lock);
    00219    if(obj->up) { /* so you say... let's make sure */
    00220       res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
    00221       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
    00222          obj->up = 0; /* Liar!*/
    00223       } else {
    00224          res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS);
    00225          if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
    00226             obj->up = 0; /* Liar!*/
    00227          } else {
    00228             res = SQLExecute(stmt);
    00229             if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
    00230                obj->up = 0; /* Liar!*/
    00231             }
    00232          }
    00233       }
    00234       SQLFreeHandle (SQL_HANDLE_STMT, stmt);
    00235    }
    00236    ast_mutex_unlock(&obj->lock);
    00237
    00238    if(!obj->up) { /* Try to reconnect! */
    00239       ast_log(LOG_WARNING, "Connection is down attempting to reconnect...\n");
    00240       odbc_obj_disconnect(obj);
    00241       odbc_obj_connect(obj);
    00242    }
    00243    return obj->up;
    00244 }

int odbc_smart_direct_execute | ( | odbc_obj * | obj,
|  | SQLHSTMT | stmt,
|  | char * | sql |
| ) |  |  |

Definition at line 194 of file res_odbc.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), odbc_obj::lock, LOG_WARNING, odbc_obj_connect(), odbc_obj_disconnect(), and odbc_obj::up.

Referenced by config_odbc().

    00195 {
    00196    int res = 0;
    00197
    00198    res = SQLExecDirect (stmt, (unsigned char *)sql, SQL_NTS);
    00199    if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
    00200       ast_log(LOG_WARNING, "SQL Execute error! Attempting a reconnect...\n");
    00201       ast_mutex_lock(&obj->lock);
    00202       obj->up = 0;
    00203       ast_mutex_unlock(&obj->lock);
    00204       odbc_obj_disconnect(obj);
    00205       odbc_obj_connect(obj);
    00206       res = SQLExecDirect (stmt, (unsigned char *)sql, SQL_NTS);
    00207    }
    00208
    00209    return res;
    00210 }

int odbc_smart_execute | ( | odbc_obj * | obj,
|  | SQLHSTMT | stmt |
| ) |  |  |

Definition at line 159 of file res_odbc.c.

References ast_log(), and LOG_WARNING.

Referenced by realtime_multi_odbc(), realtime_odbc(), and update_odbc().

    00160 {
    00161    int res = 0, i;
    00162    SQLINTEGER nativeerror=0, numfields=0;
    00163    SQLSMALLINT diagbytes=0;
    00164    unsigned char state[10], diagnostic[256];
    00165
    00166    res = SQLExecute(stmt);
    00167    if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
    00168       if (res == SQL_ERROR) {
    00169          SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
    00170          for (i=0; i< numfields + 1; i++) {
    00171             SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
    00172             ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
    00173             if (i > 10) {
    00174                ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
    00175                break;
    00176             }
    00177          }
    00178       }
    00179 /*
    00180       ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
    00181       ast_mutex_lock(&obj->lock);
    00182       obj->up = 0;
    00183       ast_mutex_unlock(&obj->lock);
    00184       odbc_obj_disconnect(obj);
    00185       odbc_obj_connect(obj);
    00186       res = SQLExecute(stmt);
    00187 */
    00188    }
    00189
    00190    return res;
    00191 }

int register_odbc_obj | ( | char * | name,
|  | odbc_obj * | obj |
| ) |  |  |

Definition at line 404 of file res_odbc.c.

References ODBC_REGISTRY, and odbc_write().

Referenced by load_odbc_config().

    00405 {
    00406    if (obj != NULL)
    00407       return odbc_write(ODBC_REGISTRY, name, obj);
    00408    return 0;
    00409 }