km_val.c 7.7 KB

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