db_query.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2007-2008 1&1 Internet AG
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. /**
  24. * \file lib/srdb1/db_query.h
  25. * \brief Query helper for database drivers
  26. * \ingroup db1
  27. *
  28. * This helper methods for database queries are used from the database
  29. * SQL driver to do the actual work. Each function uses some functions from
  30. * the actual driver with function pointers to the concrete, specific
  31. * implementation.
  32. */
  33. #ifndef DB1_QUERY_H
  34. #define DB1_QUERY_H
  35. #include "db_key.h"
  36. #include "db_op.h"
  37. #include "db_val.h"
  38. #include "db_con.h"
  39. #include "db_row.h"
  40. #include "db_res.h"
  41. #include "db_cap.h"
  42. /**
  43. * \brief Helper function for db queries
  44. *
  45. * This method evaluates the actual arguments for the database query and
  46. * setups the string that is used for the query in the db module.
  47. * Then its submit the query and stores the result if necessary. It uses for
  48. * its work the implementation in the concrete database module.
  49. *
  50. * \param _h structure representing database connection
  51. * \param _k key names, if not present the whole table will be returned
  52. * \param _op operators
  53. * \param _v values of the keys that must match
  54. * \param _c column names that should be returned
  55. * \param _n number of key/value pairs that are compared, if zero then no comparison is done
  56. * \param _nc number of colums that should be returned
  57. * \param _o order by the specificied column, optional
  58. * \param _r the result that is returned, set to NULL if you want to use fetch_result later
  59. * \param (*val2str) function pointer to the db specific val conversion function
  60. * \param (*submit_query) function pointer to the db specific query submit function
  61. * \param (*store_result) function pointer to the db specific store result function
  62. * \return zero on success, negative on errors
  63. */
  64. int db_do_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
  65. const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
  66. const db_key_t _o, db1_res_t** _r, int (*val2str) (const db1_con_t*,
  67. const db_val_t*, char*, int*), int (*submit_query)(const db1_con_t* _h,
  68. const str* _c), int (*store_result)(const db1_con_t* _h, db1_res_t** _r));
  69. /**
  70. * \brief Helper function for db queries with update lock
  71. *
  72. * This method evaluates the actual arguments for the database query and
  73. * setups the string that is used for the query in the db module.
  74. * Then its submit the query and stores the result if necessary. It uses for
  75. * its work the implementation in the concrete database module.
  76. *
  77. * \param _h structure representing database connection
  78. * \param _k key names, if not present the whole table will be returned
  79. * \param _op operators
  80. * \param _v values of the keys that must match
  81. * \param _c column names that should be returned
  82. * \param _n number of key/value pairs that are compared, if zero then no comparison is done
  83. * \param _nc number of colums that should be returned
  84. * \param _o order by the specificied column, optional
  85. * \param _r the result that is returned, set to NULL if you want to use fetch_result later
  86. * \param (*val2str) function pointer to the db specific val conversion function
  87. * \param (*submit_query) function pointer to the db specific query submit function
  88. * \param (*store_result) function pointer to the db specific store result function
  89. * \return zero on success, negative on errors
  90. */
  91. int db_do_query_lock(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
  92. const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
  93. const db_key_t _o, db1_res_t** _r, int (*val2str) (const db1_con_t*,
  94. const db_val_t*, char*, int*), int (*submit_query)(const db1_con_t* _h,
  95. const str* _c), int (*store_result)(const db1_con_t* _h, db1_res_t** _r));
  96. /**
  97. * \brief Helper function for raw db queries
  98. *
  99. * This method evaluates the actual arguments for the database raw query
  100. * and setups the string that is used for the query in the db module.
  101. * Then its submit the query and stores the result if necessary.
  102. * It uses for its work the implementation in the concrete database module.
  103. *
  104. * \param _h structure representing database connection
  105. * \param _s char holding the raw query
  106. * \param _r the result that is returned
  107. * \param (*submit_query) function pointer to the db specific query submit function
  108. * \param (*store_result) function pointer to the db specific store result function
  109. * \return zero on success, negative on errors
  110. */
  111. int db_do_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r,
  112. int (*submit_query)(const db1_con_t* _h, const str* _c),
  113. int (*store_result)(const db1_con_t* _h, db1_res_t** _r));
  114. /**
  115. * \brief Helper function for db insert operations
  116. *
  117. * This method evaluates the actual arguments for the database operation
  118. * and setups the string that is used for the insert operation in the db
  119. * module. Then its submit the query for the operation. It uses for its work
  120. * the implementation in the concrete database module.
  121. *
  122. * \param _h structure representing database connection
  123. * \param _k key names
  124. * \param _v values of the keys
  125. * \param _n number of key/value pairs
  126. * \param (*val2str) function pointer to the db specific val conversion function
  127. * \param (*submit_query) function pointer to the db specific query submit function
  128. * \return zero on success, negative on errors
  129. */
  130. int db_do_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
  131. const int _n, int (*val2str) (const db1_con_t*, const db_val_t*, char*, int*),
  132. int (*submit_query)(const db1_con_t* _h, const str* _c));
  133. /**
  134. * \brief Helper function for db delete operations
  135. *
  136. * This method evaluates the actual arguments for the database operation
  137. * and setups the string that is used for the delete operation in the db
  138. * module. Then its submit the query for the operation. It uses for its work
  139. * the implementation in the concrete database module.
  140. *
  141. * \param _h structure representing database connection
  142. * \param _k key names
  143. * \param _o operators
  144. * \param _v values of the keys that must match
  145. * \param _n number of key/value pairs that are compared, if zero then the whole table is deleted
  146. * \param (*val2str) function pointer to the db specific val conversion function
  147. * \param (*submit_query) function pointer to the db specific query submit function
  148. * \return zero on success, negative on errors
  149. */
  150. int db_do_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
  151. const db_val_t* _v, const int _n, int (*val2str) (const db1_con_t*,
  152. const db_val_t*, char*, int*), int (*submit_query)(const db1_con_t* _h,
  153. const str* _c));
  154. /**
  155. * \brief Helper function for db update operations
  156. *
  157. * This method evaluates the actual arguments for the database operation
  158. * and setups the string that is used for the update operation in the db
  159. * module. Then its submit the query for the operation. It uses for its work
  160. * the implementation in the concrete database module.
  161. *
  162. * \param _h structure representing database connection
  163. * \param _k key names, if not present the whole table will be returned
  164. * \param _o operators
  165. * \param _v values of the keys that must match
  166. * \param _uk: updated columns
  167. * \param _uv: updated values of the columns
  168. * \param _n number of key/value pairs that are compared, if zero then no comparison is done
  169. * \param _un: number of columns that should be updated
  170. * \param (*val2str) function pointer to the db specific val conversion function
  171. * \param (*submit_query) function pointer to the db specific query submit function
  172. * \return zero on success, negative on errors
  173. */
  174. int db_do_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
  175. const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv, const int _n,
  176. const int _un, int (*val2str) (const db1_con_t*, const db_val_t*, char*, int*),
  177. int (*submit_query)(const db1_con_t* _h, const str* _c));
  178. /**
  179. * \brief Helper function for db delete operations
  180. *
  181. * This helper method evaluates the actual arguments for the database operation
  182. * and setups the string that is used for the replace operation in the db
  183. * module. Then its submit the query for the operation. It uses for its work the
  184. * implementation in the concrete database module.
  185. *
  186. * \param _h structure representing database connection
  187. * \param _k key names, if not present the whole table will be returned
  188. * \param _v values of the keys that must match
  189. * \param _n number of key/value pairs that are compared, if zero then no comparison is done
  190. * \param (*val2str) function pointer to the db specific val conversion function
  191. * \param (*submit_query) function pointer to the db specific query submit function
  192. * \return zero on success, negative on errors
  193. */
  194. int db_do_replace(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
  195. const int _n, int (*val2str) (const db1_con_t*, const db_val_t*, char*,
  196. int*), int (*submit_query)(const db1_con_t* _h, const str* _c));
  197. /**
  198. * \brief Helper function for db insert delayed operations
  199. *
  200. * This method evaluates the actual arguments for the database operation
  201. * and setups the string that is used for the insert delayed operation in the db
  202. * module. Then its submit the query for the operation. It uses for its work
  203. * the implementation in the concrete database module.
  204. *
  205. * \param _h structure representing database connection
  206. * \param _k key names
  207. * \param _v values of the keys
  208. * \param _n number of key/value pairs
  209. * \param (*val2str) function pointer to the db specific val conversion function
  210. * \param (*submit_query) function pointer to the db specific query submit function
  211. * \return zero on success, negative on errors
  212. */
  213. int db_do_insert_delayed(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
  214. const int _n, int (*val2str) (const db1_con_t*, const db_val_t*, char*, int*),
  215. int (*submit_query)(const db1_con_t* _h, const str* _c));
  216. /**
  217. * \brief Initialisation function - should be called from db.c at start-up
  218. *
  219. * This initialises the db_query module, and should be called before any functions in db_query are called.
  220. *
  221. * \return zero on success, negative on errors
  222. */
  223. int db_query_init(void);
  224. #endif