2
0

my_res.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. * Copyright (C) 2006-2007 iptelorg GmbH
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "my_res.h"
  22. #include "my_cmd.h"
  23. #include "../../mem/mem.h"
  24. #include "../../dprint.h"
  25. #include "../../lib/srdb2/db_gen.h"
  26. #include <mysql/mysql.h>
  27. void my_res_free(db_res_t* res, struct my_res* payload)
  28. {
  29. struct my_cmd* mcmd;
  30. mcmd = DB_GET_PAYLOAD(res->cmd);
  31. if (mcmd->st && mysql_stmt_free_result(mcmd->st)) {
  32. ERR("mysql: Error while freeing MySQL result: %d, %s\n",
  33. mysql_stmt_errno(mcmd->st), mysql_stmt_error(mcmd->st));
  34. }
  35. db_drv_free(&payload->gen);
  36. pkg_free(payload);
  37. }
  38. /*
  39. * Attach a mysql specific structure to db_res, this structure contains a pointer
  40. * to my_res_free which releases the mysql result stored in the mysql statement
  41. * and if there is a cursor open in the statement then it will be closed as well
  42. */
  43. int my_res(db_res_t* res)
  44. {
  45. struct my_res* mr;
  46. mr = (struct my_res*)pkg_malloc(sizeof(struct my_res));
  47. if (mr == NULL) {
  48. ERR("mysql: No memory left\n");
  49. return -1;
  50. }
  51. if (db_drv_init(&mr->gen, my_res_free) < 0) goto error;
  52. DB_SET_PAYLOAD(res, mr);
  53. return 0;
  54. error:
  55. if (mr) {
  56. db_drv_free(&mr->gen);
  57. pkg_free(mr);
  58. }
  59. return -1;
  60. }