val.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2007,2008 TRUNK MOBILE
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #include <oci.h>
  26. #include "../../dprint.h"
  27. #include "ora_con.h"
  28. #include "dbase.h"
  29. #include "val.h"
  30. /*
  31. * Convert value to sql-string as db bind index
  32. */
  33. int db_oracle_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
  34. {
  35. int ret;
  36. if (!_c || !_v || !_s || !_len || *_len <= 0) {
  37. LM_ERR("invalid parameter value\n");
  38. return -1;
  39. }
  40. ret = snprintf(_s, *_len, ":%u", ++CON_ORA(_c)->bindpos);
  41. if ((unsigned)ret >= (unsigned)*_len)
  42. return sql_buf_small();
  43. *_len = ret;
  44. return 0;
  45. }
  46. /*
  47. * Called after val2str to realy binding
  48. */
  49. int db_oracle_val2bind(bmap_t* _m, const db_val_t* _v, OCIDate* _o)
  50. {
  51. if (VAL_NULL(_v)) {
  52. _m->addr = NULL;
  53. _m->size = 0;
  54. _m->type = SQLT_NON;
  55. return 0;
  56. }
  57. switch (VAL_TYPE(_v)) {
  58. case DB1_INT:
  59. _m->addr = (int*)&VAL_INT(_v);
  60. _m->size = sizeof(VAL_INT(_v));
  61. _m->type = SQLT_INT;
  62. break;
  63. case DB1_BIGINT:
  64. LM_ERR("BIGINT not supported");
  65. return -1;
  66. case DB1_BITMAP:
  67. _m->addr = (unsigned*)&VAL_BITMAP(_v);
  68. _m->size = sizeof(VAL_BITMAP(_v));
  69. _m->type = SQLT_UIN;
  70. break;
  71. case DB1_DOUBLE:
  72. _m->addr = (double*)&VAL_DOUBLE(_v);
  73. _m->size = sizeof(VAL_DOUBLE(_v));
  74. _m->type = SQLT_FLT;
  75. break;
  76. case DB1_STRING:
  77. _m->addr = (char*)VAL_STRING(_v);
  78. _m->size = strlen(VAL_STRING(_v))+1;
  79. _m->type = SQLT_STR;
  80. break;
  81. case DB1_STR:
  82. {
  83. unsigned len = VAL_STR(_v).len;
  84. char *estr, *pstr = VAL_STR(_v).s;
  85. estr = (char*)memchr(pstr, 0, len);
  86. if (estr) {
  87. LM_WARN("truncate STR len from %u to: '%s'\n",
  88. len, pstr);
  89. len = (unsigned)(estr - pstr) + 1;
  90. }
  91. _m->size = len;
  92. _m->addr = pstr;
  93. _m->type = SQLT_CHR;
  94. }
  95. break;
  96. case DB1_DATETIME:
  97. {
  98. struct tm* tm = localtime(&VAL_TIME(_v));
  99. if (tm->tm_sec == 60)
  100. --tm->tm_sec;
  101. OCIDateSetDate(_o, (ub2)(tm->tm_year + 1900),
  102. (ub1)(tm->tm_mon + 1), (ub1)tm->tm_mday);
  103. OCIDateSetTime(_o, (ub1)tm->tm_hour, (ub1)tm->tm_min,
  104. (ub1)tm->tm_sec);
  105. _m->addr = _o;
  106. _m->size = sizeof(*_o);
  107. _m->type = SQLT_ODT;
  108. }
  109. break;
  110. case DB1_BLOB:
  111. _m->addr = VAL_BLOB(_v).s;
  112. _m->size = VAL_BLOB(_v).len;
  113. _m->type = SQLT_CLOB;
  114. break;
  115. default:
  116. LM_ERR("unknown data type\n");
  117. return -1;
  118. }
  119. return 0;
  120. }