sqlops.c 9.0 KB

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