km_val.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. * Copyright (C) 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 :: Data conversions
  25. * \ingroup db_mysql
  26. * Module: \ref db_mysql
  27. */
  28. #include "../../dprint.h"
  29. #include "../../lib/srdb1/db_ut.h"
  30. #include "km_val.h"
  31. #include "km_my_con.h"
  32. /*!
  33. * \brief Converting a value to a string
  34. *
  35. * Converting a value to a string, used when converting result from a query
  36. * \param _c database connection
  37. * \param _v source value
  38. * \param _s target string
  39. * \param _len target string length
  40. * \return 0 on success, negative on error
  41. */
  42. int db_mysql_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
  43. {
  44. int l, tmp;
  45. char* old_s;
  46. tmp = db_val2str(_c, _v, _s, _len);
  47. if (tmp < 1)
  48. return tmp;
  49. switch(VAL_TYPE(_v)) {
  50. case DB1_STRING:
  51. l = strlen(VAL_STRING(_v));
  52. if (*_len < (l * 2 + 3)) {
  53. LM_ERR("destination buffer too short\n");
  54. return -6;
  55. } else {
  56. old_s = _s;
  57. *_s++ = '\'';
  58. _s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STRING(_v), l);
  59. *_s++ = '\'';
  60. *_s = '\0'; /* FIXME */
  61. *_len = _s - old_s;
  62. return 0;
  63. }
  64. break;
  65. case DB1_STR:
  66. if (*_len < (VAL_STR(_v).len * 2 + 3)) {
  67. LM_ERR("destination buffer too short\n");
  68. return -7;
  69. } else {
  70. old_s = _s;
  71. *_s++ = '\'';
  72. _s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, VAL_STR(_v).len);
  73. *_s++ = '\'';
  74. *_s = '\0';
  75. *_len = _s - old_s;
  76. return 0;
  77. }
  78. break;
  79. case DB1_BLOB:
  80. l = VAL_BLOB(_v).len;
  81. if (*_len < (l * 2 + 3)) {
  82. LM_ERR("destination buffer too short\n");
  83. return -9;
  84. } else {
  85. old_s = _s;
  86. *_s++ = '\'';
  87. _s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, l);
  88. *_s++ = '\'';
  89. *_s = '\0';
  90. *_len = _s - old_s;
  91. return 0;
  92. }
  93. break;
  94. default:
  95. LM_DBG("unknown data type\n");
  96. return -10;
  97. }
  98. }