db_val.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. * Copyright (C) 2008-2009 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. #include "db_ut.h"
  24. #include <stdio.h>
  25. #include <time.h>
  26. /*!
  27. * \brief Convert a str to a db value
  28. *
  29. * Convert a str to a db value, copy strings if _cpy is not zero.
  30. * Copying is not necessary if the result from the database client library
  31. * is freed after the result inside the server is processed. If the result
  32. * is freed earlier, e.g. because its saved inside some temporary storage,
  33. * then it must be copied in order to be use it reliable.
  34. *
  35. * \param _t destination value type
  36. * \param _v destination value
  37. * \param _s source string
  38. * \param _l string length
  39. * \param _cpy when set to zero does not copy strings, otherwise copy strings
  40. * \return 0 on success, negative on error
  41. */
  42. int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
  43. const unsigned int _cpy)
  44. {
  45. static str dummy_string = {"", 0};
  46. if (!_v) {
  47. LM_ERR("invalid parameter value\n");
  48. return -1;
  49. }
  50. /* A NULL string is a SQL NULL value, otherwise its an empty value */
  51. if (!_s) {
  52. LM_DBG("converting NULL value\n");
  53. memset(_v, 0, sizeof(db_val_t));
  54. /* Initialize the string pointers to a dummy empty
  55. * string so that we do not crash when the NULL flag
  56. * is set but the module does not check it properly
  57. */
  58. VAL_STRING(_v) = dummy_string.s;
  59. VAL_STR(_v) = dummy_string;
  60. VAL_BLOB(_v) = dummy_string;
  61. VAL_TYPE(_v) = _t;
  62. VAL_NULL(_v) = 1;
  63. return 0;
  64. }
  65. VAL_NULL(_v) = 0;
  66. switch(_t) {
  67. case DB1_INT:
  68. LM_DBG("converting INT [%s]\n", _s);
  69. if (db_str2int(_s, &VAL_INT(_v)) < 0) {
  70. LM_ERR("error while converting integer value from string\n");
  71. return -2;
  72. } else {
  73. VAL_TYPE(_v) = DB1_INT;
  74. return 0;
  75. }
  76. break;
  77. case DB1_BIGINT:
  78. LM_DBG("converting BIGINT [%s]\n", _s);
  79. if (db_str2longlong(_s, &VAL_BIGINT(_v)) < 0) {
  80. LM_ERR("error while converting big integer value from string\n");
  81. return -3;
  82. } else {
  83. VAL_TYPE(_v) = DB1_BIGINT;
  84. return 0;
  85. }
  86. break;
  87. case DB1_BITMAP:
  88. LM_DBG("converting BITMAP [%s]\n", _s);
  89. if (db_str2int(_s, &VAL_INT(_v)) < 0) {
  90. LM_ERR("error while converting bitmap value from string\n");
  91. return -4;
  92. } else {
  93. VAL_TYPE(_v) = DB1_BITMAP;
  94. return 0;
  95. }
  96. break;
  97. case DB1_DOUBLE:
  98. LM_DBG("converting DOUBLE [%s]\n", _s);
  99. if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) {
  100. LM_ERR("error while converting double value from string\n");
  101. return -5;
  102. } else {
  103. VAL_TYPE(_v) = DB1_DOUBLE;
  104. return 0;
  105. }
  106. break;
  107. case DB1_STRING:
  108. LM_DBG("converting STRING [%s]\n", _s);
  109. if (_cpy == 0) {
  110. VAL_STRING(_v) = _s;
  111. } else {
  112. VAL_STRING(_v) = pkg_malloc(_l + 1);
  113. if (VAL_STRING(_v) == NULL) {
  114. LM_ERR("no private memory left\n");
  115. return -6;
  116. }
  117. LM_DBG("allocate %d bytes memory for STRING at %p\n", _l + 1, VAL_STRING(_v));
  118. strncpy((char*)VAL_STRING(_v), _s, _l);
  119. ((char*)VAL_STRING(_v))[_l] = '\0';
  120. VAL_FREE(_v) = 1;
  121. }
  122. VAL_TYPE(_v) = DB1_STRING;
  123. return 0;
  124. case DB1_STR:
  125. LM_DBG("converting STR [%.*s]\n", _l, _s);
  126. if (_cpy == 0) {
  127. VAL_STR(_v).s = (char*) _s;
  128. } else {
  129. VAL_STR(_v).s = pkg_malloc(_l);
  130. if (VAL_STR(_v).s == NULL) {
  131. LM_ERR("no private memory left\n");
  132. return -7;
  133. }
  134. LM_DBG("allocate %d bytes memory for STR at %p\n", _l, VAL_STR(_v).s);
  135. strncpy(VAL_STR(_v).s, _s, _l);
  136. VAL_FREE(_v) = 1;
  137. }
  138. VAL_STR(_v).len = _l;
  139. VAL_TYPE(_v) = DB1_STR;
  140. return 0;
  141. case DB1_DATETIME:
  142. LM_DBG("converting DATETIME [%s]\n", _s);
  143. if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
  144. LM_ERR("error while converting datetime value from string\n");
  145. return -8;
  146. } else {
  147. VAL_TYPE(_v) = DB1_DATETIME;
  148. return 0;
  149. }
  150. break;
  151. case DB1_BLOB:
  152. LM_DBG("converting BLOB [%.*s]\n", _l, _s);
  153. if (_cpy == 0) {
  154. VAL_BLOB(_v).s = (char*) _s;
  155. } else {
  156. VAL_BLOB(_v).s = pkg_malloc(_l);
  157. if (VAL_BLOB(_v).s == NULL) {
  158. LM_ERR("no private memory left\n");
  159. return -9;
  160. }
  161. LM_DBG("allocate %d bytes memory for BLOB at %p\n", _l, VAL_BLOB(_v).s);
  162. strncpy(VAL_BLOB(_v).s, _s, _l);
  163. VAL_FREE(_v) = 1;
  164. }
  165. VAL_BLOB(_v).len = _l;
  166. VAL_TYPE(_v) = DB1_BLOB;
  167. return 0;
  168. default:
  169. return -10;
  170. }
  171. return -10;
  172. }
  173. /*!
  174. * \brief Convert a numerical value to a string
  175. *
  176. * Convert a numerical value to a string, used when converting result from a query.
  177. * Implement common functionality needed from the databases, does parameter checking.
  178. * \param _c database connection
  179. * \param _v source value
  180. * \param _s target string
  181. * \param _len target string length
  182. * \return 0 on success, negative on error, 1 if value must be converted by other means
  183. */
  184. int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
  185. {
  186. if (!_c || !_v || !_s || !_len || !*_len) {
  187. LM_ERR("invalid parameter value\n");
  188. return -1;
  189. }
  190. if (VAL_NULL(_v)) {
  191. if (*_len < sizeof("NULL")) {
  192. LM_ERR("buffer too small\n");
  193. return -1;
  194. }
  195. *_len = snprintf(_s, *_len, "NULL");
  196. return 0;
  197. }
  198. switch(VAL_TYPE(_v)) {
  199. case DB1_INT:
  200. if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
  201. LM_ERR("error while converting string to int\n");
  202. return -2;
  203. } else {
  204. return 0;
  205. }
  206. break;
  207. case DB1_BIGINT:
  208. if (db_longlong2str(VAL_BIGINT(_v), _s, _len) < 0) {
  209. LM_ERR("error while converting string to big int\n");
  210. return -3;
  211. } else {
  212. return 0;
  213. }
  214. break;
  215. case DB1_BITMAP:
  216. if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
  217. LM_ERR("error while converting string to int\n");
  218. return -4;
  219. } else {
  220. return 0;
  221. }
  222. break;
  223. case DB1_DOUBLE:
  224. if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
  225. LM_ERR("error while converting string to double\n");
  226. return -5;
  227. } else {
  228. return 0;
  229. }
  230. break;
  231. case DB1_DATETIME:
  232. if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
  233. LM_ERR("failed to convert string to time_t\n");
  234. return -8;
  235. } else {
  236. return 0;
  237. }
  238. break;
  239. default:
  240. return 1;
  241. }
  242. }