Thu Apr 28 2011 17:14:02

Asterisk developer's documentation


res_odbc.h File Reference

ODBC resource manager. More...

#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include "asterisk/linkedlists.h"
#include "asterisk/strings.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_cache_tables::_columns
struct  odbc_cache_columns
struct  odbc_cache_tables
struct  odbc_obj
 ODBC container. More...

Defines

#define ast_odbc_release_table(ptr)   if (ptr) { AST_RWLIST_UNLOCK(&(ptr)->columns); }
 Release a table returned from ast_odbc_find_table.

Enumerations

enum  { RES_ODBC_SANITY_CHECK = (1 << 0), RES_ODBC_INDEPENDENT_CONNECTION = (1 << 1) }
 

Flags for use with.

More...
enum  odbc_status { ODBC_SUCCESS = 0, ODBC_FAIL = -1 }

Functions

SQLRETURN ast_odbc_ast_str_SQLGetData (struct ast_str **buf, int pmaxlen, SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLLEN *StrLen_or_Ind)
 Wrapper for SQLGetData to use with dynamic strings.
int ast_odbc_backslash_is_escape (struct odbc_obj *obj)
 Checks if the database natively supports backslash as an escape character.
int ast_odbc_clear_cache (const char *database, const char *tablename)
 Remove a cache entry from memory.
SQLHSTMT ast_odbc_direct_execute (struct odbc_obj *obj, SQLHSTMT(*exec_cb)(struct odbc_obj *obj, void *data), void *data)
 Executes an non prepared statement and returns the resulting statement handle.
struct odbc_cache_columnsast_odbc_find_column (struct odbc_cache_tables *table, const char *colname)
 Find a column entry within a cached table structure.
struct odbc_cache_tablesast_odbc_find_table (const char *database, const char *tablename)
 Find or create an entry describing the table specified.
SQLHSTMT ast_odbc_prepare_and_execute (struct odbc_obj *obj, SQLHSTMT(*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
 Prepares, executes, and returns the resulting statement handle.
void ast_odbc_release_obj (struct odbc_obj *obj)
 Releases an ODBC object previously allocated by odbc_request_obj()
struct odbc_objast_odbc_request_obj (const char *name, int check)
struct odbc_objast_odbc_request_obj2 (const char *name, struct ast_flags flags)
 Retrieves a connected ODBC object.
struct odbc_objast_odbc_retrieve_transaction_obj (struct ast_channel *chan, const char *objname)
 Retrieve a stored ODBC object, if a transaction has been started.
int ast_odbc_sanity_check (struct odbc_obj *obj)
 Checks an ODBC object to ensure it is still connected.
int ast_odbc_smart_execute (struct odbc_obj *obj, SQLHSTMT stmt)
 Executes a prepared statement handle.

Detailed Description

ODBC resource manager.

Definition in file res_odbc.h.


Define Documentation

#define ast_odbc_release_table (   ptr)    if (ptr) { AST_RWLIST_UNLOCK(&(ptr)->columns); }

Release a table returned from ast_odbc_find_table.

Definition at line 212 of file res_odbc.h.

Referenced by update2_prepare(), and update_odbc().


Enumeration Type Documentation

anonymous enum

Flags for use with.

See also:
ast_odbc_request_obj2
Enumerator:
RES_ODBC_SANITY_CHECK 
RES_ODBC_INDEPENDENT_CONNECTION 

Definition at line 39 of file res_odbc.h.

Enumerator:
ODBC_SUCCESS 
ODBC_FAIL 

Definition at line 36 of file res_odbc.h.


Function Documentation

SQLRETURN ast_odbc_ast_str_SQLGetData ( struct ast_str **  buf,
int  pmaxlen,
SQLHSTMT  StatementHandle,
SQLUSMALLINT  ColumnNumber,
SQLSMALLINT  TargetType,
SQLLEN *  StrLen_or_Ind 
)

Wrapper for SQLGetData to use with dynamic strings.

Parameters:
bufAddress of the pointer to the ast_str structure.
pmaxlenThe maximum size of the resulting string, or 0 for no limit.
StatementHandleThe statement handle from which to retrieve data.
ColumnNumberColumn number (1-based offset) for which to retrieve data.
TargetTypeThe SQL constant indicating what kind of data is to be retrieved (usually SQL_CHAR)
StrLen_or_IndA pointer to a length indicator, specifying the total length of data.

Definition at line 679 of file res_odbc.c.

References ast_str_buffer(), ast_str_make_space(), ast_str_size(), and ast_str_update().

Referenced by acf_odbc_read(), and cli_odbc_read().

{
   SQLRETURN res;

   if (pmaxlen == 0) {
      if (SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), 0, StrLen_or_Ind) == SQL_SUCCESS_WITH_INFO) {
         ast_str_make_space(buf, *StrLen_or_Ind + 1);
      }
   } else if (pmaxlen > 0) {
      ast_str_make_space(buf, pmaxlen);
   }
   res = SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), ast_str_size(*buf), StrLen_or_Ind);
   ast_str_update(*buf);

   return res;
}
int ast_odbc_backslash_is_escape ( struct odbc_obj obj)

Checks if the database natively supports backslash as an escape character.

Parameters:
objThe ODBC object
Returns:
Returns 1 if backslash is a native escape character, 0 if an ESCAPE clause is needed to support '\'

Definition at line 1048 of file res_odbc.c.

References odbc_class::backslash_is_escape, and odbc_obj::parent.

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

{
   return obj->parent->backslash_is_escape;
}
int ast_odbc_clear_cache ( const char *  database,
const char *  tablename 
)

Remove a cache entry from memory.

Parameters:
databaseName of an ODBC class (used to ensure like-named tables in different databases are not confused)
tablenameTablename for which a cached record should be removed
Return values:
0if the cache entry was removed, or -1 if no matching entry was found.
Since:
1.6.1

Definition at line 551 of file res_odbc.c.

References AST_LIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, odbc_cache_tables::connection, destroy_table_cache(), odbc_class::list, and odbc_cache_tables::table.

{
   struct odbc_cache_tables *tableptr;

   AST_RWLIST_WRLOCK(&odbc_tables);
   AST_RWLIST_TRAVERSE_SAFE_BEGIN(&odbc_tables, tableptr, list) {
      if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) {
         AST_LIST_REMOVE_CURRENT(list);
         destroy_table_cache(tableptr);
         break;
      }
   }
   AST_RWLIST_TRAVERSE_SAFE_END
   AST_RWLIST_UNLOCK(&odbc_tables);
   return tableptr ? 0 : -1;
}
SQLHSTMT ast_odbc_direct_execute ( struct odbc_obj obj,
SQLHSTMT(*)(struct odbc_obj *obj, void *data)  exec_cb,
void *  data 
)

Executes an non prepared statement and returns the resulting statement handle.

Parameters:
objThe ODBC object
exec_cbA function callback, which, when called, should return a statement handle with result columns bound.
dataA parameter to be passed to the exec_cb parameter function, indicating which statement handle is to be prepared.
Return values:
astatement handle
NULLon error

Definition at line 568 of file res_odbc.c.

References ast_log(), ast_odbc_sanity_check(), odbc_class::dsn, LOG_WARNING, odbc_class::name, odbc_obj::parent, and odbc_obj::tx.

Referenced by acf_odbc_read(), acf_odbc_write(), cli_odbc_read(), cli_odbc_write(), and odbc_log().

{
   int attempt;
   SQLHSTMT stmt;

   for (attempt = 0; attempt < 2; attempt++) {
      stmt = exec_cb(obj, data);

      if (stmt) {
         break;
      } else if (obj->tx) {
         ast_log(LOG_WARNING, "Failed to execute, but unable to reconnect, as we're transactional.\n");
         break;
      } else if (attempt == 0) {
         ast_log(LOG_WARNING, "SQL Execute error! Verifying connection to %s [%s]...\n", obj->parent->name, obj->parent->dsn);
      }
      if (!ast_odbc_sanity_check(obj)) {
         break;
      }
   }

   return stmt;
}
struct odbc_cache_columns* ast_odbc_find_column ( struct odbc_cache_tables table,
const char *  colname 
) [read]

Find a column entry within a cached table structure.

Parameters:
tableCached table structure, as returned from ast_odbc_find_table()
colnameThe column name requested
Return values:
Astructure describing the column type, or NULL, if the column is not found.
Since:
1.6.1

Definition at line 540 of file res_odbc.c.

References AST_RWLIST_TRAVERSE, odbc_cache_tables::columns, odbc_class::list, and odbc_cache_columns::name.

Referenced by update2_prepare(), and update_odbc().

{
   struct odbc_cache_columns *col;
   AST_RWLIST_TRAVERSE(&table->columns, col, list) {
      if (strcasecmp(col->name, colname) == 0) {
         return col;
      }
   }
   return NULL;
}
struct odbc_cache_tables* ast_odbc_find_table ( const char *  database,
const char *  tablename 
) [read]

Find or create an entry describing the table specified.

Parameters:
databaseName of an ODBC class on which to query the table
tablenameTablename to describe
Return values:
Astructure describing the table layout, or NULL, if the table is not found or another error occurs. When a structure is returned, the contained columns list will be rdlock'ed, to ensure that it will be retained in memory.
Since:
1.6.1

Definition at line 425 of file res_odbc.c.

References ast_calloc, AST_LIST_INSERT_TAIL, ast_log(), ast_odbc_release_obj(), ast_odbc_request_obj(), ast_odbc_sanity_check(), AST_RWLIST_HEAD_INIT, AST_RWLIST_INSERT_TAIL, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_verb, odbc_cache_tables::columns, odbc_obj::con, odbc_cache_tables::connection, odbc_cache_columns::decimals, destroy_table_cache(), odbc_class::list, LOG_ERROR, LOG_WARNING, odbc_cache_columns::name, odbc_cache_columns::nullable, odbc_cache_columns::octetlen, odbc_cache_columns::radix, odbc_cache_columns::size, odbc_cache_tables::table, and odbc_cache_columns::type.

Referenced by require_odbc(), update2_prepare(), and update_odbc().

{
   struct odbc_cache_tables *tableptr;
   struct odbc_cache_columns *entry;
   char columnname[80];
   SQLLEN sqlptr;
   SQLHSTMT stmt = NULL;
   int res = 0, error = 0, try = 0;
   struct odbc_obj *obj = ast_odbc_request_obj(database, 0);

   AST_RWLIST_RDLOCK(&odbc_tables);
   AST_RWLIST_TRAVERSE(&odbc_tables, tableptr, list) {
      if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) {
         break;
      }
   }
   if (tableptr) {
      AST_RWLIST_RDLOCK(&tableptr->columns);
      AST_RWLIST_UNLOCK(&odbc_tables);
      if (obj) {
         ast_odbc_release_obj(obj);
      }
      return tableptr;
   }

   if (!obj) {
      ast_log(LOG_WARNING, "Unable to retrieve database handle for table description '%s@%s'\n", tablename, database);
      AST_RWLIST_UNLOCK(&odbc_tables);
      return NULL;
   }

   /* Table structure not already cached; build it now. */
   do {
      res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
      if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
         if (try == 0) {
            try = 1;
            ast_odbc_sanity_check(obj);
            continue;
         }
         ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", database);
         break;
      }

      res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)tablename, SQL_NTS, (unsigned char *)"%", SQL_NTS);
      if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
         if (try == 0) {
            try = 1;
            SQLFreeHandle(SQL_HANDLE_STMT, stmt);
            ast_odbc_sanity_check(obj);
            continue;
         }
         ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'.\n", database);
         break;
      }

      if (!(tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + strlen(database) + 1 + strlen(tablename) + 1))) {
         ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", tablename, database);
         break;
      }

      tableptr->connection = (char *)tableptr + sizeof(*tableptr);
      tableptr->table = (char *)tableptr + sizeof(*tableptr) + strlen(database) + 1;
      strcpy(tableptr->connection, database); /* SAFE */
      strcpy(tableptr->table, tablename); /* SAFE */
      AST_RWLIST_HEAD_INIT(&(tableptr->columns));

      while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
         SQLGetData(stmt,  4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);

         if (!(entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1))) {
            ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, tablename, database);
            error = 1;
            break;
         }
         entry->name = (char *)entry + sizeof(*entry);
         strcpy(entry->name, columnname);

         SQLGetData(stmt,  5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
         SQLGetData(stmt,  7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
         SQLGetData(stmt,  9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
         SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
         SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
         SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);

         /* Specification states that the octenlen should be the maximum number of bytes
          * returned in a char or binary column, but it seems that some drivers just set
          * it to NULL. (Bad Postgres! No biscuit!) */
         if (entry->octetlen == 0) {
            entry->octetlen = entry->size;
         }

         ast_verb(10, "Found %s column with type %hd with len %ld, octetlen %ld, and numlen (%hd,%hd)\n", entry->name, entry->type, (long) entry->size, (long) entry->octetlen, entry->decimals, entry->radix);
         /* Insert column info into column list */
         AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
      }
      SQLFreeHandle(SQL_HANDLE_STMT, stmt);

      AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
      AST_RWLIST_RDLOCK(&(tableptr->columns));
      break;
   } while (1);

   AST_RWLIST_UNLOCK(&odbc_tables);

   if (error) {
      destroy_table_cache(tableptr);
      tableptr = NULL;
   }
   if (obj) {
      ast_odbc_release_obj(obj);
   }
   return tableptr;
}
SQLHSTMT ast_odbc_prepare_and_execute ( struct odbc_obj obj,
SQLHSTMT(*)(struct odbc_obj *obj, void *data)  prepare_cb,
void *  data 
)

Prepares, executes, and returns the resulting statement handle.

Parameters:
objThe ODBC object
prepare_cbA function callback, which, when called, should return a statement handle prepared, with any necessary parameters or result columns bound.
dataA parameter to be passed to the prepare_cb parameter function, indicating which statement handle is to be prepared.
Return values:
astatement handle
NULLon error

Definition at line 592 of file res_odbc.c.

References ast_log(), ast_odbc_sanity_check(), ast_tvnow(), odbc_class::dsn, odbc_obj::last_used, LOG_WARNING, odbc_class::name, odbc_obj::parent, odbc_obj::tx, and odbc_obj::up.

Referenced by config_odbc(), destroy_odbc(), odbc_log(), realtime_multi_odbc(), realtime_odbc(), store_odbc(), update2_odbc(), and update_odbc().

{
   int res = 0, i, attempt;
   SQLINTEGER nativeerror=0, numfields=0;
   SQLSMALLINT diagbytes=0;
   unsigned char state[10], diagnostic[256];
   SQLHSTMT stmt;

   for (attempt = 0; attempt < 2; attempt++) {
      /* This prepare callback may do more than just prepare -- it may also
       * bind parameters, bind results, etc.  The real key, here, is that
       * when we disconnect, all handles become invalid for most databases.
       * We must therefore redo everything when we establish a new
       * connection. */
      stmt = prepare_cb(obj, data);

      if (stmt) {
         res = SQLExecute(stmt);
         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
            if (res == SQL_ERROR) {
               SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
               for (i = 0; i < numfields; i++) {
                  SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
                  ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
                  if (i > 10) {
                     ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
                     break;
                  }
               }
            }

            if (obj->tx) {
               ast_log(LOG_WARNING, "SQL Execute error, but unable to reconnect, as we're transactional.\n");
               break;
            } else {
               ast_log(LOG_WARNING, "SQL Execute error %d! Verifying connection to %s [%s]...\n", res, obj->parent->name, obj->parent->dsn);
               SQLFreeHandle(SQL_HANDLE_STMT, stmt);
               stmt = NULL;

               obj->up = 0;
               /*
                * While this isn't the best way to try to correct an error, this won't automatically
                * fail when the statement handle invalidates.
                */
               if (!ast_odbc_sanity_check(obj)) {
                  break;
               }
               continue;
            }
         } else {
            obj->last_used = ast_tvnow();
         }
         break;
      } else if (attempt == 0) {
         ast_odbc_sanity_check(obj);
      }
   }

   return stmt;
}
void ast_odbc_release_obj ( struct odbc_obj obj)

Releases an ODBC object previously allocated by odbc_request_obj()

Parameters:
objThe ODBC object

Definition at line 1042 of file res_odbc.c.

References find_transaction(), and odbc_release_obj2().

Referenced by acf_odbc_read(), acf_odbc_write(), ast_odbc_find_table(), cli_odbc_read(), cli_odbc_write(), config_odbc(), destroy_odbc(), load_config(), odbc_log(), odbc_register_class(), realtime_multi_odbc(), realtime_odbc(), store_odbc(), update2_odbc(), and update_odbc().

{
   struct odbc_txn_frame *tx = find_transaction(NULL, obj, NULL, 0);
   odbc_release_obj2(obj, tx);
}
struct odbc_obj* ast_odbc_request_obj ( const char *  name,
int  check 
) [read]

Definition at line 1337 of file res_odbc.c.

References ast_odbc_request_obj2(), and RES_ODBC_SANITY_CHECK.

Referenced by acf_odbc_read(), acf_odbc_write(), ast_odbc_find_table(), cli_odbc_read(), cli_odbc_write(), config_odbc(), destroy_odbc(), load_config(), odbc_log(), odbc_register_class(), realtime_multi_odbc(), realtime_odbc(), store_odbc(), update2_odbc(), and update_odbc().

{
   struct ast_flags flags = { check ? RES_ODBC_SANITY_CHECK : 0 };
#ifdef DEBUG_THREADS
   return _ast_odbc_request_obj2(name, flags, file, function, lineno);
#else
   return ast_odbc_request_obj2(name, flags);
#endif
}
struct odbc_obj* ast_odbc_request_obj2 ( const char *  name,
struct ast_flags  flags 
) [read]

Retrieves a connected ODBC object.

Parameters:
nameThe name of the ODBC class for which a connection is needed.
flagsSet of flags used to control which connection is returned.
Return values:
ODBCobject
NULLif there is no connection available with the requested name.

Connection classes may, in fact, contain multiple connection handles. If the connection is pooled, then each connection will be dedicated to the thread which requests it. Note that all connections should be released when the thread is done by calling odbc_release_obj(), below.

Definition at line 1157 of file res_odbc.c.

References ao2_alloc, ao2_callback, ao2_link, ao2_ref, aoro2_class_cb(), aoro2_obj_cb(), ast_assert, ast_atomic_fetchadd_int(), ast_copy_string(), ast_log(), ast_mutex_init(), ast_odbc_sanity_check(), ast_test_flag, ast_tvdiff_sec(), ast_tvnow(), class_container, odbc_obj::con, odbc_class::count, EOR_TX, odbc_class::idlecheck, odbc_class::isolation, odbc_obj::last_used, odbc_obj::lock, LOG_WARNING, NO_TX, odbc_class::obj_container, ODBC_FAIL, odbc_obj_connect(), odbc_obj_destructor(), odbc_obj::parent, RES_ODBC_INDEPENDENT_CONNECTION, RES_ODBC_SANITY_CHECK, USE_TX, and odbc_obj::used.

Referenced by acf_transaction_write(), and ast_odbc_request_obj().

{
   struct odbc_obj *obj = NULL;
   struct odbc_class *class;
   SQLINTEGER nativeerror=0, numfields=0;
   SQLSMALLINT diagbytes=0, i;
   unsigned char state[10], diagnostic[256];

   if (!(class = ao2_callback(class_container, 0, aoro2_class_cb, (char *) name))) {
      return NULL;
   }

   ast_assert(ao2_ref(class, 0) > 1);

   if (class->haspool) {
      /* Recycle connections before building another */
      obj = ao2_callback(class->obj_container, 0, aoro2_obj_cb, EOR_TX);

      if (obj) {
         ast_assert(ao2_ref(obj, 0) > 1);
      }

      if (!obj && (ast_atomic_fetchadd_int(&class->count, +1) < class->limit)) {
         obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
         if (!obj) {
            ao2_ref(class, -1);
            ast_atomic_fetchadd_int(&class->count, -1);
            return NULL;
         }
         ast_assert(ao2_ref(obj, 0) == 1);
         ast_mutex_init(&obj->lock);
         /* obj inherits the outstanding reference to class */
         obj->parent = class;
         class = NULL;
         if (odbc_obj_connect(obj) == ODBC_FAIL) {
            ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
            ao2_ref(obj, -1);
            obj = NULL;
            ast_assert(ao2_ref(class, 0) > 0);
            ast_atomic_fetchadd_int(&class->count, -1);
         } else {
            obj->used = 1;
            ao2_link(obj->parent->obj_container, obj);
         }
      } else {
         /* If construction fails due to the limit, remove our increment. */
         if (!obj) {
            ast_atomic_fetchadd_int(&class->count, -1);
         }
         /* Object is not constructed, so delete outstanding reference to class. */
         ao2_ref(class, -1);
         class = NULL;
      }

      if (obj && ast_test_flag(&flags, RES_ODBC_INDEPENDENT_CONNECTION)) {
         /* Ensure this connection has autocommit turned off. */
         if (SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_OFF, 0) == SQL_ERROR) {
            SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
            for (i = 0; i < numfields; i++) {
               SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
               ast_log(LOG_WARNING, "SQLSetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
               if (i > 10) {
                  ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
                  break;
               }
            }
         }
      }
   } else if (ast_test_flag(&flags, RES_ODBC_INDEPENDENT_CONNECTION)) {
      /* Non-pooled connections -- but must use a separate connection handle */
      if (!(obj = ao2_callback(class->obj_container, 0, aoro2_obj_cb, USE_TX))) {
         obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
         if (!obj) {
            ao2_ref(class, -1);
            return NULL;
         }
         ast_mutex_init(&obj->lock);
         /* obj inherits the outstanding reference to class */
         obj->parent = class;
         class = NULL;
         if (odbc_obj_connect(obj) == ODBC_FAIL) {
            ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
            ao2_ref(obj, -1);
            obj = NULL;
         } else {
            obj->used = 1;
            ao2_link(obj->parent->obj_container, obj);
            ast_atomic_fetchadd_int(&obj->parent->count, +1);
         }
      }

      if (obj && SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_OFF, 0) == SQL_ERROR) {
         SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
         for (i = 0; i < numfields; i++) {
            SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
            ast_log(LOG_WARNING, "SetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
            if (i > 10) {
               ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
               break;
            }
         }
      }
   } else {
      /* Non-pooled connection: multiple modules can use the same connection. */
      if ((obj = ao2_callback(class->obj_container, 0, aoro2_obj_cb, NO_TX))) {
         /* Object is not constructed, so delete outstanding reference to class. */
         ast_assert(ao2_ref(class, 0) > 1);
         ao2_ref(class, -1);
         class = NULL;
      } else {
         /* No entry: build one */
         if (!(obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor))) {
            ast_assert(ao2_ref(class, 0) > 1);
            ao2_ref(class, -1);
            return NULL;
         }
         ast_mutex_init(&obj->lock);
         /* obj inherits the outstanding reference to class */
         obj->parent = class;
         class = NULL;
         if (odbc_obj_connect(obj) == ODBC_FAIL) {
            ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
            ao2_ref(obj, -1);
            obj = NULL;
         } else {
            ao2_link(obj->parent->obj_container, obj);
            ast_assert(ao2_ref(obj, 0) > 1);
         }
      }

      if (obj && SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_ON, 0) == SQL_ERROR) {
         SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
         for (i = 0; i < numfields; i++) {
            SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
            ast_log(LOG_WARNING, "SetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
            if (i > 10) {
               ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
               break;
            }
         }
      }
   }

   /* Set the isolation property */
   if (obj && SQLSetConnectAttr(obj->con, SQL_ATTR_TXN_ISOLATION, (void *)(long)obj->parent->isolation, 0) == SQL_ERROR) {
      SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
      for (i = 0; i < numfields; i++) {
         SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
         ast_log(LOG_WARNING, "SetConnectAttr (Txn isolation) returned an error: %s: %s\n", state, diagnostic);
         if (i > 10) {
            ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
            break;
         }
      }
   }

   if (obj && ast_test_flag(&flags, RES_ODBC_SANITY_CHECK)) {
      ast_odbc_sanity_check(obj);
   } else if (obj && obj->parent->idlecheck > 0 && ast_tvdiff_sec(ast_tvnow(), obj->last_used) > obj->parent->idlecheck)
      odbc_obj_connect(obj);

#ifdef DEBUG_THREADS
   if (obj) {
      ast_copy_string(obj->file, file, sizeof(obj->file));
      ast_copy_string(obj->function, function, sizeof(obj->function));
      obj->lineno = lineno;
   }
#endif
   ast_assert(class == NULL);

   if (obj) {
      ast_assert(ao2_ref(obj, 0) > 1);
   }
   return obj;
}
struct odbc_obj* ast_odbc_retrieve_transaction_obj ( struct ast_channel chan,
const char *  objname 
) [read]

Retrieve a stored ODBC object, if a transaction has been started.

Parameters:
chanChannel associated with the transaction.
objnameName of the database handle. This name corresponds to the name passed to
See also:
ast_odbc_request_obj2 (or formerly, to ast_odbc_request_obj). Note that the existence of this parameter name explicitly allows for multiple transactions to be open at once, albeit to different databases.
Return values:
Astored ODBC object, if a transaction was already started.
NULL,ifno transaction yet exists.

Definition at line 1348 of file res_odbc.c.

References ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, AST_LIST_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_datastore::data, odbc_class::list, odbc_class::name, odbc_txn_frame::obj, odbc_obj::parent, and txn_info.

Referenced by acf_odbc_write().

{
   struct ast_datastore *txn_store;
   AST_LIST_HEAD(, odbc_txn_frame) *oldlist;
   struct odbc_txn_frame *txn = NULL;

   if (!chan) {
      /* No channel == no transaction */
      return NULL;
   }

   ast_channel_lock(chan);
   if ((txn_store = ast_channel_datastore_find(chan, &txn_info, NULL))) {
      oldlist = txn_store->data;
   } else {
      ast_channel_unlock(chan);
      return NULL;
   }

   AST_LIST_LOCK(oldlist);
   ast_channel_unlock(chan);

   AST_LIST_TRAVERSE(oldlist, txn, list) {
      if (txn->obj && txn->obj->parent && !strcmp(txn->obj->parent->name, objname)) {
         AST_LIST_UNLOCK(oldlist);
         return txn->obj;
      }
   }
   AST_LIST_UNLOCK(oldlist);
   return NULL;
}
int ast_odbc_sanity_check ( struct odbc_obj obj)

Checks an ODBC object to ensure it is still connected.

Parameters:
objThe ODBC object
Return values:
0if connected
-1otherwise.

Definition at line 696 of file res_odbc.c.

References ast_log(), ast_strlen_zero(), odbc_obj::con, LOG_WARNING, odbc_obj_connect(), odbc_obj_disconnect(), odbc_obj::parent, odbc_class::sanitysql, odbc_obj::tx, and odbc_obj::up.

Referenced by ast_odbc_direct_execute(), ast_odbc_find_table(), ast_odbc_prepare_and_execute(), ast_odbc_request_obj2(), and handle_cli_odbc_show().

{
   char *test_sql = "select 1";
   SQLHSTMT stmt;
   int res = 0;

   if (!ast_strlen_zero(obj->parent->sanitysql))
      test_sql = obj->parent->sanitysql;

   if (obj->up) {
      res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
      if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
         obj->up = 0;
      } else {
         res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS);
         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
            obj->up = 0;
         } else {
            res = SQLExecute(stmt);
            if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
               obj->up = 0;
            }
         }
      }
      SQLFreeHandle (SQL_HANDLE_STMT, stmt);
   }

   if (!obj->up && !obj->tx) { /* Try to reconnect! */
      ast_log(LOG_WARNING, "Connection is down attempting to reconnect...\n");
      odbc_obj_disconnect(obj);
      odbc_obj_connect(obj);
   }
   return obj->up;
}
int ast_odbc_smart_execute ( struct odbc_obj obj,
SQLHSTMT  stmt 
)

Executes a prepared statement handle.

Parameters:
objThe non-NULL result of odbc_request_obj()
stmtThe prepared statement handle
Return values:
0on success
-1on failure

This function was originally designed simply to execute a prepared statement handle and to retry if the initial execution failed. Unfortunately, it did this by disconnecting and reconnecting the database handle which on most databases causes the statement handle to become invalid. Therefore, this method has been deprecated in favor of odbc_prepare_and_execute() which allows the statement to be prepared multiple times, if necessary, in case of a loss of connection.

This function really only ever worked with MySQL, where the statement handle is not prepared on the server. If you are not using MySQL, you should avoid it.

Definition at line 653 of file res_odbc.c.

References ast_log(), ast_tvnow(), odbc_obj::last_used, and LOG_WARNING.

{
   int res = 0, i;
   SQLINTEGER nativeerror=0, numfields=0;
   SQLSMALLINT diagbytes=0;
   unsigned char state[10], diagnostic[256];

   res = SQLExecute(stmt);
   if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
      if (res == SQL_ERROR) {
         SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
         for (i = 0; i < numfields; i++) {
            SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
            ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
            if (i > 10) {
               ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
               break;
            }
         }
      }
   } else
      obj->last_used = ast_tvnow();
   
   return res;
}