km_val.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include <string.h>
  42. #include <time.h>
  43. /*!
  44. * \brief Convert a str to a db value, does not copy strings
  45. *
  46. * Convert a str to a db value, does not copy strings.
  47. * The postgresql module uses a custom escape function for BLOBs.
  48. * If the _s is linked in the db_val result, it will be returned zero
  49. * \param _t destination value type
  50. * \param _v destination value
  51. * \param _s source string
  52. * \param _l string length
  53. * \return 0 on success, negative on error
  54. */
  55. int db_postgres_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l)
  56. {
  57. static str dummy_string = {"", 0};
  58. char *tmp_s;
  59. if (!_v) {
  60. LM_ERR("invalid parameter value\n");
  61. }
  62. /*
  63. * We implemented the mysql behaviour in the db_postgres_convert_rows function,
  64. * so a NULL string is a NULL value, otherwise its an empty value
  65. */
  66. if (!_s) {
  67. memset(_v, 0, sizeof(db_val_t));
  68. /* Initialize the string pointers to a dummy empty
  69. * string so that we do not crash when the NULL flag
  70. * is set but the module does not check it properly
  71. */
  72. VAL_STRING(_v) = dummy_string.s;
  73. VAL_STR(_v) = dummy_string;
  74. VAL_BLOB(_v) = dummy_string;
  75. VAL_TYPE(_v) = _t;
  76. VAL_NULL(_v) = 1;
  77. return 0;
  78. }
  79. VAL_NULL(_v) = 0;
  80. switch(_t) {
  81. case DB_INT:
  82. LM_DBG("converting INT [%s]\n", _s);
  83. if (db_str2int(_s, &VAL_INT(_v)) < 0) {
  84. LM_ERR("failed to convert INT value from string\n");
  85. return -2;
  86. } else {
  87. VAL_TYPE(_v) = DB_INT;
  88. return 0;
  89. }
  90. break;
  91. case DB_BIGINT:
  92. LM_DBG("converting BIGINT [%s]\n", _s);
  93. if (db_str2longlong(_s, &VAL_BIGINT(_v)) < 0) {
  94. LM_ERR("failed to convert BIGINT value from string\n");
  95. return -3;
  96. } else {
  97. VAL_TYPE(_v) = DB_BIGINT;
  98. return 0;
  99. }
  100. break;
  101. case DB_BITMAP:
  102. LM_DBG("converting BITMAP [%s]\n", _s);
  103. if (db_str2int(_s, &VAL_INT(_v)) < 0) {
  104. LM_ERR("failed to convert BITMAP value from string\n");
  105. return -4;
  106. } else {
  107. VAL_TYPE(_v) = DB_BITMAP;
  108. return 0;
  109. }
  110. break;
  111. case DB_DOUBLE:
  112. LM_DBG("converting DOUBLE [%s]\n", _s);
  113. if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) {
  114. LM_ERR("failed to convert DOUBLE value from string\n");
  115. return -5;
  116. } else {
  117. VAL_TYPE(_v) = DB_DOUBLE;
  118. return 0;
  119. }
  120. break;
  121. case DB_STRING:
  122. LM_DBG("converting STRING [%s]\n", _s);
  123. VAL_STRING(_v) = _s;
  124. VAL_TYPE(_v) = DB_STRING;
  125. VAL_FREE(_v) = 1;
  126. return 0;
  127. case DB_STR:
  128. LM_DBG("converting STR [%.*s]\n", _l, _s);
  129. VAL_STR(_v).s = (char*)_s;
  130. VAL_STR(_v).len = _l;
  131. VAL_TYPE(_v) = DB_STR;
  132. VAL_FREE(_v) = 1;
  133. _s = 0;
  134. return 0;
  135. case DB_DATETIME:
  136. LM_DBG("converting DATETIME [%s]\n", _s);
  137. if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
  138. LM_ERR("failed to convert datetime\n");
  139. return -6;
  140. } else {
  141. VAL_TYPE(_v) = DB_DATETIME;
  142. return 0;
  143. }
  144. break;
  145. case DB_BLOB:
  146. LM_DBG("converting BLOB [%.*s]\n", _l, _s);
  147. /*
  148. * The string is stored in new allocated memory, which we could
  149. * not free later thus we need to copy it to some new memory here.
  150. */
  151. tmp_s = (char*)PQunescapeBytea((unsigned char*)_s, (size_t*)(void*)&(VAL_BLOB(_v).len));
  152. if(tmp_s==NULL) {
  153. LM_ERR("PQunescapeBytea failed\n");
  154. return -7;
  155. }
  156. VAL_BLOB(_v).s = pkg_malloc(VAL_BLOB(_v).len);
  157. if (VAL_BLOB(_v).s == NULL) {
  158. LM_ERR("no private memory left\n");
  159. PQfreemem(tmp_s);
  160. return -8;
  161. }
  162. LM_DBG("allocate %d bytes memory for BLOB at %p", VAL_BLOB(_v).len, VAL_BLOB(_v).s);
  163. memcpy(VAL_BLOB(_v).s, tmp_s, VAL_BLOB(_v).len);
  164. PQfreemem(tmp_s);
  165. VAL_TYPE(_v) = DB_BLOB;
  166. VAL_FREE(_v) = 1;
  167. LM_DBG("got blob len %d\n", _l);
  168. return 0;
  169. }
  170. return -9;
  171. }
  172. /*!
  173. * \brief Converting a value to a string
  174. *
  175. * Converting a value to a string, used when converting result from a query
  176. * \param _con database connection
  177. * \param _v source value
  178. * \param _s target string
  179. * \param _len target string length
  180. * \return 0 on success, negative on error
  181. */
  182. int db_postgres_val2str(const db_con_t* _con, const db_val_t* _v, char* _s, int* _len)
  183. {
  184. int l, ret;
  185. int pgret;
  186. char *tmp_s;
  187. size_t tmp_len;
  188. char* old_s;
  189. if ((!_v) || (!_s) || (!_len) || (!*_len)) {
  190. LM_ERR("invalid parameter value\n");
  191. return -1;
  192. }
  193. if (VAL_NULL(_v)) {
  194. if (*_len < sizeof("NULL")) {
  195. LM_ERR("buffer too small\n");
  196. return -1;
  197. }
  198. *_len = snprintf(_s, *_len, "NULL");
  199. return 0;
  200. }
  201. switch(VAL_TYPE(_v)) {
  202. case DB_INT:
  203. if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
  204. LM_ERR("failed to convert string to int\n");
  205. return -2;
  206. } else {
  207. return 0;
  208. }
  209. break;
  210. case DB_BIGINT:
  211. if (db_longlong2str(VAL_BIGINT(_v), _s, _len) < 0) {
  212. LM_ERR("failed to convert string to big int\n");
  213. return -3;
  214. } else {
  215. return 0;
  216. }
  217. break;
  218. case DB_BITMAP:
  219. if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
  220. LM_ERR("failed to convert string to int\n");
  221. return -4;
  222. } else {
  223. return 0;
  224. }
  225. break;
  226. case DB_DOUBLE:
  227. if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
  228. LM_ERR("failed to convert string to double\n");
  229. return -5;
  230. } else {
  231. return 0;
  232. }
  233. break;
  234. case DB_STRING:
  235. l = strlen(VAL_STRING(_v));
  236. if (*_len < (l * 2 + 3)) {
  237. LM_ERR("destination buffer too short for string\n");
  238. return -6;
  239. } else {
  240. old_s = _s;
  241. *_s++ = '\'';
  242. ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
  243. l, &pgret);
  244. if(pgret!=0)
  245. {
  246. LM_ERR("PQescapeStringConn failed\n");
  247. return -6;
  248. }
  249. LM_DBG("PQescapeStringConn: in: %d chars,"
  250. " out: %d chars\n", l, ret);
  251. _s += ret;
  252. *_s++ = '\'';
  253. *_s = '\0'; /* FIXME */
  254. *_len = _s - old_s;
  255. return 0;
  256. }
  257. break;
  258. case DB_STR:
  259. l = VAL_STR(_v).len;
  260. if (*_len < (l * 2 + 3)) {
  261. LM_ERR("destination buffer too short for str\n");
  262. return -7;
  263. } else {
  264. old_s = _s;
  265. *_s++ = '\'';
  266. ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
  267. l, &pgret);
  268. if(pgret!=0)
  269. {
  270. LM_ERR("PQescapeStringConn failed \n");
  271. return -7;
  272. }
  273. LM_DBG("PQescapeStringConn: in: %d chars, out: %d chars\n", l, ret);
  274. _s += ret;
  275. *_s++ = '\'';
  276. *_s = '\0'; /* FIXME */
  277. *_len = _s - old_s;
  278. return 0;
  279. }
  280. break;
  281. case DB_DATETIME:
  282. if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
  283. LM_ERR("failed to convert string to time_t\n");
  284. return -8;
  285. } else {
  286. return 0;
  287. }
  288. break;
  289. case DB_BLOB:
  290. l = VAL_BLOB(_v).len;
  291. /* this estimation is not always correct, thus we need to check later again */
  292. if (*_len < (l * 2 + 3)) {
  293. LM_ERR("destination buffer too short for blob\n");
  294. return -9;
  295. } else {
  296. *_s++ = '\'';
  297. tmp_s = (char*)PQescapeByteaConn(CON_CONNECTION(_con), (unsigned char*)VAL_STRING(_v),
  298. (size_t)l, (size_t*)&tmp_len);
  299. if(tmp_s==NULL)
  300. {
  301. LM_ERR("PQescapeByteaConn failed\n");
  302. return -9;
  303. }
  304. if (tmp_len > *_len) {
  305. LM_ERR("escaped result too long\n");
  306. return -9;
  307. }
  308. memcpy(_s, tmp_s, tmp_len);
  309. PQfreemem(tmp_s);
  310. tmp_len = strlen(_s);
  311. *(_s + tmp_len) = '\'';
  312. *(_s + tmp_len + 1) = '\0';
  313. *_len = tmp_len + 2;
  314. return 0;
  315. }
  316. break;
  317. default:
  318. LM_DBG("unknown data type\n");
  319. return -10;
  320. }
  321. }