km_val.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. * Copyright (C) 2008 1&1 Internet AG
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /*! \file
  22. * \brief DB_MYSQL :: Data conversions
  23. * \ingroup db_mysql
  24. * Module: \ref db_mysql
  25. */
  26. #include "../../dprint.h"
  27. #include "../../lib/srdb1/db_ut.h"
  28. #include "km_val.h"
  29. #include "km_my_con.h"
  30. /*!
  31. * \brief Converting a value to a string
  32. *
  33. * Converting a value to a string, used when converting result from a query
  34. * \param _c database connection
  35. * \param _v source value
  36. * \param _s target string
  37. * \param _len target string length
  38. * \return 0 on success, negative on error
  39. */
  40. int db_mysql_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
  41. {
  42. int l, tmp;
  43. char* old_s;
  44. tmp = db_val2str(_c, _v, _s, _len);
  45. if (tmp < 1)
  46. return tmp;
  47. switch(VAL_TYPE(_v)) {
  48. case DB1_STRING:
  49. l = strlen(VAL_STRING(_v));
  50. if (*_len < (l * 2 + 3)) {
  51. LM_ERR("destination buffer too short\n");
  52. return -6;
  53. } else {
  54. old_s = _s;
  55. *_s++ = '\'';
  56. _s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STRING(_v), l);
  57. *_s++ = '\'';
  58. *_s = '\0'; /* FIXME */
  59. *_len = _s - old_s;
  60. return 0;
  61. }
  62. break;
  63. case DB1_STR:
  64. if (*_len < (VAL_STR(_v).len * 2 + 3)) {
  65. LM_ERR("destination buffer too short\n");
  66. return -7;
  67. } else {
  68. old_s = _s;
  69. *_s++ = '\'';
  70. _s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, VAL_STR(_v).len);
  71. *_s++ = '\'';
  72. *_s = '\0';
  73. *_len = _s - old_s;
  74. return 0;
  75. }
  76. break;
  77. case DB1_BLOB:
  78. l = VAL_BLOB(_v).len;
  79. if (*_len < (l * 2 + 3)) {
  80. LM_ERR("destination buffer too short\n");
  81. return -9;
  82. } else {
  83. old_s = _s;
  84. *_s++ = '\'';
  85. _s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, l);
  86. *_s++ = '\'';
  87. *_s = '\0';
  88. *_len = _s - old_s;
  89. return 0;
  90. }
  91. break;
  92. default:
  93. LM_DBG("unknown data type\n");
  94. return -10;
  95. }
  96. }