km_val.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2003 August.Net Services, LLC
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * History
  24. * -------
  25. * 2003-04-06 initial code written (Greg Fausak/Andy Fullford)
  26. * 2003-04-14 gmtime changed to localtime because mktime later
  27. * expects localtime, changed daylight saving bug
  28. * previously found in mysql module (janakj)
  29. */
  30. /*! \file
  31. * \brief DB_POSTGRES :: Core
  32. * \ingroup db_postgres
  33. * Module: \ref db_postgres
  34. */
  35. #include "../../db/db_val.h"
  36. #include "../../db/db_ut.h"
  37. #include "../../dprint.h"
  38. #include "pg_con.h"
  39. #include "../../mem/mem.h"
  40. #include "val.h"
  41. /*!
  42. * \brief Convert a str to a db value, copy strings
  43. *
  44. * Convert a str to a db value, copy strings.
  45. * The postgresql module uses a custom escape function for BLOBs.
  46. * If the _s is linked in the db_val result, it will be returned zero
  47. * \param _t destination value type
  48. * \param _v destination value
  49. * \param _s source string
  50. * \param _l string length
  51. * \return 0 on success, negative on error
  52. */
  53. int db_postgres_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l)
  54. {
  55. if ( (_t != DB_BLOB && _v != NULL) || _v == NULL) {
  56. return db_str2val(_t, _v, _s, _l, 1);
  57. } else {
  58. char * tmp_s = NULL;
  59. LM_DBG("converting BLOB [%.*s]\n", _l, _s);
  60. /*
  61. * The string is stored in new allocated memory, which we could
  62. * not free later thus we need to copy it to some new memory here.
  63. */
  64. tmp_s = (char*)PQunescapeBytea((unsigned char*)_s, (size_t*)(void*)&(VAL_BLOB(_v).len));
  65. if(tmp_s==NULL) {
  66. LM_ERR("PQunescapeBytea failed\n");
  67. return -7;
  68. }
  69. VAL_BLOB(_v).s = pkg_malloc(VAL_BLOB(_v).len);
  70. if (VAL_BLOB(_v).s == NULL) {
  71. LM_ERR("no private memory left\n");
  72. PQfreemem(tmp_s);
  73. return -8;
  74. }
  75. LM_DBG("allocate %d bytes memory for BLOB at %p", VAL_BLOB(_v).len, VAL_BLOB(_v).s);
  76. memcpy(VAL_BLOB(_v).s, tmp_s, VAL_BLOB(_v).len);
  77. PQfreemem(tmp_s);
  78. VAL_TYPE(_v) = DB_BLOB;
  79. VAL_FREE(_v) = 1;
  80. LM_DBG("got blob len %d\n", _l);
  81. return 0;
  82. }
  83. }
  84. /*!
  85. * \brief Converting a value to a string
  86. *
  87. * Converting a value to a string, used when converting result from a query
  88. * \param _con database connection
  89. * \param _v source value
  90. * \param _s target string
  91. * \param _len target string length
  92. * \return 0 on success, negative on error
  93. */
  94. int db_postgres_val2str(const db_con_t* _con, const db_val_t* _v, char* _s, int* _len)
  95. {
  96. int l, ret, tmp;
  97. int pgret;
  98. char *tmp_s;
  99. size_t tmp_len;
  100. char* old_s;
  101. tmp = db_val2str(_con, _v, _s, _len);
  102. if (tmp < 1)
  103. return tmp;
  104. switch(VAL_TYPE(_v)) {
  105. case DB_STRING:
  106. l = strlen(VAL_STRING(_v));
  107. if (*_len < (l * 2 + 3)) {
  108. LM_ERR("destination buffer too short for string\n");
  109. return -6;
  110. } else {
  111. old_s = _s;
  112. *_s++ = '\'';
  113. ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
  114. l, &pgret);
  115. if(pgret!=0)
  116. {
  117. LM_ERR("PQescapeStringConn failed\n");
  118. return -6;
  119. }
  120. LM_DBG("PQescapeStringConn: in: %d chars,"
  121. " out: %d chars\n", l, ret);
  122. _s += ret;
  123. *_s++ = '\'';
  124. *_s = '\0'; /* FIXME */
  125. *_len = _s - old_s;
  126. return 0;
  127. }
  128. break;
  129. case DB_STR:
  130. l = VAL_STR(_v).len;
  131. if (*_len < (l * 2 + 3)) {
  132. LM_ERR("destination buffer too short for str\n");
  133. return -7;
  134. } else {
  135. old_s = _s;
  136. *_s++ = '\'';
  137. ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
  138. l, &pgret);
  139. if(pgret!=0)
  140. {
  141. LM_ERR("PQescapeStringConn failed \n");
  142. return -7;
  143. }
  144. LM_DBG("PQescapeStringConn: in: %d chars, out: %d chars\n", l, ret);
  145. _s += ret;
  146. *_s++ = '\'';
  147. *_s = '\0'; /* FIXME */
  148. *_len = _s - old_s;
  149. return 0;
  150. }
  151. break;
  152. case DB_BLOB:
  153. l = VAL_BLOB(_v).len;
  154. /* this estimation is not always correct, thus we need to check later again */
  155. if (*_len < (l * 2 + 3)) {
  156. LM_ERR("destination buffer too short for blob\n");
  157. return -9;
  158. } else {
  159. *_s++ = '\'';
  160. tmp_s = (char*)PQescapeByteaConn(CON_CONNECTION(_con), (unsigned char*)VAL_STRING(_v),
  161. (size_t)l, (size_t*)&tmp_len);
  162. if(tmp_s==NULL)
  163. {
  164. LM_ERR("PQescapeByteaConn failed\n");
  165. return -9;
  166. }
  167. if (tmp_len > *_len) {
  168. LM_ERR("escaped result too long\n");
  169. return -9;
  170. }
  171. memcpy(_s, tmp_s, tmp_len);
  172. PQfreemem(tmp_s);
  173. tmp_len = strlen(_s);
  174. *(_s + tmp_len) = '\'';
  175. *(_s + tmp_len + 1) = '\0';
  176. *_len = tmp_len + 2;
  177. return 0;
  178. }
  179. break;
  180. default:
  181. LM_DBG("unknown data type\n");
  182. return -10;
  183. }
  184. }