km_res.c 5.9 KB

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