my_res.c 2.1 KB

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