db_con.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_con.h"
  32. #include "../../mem/mem.h"
  33. #include "../../dprint.h"
  34. #include <string.h>
  35. #include <stdlib.h>
  36. /*
  37. * Default implementation of the connect function is noop,
  38. * db drivers can override the function pointer in db_con
  39. * structures
  40. */
  41. static int db_con_connect(db_con_t* con)
  42. {
  43. return 0;
  44. }
  45. /*
  46. * Default implementation of the disconnect function is noop,
  47. * db drivers can override the function pointer in db_con
  48. * structures
  49. */
  50. static void db_con_disconnect(db_con_t* con)
  51. {
  52. }
  53. /*
  54. * Create a new generic db_con structure representing a
  55. * database connection and call the driver specific function
  56. * in the driver that is associated with the structure based
  57. * on the scheme of uri parameter
  58. */
  59. db_con_t* db_con(db_ctx_t* ctx, db_uri_t* uri)
  60. {
  61. db_con_t* newp;
  62. newp = (db_con_t*)pkg_malloc(sizeof(db_con_t));
  63. if (newp == NULL) {
  64. ERR("db_con: No memory left\n");
  65. goto error;
  66. }
  67. memset(newp, '\0', sizeof(db_con_t));
  68. if (db_gen_init(&newp->gen) < 0) goto error;
  69. newp->uri = uri;
  70. newp->ctx = ctx;
  71. newp->connect = db_con_connect;
  72. newp->disconnect = db_con_disconnect;
  73. /* Call db_ctx function if the driver has it */
  74. if (db_drv_call(&uri->scheme, "db_con", newp, ctx->con_n) < 0) {
  75. goto error;
  76. }
  77. return newp;
  78. error:
  79. if (newp) {
  80. db_gen_free(&newp->gen);
  81. pkg_free(newp);
  82. }
  83. return NULL;
  84. }
  85. /*
  86. * Releaase all memory used by the structure
  87. */
  88. void db_con_free(db_con_t* con)
  89. {
  90. if (con == NULL) return;
  91. db_gen_free(&con->gen);
  92. if (con->uri) db_uri_free(con->uri);
  93. pkg_free(con);
  94. }
  95. /** @} */