km_val.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /* use common function for non BLOB, NULL setting and input parameter checking */
  56. if ( _t != DB_BLOB || _s == NULL || _v == NULL) {
  57. return db_str2val(_t, _v, _s, _l, 1);
  58. } else {
  59. char * tmp_s = NULL;
  60. LM_DBG("converting BLOB [%.*s]\n", _l, _s);
  61. /*
  62. * The string is stored in new allocated memory, which we could
  63. * not free later thus we need to copy it to some new memory here.
  64. */
  65. tmp_s = (char*)PQunescapeBytea((unsigned char*)_s, (size_t*)(void*)&(VAL_BLOB(_v).len));
  66. if(tmp_s==NULL) {
  67. LM_ERR("PQunescapeBytea failed\n");
  68. return -7;
  69. }
  70. VAL_BLOB(_v).s = pkg_malloc(VAL_BLOB(_v).len);
  71. if (VAL_BLOB(_v).s == NULL) {
  72. LM_ERR("no private memory left\n");
  73. PQfreemem(tmp_s);
  74. return -8;
  75. }
  76. LM_DBG("allocate %d bytes memory for BLOB at %p", VAL_BLOB(_v).len, VAL_BLOB(_v).s);
  77. memcpy(VAL_BLOB(_v).s, tmp_s, VAL_BLOB(_v).len);
  78. PQfreemem(tmp_s);
  79. VAL_TYPE(_v) = DB_BLOB;
  80. VAL_FREE(_v) = 1;
  81. LM_DBG("got blob len %d\n", _l);
  82. return 0;
  83. }
  84. }
  85. /*!
  86. * \brief Converting a value to a string
  87. *
  88. * Converting a value to a string, used when converting result from a query
  89. * \param _con database connection
  90. * \param _v source value
  91. * \param _s target string
  92. * \param _len target string length
  93. * \return 0 on success, negative on error
  94. */
  95. int db_postgres_val2str(const db_con_t* _con, const db_val_t* _v, char* _s, int* _len)
  96. {
  97. int l, ret, tmp;
  98. int pgret;
  99. char *tmp_s;
  100. size_t tmp_len;
  101. char* old_s;
  102. tmp = db_val2str(_con, _v, _s, _len);
  103. if (tmp < 1)
  104. return tmp;
  105. switch(VAL_TYPE(_v)) {
  106. case DB_STRING:
  107. l = strlen(VAL_STRING(_v));
  108. if (*_len < (l * 2 + 3)) {
  109. LM_ERR("destination buffer too short for string\n");
  110. return -6;
  111. } else {
  112. old_s = _s;
  113. *_s++ = '\'';
  114. ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
  115. l, &pgret);
  116. if(pgret!=0)
  117. {
  118. LM_ERR("PQescapeStringConn failed\n");
  119. return -6;
  120. }
  121. LM_DBG("PQescapeStringConn: in: %d chars,"
  122. " out: %d chars\n", l, ret);
  123. _s += ret;
  124. *_s++ = '\'';
  125. *_s = '\0'; /* FIXME */
  126. *_len = _s - old_s;
  127. return 0;
  128. }
  129. break;
  130. case DB_STR:
  131. l = VAL_STR(_v).len;
  132. if (*_len < (l * 2 + 3)) {
  133. LM_ERR("destination buffer too short for str\n");
  134. return -7;
  135. } else {
  136. old_s = _s;
  137. *_s++ = '\'';
  138. ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
  139. l, &pgret);
  140. if(pgret!=0)
  141. {
  142. LM_ERR("PQescapeStringConn failed \n");
  143. return -7;
  144. }
  145. LM_DBG("PQescapeStringConn: in: %d chars, out: %d chars\n", l, ret);
  146. _s += ret;
  147. *_s++ = '\'';
  148. *_s = '\0'; /* FIXME */
  149. *_len = _s - old_s;
  150. return 0;
  151. }
  152. break;
  153. case DB_BLOB:
  154. l = VAL_BLOB(_v).len;
  155. /* this estimation is not always correct, thus we need to check later again */
  156. if (*_len < (l * 2 + 3)) {
  157. LM_ERR("destination buffer too short for blob\n");
  158. return -9;
  159. } else {
  160. *_s++ = '\'';
  161. tmp_s = (char*)PQescapeByteaConn(CON_CONNECTION(_con), (unsigned char*)VAL_STRING(_v),
  162. (size_t)l, (size_t*)&tmp_len);
  163. if(tmp_s==NULL)
  164. {
  165. LM_ERR("PQescapeByteaConn failed\n");
  166. return -9;
  167. }
  168. if (tmp_len > *_len) {
  169. LM_ERR("escaped result too long\n");
  170. return -9;
  171. }
  172. memcpy(_s, tmp_s, tmp_len);
  173. PQfreemem(tmp_s);
  174. tmp_len = strlen(_s);
  175. *(_s + tmp_len) = '\'';
  176. *(_s + tmp_len + 1) = '\0';
  177. *_len = tmp_len + 2;
  178. return 0;
  179. }
  180. break;
  181. default:
  182. LM_DBG("unknown data type\n");
  183. return -10;
  184. }
  185. }