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. pkg_free(gp);
  99. return -1;
  100. }
  101. if(pv_parse_spec(in, gp->v.pvs)==NULL)
  102. {
  103. LM_ERR("invalid PV identifier\n");
  104. pkg_free(gp->v.pvs);
  105. pkg_free(gp);
  106. return -1;
  107. }
  108. } else {
  109. gp->type = GPARAM_TYPE_INT;
  110. if(str2sint(in, &gp->v.i) != 0)
  111. {
  112. LM_ERR("bad number <%.*s>\n", in->len, in->s);
  113. return -1;
  114. }
  115. }
  116. return 0;
  117. }
  118. int pv_parse_dbr_name(pv_spec_p sp, str *in)
  119. {
  120. sql_pv_t *spv=NULL;
  121. char *p;
  122. str pvs;
  123. str tok;
  124. spv = (sql_pv_t*)pkg_malloc(sizeof(sql_pv_t));
  125. if(spv==NULL)
  126. return -1;
  127. memset(spv, 0, sizeof(sql_pv_t));
  128. p = in->s;
  129. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  130. p++;
  131. if(p>in->s+in->len || *p=='\0')
  132. goto error;
  133. spv->resname.s = p;
  134. while(p < in->s + in->len)
  135. {
  136. if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  137. break;
  138. p++;
  139. }
  140. if(p>in->s+in->len || *p=='\0')
  141. goto error;
  142. spv->resname.len = p - spv->resname.s;
  143. spv->res = sql_get_result(&spv->resname);
  144. if(*p!='=')
  145. {
  146. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  147. p++;
  148. if(p>in->s+in->len || *p=='\0' || *p!='=')
  149. goto error;
  150. }
  151. p++;
  152. if(*p!='>')
  153. goto error;
  154. p++;
  155. pvs.len = in->len - (int)(p - in->s);
  156. pvs.s = p;
  157. p = pvs.s+pvs.len-1;
  158. while(p>pvs.s && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  159. p--;
  160. if(p==pvs.s)
  161. {
  162. LM_ERR("invalid key in [%.*s]\n", in->len, in->s);
  163. goto error;
  164. }
  165. pvs.len = p - pvs.s + 1;
  166. LM_DBG("res [%.*s] - key [%.*s]\n", spv->resname.len, spv->resname.s,
  167. pvs.len, pvs.s);
  168. if(pvs.len==4 && strncmp(pvs.s, "rows", 4)==0)
  169. {
  170. spv->type = 1;
  171. } else if(pvs.len==4 && strncmp(pvs.s, "cols", 4)==0) {
  172. spv->type = 2;
  173. } else if(pvs.s[0]=='[') {
  174. spv->type = 3;
  175. p = pvs.s+1;
  176. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  177. p++;
  178. if(p>in->s+in->len || *p=='\0')
  179. goto error_index;
  180. tok.s = p;
  181. while(p < in->s + in->len)
  182. {
  183. if(*p==',' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  184. break;
  185. p++;
  186. }
  187. if(p>in->s+in->len || *p=='\0')
  188. goto error_index;
  189. tok.len = p - tok.s;
  190. if(sql_parse_index(&tok, &spv->row)!=0)
  191. goto error_index;
  192. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  193. p++;
  194. if(p>in->s+in->len || *p=='\0' || *p!=',')
  195. goto error_index;
  196. p++;
  197. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  198. p++;
  199. if(p>in->s+in->len || *p=='\0')
  200. goto error_index;
  201. tok.s = p;
  202. while(p < in->s + in->len)
  203. {
  204. if(*p==']' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  205. break;
  206. p++;
  207. }
  208. if(p>in->s+in->len || *p=='\0')
  209. goto error_index;
  210. tok.len = p - tok.s;
  211. if(sql_parse_index(&tok, &spv->col)!=0)
  212. goto error_index;
  213. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  214. p++;
  215. if(p>in->s+in->len || *p=='\0' || *p!=']')
  216. goto error_index;
  217. } else if(pvs.len>9 && strncmp(pvs.s, "colname", 7)==0) {
  218. spv->type = 4;
  219. p = pvs.s+7;
  220. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  221. p++;
  222. if(p>in->s+in->len || *p=='\0' || *p!='[')
  223. goto error_index;
  224. p++;
  225. tok.s = p;
  226. while(p < in->s + in->len)
  227. {
  228. if(*p==']' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  229. break;
  230. p++;
  231. }
  232. if(p>in->s+in->len || *p=='\0')
  233. goto error_index;
  234. tok.len = p - tok.s;
  235. if(sql_parse_index(&tok, &spv->col)!=0)
  236. goto error_index;
  237. while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  238. p++;
  239. if(p>in->s+in->len || *p=='\0' || *p!=']')
  240. goto error_index;
  241. } else {
  242. LM_ERR("unknow key [%.*s]\n", pvs.len, pvs.s);
  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. }