km_res.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. LM_ERR("could not allocate columns\n");
  64. return -3;
  65. }
  66. fields = mysql_fetch_fields(RES_RESULT(_r));
  67. for(col = 0; col < RES_COL_N(_r); col++) {
  68. RES_NAMES(_r)[col] = (str*)pkg_malloc(sizeof(str));
  69. if (! RES_NAMES(_r)[col]) {
  70. LM_ERR("no private memory left\n");
  71. db_free_columns(_r);
  72. return -4;
  73. }
  74. LM_DBG("allocate %lu bytes for RES_NAMES[%d] at %p\n",
  75. (unsigned long)sizeof(str), col, RES_NAMES(_r)[col]);
  76. /* The pointer that is here returned is part of the result structure. */
  77. RES_NAMES(_r)[col]->s = fields[col].name;
  78. RES_NAMES(_r)[col]->len = strlen(fields[col].name);
  79. LM_DBG("RES_NAMES(%p)[%d]=[%.*s]\n", RES_NAMES(_r)[col], col,
  80. RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s);
  81. switch(fields[col].type) {
  82. case MYSQL_TYPE_TINY:
  83. case MYSQL_TYPE_SHORT:
  84. case MYSQL_TYPE_LONG:
  85. case MYSQL_TYPE_INT24:
  86. case MYSQL_TYPE_TIMESTAMP:
  87. LM_DBG("use DB1_INT result type\n");
  88. RES_TYPES(_r)[col] = DB1_INT;
  89. break;
  90. case MYSQL_TYPE_LONGLONG:
  91. LM_DBG("use DB1_BIGINT result type\n");
  92. RES_TYPES(_r)[col] = DB1_BIGINT;
  93. break;
  94. case MYSQL_TYPE_FLOAT:
  95. case MYSQL_TYPE_DOUBLE:
  96. LM_DBG("use DB1_DOUBLE result type\n");
  97. RES_TYPES(_r)[col] = DB1_DOUBLE;
  98. break;
  99. case MYSQL_TYPE_DATETIME:
  100. LM_DBG("use DB1_DATETIME result type\n");
  101. RES_TYPES(_r)[col] = DB1_DATETIME;
  102. break;
  103. case MYSQL_TYPE_BLOB:
  104. LM_DBG("use DB1_BLOB result type\n");
  105. RES_TYPES(_r)[col] = DB1_BLOB;
  106. break;
  107. case FIELD_TYPE_SET:
  108. LM_DBG("use DB1_BITMAP result type\n");
  109. RES_TYPES(_r)[col] = DB1_BITMAP;
  110. break;
  111. case MYSQL_TYPE_DECIMAL:
  112. #if MYSQL_VERSION_ID > 49999
  113. case MYSQL_TYPE_NEWDECIMAL:
  114. #endif
  115. case MYSQL_TYPE_STRING:
  116. case MYSQL_TYPE_VAR_STRING:
  117. LM_DBG("use DB1_STRING result type\n");
  118. RES_TYPES(_r)[col] = DB1_STRING;
  119. break;
  120. default:
  121. LM_WARN("unhandled data type column (%.*s) type id (%d), "
  122. "use DB1_STRING as default\n", RES_NAMES(_r)[col]->len,
  123. RES_NAMES(_r)[col]->s, fields[col].type);
  124. RES_TYPES(_r)[col] = DB1_STRING;
  125. break;
  126. }
  127. }
  128. return 0;
  129. }
  130. /*!
  131. * \brief Convert rows from mysql to db API representation
  132. * \param _h database connection
  133. * \param _r database result set
  134. * \return 0 on success, negative on failure
  135. */
  136. static inline int db_mysql_convert_rows(const db1_con_t* _h, db1_res_t* _r)
  137. {
  138. int row;
  139. if ((!_h) || (!_r)) {
  140. LM_ERR("invalid parameter\n");
  141. return -1;
  142. }
  143. RES_ROW_N(_r) = mysql_num_rows(RES_RESULT(_r));
  144. if (!RES_ROW_N(_r)) {
  145. LM_DBG("no rows returned from the query\n");
  146. RES_ROWS(_r) = 0;
  147. return 0;
  148. }
  149. if (db_allocate_rows(_r) < 0) {
  150. LM_ERR("could not allocate rows");
  151. return -2;
  152. }
  153. for(row = 0; row < RES_ROW_N(_r); row++) {
  154. RES_ROW(_r) = mysql_fetch_row(RES_RESULT(_r));
  155. if (!RES_ROW(_r)) {
  156. LM_ERR("driver error: %s\n", mysql_error(CON_CONNECTION(_h)));
  157. RES_ROW_N(_r) = row;
  158. db_free_rows(_r);
  159. return -3;
  160. }
  161. if (db_mysql_convert_row(_h, _r, &(RES_ROWS(_r)[row])) < 0) {
  162. LM_ERR("error while converting row #%d\n", row);
  163. RES_ROW_N(_r) = row;
  164. db_free_rows(_r);
  165. return -4;
  166. }
  167. }
  168. return 0;
  169. }
  170. /*!
  171. * \brief Fill the result structure with data from database
  172. * \param _h database connection
  173. * \param _r database result
  174. * \return 0 on success, negative on failure
  175. */
  176. int db_mysql_convert_result(const db1_con_t* _h, db1_res_t* _r)
  177. {
  178. if ((!_h) || (!_r)) {
  179. LM_ERR("invalid parameter\n");
  180. return -1;
  181. }
  182. if (db_mysql_get_columns(_h, _r) < 0) {
  183. LM_ERR("error while getting column names\n");
  184. return -2;
  185. }
  186. if (db_mysql_convert_rows(_h, _r) < 0) {
  187. LM_ERR("error while converting rows\n");
  188. db_free_columns(_r);
  189. return -3;
  190. }
  191. return 0;
  192. }
  193. /*!
  194. * \brief Allocate new result set with private structure
  195. * \return db1_res_t object on success, NULL on failure
  196. */
  197. db1_res_t* db_mysql_new_result(void)
  198. {
  199. db1_res_t* obj;
  200. obj = db_new_result();
  201. if (!obj)
  202. return NULL;
  203. RES_PTR(obj) = pkg_malloc(sizeof(struct my_res));
  204. if (!RES_PTR(obj)) {
  205. db_free_result(obj);
  206. return NULL;
  207. }
  208. return obj;
  209. }