res_odbc.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. * Copyright (C) 2004 - 2005, Anthony Minessale II
  6. * Copyright (C) 2006, Tilghman Lesher
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. * Anthony Minessale <anthmct@yahoo.com>
  10. * Tilghman Lesher <res_odbc_200603@the-tilghman.com>
  11. *
  12. * See http://www.asterisk.org for more information about
  13. * the Asterisk project. Please do not directly contact
  14. * any of the maintainers of this project for assistance;
  15. * the project provides a web site, mailing lists and IRC
  16. * channels for your use.
  17. *
  18. * This program is free software, distributed under the terms of
  19. * the GNU General Public License Version 2. See the LICENSE file
  20. * at the top of the source tree.
  21. */
  22. /*! \file
  23. * \brief ODBC resource manager
  24. */
  25. #ifndef _ASTERISK_RES_ODBC_H
  26. #define _ASTERISK_RES_ODBC_H
  27. #include <sql.h>
  28. #include <sqlext.h>
  29. #include <sqltypes.h>
  30. #include "asterisk/linkedlists.h"
  31. #include "asterisk/strings.h"
  32. typedef enum { ODBC_SUCCESS=0, ODBC_FAIL=-1} odbc_status;
  33. /*! \brief Flags for use with \see ast_odbc_request_obj2 */
  34. enum {
  35. RES_ODBC_SANITY_CHECK = (1 << 0),
  36. RES_ODBC_INDEPENDENT_CONNECTION = (1 << 1),
  37. RES_ODBC_CONNECTED = (1 << 2),
  38. };
  39. /*! \brief ODBC container */
  40. struct odbc_obj {
  41. SQLHDBC con; /*!< ODBC Connection Handle */
  42. struct odbc_class *parent; /*!< Information about the connection is protected */
  43. #ifdef DEBUG_THREADS
  44. char file[80];
  45. char function[80];
  46. int lineno;
  47. #endif
  48. AST_LIST_ENTRY(odbc_obj) list;
  49. };
  50. /*!\brief These structures are used for adaptive capabilities */
  51. struct odbc_cache_columns {
  52. char *name;
  53. SQLSMALLINT type;
  54. SQLINTEGER size;
  55. SQLSMALLINT decimals;
  56. SQLSMALLINT radix;
  57. SQLSMALLINT nullable;
  58. SQLINTEGER octetlen;
  59. AST_RWLIST_ENTRY(odbc_cache_columns) list;
  60. };
  61. struct odbc_cache_tables {
  62. char *connection;
  63. char *table;
  64. AST_RWLIST_HEAD(_columns, odbc_cache_columns) columns;
  65. AST_RWLIST_ENTRY(odbc_cache_tables) list;
  66. };
  67. /* functions */
  68. /*!
  69. * \brief Executes a prepared statement handle
  70. * \param obj The non-NULL result of odbc_request_obj()
  71. * \param stmt The prepared statement handle
  72. * \retval 0 on success
  73. * \retval -1 on failure
  74. *
  75. * This function was originally designed simply to execute a prepared
  76. * statement handle and to retry if the initial execution failed.
  77. * Unfortunately, it did this by disconnecting and reconnecting the database
  78. * handle which on most databases causes the statement handle to become
  79. * invalid. Therefore, this method has been deprecated in favor of
  80. * odbc_prepare_and_execute() which allows the statement to be prepared
  81. * multiple times, if necessary, in case of a loss of connection.
  82. *
  83. * This function really only ever worked with MySQL, where the statement handle is
  84. * not prepared on the server. If you are not using MySQL, you should avoid it.
  85. */
  86. int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt) __attribute__((deprecated));
  87. /*!
  88. * \brief Retrieves a connected ODBC object
  89. *
  90. * \deprecated
  91. *
  92. * This is only around for backwards-compatibility with older versions of Asterisk.
  93. */
  94. struct odbc_obj *_ast_odbc_request_obj2(const char *name, struct ast_flags flags, const char *file, const char *function, int lineno);
  95. /*!
  96. * \brief Get a ODBC connection object
  97. *
  98. * The "check" parameter is leftover from an earlier implementation where database connections
  99. * were cached by res_odbc. Since connections are managed by unixODBC now, this parameter is
  100. * only kept around for API compatibility.
  101. *
  102. * \param name The name of the res_odbc.conf section describing the database to connect to
  103. * \param check unused
  104. * \return A connection to the database. Call ast_odbc_release_obj() when finished.
  105. */
  106. struct odbc_obj *_ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno);
  107. #define ast_odbc_request_obj2(a, b) _ast_odbc_request_obj2(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  108. #define ast_odbc_request_obj(a, b) _ast_odbc_request_obj(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  109. /*!
  110. * \brief Releases an ODBC object previously allocated by ast_odbc_request_obj()
  111. * \param obj The ODBC object
  112. */
  113. void ast_odbc_release_obj(struct odbc_obj *obj);
  114. /*!
  115. * \brief Checks an ODBC object to ensure it is still connected
  116. * \param obj The ODBC object
  117. * \retval 0 if connected
  118. * \retval -1 otherwise.
  119. */
  120. int ast_odbc_sanity_check(struct odbc_obj *obj);
  121. /*! \brief Checks if the database natively supports backslash as an escape character.
  122. * \param obj The ODBC object
  123. * \return Returns 1 if backslash is a native escape character, 0 if an ESCAPE clause is needed to support '\'
  124. */
  125. int ast_odbc_backslash_is_escape(struct odbc_obj *obj);
  126. /*! \brief Executes an non prepared statement and returns the resulting
  127. * statement handle.
  128. * \param obj The ODBC object
  129. * \param exec_cb A function callback, which, when called, should return a statement handle with result columns bound.
  130. * \param data A parameter to be passed to the exec_cb parameter function, indicating which statement handle is to be prepared.
  131. * \retval a statement handle
  132. * \retval NULL on error
  133. */
  134. SQLHSTMT ast_odbc_direct_execute(struct odbc_obj *obj, SQLHSTMT (*exec_cb)(struct odbc_obj *obj, void *data), void *data);
  135. /*!
  136. * \brief Prepares, executes, and returns the resulting statement handle.
  137. * \param obj The ODBC object
  138. * \param prepare_cb A function callback, which, when called, should return a statement handle prepared, with any necessary parameters or result columns bound.
  139. * \param data A parameter to be passed to the prepare_cb parameter function, indicating which statement handle is to be prepared.
  140. * \retval a statement handle
  141. * \retval NULL on error
  142. */
  143. SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data);
  144. /*!
  145. * \brief Find or create an entry describing the table specified.
  146. * \param database Name of an ODBC class on which to query the table
  147. * \param tablename Tablename to describe
  148. * \retval A structure describing the table layout, or NULL, if the table is not found or another error occurs.
  149. * When a structure is returned, the contained columns list will be
  150. * rdlock'ed, to ensure that it will be retained in memory. The information
  151. * will be cached until a reload event or when ast_odbc_clear_cache() is called
  152. * with the relevant parameters.
  153. * \since 1.6.1
  154. */
  155. struct odbc_cache_tables *ast_odbc_find_table(const char *database, const char *tablename);
  156. /*!
  157. * \brief Find a column entry within a cached table structure
  158. * \param table Cached table structure, as returned from ast_odbc_find_table()
  159. * \param colname The column name requested
  160. * \retval A structure describing the column type, or NULL, if the column is not found.
  161. * \since 1.6.1
  162. */
  163. struct odbc_cache_columns *ast_odbc_find_column(struct odbc_cache_tables *table, const char *colname);
  164. /*!
  165. * \brief Remove a cache entry from memory
  166. * This function may be called to clear entries created and cached by the
  167. * ast_odbc_find_table() API call.
  168. * \param database Name of an ODBC class (used to ensure like-named tables in different databases are not confused)
  169. * \param tablename Tablename for which a cached record should be removed
  170. * \retval 0 if the cache entry was removed, or -1 if no matching entry was found.
  171. * \since 1.6.1
  172. */
  173. int ast_odbc_clear_cache(const char *database, const char *tablename);
  174. /*!
  175. * \brief Release a table returned from ast_odbc_find_table
  176. */
  177. #define ast_odbc_release_table(ptr) if (ptr) { AST_RWLIST_UNLOCK(&(ptr)->columns); }
  178. /*!\brief Wrapper for SQLGetData to use with dynamic strings
  179. * \param buf Address of the pointer to the ast_str structure.
  180. * \param pmaxlen The maximum size of the resulting string, or 0 for no limit.
  181. * \param StatementHandle The statement handle from which to retrieve data.
  182. * \param ColumnNumber Column number (1-based offset) for which to retrieve data.
  183. * \param TargetType The SQL constant indicating what kind of data is to be retrieved (usually SQL_CHAR)
  184. * \param StrLen_or_Ind A pointer to a length indicator, specifying the total length of data.
  185. */
  186. SQLRETURN ast_odbc_ast_str_SQLGetData(struct ast_str **buf, int pmaxlen, SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLLEN *StrLen_or_Ind);
  187. /*!
  188. * \brief Shortcut for printing errors to logs after a failed SQL operation.
  189. *
  190. * \param handle_type The type of SQL handle on which to gather diagnostics
  191. * \param handle The SQL handle to gather diagnostics from
  192. * \param operation The name of the failed operation.
  193. * \return The error string that was printed to the logs
  194. */
  195. struct ast_str *ast_odbc_print_errors(SQLSMALLINT handle_type, SQLHANDLE handle, const char *operation);
  196. /*!
  197. * \brief Get the transaction isolation setting for an ODBC class
  198. */
  199. unsigned int ast_odbc_class_get_isolation(struct odbc_class *class);
  200. /*!
  201. * \brief Get the transaction forcecommit setting for an ODBC class
  202. */
  203. unsigned int ast_odbc_class_get_forcecommit(struct odbc_class *class);
  204. /*!
  205. * \brief Get the name of an ODBC class.
  206. */
  207. const char *ast_odbc_class_get_name(struct odbc_class *class);
  208. /*!
  209. * \brief Convert from textual transaction isolation values to their numeric constants
  210. */
  211. int ast_odbc_text2isolation(const char *txt);
  212. /*!
  213. * \brief Convert from numeric transaction isolation values to their textual counterparts
  214. */
  215. const char *ast_odbc_isolation2text(int iso);
  216. /*!
  217. * \brief Return the current configured maximum number of connections for a class
  218. */
  219. unsigned int ast_odbc_get_max_connections(const char *name);
  220. #endif /* _ASTERISK_RES_ODBC_H */