sql_var.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2008 Elena-Ramona Modroiu (asipto.com)
  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. /*! \file
  23. * \ingroup sqlops
  24. * \brief SIP-router SQL-operations :: Variables
  25. *
  26. * - Module: \ref sqlops
  27. */
  28. #include "../../mem/mem.h"
  29. #include "../../dprint.h"
  30. #include "../../mod_fix.h"
  31. #include "sql_api.h"
  32. #include "sql_var.h"
  33. typedef struct _sql_pv {
  34. str resname;
  35. sql_result_t *res;
  36. int type;
  37. gparam_t row;
  38. gparam_t col;
  39. } sql_pv_t;
  40. int pv_get_dbr(struct sip_msg *msg, pv_param_t *param,
  41. pv_value_t *res)
  42. {
  43. sql_pv_t *spv;
  44. int row;
  45. int col;
  46. spv = (sql_pv_t*)param->pvn.u.dname;
  47. if(spv->res==NULL)
  48. {
  49. spv->res = sql_get_result(&spv->resname);
  50. if(spv->res==NULL)
  51. return pv_get_null(msg, param, res);
  52. }
  53. switch(spv->type)
  54. {
  55. case 1:
  56. return pv_get_sintval(msg, param, res, spv->res->nrows);
  57. break;
  58. case 2:
  59. return pv_get_sintval(msg, param, res, spv->res->ncols);
  60. break;
  61. case 3:
  62. if(fixup_get_ivalue(msg, &spv->row, &row)!=0)
  63. return pv_get_null(msg, param, res);
  64. if(fixup_get_ivalue(msg, &spv->col, &col)!=0)
  65. return pv_get_null(msg, param, res);
  66. if(row>=spv->res->nrows)
  67. return pv_get_null(msg, param, res);
  68. if(col>=spv->res->ncols)
  69. return pv_get_null(msg, param, res);
  70. if(spv->res->vals[row][col].flags&PV_VAL_NULL)
  71. return pv_get_null(msg, param, res);
  72. if(spv->res->vals[row][col].flags&PV_VAL_INT)
  73. return pv_get_sintval(msg, param, res,
  74. spv->res->vals[row][col].value.n);
  75. return pv_get_strval(msg, param, res,
  76. &spv->res->vals[row][col].value.s);
  77. break;
  78. case 4:
  79. if(fixup_get_ivalue(msg, &spv->col, &col)!=0)
  80. return pv_get_null(msg, param, res);
  81. if(col>=spv->res->ncols)
  82. return pv_get_null(msg, param, res);
  83. return pv_get_strval(msg, param, res,
  84. &spv->res->cols[col].name);
  85. break;
  86. }
  87. return 0;
  88. }
  89. int sql_parse_index(str *in, gparam_t *gp)
  90. {
  91. if(in->s[0]==PV_MARKER)
  92. {
  93. gp->type = GPARAM_TYPE_PVS;
  94. gp->v.pvs = (pv_spec_t*)pkg_malloc(sizeof(pv_spec_t));
  95. if (gp->v.pvs == NULL)
  96. {
  97. LM_ERR("no pkg memory left for pv_spec_t\n");
  98. return -1;
  99. }
  100. if(pv_parse_spec(in, gp->v.pvs)==NULL)
  101. {
  102. LM_ERR("invalid PV identifier\n");
  103. pkg_free(gp->v.pvs);
  104. return -1;
  105. }
  106. } else {
  107. gp->type = GPARAM_TYPE_INT;
  108. if(str2sint(in, &gp->v.i) != 0)
  109. {
  110. LM_ERR("bad number <%.*s>\n", in->len, in->s);
  111. return -1;
  112. }
  113. }
  114. return 0;
  115. }
  116. int pv_parse_dbr_name(pv_spec_p sp, str *in)
  117. {
  118. sql_pv_t *spv=NULL;
  119. char *p;
  120. str pvs;
  121. str tok;
  122. spv = (sql_pv_t*)pkg_malloc(sizeof(sql_pv_t));
  123. if(spv==NULL)
  124. return -1;
  125. memset(spv, 0, sizeof(sql_pv_t));
  126. p = in->s;
  127. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  128. p++;
  129. if(p>in->s+in->len || *p=='\0')
  130. goto error;
  131. spv->resname.s = p;
  132. while(p < in->s + in->len)
  133. {
  134. if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  135. break;
  136. p++;
  137. }
  138. if(p>in->s+in->len || *p=='\0')
  139. goto error;
  140. spv->resname.len = p - spv->resname.s;
  141. spv->res = sql_get_result(&spv->resname);
  142. if(*p!='=')
  143. {
  144. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  145. p++;
  146. if(p>in->s+in->len || *p=='\0' || *p!='=')
  147. goto error;
  148. }
  149. p++;
  150. if(*p!='>')
  151. goto error;
  152. p++;
  153. pvs.len = in->len - (int)(p - in->s);
  154. pvs.s = p;
  155. p = pvs.s+pvs.len-1;
  156. while(p>pvs.s && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  157. p--;
  158. if(p==pvs.s)
  159. {
  160. LM_ERR("invalid key in [%.*s]\n", in->len, in->s);
  161. goto error;
  162. }
  163. pvs.len = p - pvs.s + 1;
  164. LM_DBG("res [%.*s] - key [%.*s]\n", spv->resname.len, spv->resname.s,
  165. pvs.len, pvs.s);
  166. if(pvs.len==4 && strncmp(pvs.s, "rows", 4)==0)
  167. {
  168. spv->type = 1;
  169. } else if(pvs.len==4 && strncmp(pvs.s, "cols", 4)==0) {
  170. spv->type = 2;
  171. } else if(pvs.s[0]=='[') {
  172. spv->type = 3;
  173. p = pvs.s+1;
  174. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  175. p++;
  176. if(p>in->s+in->len || *p=='\0')
  177. goto error_index;
  178. tok.s = p;
  179. while(p < in->s + in->len)
  180. {
  181. if(*p==',' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  182. break;
  183. p++;
  184. }
  185. if(p>in->s+in->len || *p=='\0')
  186. goto error_index;
  187. tok.len = p - tok.s;
  188. if(sql_parse_index(&tok, &spv->row)!=0)
  189. goto error_index;
  190. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  191. p++;
  192. if(p>in->s+in->len || *p=='\0' || *p!=',')
  193. goto error_index;
  194. p++;
  195. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  196. p++;
  197. if(p>in->s+in->len || *p=='\0')
  198. goto error_index;
  199. tok.s = p;
  200. while(p < in->s + in->len)
  201. {
  202. if(*p==']' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  203. break;
  204. p++;
  205. }
  206. if(p>in->s+in->len || *p=='\0')
  207. goto error_index;
  208. tok.len = p - tok.s;
  209. if(sql_parse_index(&tok, &spv->col)!=0)
  210. goto error_index;
  211. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  212. p++;
  213. if(p>in->s+in->len || *p=='\0' || *p!=']')
  214. goto error_index;
  215. } else if(pvs.len>9 && strncmp(pvs.s, "colname", 7)==0) {
  216. spv->type = 4;
  217. p = pvs.s+7;
  218. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  219. p++;
  220. if(p>in->s+in->len || *p=='\0' || *p!='[')
  221. goto error_index;
  222. p++;
  223. tok.s = p;
  224. while(p < in->s + in->len)
  225. {
  226. if(*p==']' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  227. break;
  228. p++;
  229. }
  230. if(p>in->s+in->len || *p=='\0')
  231. goto error_index;
  232. tok.len = p - tok.s;
  233. if(sql_parse_index(&tok, &spv->col)!=0)
  234. goto error_index;
  235. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  236. p++;
  237. if(p>in->s+in->len || *p=='\0' || *p!=']')
  238. goto error_index;
  239. } else {
  240. LM_ERR("unknown key [%.*s]\n", pvs.len, pvs.s);
  241. if(spv!=NULL)
  242. pkg_free(spv);
  243. return -1;
  244. }
  245. sp->pvp.pvn.u.dname = (void*)spv;
  246. sp->pvp.pvn.type = PV_NAME_PVAR;
  247. return 0;
  248. error:
  249. LM_ERR("invalid pv name [%.*s]\n", in->len, in->s);
  250. if(spv!=NULL)
  251. pkg_free(spv);
  252. return -1;
  253. error_index:
  254. LM_ERR("invalid index in [%.*s]\n", pvs.len, pvs.s);
  255. if(spv!=NULL)
  256. pkg_free(spv);
  257. return -1;
  258. }