Asterisk - The Open Source Telephony Project GIT-master-f36a736
|
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"
Go to the source code of this file.
Data Structures | |
struct | odbc_class |
struct | odbc_tables |
struct | odbc_txn_frame |
Functions | |
static void | __init_errors_buf (void) |
static void | __reg_module (void) |
static void | __unreg_module (void) |
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) |
static int | aoro2_class_cb (void *obj, void *arg, int flags) |
struct ast_module * | AST_MODULE_SELF_SYM (void) |
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... | |
unsigned int | ast_odbc_class_get_forcecommit (struct odbc_class *class) |
Get the transaction forcecommit setting for an ODBC class. More... | |
unsigned int | ast_odbc_class_get_isolation (struct odbc_class *class) |
Get the transaction isolation setting for an ODBC class. More... | |
const char * | ast_odbc_class_get_name (struct odbc_class *class) |
Get the name of an ODBC class. 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... | |
SQLRETURN | ast_odbc_execute_sql (struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql) |
Execute a unprepared SQL query. 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... | |
unsigned int | ast_odbc_get_max_connections (const char *name) |
Return the current configured maximum number of connections for a class. More... | |
const char * | ast_odbc_isolation2text (int iso) |
Convert from numeric transaction isolation values to their textual counterparts. More... | |
int | ast_odbc_prepare (struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql) |
Prepares a SQL query on a statement. 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... | |
struct ast_str * | ast_odbc_print_errors (SQLSMALLINT handle_type, SQLHANDLE handle, const char *operation) |
Shortcut for printing errors to logs after a failed SQL operation. More... | |
void | ast_odbc_release_obj (struct odbc_obj *obj) |
Releases an ODBC object previously allocated by ast_odbc_request_obj() More... | |
int | ast_odbc_smart_execute (struct odbc_obj *obj, SQLHSTMT stmt) |
Executes a prepared statement handle. More... | |
int | ast_odbc_text2isolation (const char *txt) |
Convert from textual transaction isolation values to their numeric constants. More... | |
static int | connection_dead (struct odbc_obj *connection, struct odbc_class *class) |
Determine if the connection has died. More... | |
static void | destroy_table_cache (struct odbc_cache_tables *table) |
static char * | handle_cli_odbc_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static int | load_module (void) |
static int | load_odbc_config (void) |
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 void | odbc_register_class (struct odbc_class *class, int connect) |
static int | reload (void) |
static int | unload_module (void) |
Variables | |
static struct ast_module_info | __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "ODBC resource" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_REALTIME_DEPEND, .requires = "res_odbc_transaction", } |
static const struct ast_module_info * | ast_module_info = &__mod_info |
static struct ao2_container * | class_container |
static struct ast_cli_entry | cli_odbc [] |
static struct ast_threadstorage | errors_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_errors_buf , .custom_init = NULL , } |
static struct odbc_tables | odbc_tables = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , } |
|
static |
Definition at line 113 of file res_odbc.c.
|
static |
Definition at line 1165 of file res_odbc.c.
|
static |
Definition at line 1165 of file res_odbc.c.
struct odbc_obj * _ast_odbc_request_obj | ( | const char * | name, |
int | check, | ||
const char * | file, | ||
const char * | function, | ||
int | lineno | ||
) |
Definition at line 1000 of file res_odbc.c.
References _ast_odbc_request_obj2(), make_ari_stubs::file, ast_flags::flags, name, 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 | ||
) |
Definition at line 908 of file res_odbc.c.
References ao2_alloc, ao2_bump, ao2_callback, ao2_ref, aoro2_class_cb(), ast_cond_signal, ast_cond_wait, ast_debug, AST_LIST_REMOVE_HEAD, ast_mutex_lock, ast_mutex_unlock, class_container, connection_dead(), odbc_class::list, name, NULL, ODBC_FAIL, odbc_obj_connect(), odbc_obj_destructor(), and odbc_obj::parent.
Referenced by _ast_odbc_request_obj().
|
static |
Definition at line 838 of file res_odbc.c.
References CMP_MATCH, CMP_STOP, and name.
Referenced by _ast_odbc_request_obj2(), and ast_odbc_get_max_connections().
struct ast_module * AST_MODULE_SELF_SYM | ( | void | ) |
Definition at line 1165 of file res_odbc.c.
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 498 of file res_odbc.c.
References ast_str_buffer(), ast_str_make_space, ast_str_size(), ast_str_update(), and buf.
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 |
1 | if backslash is a native escape character |
0 | if an ESCAPE clause is needed to support '\' |
Definition at line 833 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().
unsigned int ast_odbc_class_get_forcecommit | ( | struct odbc_class * | class | ) |
Get the transaction forcecommit setting for an ODBC class.
Definition at line 545 of file res_odbc.c.
Referenced by create_transaction().
unsigned int ast_odbc_class_get_isolation | ( | struct odbc_class * | class | ) |
Get the transaction isolation setting for an ODBC class.
Definition at line 540 of file res_odbc.c.
Referenced by create_transaction().
const char * ast_odbc_class_get_name | ( | struct odbc_class * | class | ) |
Get the name of an ODBC class.
Definition at line 550 of file res_odbc.c.
Referenced by ast_odbc_retrieve_transaction_obj().
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. |
-1 | if no matching entry was found. |
Definition at line 343 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. |
NULL | on error |
Definition at line 360 of file res_odbc.c.
References ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, ast_tvdiff_ms(), ast_tvnow(), odbc_class::lock, LOG_WARNING, odbc_class::logging, odbc_class::longest_query_execution_time, odbc_class::name, NULL, odbc_obj::parent, odbc_class::slowquerylimit, odbc_obj::sql_text, and odbc_class::sql_text.
Referenced by acf_odbc_read(), acf_odbc_write(), cli_odbc_read(), cli_odbc_write(), connection_dead(), and odbc_log().
SQLRETURN ast_odbc_execute_sql | ( | struct odbc_obj * | obj, |
SQLHSTMT * | stmt, | ||
const char * | sql | ||
) |
Execute a unprepared SQL query.
obj | The ODBC object |
stmt | The statement |
sql | The SQL query |
Definition at line 469 of file res_odbc.c.
References ast_atomic_fetchadd_int(), ast_free, ast_strdup, odbc_class::logging, odbc_obj::parent, odbc_class::queries_executed, and odbc_obj::sql_text.
Referenced by execute(), and execute_cb().
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 |
Definition at line 332 of file res_odbc.c.
References AST_RWLIST_TRAVERSE, odbc_cache_columns::name, NULL, and table.
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.
XXX This creates a connection and disconnects it. In some situations, the caller of this function has its own connection and could donate it to this function instead of needing to create another one.
XXX The automatic readlock of the columns is awkward. It's done because it's possible for multiple threads to have references to the table, and the table is not refcounted. Possible changes here would be
Definition at line 232 of file res_odbc.c.
References ast_calloc, ast_debug, AST_LIST_INSERT_TAIL, ast_log, ast_odbc_release_obj(), ast_odbc_request_obj, AST_RWLIST_HEAD_INIT, AST_RWLIST_INSERT_TAIL, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, odbc_cache_tables::columns, odbc_obj::con, odbc_cache_tables::connection, destroy_table_cache(), error(), odbc_obj::list, LOG_ERROR, LOG_WARNING, NULL, and odbc_cache_tables::table.
Referenced by require_odbc(), update2_odbc(), and update_odbc().
unsigned int ast_odbc_get_max_connections | ( | const char * | name | ) |
Return the current configured maximum number of connections for a class.
Definition at line 848 of file res_odbc.c.
References ao2_callback, ao2_ref, aoro2_class_cb(), class_container, and name.
Referenced by release_obj_or_dsn().
const char * ast_odbc_isolation2text | ( | int | iso | ) |
Convert from numeric transaction isolation values to their textual counterparts.
Definition at line 132 of file res_odbc.c.
Referenced by acf_transaction_read().
int ast_odbc_prepare | ( | struct odbc_obj * | obj, |
SQLHSTMT * | stmt, | ||
const char * | sql | ||
) |
Prepares a SQL query on a statement.
obj | The ODBC object |
stmt | The statement |
sql | The SQL query |
Definition at line 454 of file res_odbc.c.
References ast_atomic_fetchadd_int(), ast_free, ast_strdup, odbc_class::logging, odbc_obj::parent, odbc_class::prepares_executed, and odbc_obj::sql_text.
Referenced by config_odbc_prepare(), custom_prepare(), generic_prepare(), length_determination_odbc_prepare(), and update2_prepare().
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. |
NULL | on error |
Definition at line 398 of file res_odbc.c.
References ast_atomic_fetchadd_int(), ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, ast_odbc_print_errors(), ast_tvdiff_ms(), ast_tvnow(), odbc_class::lock, LOG_WARNING, odbc_class::logging, odbc_class::longest_query_execution_time, odbc_class::name, NULL, odbc_obj::parent, odbc_class::queries_executed, odbc_class::slowquerylimit, odbc_obj::sql_text, and odbc_class::sql_text.
Referenced by config_odbc(), destroy_odbc(), odbc_log(), realtime_multi_odbc(), realtime_odbc(), store_odbc(), update2_odbc(), and update_odbc().
struct ast_str * ast_odbc_print_errors | ( | SQLSMALLINT | handle_type, |
SQLHANDLE | handle, | ||
const char * | operation | ||
) |
Shortcut for printing errors to logs after a failed SQL operation.
handle_type | The type of SQL handle on which to gather diagnostics |
handle | The SQL handle to gather diagnostics from |
operation | The name of the failed operation. |
Definition at line 515 of file res_odbc.c.
References ast_log, ast_str_append(), ast_str_reset(), ast_str_strlen(), ast_str_thread_get(), errors_buf, and LOG_WARNING.
Referenced by acf_transaction_write(), ast_odbc_prepare_and_execute(), ast_odbc_smart_execute(), commit_exec(), create_transaction(), custom_prepare(), release_transaction(), rollback_exec(), and update2_prepare().
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 804 of file res_odbc.c.
References ao2_ref, ast_cond_signal, ast_debug, ast_free, AST_LIST_INSERT_HEAD, ast_mutex_lock, ast_mutex_unlock, NULL, odbc_obj::parent, and odbc_obj::sql_text.
Referenced by ast_odbc_find_table(), config_odbc(), create_transaction(), destroy_odbc(), dsn_destructor(), get_dsn(), load_config(), odbc_log(), odbc_register_class(), realtime_multi_odbc(), realtime_odbc(), release_obj_or_dsn(), release_transaction(), store_odbc(), update2_odbc(), and update_odbc().
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 480 of file res_odbc.c.
References ast_atomic_fetchadd_int(), ast_odbc_print_errors(), odbc_class::logging, odbc_obj::parent, and odbc_class::queries_executed.
int ast_odbc_text2isolation | ( | const char * | txt | ) |
Convert from textual transaction isolation values to their numeric constants.
Definition at line 147 of file res_odbc.c.
Referenced by acf_transaction_write(), and load_odbc_config().
|
static |
Determine if the connection has died.
connection | The connection to check |
class | The ODBC class |
1 | Yep, it's dead |
0 | It's alive and well |
Definition at line 872 of file res_odbc.c.
References ast_strlen_zero(), and odbc_obj::con.
Referenced by _ast_odbc_request_obj2().
|
static |
Definition at line 201 of file res_odbc.c.
References ast_debug, ast_free, AST_RWLIST_HEAD_DESTROY, AST_RWLIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and table.
Referenced by ast_odbc_clear_cache(), ast_odbc_find_table(), and reload().
|
static |
Definition at line 707 of file res_odbc.c.
References a, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_cli(), ast_localtime(), ast_mutex_lock, ast_mutex_unlock, ast_strdup, ast_strftime(), class_container, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, test_val::name, NULL, and ast_cli_entry::usage.
|
static |
Definition at line 1141 of file res_odbc.c.
References AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_list, ao2_match_by_addr(), ARRAY_LEN, ast_cli_register_multiple, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_module_shutdown_ref, class_container, cli_odbc, load_odbc_config(), NULL, and ast_module_info::self.
|
static |
Definition at line 555 of file res_odbc.c.
References ao2_alloc, ao2_ref, ast_category_browse(), ast_cond_init, ast_config_destroy(), ast_config_load, ast_copy_string(), ast_log, ast_mutex_init, ast_odbc_text2isolation(), ast_strdup, ast_strlen_zero(), ast_true(), ast_variable_browse(), config, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, odbc_class::conntimeout, dsn, enabled, odbc_class::forcecommit, odbc_class::isolation, LOG_ERROR, LOG_NOTICE, LOG_WARNING, odbc_class::logging, odbc_class::maxconnections, ast_variable::name, ast_variable::next, NULL, odbc_class_destructor(), odbc_register_class(), odbc_class::password, odbc_class::sanitysql, setenv(), odbc_class::slowquerylimit, odbc_class::username, and ast_variable::value.
Referenced by load_module(), and reload().
|
static |
Definition at line 166 of file res_odbc.c.
References ao2_ref, ast_cond_destroy, ast_free, AST_LIST_REMOVE_HEAD, ast_mutex_destroy, and odbc_obj::list.
Referenced by load_odbc_config().
|
static |
Definition at line 1038 of file res_odbc.c.
References ast_assert, ast_debug, ast_log, AST_TIME_T_LEN, ast_time_t_to_string(), ast_tvnow(), odbc_obj::con, odbc_class::conntimeout, odbc_class::dsn, odbc_class::env, odbc_class::last_negative_connect, LOG_WARNING, odbc_class::name, odbc_class::negative_connection_cache, NULL, ODBC_FAIL, ODBC_SUCCESS, odbc_obj::parent, odbc_class::password, and odbc_class::username.
Referenced by _ast_odbc_request_obj2().
|
static |
Definition at line 194 of file res_odbc.c.
References odbc_obj_disconnect().
Referenced by _ast_odbc_request_obj2().
|
static |
Definition at line 1011 of file res_odbc.c.
References ast_debug, ast_log, odbc_obj::con, LOG_WARNING, NULL, and ODBC_SUCCESS.
Referenced by odbc_obj_destructor().
|
static |
Definition at line 784 of file res_odbc.c.
References ao2_link, ast_odbc_release_obj(), ast_odbc_request_obj, and class_container.
Referenced by load_odbc_config().
|
static |
Definition at line 1099 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(), load_odbc_config(), and table.
|
static |
Definition at line 1133 of file res_odbc.c.
References ao2_cleanup, ARRAY_LEN, ast_cli_unregister_multiple(), class_container, and cli_odbc.
|
static |
Definition at line 1165 of file res_odbc.c.
|
static |
Definition at line 1165 of file res_odbc.c.
|
static |
Definition at line 105 of file res_odbc.c.
Referenced by _ast_odbc_request_obj2(), ast_odbc_get_max_connections(), handle_cli_odbc_show(), load_module(), odbc_register_class(), reload(), and unload_module().
|
static |
Definition at line 780 of file res_odbc.c.
Referenced by load_module(), and unload_module().
|
static |
Definition at line 113 of file res_odbc.c.
Referenced by ast_odbc_print_errors().
|
static |