Asterisk - The Open Source Telephony Project
21.4.1
|
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. | |
unsigned int | ast_odbc_class_get_isolation (struct odbc_class *class) |
Get the transaction isolation setting for an ODBC class. | |
const char * | ast_odbc_class_get_name (struct odbc_class *class) |
Get the name of an ODBC class. | |
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. | |
const char * | ast_odbc_isolation2text (int iso) |
Convert from numeric transaction isolation values to their textual counterparts. | |
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. | |
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 = "da6642af068ee5e6490c5b1d2cc1d238" , .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} } , } |
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_size(), and ast_str_update().
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 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. |
-1 | if no matching entry was found. |
Definition at line 343 of file res_odbc.c.
References AST_LIST_REMOVE_CURRENT, AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.
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_tvdiff_ms(), ast_tvnow(), odbc_class::lock, odbc_class::logging, odbc_class::longest_query_execution_time, odbc_obj::parent, odbc_class::slowquerylimit, odbc_obj::sql_text, and odbc_class::sql_text.
Referenced by acf_odbc_write(), and connection_dead().
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_strdup, odbc_class::logging, odbc_obj::parent, odbc_class::queries_executed, and odbc_obj::sql_text.
Referenced by execute().
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.
Referenced by 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_odbc_release_obj(), ast_odbc_request_obj, AST_RWLIST_HEAD_INIT, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, and odbc_obj::con.
Referenced by update2_odbc(), and update_odbc().
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_strdup, odbc_class::logging, odbc_obj::parent, odbc_class::prepares_executed, and odbc_obj::sql_text.
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_odbc_print_errors(), ast_tvdiff_ms(), ast_tvnow(), odbc_class::lock, odbc_class::logging, odbc_class::longest_query_execution_time, odbc_obj::parent, odbc_class::queries_executed, odbc_class::slowquerylimit, odbc_obj::sql_text, and odbc_class::sql_text.
Referenced by destroy_odbc(), 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_str_append(), ast_str_reset(), ast_str_strlen(), and ast_str_thread_get().
Referenced by ast_odbc_prepare_and_execute(), and ast_odbc_smart_execute().
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_debug, AST_LIST_INSERT_HEAD, odbc_obj::parent, and odbc_obj::sql_text.
Referenced by ast_odbc_find_table(), destroy_odbc(), get_dsn(), realtime_multi_odbc(), realtime_odbc(), release_obj_or_dsn(), 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.
|
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 odbc_obj::con.
|
static |
Definition at line 780 of file res_odbc.c.