km_bdb_val.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * $Id$
  3. *
  4. * db_berkeley module, portions of this code were templated using
  5. * the dbtext and postgres modules.
  6. * Copyright (C) 2007 Cisco Systems
  7. *
  8. * This file is part of SIP-router, a free SIP server.
  9. *
  10. * SIP-router is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * SIP-router is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2007-09-19 genesis (wiquan)
  27. */
  28. /*! \file
  29. * Berkeley DB :
  30. *
  31. * \ingroup database
  32. */
  33. #include "../../lib/srdb1/db_val.h"
  34. #include "../../lib/srdb1/db_ut.h"
  35. #include "km_db_berkeley.h"
  36. #include "km_bdb_res.h"
  37. #include "km_bdb_val.h"
  38. #include <string.h>
  39. /**
  40. * A copy of db_ut::db_time2str EXCEPT does not wrap the date in single-quotes
  41. *
  42. * Convert a time_t value to string (w.o single-quote)
  43. * \param _v source value
  44. * \param _s target string
  45. * \param _l available length and target length
  46. * \return -1 on error, 0 on success
  47. * \todo This functions add quotes to the time value. This
  48. * should be done in the val2str function, as some databases
  49. * like db_berkeley don't need or like this at all.
  50. */
  51. inline int km_bdb_time2str(time_t _v, char* _s, int* _l)
  52. {
  53. struct tm* t;
  54. int l;
  55. if ((!_s) || (!_l) || (*_l < 2)) {
  56. LM_ERR("Invalid parameter value\n");
  57. return -1;
  58. }
  59. // *_s++ = '\'';
  60. /* Convert time_t structure to format accepted by the database */
  61. t = localtime(&_v);
  62. l = strftime(_s, *_l -1, "%Y-%m-%d %H:%M:%S", t);
  63. if (l == 0) {
  64. LM_ERR("Error during time conversion\n");
  65. /* the value of _s is now unspecified */
  66. _s = NULL;
  67. _l = 0;
  68. return -1;
  69. }
  70. *_l = l;
  71. // *(_s + l) = '\'';
  72. // *_l = l + 2;
  73. return 0;
  74. }
  75. /**
  76. * Does not copy strings
  77. */
  78. int bdb_str2val(db_type_t _t, db_val_t* _v, char* _s, int _l)
  79. {
  80. static str dummy_string = {"", 0};
  81. if(!_s)
  82. {
  83. memset(_v, 0, sizeof(db_val_t));
  84. /* Initialize the string pointers to a dummy empty
  85. * string so that we do not crash when the NULL flag
  86. * is set but the module does not check it properly
  87. */
  88. VAL_STRING(_v) = dummy_string.s;
  89. VAL_STR(_v) = dummy_string;
  90. VAL_BLOB(_v) = dummy_string;
  91. VAL_TYPE(_v) = _t;
  92. VAL_NULL(_v) = 1;
  93. return 0;
  94. }
  95. VAL_NULL(_v) = 0;
  96. switch(_t) {
  97. case DB1_INT:
  98. if (db_str2int(_s, &VAL_INT(_v)) < 0) {
  99. LM_ERR("Error while converting INT value from string\n");
  100. return -2;
  101. } else {
  102. VAL_TYPE(_v) = DB1_INT;
  103. return 0;
  104. }
  105. break;
  106. case DB1_BIGINT:
  107. LM_ERR("BIGINT not supported");
  108. return -1;
  109. case DB1_BITMAP:
  110. if (db_str2int(_s, &VAL_INT(_v)) < 0) {
  111. LM_ERR("Error while converting BITMAP value from string\n");
  112. return -3;
  113. } else {
  114. VAL_TYPE(_v) = DB1_BITMAP;
  115. return 0;
  116. }
  117. break;
  118. case DB1_DOUBLE:
  119. if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) {
  120. LM_ERR("Error while converting DOUBLE value from string\n");
  121. return -4;
  122. } else {
  123. VAL_TYPE(_v) = DB1_DOUBLE;
  124. return 0;
  125. }
  126. break;
  127. case DB1_STRING:
  128. VAL_STRING(_v) = _s;
  129. VAL_TYPE(_v) = DB1_STRING;
  130. VAL_FREE(_v) = 1;
  131. if( strlen(_s)==4 && !strncasecmp(_s, "NULL", 4) )
  132. VAL_NULL(_v) = 1;
  133. return 0;
  134. case DB1_STR:
  135. VAL_STR(_v).s = (char*)_s;
  136. VAL_STR(_v).len = _l;
  137. VAL_TYPE(_v) = DB1_STR;
  138. VAL_FREE(_v) = 1;
  139. if( strlen(_s)==4 && !strncasecmp(_s, "NULL", 4) )
  140. VAL_NULL(_v) = 1;
  141. return 0;
  142. case DB1_DATETIME:
  143. if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
  144. LM_ERR("Error converting datetime\n");
  145. return -5;
  146. } else {
  147. VAL_TYPE(_v) = DB1_DATETIME;
  148. return 0;
  149. }
  150. break;
  151. case DB1_BLOB:
  152. VAL_BLOB(_v).s = _s;
  153. VAL_TYPE(_v) = DB1_BLOB;
  154. LM_DBG("got blob len %d\n", _l);
  155. return 0;
  156. }
  157. return -6;
  158. }
  159. /*
  160. * Used when converting result from a query
  161. */
  162. int km_bdb_val2str(db_val_t* _v, char* _s, int* _len)
  163. {
  164. int l;
  165. if (VAL_NULL(_v))
  166. {
  167. *_len = snprintf(_s, *_len, "NULL");
  168. return 0;
  169. }
  170. switch(VAL_TYPE(_v)) {
  171. case DB1_INT:
  172. if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
  173. LM_ERR("Error while converting int to string\n");
  174. return -2;
  175. } else {
  176. LM_DBG("Converted int to string\n");
  177. return 0;
  178. }
  179. break;
  180. case DB1_BITMAP:
  181. if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
  182. LM_ERR("Error while converting bitmap to string\n");
  183. return -3;
  184. } else {
  185. LM_DBG("Converted bitmap to string\n");
  186. return 0;
  187. }
  188. break;
  189. case DB1_DOUBLE:
  190. if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
  191. LM_ERR("Error while converting double to string\n");
  192. return -3;
  193. } else {
  194. LM_DBG("Converted double to string\n");
  195. return 0;
  196. }
  197. break;
  198. case DB1_STRING:
  199. l = strlen(VAL_STRING(_v));
  200. if (*_len < l )
  201. { LM_ERR("Destination buffer too short for string\n");
  202. return -4;
  203. }
  204. else
  205. { LM_DBG("Converted string to string\n");
  206. strncpy(_s, VAL_STRING(_v) , l);
  207. _s[l] = 0;
  208. *_len = l;
  209. return 0;
  210. }
  211. break;
  212. case DB1_STR:
  213. l = VAL_STR(_v).len;
  214. if (*_len < l)
  215. {
  216. LM_ERR("Destination buffer too short for str\n");
  217. return -5;
  218. }
  219. else
  220. {
  221. LM_DBG("Converted str to string\n");
  222. strncpy(_s, VAL_STR(_v).s , VAL_STR(_v).len);
  223. *_len = VAL_STR(_v).len;
  224. return 0;
  225. }
  226. break;
  227. case DB1_DATETIME:
  228. if (km_bdb_time2str(VAL_TIME(_v), _s, _len) < 0) {
  229. LM_ERR("Error while converting time_t to string\n");
  230. return -6;
  231. } else {
  232. LM_DBG("Converted time_t to string\n");
  233. return 0;
  234. }
  235. break;
  236. case DB1_BLOB:
  237. l = VAL_BLOB(_v).len;
  238. if (*_len < l)
  239. {
  240. LM_ERR("Destination buffer too short for blob\n");
  241. return -7;
  242. }
  243. else
  244. {
  245. LM_DBG("Converting BLOB [%s]\n", _s);
  246. _s = VAL_BLOB(_v).s;
  247. *_len = 0;
  248. return -8;
  249. }
  250. break;
  251. default:
  252. LM_DBG("Unknown data type\n");
  253. return -8;
  254. }
  255. }