km_pg_con.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2004 iptel.org
  5. * Copyright (C) 2008 1&1 Internet AG
  6. *
  7. * This file is part of Kamailio, a free SIP server.
  8. *
  9. * Kamailio 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. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /*! \file
  24. * \brief DB_POSTGRES :: Core
  25. * \ingroup db_postgres
  26. * Module: \ref db_postgres
  27. */
  28. #include "pg_con.h"
  29. #include "../../mem/mem.h"
  30. #include "../../dprint.h"
  31. #include "../../ut.h"
  32. #include <string.h>
  33. #include <time.h>
  34. /*!
  35. * \brief Create a new connection
  36. *
  37. * Create a new connection structure in private memory, open the PostgreSQL
  38. * connection and set reference count to 1
  39. * \param id database id
  40. * \return postgres connection structure, 0 on error
  41. */
  42. struct pg_con* db_postgres_new_connection(struct db_id* id)
  43. {
  44. struct pg_con* ptr;
  45. char *ports;
  46. LM_DBG("db_id = %p\n", id);
  47. if (!id) {
  48. LM_ERR("invalid db_id parameter value\n");
  49. return 0;
  50. }
  51. ptr = (struct pg_con*)pkg_malloc(sizeof(struct pg_con));
  52. if (!ptr) {
  53. LM_ERR("failed trying to allocated %lu bytes for connection structure."
  54. "\n", (unsigned long)sizeof(struct pg_con));
  55. return 0;
  56. }
  57. LM_DBG("%p=pkg_malloc(%lu)\n", ptr, (unsigned long)sizeof(struct pg_con));
  58. memset(ptr, 0, sizeof(struct pg_con));
  59. ptr->ref = 1;
  60. if (id->port) {
  61. ports = int2str(id->port, 0);
  62. LM_DBG("opening connection: postgres://xxxx:xxxx@%s:%d/%s\n", ZSW(id->host),
  63. id->port, ZSW(id->database));
  64. } else {
  65. ports = NULL;
  66. LM_DBG("opening connection: postgres://xxxx:xxxx@%s/%s\n", ZSW(id->host),
  67. ZSW(id->database));
  68. }
  69. ptr->con = PQsetdbLogin(id->host, ports, NULL, NULL, id->database, id->username, id->password);
  70. LM_DBG("PQsetdbLogin(%p)\n", ptr->con);
  71. if( (ptr->con == 0) || (PQstatus(ptr->con) != CONNECTION_OK) )
  72. {
  73. LM_ERR("%s\n", PQerrorMessage(ptr->con));
  74. PQfinish(ptr->con);
  75. goto err;
  76. }
  77. ptr->connected = 1;
  78. ptr->timestamp = time(0);
  79. ptr->id = id;
  80. return ptr;
  81. err:
  82. if (ptr) {
  83. LM_ERR("cleaning up %p=pkg_free()\n", ptr);
  84. pkg_free(ptr);
  85. }
  86. return 0;
  87. }
  88. /*!
  89. * \brief Close the connection and release memory
  90. * \param con connection
  91. */
  92. void db_postgres_free_connection(struct pool_con* con)
  93. {
  94. if (!con) return;
  95. struct pg_con * _c;
  96. _c = (struct pg_con*)con;
  97. if (_c->res) {
  98. LM_DBG("PQclear(%p)\n", _c->res);
  99. PQclear(_c->res);
  100. _c->res = 0;
  101. }
  102. if (_c->id) free_db_id(_c->id);
  103. if (_c->con) {
  104. LM_DBG("PQfinish(%p)\n", _c->con);
  105. PQfinish(_c->con);
  106. _c->con = 0;
  107. }
  108. LM_DBG("pkg_free(%p)\n", _c);
  109. pkg_free(_c);
  110. }