sqlops.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*! \file
  23. * \ingroup sqlops
  24. * \brief SIP-router SQL-operations :: Module interface
  25. *
  26. * - Module: \ref sqlops
  27. */
  28. /*! \defgroup sqlops SIP-Router :: SQL Operations
  29. * \note Kamailio module - part of modules_k
  30. * The module adds support for raw SQL queries in the configuration file.
  31. */
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <sys/types.h>
  36. #include <sys/ipc.h>
  37. #include <unistd.h>
  38. #include <fcntl.h>
  39. #include "../../sr_module.h"
  40. #include "../../dprint.h"
  41. #include "../../pvar.h"
  42. #include "sql_api.h"
  43. #include "sql_var.h"
  44. #include "sql_trans.h"
  45. MODULE_VERSION
  46. static int bind_sqlops(sqlops_api_t* api);
  47. /** module functions */
  48. static int sql_query(struct sip_msg*, char*, char*, char*);
  49. static int sql_query2(struct sip_msg*, char*, char*);
  50. #ifdef WITH_XAVP
  51. static int sql_xquery(struct sip_msg *msg, char *dbl, char *query, char *res);
  52. #endif
  53. static int sql_pvquery(struct sip_msg *msg, char *dbl, char *query, char *res);
  54. static int sql_rfree(struct sip_msg*, char*, char*);
  55. static int child_init(int rank);
  56. static void destroy(void);
  57. static int fixup_sql_query(void** param, int param_no);
  58. #ifdef WITH_XAVP
  59. static int fixup_sql_xquery(void** param, int param_no);
  60. #endif
  61. static int fixup_sql_pvquery(void** param, int param_no);
  62. static int fixup_sql_rfree(void** param, int param_no);
  63. static int sql_con_param(modparam_t type, void* val);
  64. static int sql_res_param(modparam_t type, void* val);
  65. static pv_export_t mod_pvs[] = {
  66. { {"dbr", sizeof("dbr")-1}, PVT_OTHER, pv_get_dbr, 0,
  67. pv_parse_dbr_name, 0, 0, 0 },
  68. { {"sqlrows", sizeof("sqlrows")-1}, PVT_OTHER, pv_get_sqlrows, 0,
  69. pv_parse_con_name, 0, 0, 0 },
  70. { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
  71. };
  72. static cmd_export_t cmds[]={
  73. {"sql_query", (cmd_function)sql_query, 3, fixup_sql_query, 0,
  74. ANY_ROUTE},
  75. {"sql_query", (cmd_function)sql_query2, 2, fixup_sql_query, 0,
  76. ANY_ROUTE},
  77. #ifdef WITH_XAVP
  78. {"sql_xquery", (cmd_function)sql_xquery, 3, fixup_sql_xquery, 0,
  79. ANY_ROUTE},
  80. #endif
  81. {"sql_pvquery", (cmd_function)sql_pvquery, 3, fixup_sql_pvquery, 0,
  82. ANY_ROUTE},
  83. {"sql_result_free", (cmd_function)sql_rfree, 1, fixup_sql_rfree, 0,
  84. ANY_ROUTE},
  85. {"bind_sqlops", (cmd_function)bind_sqlops, 0, 0, 0, 0},
  86. {0,0,0,0,0,0}
  87. };
  88. static param_export_t params[]={
  89. {"sqlcon", STR_PARAM|USE_FUNC_PARAM, (void*)sql_con_param},
  90. {"sqlres", STR_PARAM|USE_FUNC_PARAM, (void*)sql_res_param},
  91. {0,0,0}
  92. };
  93. static tr_export_t mod_trans[] = {
  94. { {"sql", sizeof("sql")-1}, tr_parse_sql },
  95. { { 0, 0 }, 0 }
  96. };
  97. /** module exports */
  98. struct module_exports exports= {
  99. "sqlops",
  100. DEFAULT_DLFLAGS, /* dlopen flags */
  101. cmds,
  102. params,
  103. 0, /* exported statistics */
  104. 0 , /* exported MI functions */
  105. mod_pvs, /* exported pseudo-variables */
  106. 0, /* extra processes */
  107. 0, /* module initialization function */
  108. 0,
  109. (destroy_function) destroy,
  110. child_init /* per-child init function */
  111. };
  112. int mod_register(char *path, int *dlflags, void *p1, void *p2)
  113. {
  114. return register_trans_mod(path, mod_trans);
  115. }
  116. static int child_init(int rank)
  117. {
  118. if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  119. return 0;
  120. return sql_connect();
  121. }
  122. /**
  123. * destroy function
  124. */
  125. static void destroy(void)
  126. {
  127. sql_destroy();
  128. }
  129. /**
  130. * parse sqlcon module parameter
  131. */
  132. int sql_con_param(modparam_t type, void *val)
  133. {
  134. if(val==NULL)
  135. goto error;
  136. return sql_parse_param((char*)val);
  137. error:
  138. return -1;
  139. }
  140. /**
  141. * parse sqlres module parameter
  142. */
  143. int sql_res_param(modparam_t type, void *val)
  144. {
  145. sql_result_t *res = NULL;
  146. str s;
  147. if(val==NULL)
  148. {
  149. LM_ERR("invalid parameter\n");
  150. goto error;
  151. }
  152. s.s = (char*)val;
  153. s.len = strlen(s.s);
  154. res = sql_get_result(&s);
  155. if(res==NULL)
  156. {
  157. LM_ERR("invalid result [%s]\n", s.s);
  158. goto error;
  159. }
  160. return 0;
  161. error:
  162. return -1;
  163. }
  164. /**
  165. *
  166. */
  167. static int sql_query(struct sip_msg *msg, char *dbl, char *query, char *res)
  168. {
  169. str sq;
  170. if(pv_printf_s(msg, (pv_elem_t*)query, &sq)!=0)
  171. {
  172. LM_ERR("cannot print the sql query\n");
  173. return -1;
  174. }
  175. return sql_do_query((sql_con_t*)dbl, &sq, (sql_result_t*)res);
  176. }
  177. static int sql_query2(struct sip_msg *msg, char *dbl, char *query)
  178. {
  179. return sql_query(msg, dbl, query, NULL);
  180. }
  181. #ifdef WITH_XAVP
  182. /**
  183. *
  184. */
  185. static int sql_xquery(struct sip_msg *msg, char *dbl, char *query, char *res)
  186. {
  187. return sql_do_xquery(msg, (sql_con_t*)dbl, (pv_elem_t*)query, (pv_elem_t*)res);
  188. }
  189. #endif
  190. /**
  191. *
  192. */
  193. static int sql_pvquery(struct sip_msg *msg, char *dbl, char *query, char *res)
  194. {
  195. return sql_do_pvquery(msg, (sql_con_t*)dbl, (pv_elem_t*)query, (pvname_list_t*)res);
  196. }
  197. /**
  198. *
  199. */
  200. static int sql_rfree(struct sip_msg *msg, char *res, char *s2)
  201. {
  202. sql_reset_result((sql_result_t*)res);
  203. return 1;
  204. }
  205. /**
  206. *
  207. */
  208. static int fixup_sql_query(void** param, int param_no)
  209. {
  210. sql_con_t *con = NULL;
  211. pv_elem_t *query = NULL;
  212. sql_result_t *res = NULL;
  213. str s;
  214. s.s = (char*)(*param);
  215. s.len = strlen(s.s);
  216. if (param_no==1) {
  217. con = sql_get_connection(&s);
  218. if(con==NULL)
  219. {
  220. LM_ERR("invalid connection [%s]\n", s.s);
  221. return E_UNSPEC;
  222. }
  223. *param = (void*)con;
  224. } else if (param_no==2) {
  225. if(pv_parse_format(&s, &query)<0)
  226. {
  227. LM_ERR("invalid query string [%s]\n", s.s);
  228. return E_UNSPEC;
  229. }
  230. *param = (void*)query;
  231. } else if (param_no==3) {
  232. res = sql_get_result(&s);
  233. if(res==NULL)
  234. {
  235. LM_ERR("invalid result [%s]\n", s.s);
  236. return E_UNSPEC;
  237. }
  238. *param = (void*)res;
  239. }
  240. return 0;
  241. }
  242. #ifdef WITH_XAVP
  243. /**
  244. *
  245. */
  246. static int fixup_sql_xquery(void** param, int param_no)
  247. {
  248. sql_con_t *con = NULL;
  249. pv_elem_t *pv = NULL;
  250. str s;
  251. s.s = (char*)(*param);
  252. s.len = strlen(s.s);
  253. if (param_no==1) {
  254. con = sql_get_connection(&s);
  255. if(con==NULL)
  256. {
  257. LM_ERR("invalid connection [%s]\n", s.s);
  258. return E_UNSPEC;
  259. }
  260. *param = (void*)con;
  261. } else if (param_no==2) {
  262. if(pv_parse_format(&s, &pv)<0)
  263. {
  264. LM_ERR("invalid query string [%s]\n", s.s);
  265. return E_UNSPEC;
  266. }
  267. *param = (void*)pv;
  268. } else if (param_no==3) {
  269. if(pv_parse_format(&s, &pv)<0)
  270. {
  271. LM_ERR("invalid result [%s]\n", s.s);
  272. return E_UNSPEC;
  273. }
  274. *param = (void*)pv;
  275. }
  276. return 0;
  277. }
  278. #endif
  279. /**
  280. *
  281. */
  282. static int fixup_sql_pvquery(void** param, int param_no)
  283. {
  284. sql_con_t *con = NULL;
  285. pv_elem_t *pv = NULL;
  286. pvname_list_t *res = NULL;
  287. pvname_list_t *pvl = NULL;
  288. str s;
  289. int i;
  290. if(*param == NULL)
  291. {
  292. LM_ERR("missing parameter %d\n", param_no);
  293. return E_UNSPEC;
  294. }
  295. s.s = (char*)(*param);
  296. s.len = strlen(s.s);
  297. if (param_no==1) {
  298. con = sql_get_connection(&s);
  299. if(con==NULL)
  300. {
  301. LM_ERR("invalid connection [%s]\n", s.s);
  302. return E_UNSPEC;
  303. }
  304. *param = (void*)con;
  305. } else if (param_no==2) {
  306. if(pv_parse_format(&s, &pv)<0)
  307. {
  308. LM_ERR("invalid query string [%s]\n", s.s);
  309. return E_UNSPEC;
  310. }
  311. *param = (void*)pv;
  312. } else if (param_no==3) {
  313. /* parse result variables into list of pv_spec_t's */
  314. res = parse_pvname_list(&s, 0);
  315. if(res==NULL)
  316. {
  317. LM_ERR("invalid result parameter [%s]\n", s.s);
  318. return E_UNSPEC;
  319. }
  320. /* check if all result variables are writable */
  321. pvl = res;
  322. i = 1;
  323. while (pvl) {
  324. if (pvl->sname.setf == NULL)
  325. {
  326. LM_ERR("result variable [%d] is read-only\n", i);
  327. return E_UNSPEC;
  328. }
  329. i++;
  330. pvl = pvl->next;
  331. }
  332. *param = (void*)res;
  333. return 0;
  334. }
  335. return 0;
  336. }
  337. /**
  338. *
  339. */
  340. static int fixup_sql_rfree(void** param, int param_no)
  341. {
  342. sql_result_t *res = NULL;
  343. str s;
  344. s.s = (char*)(*param);
  345. s.len = strlen(s.s);
  346. if (param_no==1) {
  347. res = sql_get_result(&s);
  348. if(res==NULL)
  349. {
  350. LM_ERR("invalid result [%s]\n", s.s);
  351. return E_UNSPEC;
  352. }
  353. *param = (void*)res;
  354. }
  355. return 0;
  356. }
  357. /**
  358. * @brief bind functions to SQLOPS API structure
  359. */
  360. static int bind_sqlops(sqlops_api_t* api)
  361. {
  362. if (!api) {
  363. ERR("Invalid parameter value\n");
  364. return -1;
  365. }
  366. api->query = sqlops_do_query;
  367. api->value = sqlops_get_value;
  368. api->is_null = sqlops_is_null;
  369. api->column = sqlops_get_column;
  370. api->reset = sqlops_reset_result;
  371. api->nrows = sqlops_num_rows;
  372. api->ncols = sqlops_num_columns;
  373. api->xquery = sqlops_do_xquery;
  374. return 0;
  375. }