sql_trans.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2011 SpeakUp B.V. ([email protected])
  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 <sys/types.h>
  25. #include <unistd.h>
  26. #include "../../mem/mem.h"
  27. #include "../../dprint.h"
  28. #include "../../trim.h"
  29. #include "../../ut.h"
  30. #include "../../lib/kcore/strcommon.h"
  31. #include "sql_trans.h"
  32. #define TR_BUFFER_SIZE 2048
  33. static int _tr_eval_sql_val(pv_value_t *val)
  34. {
  35. int i;
  36. static char _tr_buffer[TR_BUFFER_SIZE];
  37. if(val->flags&PV_TYPE_INT || !(val->flags&PV_VAL_STR)) {
  38. val->rs.s = sint2str(val->ri, &val->rs.len);
  39. val->flags = PV_VAL_STR;
  40. return 0;
  41. }
  42. if(val->rs.len>TR_BUFFER_SIZE/2-1) {
  43. LM_ERR("escape buffer to short");
  44. return -1;
  45. }
  46. _tr_buffer[0] = '\'';
  47. i = escape_common(_tr_buffer+1, val->rs.s, val->rs.len);
  48. _tr_buffer[++i] = '\'';
  49. _tr_buffer[++i] = '\0';
  50. memset(val, 0, sizeof(pv_value_t));
  51. val->flags = PV_VAL_STR;
  52. val->rs.s = _tr_buffer;
  53. val->rs.len = i;
  54. return 0;
  55. }
  56. int tr_eval_sql(struct sip_msg *msg, tr_param_t *tp, int subtype,
  57. pv_value_t *val)
  58. {
  59. static str _sql_null = { "NULL", 4 };
  60. static str _sql_zero = { "0", 1 };
  61. static str _sql_empty = { "''", 2 };
  62. if(val==NULL)
  63. return -1;
  64. switch(subtype) {
  65. case TR_SQL_VAL:
  66. if (val->flags&PV_VAL_NULL) {
  67. val->flags = PV_VAL_STR;
  68. val->rs = _sql_null;
  69. return 0;
  70. } else {
  71. return _tr_eval_sql_val(val);
  72. }
  73. break;
  74. case TR_SQL_VAL_INT:
  75. if (val->flags&PV_VAL_NULL) {
  76. val->flags = PV_VAL_STR;
  77. val->rs = _sql_zero;
  78. return 0;
  79. } else {
  80. return _tr_eval_sql_val(val);
  81. }
  82. break;
  83. case TR_SQL_VAL_STR:
  84. if (val->flags&PV_VAL_NULL) {
  85. val->flags = PV_VAL_STR;
  86. val->rs = _sql_empty;
  87. return 0;
  88. } else {
  89. return _tr_eval_sql_val(val);
  90. }
  91. break;
  92. default:
  93. LM_ERR("unknown subtype %d\n",
  94. subtype);
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. char* tr_parse_sql(str *in, trans_t *t)
  100. {
  101. char *p;
  102. str name;
  103. if(in==NULL || t==NULL)
  104. return NULL;
  105. p = in->s;
  106. name.s = in->s;
  107. t->type = TR_SQL;
  108. t->trf = tr_eval_sql;
  109. /* find next token */
  110. while(is_in_str(p, in) && *p!=TR_PARAM_MARKER && *p!=TR_RBRACKET) p++;
  111. if(*p=='\0') {
  112. LM_ERR("unable to find transformation start: %.*s\n", in->len, in->s);
  113. return NULL;
  114. }
  115. name.len = p - name.s;
  116. trim(&name);
  117. if(name.len==3 && strncasecmp(name.s, "val", 3)==0) {
  118. t->subtype = TR_SQL_VAL;
  119. goto done;
  120. }
  121. if(name.len==7 && strncasecmp(name.s, "val.int", 7)==0) {
  122. t->subtype = TR_SQL_VAL_INT;
  123. goto done;
  124. }
  125. if(name.len==7 && strncasecmp(name.s, "val.str", 7)==0) {
  126. t->subtype = TR_SQL_VAL_STR;
  127. goto done;
  128. }
  129. LM_ERR("unknown transformation: %.*s/%.*s/%d!\n", in->len, in->s,
  130. name.len, name.s, name.len);
  131. return NULL;
  132. done:
  133. t->name = name;
  134. return p;
  135. }