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 }; 00043 00044 /*! \brief ODBC container */ 00045 struct odbc_obj { 00046 ast_mutex_t lock; 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 struct odbc_cache_columns { 00063 char *name; 00064 SQLSMALLINT type; 00065 SQLINTEGER size; 00066 SQLSMALLINT decimals; 00067 SQLSMALLINT radix; 00068 SQLSMALLINT nullable; 00069 SQLINTEGER octetlen; 00070 AST_RWLIST_ENTRY(odbc_cache_columns) list; 00071 }; 00072 00073 struct odbc_cache_tables { 00074 char *connection; 00075 char *table; 00076 AST_RWLIST_HEAD(_columns, odbc_cache_columns) columns; 00077 AST_RWLIST_ENTRY(odbc_cache_tables) list; 00078 }; 00079 00080 /* functions */ 00081 00082 /*! 00083 * \brief Executes a prepared statement handle 00084 * \param obj The non-NULL result of odbc_request_obj() 00085 * \param stmt The prepared statement handle 00086 * \retval 0 on success 00087 * \retval -1 on failure 00088 * 00089 * This function was originally designed simply to execute a prepared 00090 * statement handle and to retry if the initial execution failed. 00091 * Unfortunately, it did this by disconnecting and reconnecting the database 00092 * handle which on most databases causes the statement handle to become 00093 * invalid. Therefore, this method has been deprecated in favor of 00094 * odbc_prepare_and_execute() which allows the statement to be prepared 00095 * multiple times, if necessary, in case of a loss of connection. 00096 * 00097 * This function really only ever worked with MySQL, where the statement handle is 00098 * not prepared on the server. If you are not using MySQL, you should avoid it. 00099 */ 00100 int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt) __attribute__((deprecated)); 00101 00102 /*! 00103 * \brief Retrieves a connected ODBC object 00104 * \param name The name of the ODBC class for which a connection is needed. 00105 * \param flags Set of flags used to control which connection is returned. 00106 * \retval ODBC object 00107 * \retval NULL if there is no connection available with the requested name. 00108 * 00109 * Connection classes may, in fact, contain multiple connection handles. If 00110 * the connection is pooled, then each connection will be dedicated to the 00111 * thread which requests it. Note that all connections should be released 00112 * when the thread is done by calling odbc_release_obj(), below. 00113 */ 00114 #ifdef DEBUG_THREADS 00115 struct odbc_obj *_ast_odbc_request_obj2(const char *name, struct ast_flags flags, const char *file, const char *function, int lineno); 00116 #define ast_odbc_request_obj2(a, b) _ast_odbc_request_obj2(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__) 00117 #else 00118 struct odbc_obj *ast_odbc_request_obj2(const char *name, struct ast_flags flags); 00119 #endif 00120 00121 #ifdef DEBUG_THREADS 00122 struct odbc_obj *_ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno); 00123 #define ast_odbc_request_obj(a, b) _ast_odbc_request_obj(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__) 00124 #else 00125 struct odbc_obj *ast_odbc_request_obj(const char *name, int check); 00126 #endif 00127 00128 /*! 00129 * \brief Retrieve a stored ODBC object, if a transaction has been started. 00130 * \param chan Channel associated with the transaction. 00131 * \param objname Name of the database handle. This name corresponds to the name passed 00132 * to \see ast_odbc_request_obj2 (or formerly, to ast_odbc_request_obj). Note that the 00133 * existence of this parameter name explicitly allows for multiple transactions to be open 00134 * at once, albeit to different databases. 00135 * \retval A stored ODBC object, if a transaction was already started. 00136 * \retval NULL, if no transaction yet exists. 00137 */ 00138 struct odbc_obj *ast_odbc_retrieve_transaction_obj(struct ast_channel *chan, const char *objname); 00139 00140 /*! 00141 * \brief Releases an ODBC object previously allocated by odbc_request_obj() 00142 * \param obj The ODBC object 00143 */ 00144 void ast_odbc_release_obj(struct odbc_obj *obj); 00145 00146 /*! 00147 * \brief Checks an ODBC object to ensure it is still connected 00148 * \param obj The ODBC object 00149 * \retval 0 if connected 00150 * \retval -1 otherwise. 00151 */ 00152 int ast_odbc_sanity_check(struct odbc_obj *obj); 00153 00154 /*! \brief Checks if the database natively supports backslash as an escape character. 00155 * \param obj The ODBC object 00156 * \return Returns 1 if backslash is a native escape character, 0 if an ESCAPE clause is needed to support '\' 00157 */ 00158 int ast_odbc_backslash_is_escape(struct odbc_obj *obj); 00159 00160 /*! \brief Executes an non prepared statement and returns the resulting 00161 * statement handle. 00162 * \param obj The ODBC object 00163 * \param exec_cb A function callback, which, when called, should return a statement handle with result columns bound. 00164 * \param data A parameter to be passed to the exec_cb parameter function, indicating which statement handle is to be prepared. 00165 * \retval a statement handle 00166 * \retval NULL on error 00167 */ 00168 SQLHSTMT ast_odbc_direct_execute(struct odbc_obj *obj, SQLHSTMT (*exec_cb)(struct odbc_obj *obj, void *data), void *data); 00169 00170 /*! 00171 * \brief Prepares, executes, and returns the resulting statement handle. 00172 * \param obj The ODBC object 00173 * \param prepare_cb A function callback, which, when called, should return a statement handle prepared, with any necessary parameters or result columns bound. 00174 * \param data A parameter to be passed to the prepare_cb parameter function, indicating which statement handle is to be prepared. 00175 * \retval a statement handle 00176 * \retval NULL on error 00177 */ 00178 SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data); 00179 00180 /*! 00181 * \brief Find or create an entry describing the table specified. 00182 * \param database Name of an ODBC class on which to query the table 00183 * \param tablename Tablename to describe 00184 * \retval A structure describing the table layout, or NULL, if the table is not found or another error occurs. 00185 * When a structure is returned, the contained columns list will be 00186 * rdlock'ed, to ensure that it will be retained in memory. 00187 * \since 1.6.1 00188 */ 00189 struct odbc_cache_tables *ast_odbc_find_table(const char *database, const char *tablename); 00190 00191 /*! 00192 * \brief Find a column entry within a cached table structure 00193 * \param table Cached table structure, as returned from ast_odbc_find_table() 00194 * \param colname The column name requested 00195 * \retval A structure describing the column type, or NULL, if the column is not found. 00196 * \since 1.6.1 00197 */ 00198 struct odbc_cache_columns *ast_odbc_find_column(struct odbc_cache_tables *table, const char *colname); 00199 00200 /*! 00201 * \brief Remove a cache entry from memory 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 #endif /* _ASTERISK_RES_ODBC_H */