db_res.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /** \ingroup DB_API
  29. * @{
  30. */
  31. #include "db_res.h"
  32. #include "../../dprint.h"
  33. #include "../../mem/mem.h"
  34. #include <string.h>
  35. db_res_t* db_res(db_cmd_t* cmd)
  36. {
  37. db_res_t* newp;
  38. int ret;
  39. newp = (db_res_t*)pkg_malloc(sizeof(db_res_t));
  40. if (newp == NULL) goto err;
  41. memset(newp, '\0', sizeof(db_res_t));
  42. if (db_gen_init(&newp->gen) < 0) goto err;
  43. newp->cmd = cmd;
  44. newp->field_count = cmd->result_count;
  45. ret = db_drv_call(&cmd->ctx->con[db_payload_idx]->uri->scheme,
  46. "db_res", newp, db_payload_idx);
  47. if (ret < 0) goto err;
  48. newp->cur_rec = db_rec(newp, cmd->result);
  49. if (newp->cur_rec == NULL) goto err;
  50. return newp;
  51. err:
  52. ERR("db_res: Cannot create db_res structure\n");
  53. if (newp) {
  54. if (newp->cur_rec) db_rec_free(newp->cur_rec);
  55. db_gen_free(&newp->gen);
  56. pkg_free(newp);
  57. }
  58. return NULL;
  59. }
  60. void db_res_free(db_res_t* r)
  61. {
  62. if (r == NULL) return;
  63. db_gen_free(&r->gen);
  64. if (r->cur_rec) db_rec_free(r->cur_rec);
  65. pkg_free(r);
  66. }
  67. db_rec_t* db_first(db_res_t* res)
  68. {
  69. if (res->cmd->first[0](res) != 0) {
  70. return NULL;
  71. }
  72. return res->cur_rec;
  73. }
  74. db_rec_t* db_next(db_res_t* res)
  75. {
  76. if (res->cmd->next[0](res) != 0) {
  77. return NULL;
  78. }
  79. return res->cur_rec;
  80. }
  81. /** @} */