Thu Apr 28 2011 16:57:17

Asterisk developer's documentation


res_odbc.c File Reference

ODBC resource manager. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/cli.h"
#include "asterisk/lock.h"
#include "asterisk/res_odbc.h"
#include "asterisk/time.h"
#include "asterisk/astobj2.h"
#include "asterisk/app.h"
#include "asterisk/strings.h"
#include "asterisk/threadstorage.h"
Include dependency graph for res_odbc.c:

Go to the source code of this file.

Data Structures

struct  odbc_class
struct  odbc_tables
struct  odbc_txn_frame

Defines

#define EOR_TX   (void *)(long)3
#define NO_TX   (void *)(long)2
#define USE_TX   (void *)(long)1

Functions

static void __fini_odbc_tables (void)
static void __init_odbc_tables (void)
static void __reg_module (void)
static void __unreg_module (void)
static int acf_transaction_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int acf_transaction_write (struct ast_channel *chan, const char *cmd, char *s, const char *value)
static int aoro2_class_cb (void *obj, void *arg, int flags)
static int aoro2_obj_cb (void *vobj, void *arg, int flags)
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.
 AST_THREADSTORAGE_CUSTOM_SCOPE (errors_buf, NULL, ast_free_ptr, static)
static int commit_exec (struct ast_channel *chan, void *data)
static void destroy_table_cache (struct odbc_cache_tables *table)
static struct odbc_txn_framefind_transaction (struct ast_channel *chan, struct odbc_obj *obj, const char *name, int active)
static char * handle_cli_odbc_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static const char * isolation2text (int iso)
static int load_module (void)
static int load_odbc_config (void)
static int mark_transaction_active (struct ast_channel *chan, struct odbc_txn_frame *tx)
static int null_hash_fn (const void *obj, const int flags)
static void odbc_class_destructor (void *data)
static odbc_status odbc_obj_connect (struct odbc_obj *obj)
static void odbc_obj_destructor (void *data)
static odbc_status odbc_obj_disconnect (struct odbc_obj *obj)
static int odbc_register_class (struct odbc_class *class, int connect)
static void odbc_release_obj2 (struct odbc_obj *obj, struct odbc_txn_frame *tx)
static void odbc_txn_free (void *data)
static struct odbc_txn_framerelease_transaction (struct odbc_txn_frame *tx)
static int reload (void)
static int rollback_exec (struct ast_channel *chan, void *data)
static int text2isolation (const char *txt)
static int unload_module (void)

Variables

static struct ast_module_info
__MODULE_INFO_SECTION 
__mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS , .description = "ODBC resource" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, }
static const char * app_commit = "ODBC_Commit"
static const char * app_rollback = "ODBC_Rollback"
static struct ast_module_infoast_module_info = &__mod_info
struct ao2_containerclass_container
static struct ast_cli_entry cli_odbc []
static struct ast_custom_function odbc_function
static struct odbc_tables odbc_tables
static struct ast_datastore_info txn_info

Detailed Description

ODBC resource manager.

Author:
Mark Spencer <markster@digium.com>
Anthony Minessale II <anthmct@yahoo.com>
Tilghman Lesher <tilghman@digium.com>

Definition in file res_odbc.c.


Define Documentation

#define EOR_TX   (void *)(long)3

Definition at line 1139 of file res_odbc.c.

Referenced by aoro2_obj_cb(), and ast_odbc_request_obj2().

#define NO_TX   (void *)(long)2

Definition at line 1138 of file res_odbc.c.

Referenced by aoro2_obj_cb(), and ast_odbc_request_obj2().

#define USE_TX   (void *)(long)1

Definition at line 1137 of file res_odbc.c.

Referenced by aoro2_obj_cb(), and ast_odbc_request_obj2().


Function Documentation

static void __fini_odbc_tables ( void  ) [static]

Definition at line 138 of file res_odbc.c.

{
static void __init_odbc_tables ( void  ) [static]

Definition at line 138 of file res_odbc.c.

{
static void __reg_module ( void  ) [static]

Definition at line 1708 of file res_odbc.c.

static void __unreg_module ( void  ) [static]

Definition at line 1708 of file res_odbc.c.

static int acf_transaction_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 1470 of file res_odbc.c.

References AST_APP_ARG, ast_copy_string(), AST_DECLARE_APP_ARGS, AST_STANDARD_APP_ARGS, ast_strlen_zero(), find_transaction(), odbc_txn_frame::forcecommit, odbc_txn_frame::isolation, isolation2text(), and odbc_txn_frame::name.

{
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(property);
      AST_APP_ARG(opt);
   );
   struct odbc_txn_frame *tx;

   AST_STANDARD_APP_ARGS(args, data);
   if (strcasecmp(args.property, "transaction") == 0) {
      if ((tx = find_transaction(chan, NULL, NULL, 1))) {
         ast_copy_string(buf, tx->name, len);
         return 0;
      }
   } else if (strcasecmp(args.property, "isolation") == 0) {
      if (!ast_strlen_zero(args.opt)) {
         tx = find_transaction(chan, NULL, args.opt, 0);
      } else {
         tx = find_transaction(chan, NULL, NULL, 1);
      }
      if (tx) {
         ast_copy_string(buf, isolation2text(tx->isolation), len);
         return 0;
      }
   } else if (strcasecmp(args.property, "forcecommit") == 0) {
      if (!ast_strlen_zero(args.opt)) {
         tx = find_transaction(chan, NULL, args.opt, 0);
      } else {
         tx = find_transaction(chan, NULL, NULL, 1);
      }
      if (tx) {
         ast_copy_string(buf, tx->forcecommit ? "1" : "0", len);
         return 0;
      }
   }
   return -1;
}
static int acf_transaction_write ( struct ast_channel chan,
const char *  cmd,
char *  s,
const char *  value 
) [static]

Definition at line 1508 of file res_odbc.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_false(), ast_log(), ast_odbc_request_obj2(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), ast_true(), odbc_obj::con, find_transaction(), odbc_txn_frame::forcecommit, odbc_txn_frame::isolation, odbc_class::isolation, LOG_ERROR, LOG_WARNING, mark_transaction_active(), odbc_txn_frame::obj, pbx_builtin_setvar_helper(), RES_ODBC_INDEPENDENT_CONNECTION, S_OR, text2isolation(), and odbc_obj::tx.

{
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(property);
      AST_APP_ARG(opt);
   );
   struct odbc_txn_frame *tx;
   SQLINTEGER nativeerror=0, numfields=0;
   SQLSMALLINT diagbytes=0, i;
   unsigned char state[10], diagnostic[256];

   AST_STANDARD_APP_ARGS(args, s);
   if (strcasecmp(args.property, "transaction") == 0) {
      /* Set active transaction */
      struct odbc_obj *obj;
      if ((tx = find_transaction(chan, NULL, value, 0))) {
         mark_transaction_active(chan, tx);
      } else {
         /* No such transaction, create one */
         struct ast_flags flags = { RES_ODBC_INDEPENDENT_CONNECTION };
         if (ast_strlen_zero(args.opt) || !(obj = ast_odbc_request_obj2(args.opt, flags))) {
            ast_log(LOG_ERROR, "Could not create transaction: invalid database specification '%s'\n", S_OR(args.opt, ""));
            pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "INVALID_DB");
            return -1;
         }
         if (!(tx = find_transaction(chan, obj, value, 0))) {
            pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE");
            return -1;
         }
         obj->tx = 1;
      }
      pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK");
      return 0;
   } else if (strcasecmp(args.property, "forcecommit") == 0) {
      /* Set what happens when an uncommitted transaction ends without explicit Commit or Rollback */
      if (ast_strlen_zero(args.opt)) {
         tx = find_transaction(chan, NULL, NULL, 1);
      } else {
         tx = find_transaction(chan, NULL, args.opt, 0);
      }
      if (!tx) {
         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE");
         return -1;
      }
      if (ast_true(value)) {
         tx->forcecommit = 1;
      } else if (ast_false(value)) {
         tx->forcecommit = 0;
      } else {
         ast_log(LOG_ERROR, "Invalid value for forcecommit: '%s'\n", S_OR(value, ""));
         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "INVALID_VALUE");
         return -1;
      }

      pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK");
      return 0;
   } else if (strcasecmp(args.property, "isolation") == 0) {
      /* How do uncommitted transactions affect reads? */
      int isolation = text2isolation(value);
      if (ast_strlen_zero(args.opt)) {
         tx = find_transaction(chan, NULL, NULL, 1);
      } else {
         tx = find_transaction(chan, NULL, args.opt, 0);
      }
      if (!tx) {
         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE");
         return -1;
      }
      if (isolation == 0) {
         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "INVALID_VALUE");
         ast_log(LOG_ERROR, "Invalid isolation specification: '%s'\n", S_OR(value, ""));
      } else if (SQLSetConnectAttr(tx->obj->con, SQL_ATTR_TXN_ISOLATION, (void *)(long)isolation, 0) == SQL_ERROR) {
         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "SQL_ERROR");
         SQLGetDiagField(SQL_HANDLE_DBC, tx->obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
         for (i = 0; i < numfields; i++) {
            SQLGetDiagRec(SQL_HANDLE_DBC, tx->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;
            }
         }
      } else {
         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK");
         tx->isolation = isolation;
      }
      return 0;
   } else {
      ast_log(LOG_ERROR, "Unknown property: '%s'\n", args.property);
      return -1;
   }
}
static int aoro2_class_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 1127 of file res_odbc.c.

References CMP_MATCH, CMP_STOP, and odbc_class::name.

Referenced by ast_odbc_request_obj2().

{
   struct odbc_class *class = obj;
   char *name = arg;
   if (!strcmp(class->name, name) && !class->delme) {
      return CMP_MATCH | CMP_STOP;
   }
   return 0;
}
static int aoro2_obj_cb ( void *  vobj,
void *  arg,
int  flags 
) [static]

Definition at line 1141 of file res_odbc.c.

References ast_mutex_lock(), ast_mutex_unlock(), CMP_MATCH, CMP_STOP, EOR_TX, odbc_obj::lock, NO_TX, odbc_obj::tx, USE_TX, and odbc_obj::used.

Referenced by ast_odbc_request_obj2().

{
   struct odbc_obj *obj = vobj;
   ast_mutex_lock(&obj->lock);
   if ((arg == NO_TX && !obj->tx) || (arg == EOR_TX && !obj->used) || (arg == USE_TX && obj->tx && !obj->used)) {
      obj->used = 1;
      ast_mutex_unlock(&obj->lock);
      return CMP_MATCH | CMP_STOP;
   }
   ast_mutex_unlock(&obj->lock);
   return 0;
}
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;
}
AST_THREADSTORAGE_CUSTOM_SCOPE ( errors_buf  ,
NULL  ,
ast_free_ptr  ,
static   
)
static int commit_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 1053 of file res_odbc.c.

References ast_log(), ast_str_append(), ast_str_buffer(), ast_str_reset(), ast_str_strlen(), ast_str_thread_get(), ast_strlen_zero(), odbc_obj::con, find_transaction(), LOG_WARNING, odbc_txn_frame::obj, and pbx_builtin_setvar_helper().

Referenced by load_module().

{
   struct odbc_txn_frame *tx;
   SQLINTEGER nativeerror=0, numfields=0;
   SQLSMALLINT diagbytes=0, i;
   unsigned char state[10], diagnostic[256];

   if (ast_strlen_zero(data)) {
      tx = find_transaction(chan, NULL, NULL, 1);
   } else {
      tx = find_transaction(chan, NULL, data, 0);
   }

   pbx_builtin_setvar_helper(chan, "COMMIT_RESULT", "OK");

   if (tx) {
      if (SQLEndTran(SQL_HANDLE_DBC, tx->obj->con, SQL_COMMIT) == SQL_ERROR) {
         struct ast_str *errors = ast_str_thread_get(&errors_buf, 16);
         ast_str_reset(errors);

         /* Handle possible transaction commit failure */
         SQLGetDiagField(SQL_HANDLE_DBC, tx->obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
         for (i = 0; i < numfields; i++) {
            SQLGetDiagRec(SQL_HANDLE_DBC, tx->obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
            ast_str_append(&errors, 0, "%s%s", ast_str_strlen(errors) ? "," : "", state);
            ast_log(LOG_WARNING, "SQLEndTran 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;
            }
         }
         pbx_builtin_setvar_helper(chan, "COMMIT_RESULT", ast_str_buffer(errors));
      }
   }
   return 0;
}
static void destroy_table_cache ( struct odbc_cache_tables table) [static]
static struct odbc_txn_frame* find_transaction ( struct ast_channel chan,
struct odbc_obj obj,
const char *  name,
int  active 
) [static, read]

Definition at line 204 of file res_odbc.c.

References odbc_txn_frame::active, ast_calloc, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_datastore_alloc(), ast_datastore_free(), AST_LIST_HEAD, AST_LIST_HEAD_INIT, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), chan, ast_datastore::data, odbc_class::forcecommit, odbc_txn_frame::forcecommit, odbc_class::isolation, odbc_txn_frame::isolation, odbc_class::list, LOG_ERROR, odbc_txn_frame::name, odbc_txn_frame::obj, odbc_txn_frame::owner, odbc_obj::parent, odbc_obj::tx, odbc_obj::txf, and txn_info.

Referenced by acf_transaction_read(), acf_transaction_write(), ast_odbc_release_obj(), commit_exec(), and rollback_exec().

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

   if (!chan && obj && obj->txf && obj->txf->owner) {
      chan = obj->txf->owner;
   } else 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 {
      /* Need to create a new datastore */
      if (!(txn_store = ast_datastore_alloc(&txn_info, NULL))) {
         ast_log(LOG_ERROR, "Unable to allocate a new datastore.  Cannot create a new transaction.\n");
         ast_channel_unlock(chan);
         return NULL;
      }

      if (!(oldlist = ast_calloc(1, sizeof(*oldlist)))) {
         ast_log(LOG_ERROR, "Unable to allocate datastore list head.  Cannot create a new transaction.\n");
         ast_datastore_free(txn_store);
         ast_channel_unlock(chan);
         return NULL;
      }

      txn_store->data = oldlist;
      AST_LIST_HEAD_INIT(oldlist);
      ast_channel_datastore_add(chan, txn_store);
   }

   AST_LIST_LOCK(oldlist);
   ast_channel_unlock(chan);

   /* Scanning for an object is *fast*.  Scanning for a name is much slower. */
   if (obj != NULL || active == 1) {
      AST_LIST_TRAVERSE(oldlist, txn, list) {
         if (txn->obj == obj || txn->active) {
            AST_LIST_UNLOCK(oldlist);
            return txn;
         }
      }
   }

   if (name != NULL) {
      AST_LIST_TRAVERSE(oldlist, txn, list) {
         if (!strcasecmp(txn->name, name)) {
            AST_LIST_UNLOCK(oldlist);
            return txn;
         }
      }
   }

   /* Nothing found, create one */
   if (name && obj && (txn = ast_calloc(1, sizeof(*txn) + strlen(name) + 1))) {
      struct odbc_txn_frame *otxn;

      strcpy(txn->name, name); /* SAFE */
      txn->obj = obj;
      txn->isolation = obj->parent->isolation;
      txn->forcecommit = obj->parent->forcecommit;
      txn->owner = chan;
      txn->active = 1;

      /* On creation, the txn becomes active, and all others inactive */
      AST_LIST_TRAVERSE(oldlist, otxn, list) {
         otxn->active = 0;
      }
      AST_LIST_INSERT_TAIL(oldlist, txn, list);

      obj->txf = txn;
      obj->tx = 1;
   }
   AST_LIST_UNLOCK(oldlist);

   return txn;
}
static char* handle_cli_odbc_show ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 872 of file res_odbc.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_mutex_lock(), ast_mutex_unlock(), ast_odbc_sanity_check(), ast_strdup, class_container, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, odbc_class::count, ast_cli_args::fd, odbc_obj::lock, ast_cli_args::n, ast_cli_args::pos, odbc_obj::up, ast_cli_entry::usage, odbc_obj::used, and ast_cli_args::word.

{
   struct ao2_iterator aoi = ao2_iterator_init(class_container, 0);
   struct odbc_class *class;
   struct odbc_obj *current;
   int length = 0;
   int which = 0;
   char *ret = NULL;

   switch (cmd) {
   case CLI_INIT:
      e->command = "odbc show";
      e->usage =
            "Usage: odbc show [class]\n"
            "       List settings of a particular ODBC class or,\n"
            "       if not specified, all classes.\n";
      return NULL;
   case CLI_GENERATE:
      if (a->pos != 2)
         return NULL;
      length = strlen(a->word);
      while ((class = ao2_iterator_next(&aoi))) {
         if (!strncasecmp(a->word, class->name, length) && ++which > a->n) {
            ret = ast_strdup(class->name);
         }
         ao2_ref(class, -1);
         if (ret) {
            break;
         }
      }
      ao2_iterator_destroy(&aoi);
      if (!ret && !strncasecmp(a->word, "all", length) && ++which > a->n) {
         ret = ast_strdup("all");
      }
      return ret;
   }

   ast_cli(a->fd, "\nODBC DSN Settings\n");
   ast_cli(a->fd,   "-----------------\n\n");
   aoi = ao2_iterator_init(class_container, 0);
   while ((class = ao2_iterator_next(&aoi))) {
      if ((a->argc == 2) || (a->argc == 3 && !strcmp(a->argv[2], "all")) || (!strcmp(a->argv[2], class->name))) {
         int count = 0;
         ast_cli(a->fd, "  Name:   %s\n  DSN:    %s\n", class->name, class->dsn);

         if (class->haspool) {
            struct ao2_iterator aoi2 = ao2_iterator_init(class->obj_container, 0);

            ast_cli(a->fd, "  Pooled: Yes\n  Limit:  %d\n  Connections in use: %d\n", class->limit, class->count);

            while ((current = ao2_iterator_next(&aoi2))) {
               ast_mutex_lock(&current->lock);
#ifdef DEBUG_THREADS
               ast_cli(a->fd, "    - Connection %d: %s (%s:%d %s)\n", ++count,
                  current->used ? "in use" :
                  current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected",
                  current->file, current->lineno, current->function);
#else
               ast_cli(a->fd, "    - Connection %d: %s\n", ++count,
                  current->used ? "in use" :
                  current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected");
#endif
               ast_mutex_unlock(&current->lock);
               ao2_ref(current, -1);
            }
            ao2_iterator_destroy(&aoi2);
         } else {
            /* Should only ever be one of these (unless there are transactions) */
            struct ao2_iterator aoi2 = ao2_iterator_init(class->obj_container, 0);
            while ((current = ao2_iterator_next(&aoi2))) {
               ast_cli(a->fd, "  Pooled: No\n  Connected: %s\n", current->used ? "In use" :
                  current->up && ast_odbc_sanity_check(current) ? "Yes" : "No");
               ao2_ref(current, -1);
            }
            ao2_iterator_destroy(&aoi2);
         }
         ast_cli(a->fd, "\n");
      }
      ao2_ref(class, -1);
   }
   ao2_iterator_destroy(&aoi);

   return CLI_SUCCESS;
}
static const char* isolation2text ( int  iso) [static]

Definition at line 170 of file res_odbc.c.

Referenced by acf_transaction_read().

{
   if (iso == SQL_TXN_READ_COMMITTED) {
      return "read_committed";
   } else if (iso == SQL_TXN_READ_UNCOMMITTED) {
      return "read_uncommitted";
   } else if (iso == SQL_TXN_SERIALIZABLE) {
      return "serializable";
   } else if (iso == SQL_TXN_REPEATABLE_READ) {
      return "repeatable_read";
   } else {
      return "unknown";
   }
}
static int load_odbc_config ( void  ) [static]

Definition at line 731 of file res_odbc.c.

References ao2_alloc, ao2_container_alloc, ao2_match_by_addr, ao2_ref, ast_category_browse(), ast_config_destroy(), ast_config_load, ast_copy_string(), ast_false(), ast_log(), ast_strdup, ast_strlen_zero(), ast_true(), ast_variable_browse(), config, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, odbc_class::dsn, enabled, odbc_class::forcecommit, odbc_class::idlecheck, odbc_class::isolation, odbc_class::limit, LOG_ERROR, LOG_NOTICE, LOG_WARNING, ast_variable::name, ast_variable::next, null_hash_fn(), odbc_class_destructor(), odbc_register_class(), odbc_class::password, odbc_class::sanitysql, setenv(), text2isolation(), odbc_class::username, and ast_variable::value.

Referenced by load_module(), and reload().

{
   static char *cfg = "res_odbc.conf";
   struct ast_config *config;
   struct ast_variable *v;
   char *cat;
   const char *dsn, *username, *password, *sanitysql;
   int enabled, pooling, limit, bse, forcecommit, isolation;
   unsigned int idlecheck;
   int preconnect = 0, res = 0;
   struct ast_flags config_flags = { 0 };

   struct odbc_class *new;

   config = ast_config_load(cfg, config_flags);
   if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
      ast_log(LOG_WARNING, "Unable to load config file res_odbc.conf\n");
      return -1;
   }
   for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
      if (!strcasecmp(cat, "ENV")) {
         for (v = ast_variable_browse(config, cat); v; v = v->next) {
            setenv(v->name, v->value, 1);
            ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
         }
      } else {
         /* Reset all to defaults for each class of odbc connections */
         dsn = username = password = sanitysql = NULL;
         enabled = 1;
         preconnect = idlecheck = 0;
         pooling = 0;
         limit = 0;
         bse = 1;
         forcecommit = 0;
         isolation = SQL_TXN_READ_COMMITTED;
         for (v = ast_variable_browse(config, cat); v; v = v->next) {
            if (!strcasecmp(v->name, "pooling")) {
               if (ast_true(v->value))
                  pooling = 1;
            } else if (!strncasecmp(v->name, "share", 5)) {
               /* "shareconnections" is a little clearer in meaning than "pooling" */
               if (ast_false(v->value))
                  pooling = 1;
            } else if (!strcasecmp(v->name, "limit")) {
               sscanf(v->value, "%30d", &limit);
               if (ast_true(v->value) && !limit) {
                  ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Setting limit to 1023 for ODBC class '%s'.\n", v->value, cat);
                  limit = 1023;
               } else if (ast_false(v->value)) {
                  ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
                  enabled = 0;
                  break;
               }
            } else if (!strcasecmp(v->name, "idlecheck")) {
               sscanf(v->value, "%30u", &idlecheck);
            } else if (!strcasecmp(v->name, "enabled")) {
               enabled = ast_true(v->value);
            } else if (!strcasecmp(v->name, "pre-connect")) {
               preconnect = ast_true(v->value);
            } else if (!strcasecmp(v->name, "dsn")) {
               dsn = v->value;
            } else if (!strcasecmp(v->name, "username")) {
               username = v->value;
            } else if (!strcasecmp(v->name, "password")) {
               password = v->value;
            } else if (!strcasecmp(v->name, "sanitysql")) {
               sanitysql = v->value;
            } else if (!strcasecmp(v->name, "backslash_is_escape")) {
               bse = ast_true(v->value);
            } else if (!strcasecmp(v->name, "forcecommit")) {
               forcecommit = ast_true(v->value);
            } else if (!strcasecmp(v->name, "isolation")) {
               if ((isolation = text2isolation(v->value)) == 0) {
                  ast_log(LOG_ERROR, "Unrecognized value for 'isolation': '%s' in section '%s'\n", v->value, cat);
                  isolation = SQL_TXN_READ_COMMITTED;
               }
            }
         }

         if (enabled && !ast_strlen_zero(dsn)) {
            new = ao2_alloc(sizeof(*new), odbc_class_destructor);

            if (!new) {
               res = -1;
               break;
            }

            SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
            res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);

            if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
               ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
               ao2_ref(new, -1);
               return res;
            }

            new->obj_container = ao2_container_alloc(1, null_hash_fn, ao2_match_by_addr);

            if (pooling) {
               new->haspool = pooling;
               if (limit) {
                  new->limit = limit;
               } else {
                  ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
                  new->limit = 5;
               }
            }

            new->backslash_is_escape = bse ? 1 : 0;
            new->forcecommit = forcecommit ? 1 : 0;
            new->isolation = isolation;
            new->idlecheck = idlecheck;

            if (cat)
               ast_copy_string(new->name, cat, sizeof(new->name));
            if (dsn)
               ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
            if (username && !(new->username = ast_strdup(username))) {
               ao2_ref(new, -1);
               break;
            }
            if (password && !(new->password = ast_strdup(password))) {
               ao2_ref(new, -1);
               break;
            }
            if (sanitysql && !(new->sanitysql = ast_strdup(sanitysql))) {
               ao2_ref(new, -1);
               break;
            }

            odbc_register_class(new, preconnect);
            ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
            ao2_ref(new, -1);
            new = NULL;
         }
      }
   }
   ast_config_destroy(config);
   return res;
}
static int mark_transaction_active ( struct ast_channel chan,
struct odbc_txn_frame tx 
) [static]

Definition at line 339 of file res_odbc.c.

References odbc_txn_frame::active, 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_txn_frame::owner, and txn_info.

Referenced by acf_transaction_write().

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

   if (!chan && tx && tx->owner) {
      chan = tx->owner;
   }

   ast_channel_lock(chan);
   if (!(txn_store = ast_channel_datastore_find(chan, &txn_info, NULL))) {
      ast_channel_unlock(chan);
      return -1;
   }

   oldlist = txn_store->data;
   AST_LIST_LOCK(oldlist);
   AST_LIST_TRAVERSE(oldlist, txn, list) {
      if (txn == tx) {
         txn->active = 1;
         active = txn;
      } else {
         txn->active = 0;
      }
   }
   AST_LIST_UNLOCK(oldlist);
   ast_channel_unlock(chan);
   return active ? 0 : -1;
}
static int null_hash_fn ( const void *  obj,
const int  flags 
) [static]

Definition at line 389 of file res_odbc.c.

Referenced by load_module(), and load_odbc_config().

{
   return 0;
}
static void odbc_class_destructor ( void *  data) [static]

Definition at line 370 of file res_odbc.c.

References ao2_ref, and ast_free.

Referenced by load_odbc_config().

{
   struct odbc_class *class = data;
   /* Due to refcounts, we can safely assume that any objects with a reference
    * to us will prevent our destruction, so we don't need to worry about them.
    */
   if (class->username) {
      ast_free(class->username);
   }
   if (class->password) {
      ast_free(class->password);
   }
   if (class->sanitysql) {
      ast_free(class->sanitysql);
   }
   ao2_ref(class->obj_container, -1);
   SQLFreeHandle(SQL_HANDLE_ENV, class->env);
}
static odbc_status odbc_obj_connect ( struct odbc_obj obj) [static]

Definition at line 1417 of file res_odbc.c.

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

Referenced by ast_odbc_request_obj2(), and ast_odbc_sanity_check().

{
   int res;
   SQLINTEGER err;
   short int mlen;
   unsigned char msg[200], state[10];
#ifdef NEEDTRACE
   SQLINTEGER enable = 1;
   char *tracefile = "/tmp/odbc.trace";
#endif
   ast_mutex_lock(&obj->lock);

   if (obj->up) {
      odbc_obj_disconnect(obj);
      ast_log(LOG_NOTICE, "Re-connecting %s\n", obj->parent->name);
   } else {
      ast_log(LOG_NOTICE, "Connecting %s\n", obj->parent->name);
   }

   res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &obj->con);

   if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
      ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
      ast_mutex_unlock(&obj->lock);
      return ODBC_FAIL;
   }
   SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
   SQLSetConnectAttr(obj->con, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER *) 10, 0);
#ifdef NEEDTRACE
   SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
   SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
#endif

   res = SQLConnect(obj->con,
         (SQLCHAR *) obj->parent->dsn, SQL_NTS,
         (SQLCHAR *) obj->parent->username, SQL_NTS,
         (SQLCHAR *) obj->parent->password, SQL_NTS);

   if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
      SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, state, &err, msg, 100, &mlen);
      ast_mutex_unlock(&obj->lock);
      ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
      return ODBC_FAIL;
   } else {
      ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->parent->name, obj->parent->dsn);
      obj->up = 1;
      obj->last_used = ast_tvnow();
   }

   ast_mutex_unlock(&obj->lock);
   return ODBC_SUCCESS;
}
static void odbc_obj_destructor ( void *  data) [static]

Definition at line 394 of file res_odbc.c.

References ao2_ref, ast_mutex_destroy(), odbc_obj::lock, odbc_obj_disconnect(), and odbc_obj::parent.

Referenced by ast_odbc_request_obj2().

{
   struct odbc_obj *obj = data;
   struct odbc_class *class = obj->parent;
   obj->parent = NULL;
   odbc_obj_disconnect(obj);
   ast_mutex_destroy(&obj->lock);
   ao2_ref(class, -1);
}
static odbc_status odbc_obj_disconnect ( struct odbc_obj obj) [static]

Definition at line 1380 of file res_odbc.c.

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

Referenced by ast_odbc_sanity_check(), odbc_obj_connect(), and odbc_obj_destructor().

{
   int res;
   SQLINTEGER err;
   short int mlen;
   unsigned char msg[200], state[10];

   /* Nothing to disconnect */
   if (!obj->con) {
      return ODBC_SUCCESS;
   }

   ast_mutex_lock(&obj->lock);

   res = SQLDisconnect(obj->con);

   if (obj->parent) {
      if (res == SQL_SUCCESS || res == SQL_SUCCESS_WITH_INFO) {
         ast_log(LOG_DEBUG, "Disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn);
      } else {
         ast_log(LOG_DEBUG, "res_odbc: %s [%s] already disconnected\n", obj->parent->name, obj->parent->dsn);
      }
   }

   if ((res = SQLFreeHandle(SQL_HANDLE_DBC, obj->con) == SQL_SUCCESS)) {
      obj->con = NULL;
      ast_log(LOG_DEBUG, "Database handle deallocated\n");
   } else {
      SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, state, &err, msg, 100, &mlen);
      ast_log(LOG_WARNING, "Unable to deallocate database handle? %d errno=%d %s\n", res, (int)err, msg);
   }

   obj->up = 0;
   ast_mutex_unlock(&obj->lock);
   return ODBC_SUCCESS;
}
static int odbc_register_class ( struct odbc_class class,
int  connect 
) [static]

Definition at line 961 of file res_odbc.c.

References ao2_link, ast_log(), ast_odbc_release_obj(), ast_odbc_request_obj(), class_container, and LOG_WARNING.

Referenced by load_odbc_config().

{
   struct odbc_obj *obj;
   if (class) {
      ao2_link(class_container, class);
      /* I still have a reference in the caller, so a deref is NOT missing here. */

      if (preconnect) {
         /* Request and release builds a connection */
         obj = ast_odbc_request_obj(class->name, 0);
         if (obj) {
            ast_odbc_release_obj(obj);
         }
      }

      return 0;
   } else {
      ast_log(LOG_WARNING, "Attempted to register a NULL class?\n");
      return -1;
   }
}
static void odbc_release_obj2 ( struct odbc_obj obj,
struct odbc_txn_frame tx 
) [static]

Definition at line 983 of file res_odbc.c.

References ao2_ref, ast_debug, ast_log(), odbc_obj::con, odbc_txn_frame::forcecommit, LOG_WARNING, odbc_txn_frame::obj, release_transaction(), odbc_obj::txf, and odbc_obj::used.

Referenced by ast_odbc_release_obj(), and release_transaction().

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

   ast_debug(2, "odbc_release_obj2(%p) called (obj->txf = %p)\n", obj, obj->txf);
   if (tx) {
      ast_debug(1, "called on a transactional handle with %s\n", tx->forcecommit ? "COMMIT" : "ROLLBACK");
      if (SQLEndTran(SQL_HANDLE_DBC, obj->con, tx->forcecommit ? SQL_COMMIT : SQL_ROLLBACK) == SQL_ERROR) {
         /* Handle possible transaction commit failure */
         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, "SQLEndTran returned an error: %s: %s\n", state, diagnostic);
            if (!strcmp((char *)state, "25S02") || !strcmp((char *)state, "08007")) {
               /* These codes mean that a commit failed and a transaction
                * is still active. We must rollback, or things will get
                * very, very weird for anybody using the handle next. */
               SQLEndTran(SQL_HANDLE_DBC, obj->con, SQL_ROLLBACK);
            }
            if (i > 10) {
               ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
               break;
            }
         }
      }

      /* Transaction is done, reset autocommit */
      if (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;
            }
         }
      }
   }

#ifdef DEBUG_THREADS
   obj->file[0] = '\0';
   obj->function[0] = '\0';
   obj->lineno = 0;
#endif

   /* For pooled connections, this frees the connection to be
    * reused.  For non-pooled connections, it does nothing. */
   obj->used = 0;
   if (obj->txf) {
      /* Prevent recursion -- transaction is already closed out. */
      obj->txf->obj = NULL;
      obj->txf = release_transaction(obj->txf);
   }
   ao2_ref(obj, -1);
}
static void odbc_txn_free ( void *  data) [static]

Definition at line 323 of file res_odbc.c.

References ast_debug, ast_free, AST_LIST_HEAD, AST_LIST_HEAD_DESTROY, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, odbc_class::list, and release_transaction().

{
   struct odbc_txn_frame *tx;
   AST_LIST_HEAD(, odbc_txn_frame) *oldlist = vdata;

   ast_debug(2, "odbc_txn_free(%p) called\n", vdata);

   AST_LIST_LOCK(oldlist);
   while ((tx = AST_LIST_REMOVE_HEAD(oldlist, list))) {
      release_transaction(tx);
   }
   AST_LIST_UNLOCK(oldlist);
   AST_LIST_HEAD_DESTROY(oldlist);
   ast_free(oldlist);
}
static struct odbc_txn_frame* release_transaction ( struct odbc_txn_frame tx) [static, read]

Definition at line 287 of file res_odbc.c.

References ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_debug, ast_free, AST_LIST_HEAD, AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, ast_datastore::data, odbc_class::list, odbc_txn_frame::obj, odbc_release_obj2(), odbc_txn_frame::owner, odbc_obj::txf, and txn_info.

Referenced by odbc_release_obj2(), and odbc_txn_free().

{
   if (!tx) {
      return NULL;
   }

   ast_debug(2, "release_transaction(%p) called (tx->obj = %p, tx->obj->txf = %p)\n", tx, tx->obj, tx->obj ? tx->obj->txf : NULL);

   /* If we have an owner, disassociate */
   if (tx->owner) {
      struct ast_datastore *txn_store;
      AST_LIST_HEAD(, odbc_txn_frame) *oldlist;

      ast_channel_lock(tx->owner);
      if ((txn_store = ast_channel_datastore_find(tx->owner, &txn_info, NULL))) {
         oldlist = txn_store->data;
         AST_LIST_LOCK(oldlist);
         AST_LIST_REMOVE(oldlist, tx, list);
         AST_LIST_UNLOCK(oldlist);
      }
      ast_channel_unlock(tx->owner);
      tx->owner = NULL;
   }

   if (tx->obj) {
      /* If we have any uncommitted transactions, they are handled when we release the object */
      struct odbc_obj *obj = tx->obj;
      /* Prevent recursion during destruction */
      tx->obj->txf = NULL;
      tx->obj = NULL;
      odbc_release_obj2(obj, tx);
   }
   ast_free(tx);
   return NULL;
}
static int reload ( void  ) [static]

Definition at line 1610 of file res_odbc.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ao2_unlink, AST_RWLIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, class_container, destroy_table_cache(), odbc_class::list, load_odbc_config(), and table.

{
   struct odbc_cache_tables *table;
   struct odbc_class *class;
   struct odbc_obj *current;
   struct ao2_iterator aoi = ao2_iterator_init(class_container, 0);

   /* First, mark all to be purged */
   while ((class = ao2_iterator_next(&aoi))) {
      class->delme = 1;
      ao2_ref(class, -1);
   }
   ao2_iterator_destroy(&aoi);

   load_odbc_config();

   /* Purge remaining classes */

   /* Note on how this works; this is a case of circular references, so we
    * explicitly do NOT want to use a callback here (or we wind up in
    * recursive hell).
    *
    * 1. Iterate through all the classes.  Note that the classes will currently
    * contain two classes of the same name, one of which is marked delme and
    * will be purged when all remaining objects of the class are released, and
    * the other, which was created above when we re-parsed the config file.
    * 2. On each class, there is a reference held by the master container and
    * a reference held by each connection object.  There are two cases for
    * destruction of the class, noted below.  However, in all cases, all O-refs
    * (references to objects) will first be freed, which will cause the C-refs
    * (references to classes) to be decremented (but never to 0, because the
    * class container still has a reference).
    *    a) If the class has outstanding objects, the C-ref by the class
    *    container will then be freed, which leaves only C-refs by any
    *    outstanding objects.  When the final outstanding object is released
    *    (O-refs held by applications and dialplan functions), it will in turn
    *    free the final C-ref, causing class destruction.
    *    b) If the class has no outstanding objects, when the class container
    *    removes the final C-ref, the class will be destroyed.
    */
   aoi = ao2_iterator_init(class_container, 0);
   while ((class = ao2_iterator_next(&aoi))) { /* C-ref++ (by iterator) */
      if (class->delme) {
         struct ao2_iterator aoi2 = ao2_iterator_init(class->obj_container, 0);
         while ((current = ao2_iterator_next(&aoi2))) { /* O-ref++ (by iterator) */
            ao2_unlink(class->obj_container, current); /* unlink O-ref from class (reference handled implicitly) */
            ao2_ref(current, -1); /* O-ref-- (by iterator) */
            /* At this point, either
             * a) there's an outstanding O-ref, or
             * b) the object has already been destroyed.
             */
         }
         ao2_iterator_destroy(&aoi2);
         ao2_unlink(class_container, class); /* unlink C-ref from container (reference handled implicitly) */
         /* At this point, either
          * a) there's an outstanding O-ref, which holds an outstanding C-ref, or
          * b) the last remaining C-ref is held by the iterator, which will be
          * destroyed in the next step.
          */
      }
      ao2_ref(class, -1); /* C-ref-- (by iterator) */
   }
   ao2_iterator_destroy(&aoi);

   /* Empty the cache; it will get rebuilt the next time the tables are needed. */
   AST_RWLIST_WRLOCK(&odbc_tables);
   while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
      destroy_table_cache(table);
   }
   AST_RWLIST_UNLOCK(&odbc_tables);

   return 0;
}
static int rollback_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 1090 of file res_odbc.c.

References ast_log(), ast_str_append(), ast_str_buffer(), ast_str_reset(), ast_str_strlen(), ast_str_thread_get(), ast_strlen_zero(), odbc_obj::con, find_transaction(), LOG_WARNING, odbc_txn_frame::obj, and pbx_builtin_setvar_helper().

Referenced by load_module().

{
   struct odbc_txn_frame *tx;
   SQLINTEGER nativeerror=0, numfields=0;
   SQLSMALLINT diagbytes=0, i;
   unsigned char state[10], diagnostic[256];

   if (ast_strlen_zero(data)) {
      tx = find_transaction(chan, NULL, NULL, 1);
   } else {
      tx = find_transaction(chan, NULL, data, 0);
   }

   pbx_builtin_setvar_helper(chan, "ROLLBACK_RESULT", "OK");

   if (tx) {
      if (SQLEndTran(SQL_HANDLE_DBC, tx->obj->con, SQL_ROLLBACK) == SQL_ERROR) {
         struct ast_str *errors = ast_str_thread_get(&errors_buf, 16);
         ast_str_reset(errors);

         /* Handle possible transaction commit failure */
         SQLGetDiagField(SQL_HANDLE_DBC, tx->obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
         for (i = 0; i < numfields; i++) {
            SQLGetDiagRec(SQL_HANDLE_DBC, tx->obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
            ast_str_append(&errors, 0, "%s%s", ast_str_strlen(errors) ? "," : "", state);
            ast_log(LOG_WARNING, "SQLEndTran 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;
            }
         }
         pbx_builtin_setvar_helper(chan, "ROLLBACK_RESULT", ast_str_buffer(errors));
      }
   }
   return 0;
}
static int text2isolation ( const char *  txt) [static]

Definition at line 185 of file res_odbc.c.

Referenced by acf_transaction_write(), and load_odbc_config().

{
   if (strncasecmp(txt, "read_", 5) == 0) {
      if (strncasecmp(txt + 5, "c", 1) == 0) {
         return SQL_TXN_READ_COMMITTED;
      } else if (strncasecmp(txt + 5, "u", 1) == 0) {
         return SQL_TXN_READ_UNCOMMITTED;
      } else {
         return 0;
      }
   } else if (strncasecmp(txt, "ser", 3) == 0) {
      return SQL_TXN_SERIALIZABLE;
   } else if (strncasecmp(txt, "rep", 3) == 0) {
      return SQL_TXN_REPEATABLE_READ;
   } else {
      return 0;
   }
}
static int unload_module ( void  ) [static]

Definition at line 1684 of file res_odbc.c.

{
   /* Prohibit unloading */
   return -1;
}

Variable Documentation

struct ast_module_info __MODULE_INFO_SECTION __mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS , .description = "ODBC resource" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, } [static]

Definition at line 1708 of file res_odbc.c.

const char* app_commit = "ODBC_Commit" [static]

Definition at line 1607 of file res_odbc.c.

Referenced by load_module().

const char* app_rollback = "ODBC_Rollback" [static]

Definition at line 1608 of file res_odbc.c.

Referenced by load_module().

Definition at line 1708 of file res_odbc.c.

struct ast_cli_entry cli_odbc[] [static]
Initial value:
 {
   AST_CLI_DEFINE(handle_cli_odbc_show, "List ODBC DSN(s)")
}

Definition at line 957 of file res_odbc.c.

Referenced by load_module().

Initial value:
 {
   .name = "ODBC",
   .read = acf_transaction_read,
   .write = acf_transaction_write,
}

Definition at line 1601 of file res_odbc.c.

Referenced by load_module().

struct odbc_tables odbc_tables [static]
struct ast_datastore_info txn_info [static]
Initial value:
 {
   .type = "ODBC_Transaction",
   .destroy = odbc_txn_free,
}

Definition at line 148 of file res_odbc.c.

Referenced by ast_odbc_retrieve_transaction_obj(), find_transaction(), mark_transaction_active(), and release_transaction().