Sat Apr 26 2014 22:01:41

Asterisk developer's documentation


res_odbc.h
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  * Copyright (C) 2004 - 2005, Anthony Minessale II
00006  * Copyright (C) 2006, Tilghman Lesher
00007  *
00008  * Mark Spencer <markster@digium.com>
00009  * Anthony Minessale <anthmct@yahoo.com>
00010  * Tilghman Lesher <res_odbc_200603@the-tilghman.com>
00011  *
00012  * See http://www.asterisk.org for more information about
00013  * the Asterisk project. Please do not directly contact
00014  * any of the maintainers of this project for assistance;
00015  * the project provides a web site, mailing lists and IRC
00016  * channels for your use.
00017  *
00018  * This program is free software, distributed under the terms of
00019  * the GNU General Public License Version 2. See the LICENSE file
00020  * at the top of the source tree.
00021  */
00022 
00023 /*! \file
00024  * \brief ODBC resource manager
00025  */
00026 
00027 #ifndef _ASTERISK_RES_ODBC_H
00028 #define _ASTERISK_RES_ODBC_H
00029 
00030 #include <sql.h>
00031 #include <sqlext.h>
00032 #include <sqltypes.h>
00033 #include "asterisk/linkedlists.h"
00034 #include "asterisk/strings.h"
00035 
00036 typedef enum { ODBC_SUCCESS=0, ODBC_FAIL=-1} odbc_status;
00037 
00038 /*! \brief Flags for use with \see ast_odbc_request_obj2 */
00039 enum {
00040    RES_ODBC_SANITY_CHECK = (1 << 0),
00041    RES_ODBC_INDEPENDENT_CONNECTION = (1 << 1),
00042    RES_ODBC_CONNECTED = (1 << 2),
00043 };
00044 
00045 /*! \brief ODBC container */
00046 struct odbc_obj {
00047    SQLHDBC  con;                   /*!< ODBC Connection Handle */
00048    struct odbc_class *parent;      /*!< Information about the connection is protected */
00049    struct timeval last_used;       /*!< Used by idlecheck to determine if the connection should be renegotiated */
00050 #ifdef DEBUG_THREADS
00051    char file[80];
00052    char function[80];
00053    int lineno;
00054 #endif
00055    unsigned int used:1;            /*!< Is this connection currently in use? */
00056    unsigned int up:1;
00057    unsigned int tx:1;              /*!< Should this connection be unshared, regardless of the class setting? */
00058    struct odbc_txn_frame *txf;     /*!< Reference back to the transaction frame, if applicable */
00059    AST_LIST_ENTRY(odbc_obj) list;
00060 };
00061 
00062 /*!\brief These structures are used for adaptive capabilities */
00063 struct odbc_cache_columns {
00064    char *name;
00065    SQLSMALLINT type;
00066    SQLINTEGER size;
00067    SQLSMALLINT decimals;
00068    SQLSMALLINT radix;
00069    SQLSMALLINT nullable;
00070    SQLINTEGER octetlen;
00071    AST_RWLIST_ENTRY(odbc_cache_columns) list;
00072 };
00073 
00074 struct odbc_cache_tables {
00075    char *connection;
00076    char *table;
00077    AST_RWLIST_HEAD(_columns, odbc_cache_columns) columns;
00078    AST_RWLIST_ENTRY(odbc_cache_tables) list;
00079 };
00080 
00081 /* functions */
00082 
00083 /*! 
00084  * \brief Executes a prepared statement handle
00085  * \param obj The non-NULL result of odbc_request_obj()
00086  * \param stmt The prepared statement handle
00087  * \retval 0 on success
00088  * \retval -1 on failure
00089  *
00090  * This function was originally designed simply to execute a prepared
00091  * statement handle and to retry if the initial execution failed.
00092  * Unfortunately, it did this by disconnecting and reconnecting the database
00093  * handle which on most databases causes the statement handle to become
00094  * invalid.  Therefore, this method has been deprecated in favor of
00095  * odbc_prepare_and_execute() which allows the statement to be prepared
00096  * multiple times, if necessary, in case of a loss of connection.
00097  *
00098  * This function really only ever worked with MySQL, where the statement handle is
00099  * not prepared on the server.  If you are not using MySQL, you should avoid it.
00100  */
00101 int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt) __attribute__((deprecated));
00102 
00103 /*!
00104  * \brief Retrieves a connected ODBC object
00105  * \param name The name of the ODBC class for which a connection is needed.
00106  * \param flags One or more of the following flags:
00107  * \li RES_ODBC_SANITY_CHECK Whether to ensure that a connection is valid before returning the handle.  Usually unnecessary.
00108  * \li RES_ODBC_INDEPENDENT_CONNECTION Return a handle which is independent from all others.  Usually used when starting a transaction.
00109  * \li RES_ODBC_CONNECTED Only return a connected handle.  Intended for use with peers which use idlecheck, which are checked periodically for reachability.
00110  * \return ODBC object
00111  * \retval NULL if there is no connection available with the requested name.
00112  *
00113  * Connection classes may, in fact, contain multiple connection handles.  If
00114  * the connection is pooled, then each connection will be dedicated to the
00115  * thread which requests it.  Note that all connections should be released
00116  * when the thread is done by calling ast_odbc_release_obj(), below.
00117  */
00118 struct odbc_obj *_ast_odbc_request_obj2(const char *name, struct ast_flags flags, const char *file, const char *function, int lineno);
00119 struct odbc_obj *_ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno);
00120 
00121 #define ast_odbc_request_obj2(a, b) _ast_odbc_request_obj2(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00122 #define ast_odbc_request_obj(a, b)  _ast_odbc_request_obj(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00123 
00124 /*!
00125  * \brief Retrieve a stored ODBC object, if a transaction has been started.
00126  * \param chan Channel associated with the transaction.
00127  * \param objname Name of the database handle.  This name corresponds to the name passed
00128  * to \see ast_odbc_request_obj2 (or formerly, to ast_odbc_request_obj).  Note that the
00129  * existence of this parameter name explicitly allows for multiple transactions to be open
00130  * at once, albeit to different databases.
00131  * \retval A stored ODBC object, if a transaction was already started.
00132  * \retval NULL, if no transaction yet exists.
00133  */
00134 struct odbc_obj *ast_odbc_retrieve_transaction_obj(struct ast_channel *chan, const char *objname);
00135 
00136 /*! 
00137  * \brief Releases an ODBC object previously allocated by ast_odbc_request_obj()
00138  * \param obj The ODBC object
00139  */
00140 void ast_odbc_release_obj(struct odbc_obj *obj);
00141 
00142 /*! 
00143  * \brief Checks an ODBC object to ensure it is still connected
00144  * \param obj The ODBC object
00145  * \retval 0 if connected
00146  * \retval -1 otherwise.
00147  */
00148 int ast_odbc_sanity_check(struct odbc_obj *obj);
00149 
00150 /*! \brief Checks if the database natively supports backslash as an escape character.
00151  * \param obj The ODBC object
00152  * \return Returns 1 if backslash is a native escape character, 0 if an ESCAPE clause is needed to support '\'
00153  */
00154 int ast_odbc_backslash_is_escape(struct odbc_obj *obj);
00155 
00156 /*! \brief Executes an non prepared statement and returns the resulting
00157  * statement handle.
00158  * \param obj The ODBC object
00159  * \param exec_cb A function callback, which, when called, should return a statement handle with result columns bound.
00160  * \param data A parameter to be passed to the exec_cb parameter function, indicating which statement handle is to be prepared.
00161  * \retval a statement handle
00162  * \retval NULL on error
00163  */
00164 SQLHSTMT ast_odbc_direct_execute(struct odbc_obj *obj, SQLHSTMT (*exec_cb)(struct odbc_obj *obj, void *data), void *data);
00165 
00166 /*! 
00167  * \brief Prepares, executes, and returns the resulting statement handle.
00168  * \param obj The ODBC object
00169  * \param prepare_cb A function callback, which, when called, should return a statement handle prepared, with any necessary parameters or result columns bound.
00170  * \param data A parameter to be passed to the prepare_cb parameter function, indicating which statement handle is to be prepared.
00171  * \retval a statement handle 
00172  * \retval NULL on error
00173  */
00174 SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data);
00175 
00176 /*!
00177  * \brief Find or create an entry describing the table specified.
00178  * \param database Name of an ODBC class on which to query the table
00179  * \param tablename Tablename to describe
00180  * \retval A structure describing the table layout, or NULL, if the table is not found or another error occurs.
00181  * When a structure is returned, the contained columns list will be
00182  * rdlock'ed, to ensure that it will be retained in memory.  The information
00183  * will be cached until a reload event or when ast_odbc_clear_cache() is called
00184  * with the relevant parameters.
00185  * \since 1.6.1
00186  */
00187 struct odbc_cache_tables *ast_odbc_find_table(const char *database, const char *tablename);
00188 
00189 /*!
00190  * \brief Find a column entry within a cached table structure
00191  * \param table Cached table structure, as returned from ast_odbc_find_table()
00192  * \param colname The column name requested
00193  * \retval A structure describing the column type, or NULL, if the column is not found.
00194  * \since 1.6.1
00195  */
00196 struct odbc_cache_columns *ast_odbc_find_column(struct odbc_cache_tables *table, const char *colname);
00197 
00198 /*!
00199  * \brief Remove a cache entry from memory
00200  * This function may be called to clear entries created and cached by the
00201  * ast_odbc_find_table() API call.
00202  * \param database Name of an ODBC class (used to ensure like-named tables in different databases are not confused)
00203  * \param tablename Tablename for which a cached record should be removed
00204  * \retval 0 if the cache entry was removed, or -1 if no matching entry was found.
00205  * \since 1.6.1
00206  */
00207 int ast_odbc_clear_cache(const char *database, const char *tablename);
00208 
00209 /*!
00210  * \brief Release a table returned from ast_odbc_find_table
00211  */
00212 #define ast_odbc_release_table(ptr) if (ptr) { AST_RWLIST_UNLOCK(&(ptr)->columns); }
00213 
00214 /*!\brief Wrapper for SQLGetData to use with dynamic strings
00215  * \param buf Address of the pointer to the ast_str structure.
00216  * \param pmaxlen The maximum size of the resulting string, or 0 for no limit.
00217  * \param StatementHandle The statement handle from which to retrieve data.
00218  * \param ColumnNumber Column number (1-based offset) for which to retrieve data.
00219  * \param TargetType The SQL constant indicating what kind of data is to be retrieved (usually SQL_CHAR)
00220  * \param StrLen_or_Ind A pointer to a length indicator, specifying the total length of data.
00221  */
00222 SQLRETURN ast_odbc_ast_str_SQLGetData(struct ast_str **buf, int pmaxlen, SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLLEN *StrLen_or_Ind);
00223 
00224 /*! \brief Checks if the database natively supports implicit conversion from an empty string to a number (0).
00225  * \param obj The ODBC object
00226  * \return 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 '\'
00227  */
00228 int ast_odbc_allow_empty_string_in_nontext(struct odbc_obj *obj);
00229 
00230 #endif /* _ASTERISK_RES_ODBC_H */