km_val.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * $Id$
  3. *
  4. * POSTGRES module, portions of this code were templated using
  5. * the mysql module, thus it's similarity.
  6. *
  7. * Copyright (C) 2003 August.Net Services, LLC
  8. * Copyright (C) 2008 1&1 Internet AG
  9. *
  10. * This file is part of Kamailio, a free SIP server.
  11. *
  12. * Kamailio is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version
  16. *
  17. * Kamailio is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * ---
  27. *
  28. * History
  29. * -------
  30. * 2003-04-06 initial code written (Greg Fausak/Andy Fullford)
  31. * 2003-04-14 gmtime changed to localtime because mktime later
  32. * expects localtime, changed daylight saving bug
  33. * previously found in mysql module (janakj)
  34. *
  35. */
  36. #include "../../db/db_val.h"
  37. #include "../../db/db_ut.h"
  38. #include "../../dprint.h"
  39. #include "pg_con.h"
  40. #include "../../mem/mem.h"
  41. #include "val.h"
  42. #include <string.h>
  43. #include <time.h>
  44. /*
  45. * Convert a str to a db value, does not copy strings
  46. * The postgresql module uses a custom escape function for BLOBs.
  47. * If the _s is linked in the db_val result, it will be returned zero
  48. */
  49. int db_postgres_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l)
  50. {
  51. static str dummy_string = {"", 0};
  52. if (!_v) {
  53. LM_ERR("invalid parameter value\n");
  54. }
  55. if (!_s) {
  56. memset(_v, 0, sizeof(db_val_t));
  57. /* Initialize the string pointers to a dummy empty
  58. * string so that we do not crash when the NULL flag
  59. * is set but the module does not check it properly
  60. */
  61. VAL_STRING(_v) = dummy_string.s;
  62. VAL_STR(_v) = dummy_string;
  63. VAL_BLOB(_v) = dummy_string;
  64. VAL_TYPE(_v) = _t;
  65. VAL_NULL(_v) = 1;
  66. return 0;
  67. }
  68. VAL_NULL(_v) = 0;
  69. switch(_t) {
  70. case DB_INT:
  71. LM_DBG("converting INT [%s]\n", _s);
  72. if (db_str2int(_s, &VAL_INT(_v)) < 0) {
  73. LM_ERR("failed to convert INT value from string\n");
  74. return -2;
  75. } else {
  76. VAL_TYPE(_v) = DB_INT;
  77. return 0;
  78. }
  79. break;
  80. case DB_BITMAP:
  81. LM_DBG("converting BITMAP [%s]\n", _s);
  82. if (db_str2int(_s, &VAL_INT(_v)) < 0) {
  83. LM_ERR("failed to convert BITMAP value from string\n");
  84. return -3;
  85. } else {
  86. VAL_TYPE(_v) = DB_BITMAP;
  87. return 0;
  88. }
  89. break;
  90. case DB_DOUBLE:
  91. LM_DBG("converting DOUBLE [%s]\n", _s);
  92. if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) {
  93. LM_ERR("failed to convert DOUBLE value from string\n");
  94. return -4;
  95. } else {
  96. VAL_TYPE(_v) = DB_DOUBLE;
  97. return 0;
  98. }
  99. break;
  100. case DB_STRING:
  101. LM_DBG("converting STRING [%s]\n", _s);
  102. VAL_STRING(_v) = _s;
  103. VAL_TYPE(_v) = DB_STRING;
  104. VAL_FREE(_v) = 1;
  105. return 0;
  106. case DB_STR:
  107. LM_DBG("converting STR [%.*s]\n", _l, _s);
  108. VAL_STR(_v).s = (char*)_s;
  109. VAL_STR(_v).len = _l;
  110. VAL_TYPE(_v) = DB_STR;
  111. VAL_FREE(_v) = 1;
  112. _s = 0;
  113. return 0;
  114. case DB_DATETIME:
  115. LM_DBG("converting DATETIME [%s]\n", _s);
  116. if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
  117. LM_ERR("failed to convert datetime\n");
  118. return -5;
  119. } else {
  120. VAL_TYPE(_v) = DB_DATETIME;
  121. return 0;
  122. }
  123. break;
  124. case DB_BLOB:
  125. LM_DBG("converting BLOB [%.*s]\n", _l, _s);
  126. /* PQunescapeBytea: Converts a string representation of binary data
  127. * into binary data - the reverse of PQescapeBytea.
  128. * This is needed when retrieving bytea data in text format,
  129. * but not when retrieving it in binary format.
  130. */
  131. VAL_BLOB(_v).s = (char*)PQunescapeBytea((unsigned char*)_s,
  132. (size_t*)(void*)&(VAL_BLOB(_v).len) );
  133. VAL_TYPE(_v) = DB_BLOB;
  134. VAL_FREE(_v) = 1;
  135. LM_DBG("got blob len %d\n", _l);
  136. return 0;
  137. }
  138. return -6;
  139. }
  140. /*
  141. * Used when converting result from a query
  142. */
  143. int db_postgres_val2str(const db_con_t* _con, const db_val_t* _v, char* _s, int* _len)
  144. {
  145. int l, ret;
  146. int pgret;
  147. char *tmp_s;
  148. size_t tmp_len;
  149. char* old_s;
  150. if ((!_v) || (!_s) || (!_len) || (!*_len)) {
  151. LM_ERR("invalid parameter value\n");
  152. return -1;
  153. }
  154. if (VAL_NULL(_v)) {
  155. *_len = snprintf(_s, *_len, "NULL");
  156. return 0;
  157. }
  158. switch(VAL_TYPE(_v)) {
  159. case DB_INT:
  160. if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
  161. LM_ERR("failed to convert string to int\n");
  162. return -2;
  163. } else {
  164. return 0;
  165. }
  166. break;
  167. case DB_BITMAP:
  168. if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
  169. LM_ERR("failed to convert string to int\n");
  170. return -3;
  171. } else {
  172. return 0;
  173. }
  174. break;
  175. case DB_DOUBLE:
  176. if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
  177. LM_ERR("failed to convert string to double\n");
  178. return -3;
  179. } else {
  180. return 0;
  181. }
  182. break;
  183. case DB_STRING:
  184. l = strlen(VAL_STRING(_v));
  185. if (*_len < (l * 2 + 3)) {
  186. LM_ERR("destination buffer too short for string\n");
  187. return -4;
  188. } else {
  189. old_s = _s;
  190. *_s++ = '\'';
  191. ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
  192. l, &pgret);
  193. if(pgret!=0)
  194. {
  195. LM_ERR("PQescapeStringConn failed\n");
  196. return -4;
  197. }
  198. LM_DBG("PQescapeStringConn: in: %d chars,"
  199. " out: %d chars\n", l, ret);
  200. _s += ret;
  201. *_s++ = '\'';
  202. *_s = '\0'; /* FIXME */
  203. *_len = _s - old_s;
  204. return 0;
  205. }
  206. break;
  207. case DB_STR:
  208. l = VAL_STR(_v).len;
  209. if (*_len < (l * 2 + 3)) {
  210. LM_ERR("destination buffer too short for str\n");
  211. return -5;
  212. } else {
  213. old_s = _s;
  214. *_s++ = '\'';
  215. ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
  216. l, &pgret);
  217. if(pgret!=0)
  218. {
  219. LM_ERR("PQescapeStringConn failed \n");
  220. return -5;
  221. }
  222. LM_DBG("PQescapeStringConn: in: %d chars, out: %d chars\n", l, ret);
  223. _s += ret;
  224. *_s++ = '\'';
  225. *_s = '\0'; /* FIXME */
  226. *_len = _s - old_s;
  227. return 0;
  228. }
  229. break;
  230. case DB_DATETIME:
  231. if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
  232. LM_ERR("failed to convert string to time_t\n");
  233. return -6;
  234. } else {
  235. return 0;
  236. }
  237. break;
  238. case DB_BLOB:
  239. l = VAL_BLOB(_v).len;
  240. /* this estimation is not always correct, thus we need to check later again */
  241. if (*_len < (l * 2 + 3)) {
  242. LM_ERR("destination buffer too short for blob\n");
  243. return -7;
  244. } else {
  245. *_s++ = '\'';
  246. tmp_s = (char*)PQescapeByteaConn(CON_CONNECTION(_con), (unsigned char*)VAL_STRING(_v),
  247. (size_t)l, (size_t*)&tmp_len);
  248. if(tmp_s==NULL)
  249. {
  250. LM_ERR("PQescapeBytea failed\n");
  251. return -7;
  252. }
  253. if (tmp_len > *_len) {
  254. LM_ERR("escaped result too long\n");
  255. return -7;
  256. }
  257. memcpy(_s, tmp_s, tmp_len);
  258. PQfreemem(tmp_s);
  259. tmp_len = strlen(_s);
  260. *(_s + tmp_len) = '\'';
  261. *(_s + tmp_len + 1) = '\0';
  262. *_len = tmp_len + 2;
  263. return 0;
  264. }
  265. break;
  266. default:
  267. LM_DBG("unknown data type\n");
  268. return -7;
  269. }
  270. return -8;
  271. }