km_res.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * $Id$
  3. *
  4. * MySQL module result related functions
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. * Copyright (C) 2007-2008 1&1 Internet AG
  8. *
  9. * This file is part of Kamailio, a free SIP server.
  10. *
  11. * Kamailio is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * Kamailio is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /*! \file
  26. * \brief DB_MYSQL :: Result related functions
  27. * \ingroup db_mysql
  28. * Module: \ref db_mysql
  29. */
  30. #include <string.h>
  31. #include <mysql/mysql.h>
  32. #include "../../lib/srdb1/db_res.h"
  33. #include "../../mem/mem.h"
  34. #include "../../dprint.h"
  35. #include "km_row.h"
  36. #include "km_my_con.h"
  37. #include "km_res.h"
  38. /*!
  39. * \brief Get and convert columns from a result
  40. *
  41. * Get and convert columns from a result, fills the result structure
  42. * with data from the database.
  43. * \param _h database connection
  44. * \param _r database result set
  45. * \return 0 on success, negative on failure
  46. */
  47. int db_mysql_get_columns(const db1_con_t* _h, db1_res_t* _r)
  48. {
  49. int col;
  50. MYSQL_FIELD* fields;
  51. if ((!_h) || (!_r)) {
  52. LM_ERR("invalid parameter\n");
  53. return -1;
  54. }
  55. RES_COL_N(_r) = mysql_field_count(CON_CONNECTION(_h));
  56. if (!RES_COL_N(_r)) {
  57. LM_ERR("no columns returned from the query\n");
  58. return -2;
  59. } else {
  60. LM_DBG("%d columns returned from the query\n", RES_COL_N(_r));
  61. }
  62. if (db_allocate_columns(_r, RES_COL_N(_r)) != 0) {
  63. RES_COL_N(_r) = 0;
  64. LM_ERR("could not allocate columns\n");
  65. return -3;
  66. }
  67. fields = mysql_fetch_fields(RES_RESULT(_r));
  68. for(col = 0; col < RES_COL_N(_r); col++) {
  69. RES_NAMES(_r)[col] = (str*)pkg_malloc(sizeof(str));
  70. if (! RES_NAMES(_r)[col]) {
  71. LM_ERR("no private memory left\n");
  72. db_free_columns(_r);
  73. return -4;
  74. }
  75. LM_DBG("allocate %lu bytes for RES_NAMES[%d] at %p\n",
  76. (unsigned long)sizeof(str), col, RES_NAMES(_r)[col]);
  77. /* The pointer that is here returned is part of the result structure. */
  78. RES_NAMES(_r)[col]->s = fields[col].name;
  79. RES_NAMES(_r)[col]->len = strlen(fields[col].name);
  80. LM_DBG("RES_NAMES(%p)[%d]=[%.*s]\n", RES_NAMES(_r)[col], col,
  81. RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s);
  82. switch(fields[col].type) {
  83. case MYSQL_TYPE_TINY:
  84. case MYSQL_TYPE_SHORT:
  85. case MYSQL_TYPE_LONG:
  86. case MYSQL_TYPE_INT24:
  87. case MYSQL_TYPE_TIMESTAMP:
  88. LM_DBG("use DB1_INT result type\n");
  89. RES_TYPES(_r)[col] = DB1_INT;
  90. break;
  91. case MYSQL_TYPE_LONGLONG:
  92. LM_DBG("use DB1_BIGINT result type\n");
  93. RES_TYPES(_r)[col] = DB1_BIGINT;
  94. break;
  95. case MYSQL_TYPE_FLOAT:
  96. case MYSQL_TYPE_DOUBLE:
  97. LM_DBG("use DB1_DOUBLE result type\n");
  98. RES_TYPES(_r)[col] = DB1_DOUBLE;
  99. break;
  100. case MYSQL_TYPE_DATETIME:
  101. LM_DBG("use DB1_DATETIME result type\n");
  102. RES_TYPES(_r)[col] = DB1_DATETIME;
  103. break;
  104. case MYSQL_TYPE_BLOB:
  105. LM_DBG("use DB1_BLOB result type\n");
  106. RES_TYPES(_r)[col] = DB1_BLOB;
  107. break;
  108. case FIELD_TYPE_SET:
  109. LM_DBG("use DB1_BITMAP result type\n");
  110. RES_TYPES(_r)[col] = DB1_BITMAP;
  111. break;
  112. case MYSQL_TYPE_DECIMAL:
  113. #if MYSQL_VERSION_ID > 49999
  114. case MYSQL_TYPE_NEWDECIMAL:
  115. #endif
  116. case MYSQL_TYPE_STRING:
  117. case MYSQL_TYPE_VAR_STRING:
  118. LM_DBG("use DB1_STRING result type\n");
  119. RES_TYPES(_r)[col] = DB1_STRING;
  120. break;
  121. default:
  122. LM_WARN("unhandled data type column (%.*s) type id (%d), "
  123. "use DB1_STRING as default\n", RES_NAMES(_r)[col]->len,
  124. RES_NAMES(_r)[col]->s, fields[col].type);
  125. RES_TYPES(_r)[col] = DB1_STRING;
  126. break;
  127. }
  128. }
  129. return 0;
  130. }
  131. /*!
  132. * \brief Convert rows from mysql to db API representation
  133. * \param _h database connection
  134. * \param _r database result set
  135. * \return 0 on success, negative on failure
  136. */
  137. static inline int db_mysql_convert_rows(const db1_con_t* _h, db1_res_t* _r)
  138. {
  139. int row;
  140. if ((!_h) || (!_r)) {
  141. LM_ERR("invalid parameter\n");
  142. return -1;
  143. }
  144. RES_ROW_N(_r) = mysql_num_rows(RES_RESULT(_r));
  145. if (!RES_ROW_N(_r)) {
  146. LM_DBG("no rows returned from the query\n");
  147. RES_ROWS(_r) = 0;
  148. return 0;
  149. }
  150. if (db_allocate_rows(_r) < 0) {
  151. LM_ERR("could not allocate rows");
  152. RES_ROW_N(_r) = 0;
  153. return -2;
  154. }
  155. for(row = 0; row < RES_ROW_N(_r); row++) {
  156. RES_ROW(_r) = mysql_fetch_row(RES_RESULT(_r));
  157. if (!RES_ROW(_r)) {
  158. LM_ERR("driver error: %s\n", mysql_error(CON_CONNECTION(_h)));
  159. RES_ROW_N(_r) = row;
  160. db_free_rows(_r);
  161. return -3;
  162. }
  163. if (db_mysql_convert_row(_h, _r, &(RES_ROWS(_r)[row])) < 0) {
  164. LM_ERR("error while converting row #%d\n", row);
  165. RES_ROW_N(_r) = row;
  166. db_free_rows(_r);
  167. return -4;
  168. }
  169. }
  170. return 0;
  171. }
  172. /*!
  173. * \brief Fill the result structure with data from database
  174. * \param _h database connection
  175. * \param _r database result
  176. * \return 0 on success, negative on failure
  177. */
  178. int db_mysql_convert_result(const db1_con_t* _h, db1_res_t* _r)
  179. {
  180. if ((!_h) || (!_r)) {
  181. LM_ERR("invalid parameter\n");
  182. return -1;
  183. }
  184. if (db_mysql_get_columns(_h, _r) < 0) {
  185. LM_ERR("error while getting column names\n");
  186. return -2;
  187. }
  188. if (db_mysql_convert_rows(_h, _r) < 0) {
  189. LM_ERR("error while converting rows\n");
  190. db_free_columns(_r);
  191. return -3;
  192. }
  193. return 0;
  194. }
  195. /*!
  196. * \brief Allocate new result set with private structure
  197. * \return db1_res_t object on success, NULL on failure
  198. */
  199. db1_res_t* db_mysql_new_result(void)
  200. {
  201. db1_res_t* obj;
  202. obj = db_new_result();
  203. if (!obj)
  204. return NULL;
  205. RES_PTR(obj) = pkg_malloc(sizeof(struct my_res));
  206. if (!RES_PTR(obj)) {
  207. db_free_result(obj);
  208. return NULL;
  209. }
  210. return obj;
  211. }