val.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * $Id$
  3. *
  4. * UNIXODBC module
  5. *
  6. * Copyright (C) 2005-2006 Marco Lorrai
  7. * Copyright (C) 2008 1&1 Internet AG
  8. *
  9. * This file is part of Kamailio, a free SIP server.
  10. *
  11. * Kamailio is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * Kamailio is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. *
  26. * History:
  27. * --------
  28. * 2005-12-01 initial commit (chgen)
  29. */
  30. #include "../../dprint.h"
  31. #include "../../lib/kcore/strcommon.h"
  32. #include "../../lib/srdb1/db_ut.h"
  33. #include "db_unixodbc.h"
  34. #include "val.h"
  35. #include "connection.h"
  36. /*
  37. * Used when converting the query to a result
  38. */
  39. int db_unixodbc_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
  40. const unsigned int _cpy)
  41. {
  42. /* db_unixodbc uses the NULL string for NULL SQL values */
  43. if (_v && _s && !strcmp(_s, "NULL")) {
  44. LM_DBG("converting NULL value");
  45. static str dummy_string = {"", 0};
  46. memset(_v, 0, sizeof(db_val_t));
  47. /* Initialize the string pointers to a dummy empty
  48. * string so that we do not crash when the NULL flag
  49. * is set but the module does not check it properly
  50. */
  51. VAL_STRING(_v) = dummy_string.s;
  52. VAL_STR(_v) = dummy_string;
  53. VAL_BLOB(_v) = dummy_string;
  54. VAL_TYPE(_v) = _t;
  55. VAL_NULL(_v) = 1;
  56. return 0;
  57. } else {
  58. return db_str2val(_t, _v, _s, _l, _cpy);
  59. }
  60. }
  61. /*
  62. * Used when converting a result from the query
  63. */
  64. int db_unixodbc_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
  65. {
  66. int l, tmp;
  67. char* old_s;
  68. /* db_unixodbc uses a custom escape function */
  69. tmp = db_val2str(_c, _v, _s, _len);
  70. if (tmp < 1)
  71. return tmp;
  72. switch(VAL_TYPE(_v))
  73. {
  74. case DB1_STRING:
  75. l = strlen(VAL_STRING(_v));
  76. if (*_len < (l * 2 + 3))
  77. {
  78. LM_ERR("destination buffer too short\n");
  79. return -6;
  80. }
  81. else
  82. {
  83. old_s = _s;
  84. *_s++ = '\'';
  85. if(use_escape_common)
  86. {
  87. _s += escape_common(_s, (char*)VAL_STRING(_v), l);
  88. } else {
  89. memcpy(_s, VAL_STRING(_v), l);
  90. _s += l;
  91. }
  92. *_s++ = '\'';
  93. *_s = '\0'; /* FIXME */
  94. *_len = _s - old_s;
  95. return 0;
  96. }
  97. break;
  98. case DB1_STR:
  99. l = VAL_STR(_v).len;
  100. if (*_len < (l * 2 + 3))
  101. {
  102. LM_ERR("destination buffer too short\n");
  103. return -7;
  104. }
  105. else
  106. {
  107. old_s = _s;
  108. *_s++ = '\'';
  109. if(use_escape_common)
  110. {
  111. _s += escape_common(_s, VAL_STR(_v).s, l);
  112. } else {
  113. memcpy(_s, VAL_STR(_v).s, l);
  114. _s += l;
  115. }
  116. *_s++ = '\'';
  117. *_s = '\0'; /* FIXME */
  118. *_len = _s - old_s;
  119. return 0;
  120. }
  121. break;
  122. case DB1_BLOB:
  123. l = VAL_BLOB(_v).len;
  124. if (*_len < (l * 2 + 3))
  125. {
  126. LM_ERR("destination buffer too short\n");
  127. return -9;
  128. }
  129. else
  130. {
  131. old_s = _s;
  132. *_s++ = '\'';
  133. if(use_escape_common)
  134. {
  135. _s += escape_common(_s, VAL_BLOB(_v).s, l);
  136. } else {
  137. memcpy(_s, VAL_BLOB(_v).s, l);
  138. _s += l;
  139. }
  140. *_s++ = '\'';
  141. *_s = '\0'; /* FIXME */
  142. *_len = _s - old_s;
  143. return 0;
  144. }
  145. break;
  146. default:
  147. LM_DBG("unknown data type\n");
  148. return -10;
  149. }
  150. }