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_columns * | ast_odbc_find_column (struct odbc_cache_tables *table, const char *colname) |
Find a column entry within a cached table structure. More... | |
struct odbc_cache_tables * | ast_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_obj * | ast_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... | |
ODBC resource manager.
Definition in file res_odbc.h.
#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, | |
b | |||
) | _ast_odbc_request_obj(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__) |
Definition at line 123 of file res_odbc.h.
Referenced by acf_odbc_read(), acf_odbc_write(), ast_odbc_find_table(), cli_odbc_read(), cli_odbc_write(), load_config(), odbc_log(), odbc_register_class(), and update2_odbc().
#define ast_odbc_request_obj2 | ( | a, | |
b | |||
) | _ast_odbc_request_obj2(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__) |
Definition at line 122 of file res_odbc.h.
Referenced by acf_transaction_write(), config_odbc(), destroy_odbc(), realtime_multi_odbc(), realtime_odbc(), store_odbc(), and update_odbc().
anonymous enum |
Flags for use with.
Enumerator | |
---|---|
RES_ODBC_SANITY_CHECK | |
RES_ODBC_INDEPENDENT_CONNECTION | |
RES_ODBC_CONNECTED |
Definition at line 39 of file res_odbc.h.
enum odbc_status |
Enumerator | |
---|---|
ODBC_SUCCESS | |
ODBC_FAIL |
Definition at line 36 of file res_odbc.h.
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.
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.
name | The name of the ODBC class for which a connection is needed. |
flags | One or more of the following flags:
|
NULL | if 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().
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).
obj | The ODBC object |
Definition at line 1123 of file res_odbc.c.
References odbc_class::allow_empty_strings, and odbc_obj::parent.
Referenced by update_odbc().
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.
buf | Address of the pointer to the ast_str structure. |
pmaxlen | The maximum size of the resulting string, or 0 for no limit. |
StatementHandle | The statement handle from which to retrieve data. |
ColumnNumber | Column number (1-based offset) for which to retrieve data. |
TargetType | The SQL constant indicating what kind of data is to be retrieved (usually SQL_CHAR) |
StrLen_or_Ind | A 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().
int ast_odbc_backslash_is_escape | ( | struct odbc_obj * | obj | ) |
Checks if the database natively supports backslash as an escape character.
obj | The ODBC object |
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().
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.
database | Name of an ODBC class (used to ensure like-named tables in different databases are not confused) |
tablename | Tablename for which a cached record should be removed |
0 | if the cache entry was removed, or -1 if no matching entry was found. |
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().
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.
obj | The ODBC object |
exec_cb | A function callback, which, when called, should return a statement handle with result columns bound. |
data | A parameter to be passed to the exec_cb parameter function, indicating which statement handle is to be prepared. |
a | statement handle |
NULL | on 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().
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.
table | Cached table structure, as returned from ast_odbc_find_table() |
colname | The column name requested |
A | structure describing the column type, or NULL, if the column is not found. |
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().
struct odbc_cache_tables* ast_odbc_find_table | ( | const char * | database, |
const char * | tablename | ||
) |
Find or create an entry describing the table specified.
database | Name of an ODBC class on which to query the table |
tablename | Tablename to describe |
A | structure 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. |
database | Name of an ODBC class on which to query the table |
tablename | Tablename to describe |
A | structure 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. |
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().
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.
obj | The ODBC object |
prepare_cb | A function callback, which, when called, should return a statement handle prepared, with any necessary parameters or result columns bound. |
data | A parameter to be passed to the prepare_cb parameter function, indicating which statement handle is to be prepared. |
a | statement handle |
NULL | on 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().
void ast_odbc_release_obj | ( | struct odbc_obj * | obj | ) |
Releases an ODBC object previously allocated by ast_odbc_request_obj()
obj | The 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().
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.
chan | Channel associated with the transaction. |
objname | Name of the database handle. This name corresponds to the name passed to |
A | stored ODBC object, if a transaction was already started. |
NULL,if | no 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().
int ast_odbc_sanity_check | ( | struct odbc_obj * | obj | ) |
Checks an ODBC object to ensure it is still connected.
obj | The ODBC object |
0 | if connected |
-1 | otherwise. |
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().
int ast_odbc_smart_execute | ( | struct odbc_obj * | obj, |
SQLHSTMT | stmt | ||
) |
Executes a prepared statement handle.
obj | The non-NULL result of odbc_request_obj() |
stmt | The prepared statement handle |
0 | on success |
-1 | on 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.