Wed Jan 8 2020 09:50:19

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"

Go to the source code of this file.

Data Structures

struct  odbc_cache_tables::_columns
 
struct  odbc_cache_columns
 These structures are used for adaptive capabilities. More...
 
struct  odbc_cache_tables
 
struct  odbc_obj
 ODBC container. More...
 

Macros

#define ast_odbc_release_table(ptr)   if (ptr) { AST_RWLIST_UNLOCK(&(ptr)->columns); }
 Release a table returned from ast_odbc_find_table. More...
 
#define ast_odbc_request_obj(a, b)   _ast_odbc_request_obj(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__)
 
#define ast_odbc_request_obj2(a, b)   _ast_odbc_request_obj2(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__)
 

Enumerations

enum  { RES_ODBC_SANITY_CHECK = (1 << 0), RES_ODBC_INDEPENDENT_CONNECTION = (1 << 1), RES_ODBC_CONNECTED = (1 << 2) }
 Flags for use with. More...
 
enum  odbc_status { ODBC_SUCCESS =0, ODBC_FAIL =-1 }
 

Functions

struct odbc_obj_ast_odbc_request_obj (const char *name, int check, const char *file, const char *function, int lineno)
 
struct odbc_obj_ast_odbc_request_obj2 (const char *name, struct ast_flags flags, const char *file, const char *function, int lineno)
 Retrieves a connected ODBC object. More...
 
int ast_odbc_allow_empty_string_in_nontext (struct odbc_obj *obj)
 Checks if the database natively supports implicit conversion from an empty string to a number (0). More...
 
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. More...
 
int ast_odbc_backslash_is_escape (struct odbc_obj *obj)
 Checks if the database natively supports backslash as an escape character. More...
 
int ast_odbc_clear_cache (const char *database, const char *tablename)
 Remove a cache entry from memory This function may be called to clear entries created and cached by the ast_odbc_find_table() API call. More...
 
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. More...
 
struct odbc_cache_columnsast_odbc_find_column (struct odbc_cache_tables *table, const char *colname)
 Find a column entry within a cached table structure. More...
 
struct odbc_cache_tablesast_odbc_find_table (const char *database, const char *tablename)
 Find or create an entry describing the table specified. More...
 
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. More...
 
void ast_odbc_release_obj (struct odbc_obj *obj)
 Releases an ODBC object previously allocated by ast_odbc_request_obj() More...
 
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. More...
 
int ast_odbc_sanity_check (struct odbc_obj *obj)
 Checks an ODBC object to ensure it is still connected. More...
 
int ast_odbc_smart_execute (struct odbc_obj *obj, SQLHSTMT stmt)
 Executes a prepared statement handle. More...
 

Detailed Description

ODBC resource manager.

Definition in file res_odbc.h.

Macro Definition 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 213 of file res_odbc.h.

Referenced by update2_prepare(), and update_odbc().

#define ast_odbc_request_obj (   a,
 
)    _ast_odbc_request_obj(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__)
#define ast_odbc_request_obj2 (   a,
 
)    _ast_odbc_request_obj2(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__)

Enumeration Type Documentation

anonymous enum

Flags for use with.

See Also
ast_odbc_request_obj2
Enumerator
RES_ODBC_SANITY_CHECK 
RES_ODBC_INDEPENDENT_CONNECTION 
RES_ODBC_CONNECTED 

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

struct odbc_obj* _ast_odbc_request_obj ( const char *  name,
int  check,
const char *  file,
const char *  function,
int  lineno 
)

Definition at line 1450 of file res_odbc.c.

References _ast_odbc_request_obj2(), and RES_ODBC_SANITY_CHECK.

1451 {
1452  struct ast_flags flags = { check ? RES_ODBC_SANITY_CHECK : 0 };
1453  return _ast_odbc_request_obj2(name, flags, file, function, lineno);
1454 }
struct odbc_obj * _ast_odbc_request_obj2(const char *name, struct ast_flags flags, const char *file, const char *function, int lineno)
Retrieves a connected ODBC object.
Definition: res_odbc.c:1241
unsigned int flags
Definition: utils.h:201
static const char name[]
Structure used to handle boolean flags.
Definition: utils.h:200
struct odbc_obj* _ast_odbc_request_obj2 ( const char *  name,
struct ast_flags  flags,
const char *  file,
const char *  function,
int  lineno 
)

Retrieves a connected ODBC object.

Parameters
nameThe name of the ODBC class for which a connection is needed.
flagsOne or more of the following flags:
  • RES_ODBC_SANITY_CHECK Whether to ensure that a connection is valid before returning the handle. Usually unnecessary.
  • RES_ODBC_INDEPENDENT_CONNECTION Return a handle which is independent from all others. Usually used when starting a transaction.
  • RES_ODBC_CONNECTED Only return a connected handle. Intended for use with peers which use idlecheck, which are checked periodically for reachability.
Returns
ODBC object
Return values
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 ast_odbc_release_obj(), below.

Definition at line 1241 of file res_odbc.c.

References ao2_alloc, ao2_callback, ao2_link, ao2_ref, aoro2_class_cb(), aoro2_obj_cb(), aoro2_obj_notx_cb(), ast_assert, ast_atomic_fetchadd_int(), ast_copy_string(), ast_debug, ast_log(), ast_mutex_init, ast_mutex_lock, ast_mutex_unlock, 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_class::last_negative_connect, odbc_obj::last_used, odbc_obj::lock, LOG_WARNING, odbc_class::negative_connection_cache, NO_TX, odbc_class::obj_container, ODBC_FAIL, odbc_obj_connect(), odbc_obj_destructor(), odbc_obj::parent, RES_ODBC_CONNECTED, RES_ODBC_INDEPENDENT_CONNECTION, RES_ODBC_SANITY_CHECK, odbc_obj::up, USE_TX, and odbc_obj::used.

Referenced by _ast_odbc_request_obj().

1242 {
1243  struct odbc_obj *obj = NULL;
1244  struct odbc_class *class;
1245  SQLINTEGER nativeerror=0, numfields=0;
1246  SQLSMALLINT diagbytes=0, i;
1247  unsigned char state[10], diagnostic[256];
1248 
1249  if (!(class = ao2_callback(class_container, 0, aoro2_class_cb, (char *) name))) {
1250  ast_debug(1, "Class '%s' not found!\n", name);
1251  return NULL;
1252  }
1253 
1254  ast_assert(ao2_ref(class, 0) > 1);
1255 
1256  if (class->haspool) {
1257  /* Recycle connections before building another */
1258  obj = ao2_callback(class->obj_container, 0, aoro2_obj_cb, EOR_TX);
1259 
1260  if (obj) {
1261  ast_assert(ao2_ref(obj, 0) > 1);
1262  }
1263  if (!obj && (ast_atomic_fetchadd_int(&class->count, +1) < class->limit) &&
1264  (time(NULL) > class->last_negative_connect.tv_sec + class->negative_connection_cache.tv_sec)) {
1265  obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
1266  if (!obj) {
1267  class->count--;
1268  ao2_ref(class, -1);
1269  ast_debug(3, "Unable to allocate object\n");
1270  ast_atomic_fetchadd_int(&class->count, -1);
1271  return NULL;
1272  }
1273  ast_assert(ao2_ref(obj, 0) == 1);
1274  ast_mutex_init(&obj->lock);
1275  /* obj inherits the outstanding reference to class */
1276  obj->parent = class;
1277  class = NULL;
1278  if (odbc_obj_connect(obj) == ODBC_FAIL) {
1279  ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
1280  ast_assert(ao2_ref(obj->parent, 0) > 0);
1281  /* Because it was never within the container, we have to manually decrement the count here */
1282  ast_atomic_fetchadd_int(&obj->parent->count, -1);
1283  ao2_ref(obj, -1);
1284  obj = NULL;
1285  } else {
1286  obj->used = 1;
1287  ao2_link(obj->parent->obj_container, obj);
1288  }
1289  } else {
1290  /* If construction fails due to the limit (or negative timecache), reverse our increment. */
1291  if (!obj) {
1292  ast_atomic_fetchadd_int(&class->count, -1);
1293  }
1294  /* Object is not constructed, so delete outstanding reference to class. */
1295  ao2_ref(class, -1);
1296  class = NULL;
1297  }
1298 
1299  if (!obj) {
1300  return NULL;
1301  }
1302 
1303  ast_mutex_lock(&obj->lock);
1304 
1306  /* Ensure this connection has autocommit turned off. */
1307  if (SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_OFF, 0) == SQL_ERROR) {
1308  SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1309  for (i = 0; i < numfields; i++) {
1310  SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1311  ast_log(LOG_WARNING, "SQLSetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
1312  if (i > 10) {
1313  ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
1314  break;
1315  }
1316  }
1317  }
1318  }
1319  } else if (ast_test_flag(&flags, RES_ODBC_INDEPENDENT_CONNECTION)) {
1320  /* Non-pooled connections -- but must use a separate connection handle */
1321  if (!(obj = ao2_callback(class->obj_container, 0, aoro2_obj_cb, USE_TX))) {
1322  ast_debug(1, "Object not found\n");
1323  obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
1324  if (!obj) {
1325  ao2_ref(class, -1);
1326  ast_debug(3, "Unable to allocate object\n");
1327  return NULL;
1328  }
1329  ast_mutex_init(&obj->lock);
1330  /* obj inherits the outstanding reference to class */
1331  obj->parent = class;
1332  class = NULL;
1333  if (odbc_obj_connect(obj) == ODBC_FAIL) {
1334  ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
1335  ao2_ref(obj, -1);
1336  obj = NULL;
1337  } else {
1338  obj->used = 1;
1339  ao2_link(obj->parent->obj_container, obj);
1340  ast_atomic_fetchadd_int(&obj->parent->count, +1);
1341  }
1342  }
1343 
1344  if (!obj) {
1345  return NULL;
1346  }
1347 
1348  ast_mutex_lock(&obj->lock);
1349 
1350  if (SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_OFF, 0) == SQL_ERROR) {
1351  SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1352  for (i = 0; i < numfields; i++) {
1353  SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1354  ast_log(LOG_WARNING, "SetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
1355  if (i > 10) {
1356  ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
1357  break;
1358  }
1359  }
1360  }
1361  } else {
1362  /* Non-pooled connection: multiple modules can use the same connection. */
1363  if ((obj = ao2_callback(class->obj_container, 0, aoro2_obj_notx_cb, NO_TX))) {
1364  /* Object is not constructed, so delete outstanding reference to class. */
1365  ast_assert(ao2_ref(class, 0) > 1);
1366  ao2_ref(class, -1);
1367  class = NULL;
1368  } else {
1369  /* No entry: build one */
1370  if (!(obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor))) {
1371  ast_assert(ao2_ref(class, 0) > 1);
1372  ao2_ref(class, -1);
1373  ast_debug(3, "Unable to allocate object\n");
1374  return NULL;
1375  }
1376  ast_mutex_init(&obj->lock);
1377  /* obj inherits the outstanding reference to class */
1378  obj->parent = class;
1379  class = NULL;
1380  if (odbc_obj_connect(obj) == ODBC_FAIL) {
1381  ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
1382  ao2_ref(obj, -1);
1383  obj = NULL;
1384  } else {
1385  ao2_link(obj->parent->obj_container, obj);
1386  ast_assert(ao2_ref(obj, 0) > 1);
1387  }
1388  }
1389 
1390  if (!obj) {
1391  return NULL;
1392  }
1393 
1394  ast_mutex_lock(&obj->lock);
1395 
1396  if (SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_ON, 0) == SQL_ERROR) {
1397  SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1398  for (i = 0; i < numfields; i++) {
1399  SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1400  ast_log(LOG_WARNING, "SetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
1401  if (i > 10) {
1402  ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
1403  break;
1404  }
1405  }
1406  }
1407  }
1408 
1409  ast_assert(obj != NULL);
1410 
1411  /* Set the isolation property */
1412  if (SQLSetConnectAttr(obj->con, SQL_ATTR_TXN_ISOLATION, (void *)(long)obj->parent->isolation, 0) == SQL_ERROR) {
1413  SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1414  for (i = 0; i < numfields; i++) {
1415  SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1416  ast_log(LOG_WARNING, "SetConnectAttr (Txn isolation) returned an error: %s: %s\n", state, diagnostic);
1417  if (i > 10) {
1418  ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
1419  break;
1420  }
1421  }
1422  }
1423 
1424  if (ast_test_flag(&flags, RES_ODBC_CONNECTED) && !obj->up) {
1425  /* Check if this connection qualifies for reconnection, with negative connection cache time */
1426  if (time(NULL) > obj->parent->last_negative_connect.tv_sec + obj->parent->negative_connection_cache.tv_sec) {
1427  odbc_obj_connect(obj);
1428  }
1429  } else if (ast_test_flag(&flags, RES_ODBC_SANITY_CHECK)) {
1430  ast_odbc_sanity_check(obj);
1431  } else if (obj->parent->idlecheck > 0 && ast_tvdiff_sec(ast_tvnow(), obj->last_used) > obj->parent->idlecheck) {
1432  odbc_obj_connect(obj);
1433  }
1434 
1435 #ifdef DEBUG_THREADS
1436  ast_copy_string(obj->file, file, sizeof(obj->file));
1437  ast_copy_string(obj->function, function, sizeof(obj->function));
1438  obj->lineno = lineno;
1439 #endif
1440 
1441  /* We had it locked because of the obj_connects we see here. */
1442  ast_mutex_unlock(&obj->lock);
1443 
1444  ast_assert(class == NULL);
1445 
1446  ast_assert(ao2_ref(obj, 0) > 1);
1447  return obj;
1448 }
SQLHDBC con
Definition: res_odbc.h:48
#define ao2_link(arg1, arg2)
Definition: astobj2.h:785
struct ao2_container * obj_container
Definition: res_odbc.c:142
#define ast_test_flag(p, flag)
Definition: utils.h:63
static int aoro2_obj_cb(void *vobj, void *arg, int flags)
Definition: res_odbc.c:1216
int count
Definition: res_odbc.c:134
#define LOG_WARNING
Definition: logger.h:144
int64_t ast_tvdiff_sec(struct timeval end, struct timeval start)
Computes the difference (in seconds) between two struct timeval instances.
Definition: time.h:56
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:910
static int aoro2_class_cb(void *obj, void *arg, int flags)
Definition: res_odbc.c:1202
unsigned int idlecheck
Definition: res_odbc.c:135
#define USE_TX
Definition: res_odbc.c:1212
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
#define ast_assert(a)
Definition: utils.h:738
#define ast_mutex_lock(a)
Definition: lock.h:155
struct timeval negative_connection_cache
Definition: res_odbc.c:138
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return * the previous value of *p. This can be used to handle reference co...
Definition: lock.h:603
unsigned int isolation
Definition: res_odbc.c:132
static void odbc_obj_destructor(void *data)
Definition: res_odbc.c:418
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define EOR_TX
Definition: res_odbc.c:1214
ODBC container.
Definition: res_odbc.h:46
#define ao2_ref(o, delta)
Definition: astobj2.h:472
unsigned int used
Definition: res_odbc.h:56
unsigned int up
Definition: res_odbc.h:57
struct odbc_class * parent
Definition: res_odbc.h:49
static odbc_status odbc_obj_connect(struct odbc_obj *obj)
Definition: res_odbc.c:1524
#define NO_TX
Definition: res_odbc.c:1213
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:430
static const char name[]
int ast_odbc_sanity_check(struct odbc_obj *obj)
Checks an ODBC object to ensure it is still connected.
Definition: res_odbc.c:735
ast_mutex_t lock
Definition: res_odbc.h:47
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
struct timeval last_used
Definition: res_odbc.h:50
#define ast_mutex_init(pmutex)
Definition: lock.h:152
struct timeval last_negative_connect
Definition: res_odbc.c:140
static struct ao2_container * class_container
Definition: res_odbc.c:145
#define ast_mutex_unlock(a)
Definition: lock.h:156
static int aoro2_obj_notx_cb(void *vobj, void *arg, int flags)
Definition: res_odbc.c:1232
int ast_odbc_allow_empty_string_in_nontext ( struct odbc_obj obj)

Checks if the database natively supports implicit conversion from an empty string to a number (0).

Parameters
objThe ODBC object
Returns
Returns 1 if the implicit conversion is valid and non-text columns can take empty strings, 0 if the conversion is not valid and NULLs should be used instead '\'

Definition at line 1123 of file res_odbc.c.

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

Referenced by update_odbc().

1124 {
1125  return obj->parent->allow_empty_strings;
1126 }
struct odbc_class * parent
Definition: res_odbc.h:49
unsigned int allow_empty_strings
Definition: res_odbc.c:131
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 718 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().

719 {
720  SQLRETURN res;
721 
722  if (pmaxlen == 0) {
723  if (SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), 0, StrLen_or_Ind) == SQL_SUCCESS_WITH_INFO) {
724  ast_str_make_space(buf, *StrLen_or_Ind + 1);
725  }
726  } else if (pmaxlen > 0) {
727  ast_str_make_space(buf, pmaxlen);
728  }
729  res = SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), ast_str_size(*buf), StrLen_or_Ind);
730  ast_str_update(*buf);
731 
732  return res;
733 }
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:482
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
int ast_str_make_space(struct ast_str **buf, size_t new_len)
Definition: strings.h:588
void ast_str_update(struct ast_str *buf)
Update the length of the buffer, after using ast_str merely as a buffer.
Definition: strings.h:446
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 1118 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().

1119 {
1120  return obj->parent->backslash_is_escape;
1121 }
struct odbc_class * parent
Definition: res_odbc.h:49
unsigned int backslash_is_escape
Definition: res_odbc.c:129
int ast_odbc_clear_cache ( const char *  database,
const char *  tablename 
)

Remove a cache entry from memory This function may be called to clear entries created and cached by the ast_odbc_find_table() API call.

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 577 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(), and odbc_cache_tables::table.

Referenced by unload_odbc().

578 {
579  struct odbc_cache_tables *tableptr;
580 
582  AST_RWLIST_TRAVERSE_SAFE_BEGIN(&odbc_tables, tableptr, list) {
583  if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) {
585  destroy_table_cache(tableptr);
586  break;
587  }
588  }
591  return tableptr ? 0 : -1;
592 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
char * connection
Definition: res_odbc.h:76
static void destroy_table_cache(struct odbc_cache_tables *table)
Definition: res_odbc.c:428
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:554
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:542
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:602
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 594 of file res_odbc.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_odbc_sanity_check(), odbc_class::dsn, odbc_obj::lock, 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().

595 {
596  int attempt;
597  SQLHSTMT stmt;
598 
599  ast_mutex_lock(&obj->lock);
600 
601  for (attempt = 0; attempt < 2; attempt++) {
602  stmt = exec_cb(obj, data);
603 
604  if (stmt) {
605  break;
606  } else if (obj->tx) {
607  ast_log(LOG_WARNING, "Failed to execute, but unable to reconnect, as we're transactional.\n");
608  break;
609  } else if (attempt == 0) {
610  ast_log(LOG_WARNING, "SQL Execute error! Verifying connection to %s [%s]...\n", obj->parent->name, obj->parent->dsn);
611  }
612  if (!ast_odbc_sanity_check(obj)) {
613  break;
614  }
615  }
616 
617  ast_mutex_unlock(&obj->lock);
618 
619  return stmt;
620 }
char name[80]
Definition: res_odbc.c:121
#define LOG_WARNING
Definition: logger.h:144
#define ast_mutex_lock(a)
Definition: lock.h:155
unsigned int tx
Definition: res_odbc.h:58
struct odbc_class * parent
Definition: res_odbc.h:49
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int ast_odbc_sanity_check(struct odbc_obj *obj)
Checks an ODBC object to ensure it is still connected.
Definition: res_odbc.c:735
ast_mutex_t lock
Definition: res_odbc.h:47
char dsn[80]
Definition: res_odbc.c:122
#define ast_mutex_unlock(a)
Definition: lock.h:156
struct odbc_cache_columns* ast_odbc_find_column ( struct odbc_cache_tables table,
const char *  colname 
)

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 566 of file res_odbc.c.

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

Referenced by update2_prepare(), and update_odbc().

567 {
568  struct odbc_cache_columns *col;
569  AST_RWLIST_TRAVERSE(&table->columns, col, list) {
570  if (strcasecmp(col->name, colname) == 0) {
571  return col;
572  }
573  }
574  return NULL;
575 }
These structures are used for adaptive capabilities.
Definition: res_odbc.h:64
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct odbc_cache_tables::_columns columns
struct odbc_cache_tables* ast_odbc_find_table ( const char *  database,
const char *  tablename 
)

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. The information will be cached until a reload event or when ast_odbc_clear_cache() is called with the relevant parameters.
Since
1.6.1
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 449 of file res_odbc.c.

References ast_calloc, AST_LIST_INSERT_TAIL, ast_log(), ast_mutex_lock, ast_mutex_unlock, 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_obj::lock, 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().

450 {
451  struct odbc_cache_tables *tableptr;
452  struct odbc_cache_columns *entry;
453  char columnname[80];
454  SQLLEN sqlptr;
455  SQLHSTMT stmt = NULL;
456  int res = 0, error = 0, try = 0;
457  struct odbc_obj *obj = ast_odbc_request_obj(database, 0);
458 
460  AST_RWLIST_TRAVERSE(&odbc_tables, tableptr, list) {
461  if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) {
462  break;
463  }
464  }
465  if (tableptr) {
466  AST_RWLIST_RDLOCK(&tableptr->columns);
468  if (obj) {
470  }
471  return tableptr;
472  }
473 
474  if (!obj) {
475  ast_log(LOG_WARNING, "Unable to retrieve database handle for table description '%s@%s'\n", tablename, database);
477  return NULL;
478  }
479 
480  /* Table structure not already cached; build it now. */
481  ast_mutex_lock(&obj->lock);
482  do {
483  res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
484  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
485  if (try == 0) {
486  try = 1;
488  continue;
489  }
490  ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", database);
491  break;
492  }
493 
494  res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)tablename, SQL_NTS, (unsigned char *)"%", SQL_NTS);
495  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
496  if (try == 0) {
497  try = 1;
498  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
500  continue;
501  }
502  ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'.\n", database);
503  break;
504  }
505 
506  if (!(tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + strlen(database) + 1 + strlen(tablename) + 1))) {
507  ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", tablename, database);
508  break;
509  }
510 
511  tableptr->connection = (char *)tableptr + sizeof(*tableptr);
512  tableptr->table = (char *)tableptr + sizeof(*tableptr) + strlen(database) + 1;
513  strcpy(tableptr->connection, database); /* SAFE */
514  strcpy(tableptr->table, tablename); /* SAFE */
515  AST_RWLIST_HEAD_INIT(&(tableptr->columns));
516 
517  while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
518  SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
519 
520  if (!(entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1))) {
521  ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, tablename, database);
522  error = 1;
523  break;
524  }
525  entry->name = (char *)entry + sizeof(*entry);
526  strcpy(entry->name, columnname);
527 
528  SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
529  SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
530  SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
531  SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
532  SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
533  SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
534 
535  /* Specification states that the octenlen should be the maximum number of bytes
536  * returned in a char or binary column, but it seems that some drivers just set
537  * it to NULL. (Bad Postgres! No biscuit!) */
538  if (entry->octetlen == 0) {
539  entry->octetlen = entry->size;
540  }
541 
542  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);
543  /* Insert column info into column list */
544  AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
545  }
546  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
547 
549  AST_RWLIST_RDLOCK(&(tableptr->columns));
550  break;
551  } while (1);
552  ast_mutex_unlock(&obj->lock);
553 
555 
556  if (error) {
557  destroy_table_cache(tableptr);
558  tableptr = NULL;
559  }
560  if (obj) {
562  }
563  return tableptr;
564 }
SQLHDBC con
Definition: res_odbc.h:48
#define LOG_WARNING
Definition: logger.h:144
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
SQLSMALLINT nullable
Definition: res_odbc.h:70
char * connection
Definition: res_odbc.h:76
#define ast_mutex_lock(a)
Definition: lock.h:155
#define AST_RWLIST_HEAD_INIT(head)
Initializes an rwlist head structure.
Definition: linkedlists.h:624
static void destroy_table_cache(struct odbc_cache_tables *table)
Definition: res_odbc.c:428
These structures are used for adaptive capabilities.
Definition: res_odbc.h:64
#define ast_verb(level,...)
Definition: logger.h:243
SQLINTEGER octetlen
Definition: res_odbc.h:71
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
ODBC container.
Definition: res_odbc.h:46
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct odbc_obj::@207 list
#define LOG_ERROR
Definition: logger.h:155
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:716
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
SQLINTEGER size
Definition: res_odbc.h:67
#define ast_odbc_request_obj(a, b)
Definition: res_odbc.h:123
int ast_odbc_sanity_check(struct odbc_obj *obj)
Checks an ODBC object to ensure it is still connected.
Definition: res_odbc.c:735
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:726
ast_mutex_t lock
Definition: res_odbc.h:47
#define ast_calloc(a, b)
Definition: astmm.h:82
SQLSMALLINT decimals
Definition: res_odbc.h:68
SQLSMALLINT radix
Definition: res_odbc.h:69
SQLSMALLINT type
Definition: res_odbc.h:66
void ast_odbc_release_obj(struct odbc_obj *obj)
Releases an ODBC object previously allocated by ast_odbc_request_obj()
Definition: res_odbc.c:1112
#define ast_mutex_unlock(a)
Definition: lock.h:156
struct odbc_cache_tables::_columns columns
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 622 of file res_odbc.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_odbc_sanity_check(), ast_tvnow(), odbc_class::dsn, odbc_obj::last_used, odbc_obj::lock, 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().

623 {
624  int res = 0, i, attempt;
625  SQLINTEGER nativeerror=0, numfields=0;
626  SQLSMALLINT diagbytes=0;
627  unsigned char state[10], diagnostic[256];
628  SQLHSTMT stmt;
629 
630  ast_mutex_lock(&obj->lock);
631 
632  for (attempt = 0; attempt < 2; attempt++) {
633  /* This prepare callback may do more than just prepare -- it may also
634  * bind parameters, bind results, etc. The real key, here, is that
635  * when we disconnect, all handles become invalid for most databases.
636  * We must therefore redo everything when we establish a new
637  * connection. */
638  stmt = prepare_cb(obj, data);
639 
640  if (stmt) {
641  res = SQLExecute(stmt);
642  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
643  if (res == SQL_ERROR) {
644  SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
645  for (i = 0; i < numfields; i++) {
646  SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
647  ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
648  if (i > 10) {
649  ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
650  break;
651  }
652  }
653  }
654 
655  if (obj->tx) {
656  ast_log(LOG_WARNING, "SQL Execute error, but unable to reconnect, as we're transactional.\n");
657  break;
658  } else {
659  ast_log(LOG_WARNING, "SQL Execute error %d! Verifying connection to %s [%s]...\n", res, obj->parent->name, obj->parent->dsn);
660  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
661  stmt = NULL;
662 
663  obj->up = 0;
664  /*
665  * While this isn't the best way to try to correct an error, this won't automatically
666  * fail when the statement handle invalidates.
667  */
668  if (!ast_odbc_sanity_check(obj)) {
669  break;
670  }
671  continue;
672  }
673  } else {
674  obj->last_used = ast_tvnow();
675  }
676  break;
677  } else if (attempt == 0) {
679  }
680  }
681 
682  ast_mutex_unlock(&obj->lock);
683 
684  return stmt;
685 }
char name[80]
Definition: res_odbc.c:121
#define LOG_WARNING
Definition: logger.h:144
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
#define ast_mutex_lock(a)
Definition: lock.h:155
unsigned int tx
Definition: res_odbc.h:58
unsigned int up
Definition: res_odbc.h:57
struct odbc_class * parent
Definition: res_odbc.h:49
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int ast_odbc_sanity_check(struct odbc_obj *obj)
Checks an ODBC object to ensure it is still connected.
Definition: res_odbc.c:735
ast_mutex_t lock
Definition: res_odbc.h:47
struct timeval last_used
Definition: res_odbc.h:50
char dsn[80]
Definition: res_odbc.c:122
#define ast_mutex_unlock(a)
Definition: lock.h:156
void ast_odbc_release_obj ( struct odbc_obj obj)

Releases an ODBC object previously allocated by ast_odbc_request_obj()

Parameters
objThe ODBC object

Definition at line 1112 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().

1113 {
1114  struct odbc_txn_frame *tx = find_transaction(NULL, obj, NULL, 0);
1115  odbc_release_obj2(obj, tx);
1116 }
static void odbc_release_obj2(struct odbc_obj *obj, struct odbc_txn_frame *tx)
Definition: res_odbc.c:1053
static struct odbc_txn_frame * find_transaction(struct ast_channel *chan, struct odbc_obj *obj, const char *name, int active)
Definition: res_odbc.c:224
struct odbc_obj* ast_odbc_retrieve_transaction_obj ( struct ast_channel chan,
const char *  objname 
)

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 1456 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::name, odbc_txn_frame::obj, odbc_obj::parent, and txn_info.

Referenced by acf_odbc_write().

1457 {
1458  struct ast_datastore *txn_store;
1459  AST_LIST_HEAD(, odbc_txn_frame) *oldlist;
1460  struct odbc_txn_frame *txn = NULL;
1461 
1462  if (!chan) {
1463  /* No channel == no transaction */
1464  return NULL;
1465  }
1466 
1467  ast_channel_lock(chan);
1468  if ((txn_store = ast_channel_datastore_find(chan, &txn_info, NULL))) {
1469  oldlist = txn_store->data;
1470  } else {
1471  ast_channel_unlock(chan);
1472  return NULL;
1473  }
1474 
1475  AST_LIST_LOCK(oldlist);
1476  ast_channel_unlock(chan);
1477 
1478  AST_LIST_TRAVERSE(oldlist, txn, list) {
1479  if (txn->obj && txn->obj->parent && !strcmp(txn->obj->parent->name, objname)) {
1480  AST_LIST_UNLOCK(oldlist);
1481  return txn->obj;
1482  }
1483  }
1484  AST_LIST_UNLOCK(oldlist);
1485  return NULL;
1486 }
#define ast_channel_lock(chan)
Definition: channel.h:2466
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:39
#define AST_LIST_HEAD(name, type)
Defines a structure to be used to hold a list of specified type.
Definition: linkedlists.h:172
char name[80]
Definition: res_odbc.c:121
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:139
static struct ast_datastore_info txn_info
Definition: res_odbc.c:157
Structure for a data store object.
Definition: datastore.h:54
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2604
struct odbc_txn_frame::@338 list
struct odbc_class * parent
Definition: res_odbc.h:49
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
#define ast_channel_unlock(chan)
Definition: channel.h:2467
void * data
Definition: datastore.h:56
struct odbc_obj * obj
Definition: res_odbc.c:165
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 735 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_request_obj2(), ast_odbc_direct_execute(), ast_odbc_find_table(), ast_odbc_prepare_and_execute(), data_odbc_provider_handler(), and handle_cli_odbc_show().

736 {
737  char *test_sql = "select 1";
738  SQLHSTMT stmt;
739  int res = 0;
740 
741  if (!ast_strlen_zero(obj->parent->sanitysql))
742  test_sql = obj->parent->sanitysql;
743 
744  if (obj->up) {
745  res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
746  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
747  obj->up = 0;
748  } else {
749  res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS);
750  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
751  obj->up = 0;
752  } else {
753  res = SQLExecute(stmt);
754  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
755  obj->up = 0;
756  }
757  }
758  }
759  SQLFreeHandle (SQL_HANDLE_STMT, stmt);
760  }
761 
762  if (!obj->up && !obj->tx) { /* Try to reconnect! */
763  ast_log(LOG_WARNING, "Connection is down attempting to reconnect...\n");
764  odbc_obj_disconnect(obj);
765  odbc_obj_connect(obj);
766  }
767  return obj->up;
768 }
SQLHDBC con
Definition: res_odbc.h:48
#define LOG_WARNING
Definition: logger.h:144
unsigned int tx
Definition: res_odbc.h:58
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
unsigned int up
Definition: res_odbc.h:57
struct odbc_class * parent
Definition: res_odbc.h:49
static odbc_status odbc_obj_connect(struct odbc_obj *obj)
Definition: res_odbc.c:1524
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
Definition: res_odbc.c:1488
char * sanitysql
Definition: res_odbc.c:125
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 687 of file res_odbc.c.

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

688 {
689  int res = 0, i;
690  SQLINTEGER nativeerror=0, numfields=0;
691  SQLSMALLINT diagbytes=0;
692  unsigned char state[10], diagnostic[256];
693 
694  ast_mutex_lock(&obj->lock);
695 
696  res = SQLExecute(stmt);
697  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
698  if (res == SQL_ERROR) {
699  SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
700  for (i = 0; i < numfields; i++) {
701  SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
702  ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
703  if (i > 10) {
704  ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
705  break;
706  }
707  }
708  }
709  } else {
710  obj->last_used = ast_tvnow();
711  }
712 
713  ast_mutex_unlock(&obj->lock);
714 
715  return res;
716 }
#define LOG_WARNING
Definition: logger.h:144
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
#define ast_mutex_lock(a)
Definition: lock.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
ast_mutex_t lock
Definition: res_odbc.h:47
struct timeval last_used
Definition: res_odbc.h:50
#define ast_mutex_unlock(a)
Definition: lock.h:156