km_val.c 5.3 KB

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