Asterisk - The Open Source Telephony Project
18.5.0
|
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) |
Get a ODBC connection object. More... | |
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... | |
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 nonprepared 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_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... | |
int | ast_odbc_text2isolation (const char *txt) |
Convert from textual transaction isolation values to their numeric constants. 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 216 of file res_odbc.h.
Referenced by update2_odbc(), and update_odbc().
#define ast_odbc_request_obj | ( | a, | |
b | |||
) | _ast_odbc_request_obj(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__) |
Definition at line 122 of file res_odbc.h.
Referenced by ast_odbc_find_table(), create_dsn(), create_transaction(), free_zone(), get_dsn(), get_odbc_obj(), 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 121 of file res_odbc.h.
Referenced by 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 | ||
) |
Get a ODBC connection object.
The "check" parameter is leftover from an earlier implementation where database connections were cached by res_odbc. Since connections are managed by unixODBC now, this parameter is only kept around for API compatibility.
name | The name of the res_odbc.conf section describing the database to connect to |
check | unused |
Definition at line 984 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.
This is only around for backwards-compatibility with older versions of Asterisk.
Definition at line 917 of file res_odbc.c.
References ao2_alloc, ao2_bump, ao2_callback, ao2_ref, aoro2_class_cb(), ast_cond_wait, ast_debug, AST_LIST_REMOVE_HEAD, ast_mutex_lock, ast_mutex_unlock, connection_dead(), odbc_class::list, NULL, ODBC_FAIL, odbc_obj_connect(), odbc_obj_destructor(), and odbc_obj::parent.
Referenced by _ast_odbc_request_obj().
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 507 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 842 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 554 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 549 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 559 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, or -1 if no matching entry was found. |
Definition at line 352 of file res_odbc.c.
References AST_LIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, odbc_cache_tables::connection, destroy_table_cache(), odbc_class::list, and odbc_cache_tables::table.
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 369 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 nonprepared SQL query.
obj | The ODBC object |
sql | The SQL query |
Definition at line 478 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 |
A | structure describing the column type, or NULL, if the column is not found. |
Definition at line 341 of file res_odbc.c.
References AST_RWLIST_TRAVERSE, odbc_cache_tables::columns, odbc_class::list, odbc_cache_columns::name, and NULL.
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. |
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 241 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, odbc_cache_columns::decimals, destroy_table_cache(), error(), odbc_class::list, LOG_ERROR, LOG_WARNING, odbc_cache_columns::name, NULL, 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_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 857 of file res_odbc.c.
References ao2_callback, and ao2_ref.
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 463 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. |
a | statement handle |
NULL | on error |
Definition at line 407 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(), free_zone(), 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 524 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 813 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, odbc_class::list, NULL, odbc_obj::parent, and odbc_obj::sql_text.
Referenced by ast_odbc_find_table(), config_odbc(), create_transaction(), destroy_odbc(), dsn_destructor(), free_zone(), 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_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. |
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 489 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().