km_pg_con.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2001-2004 iptel.org
  3. * Copyright (C) 2008 1&1 Internet AG
  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. /*! \file
  22. * \brief DB_POSTGRES :: Core
  23. * \ingroup db_postgres
  24. * Module: \ref db_postgres
  25. */
  26. #include "km_pg_con.h"
  27. #include "../../mem/mem.h"
  28. #include "../../dprint.h"
  29. #include "../../ut.h"
  30. #include <string.h>
  31. #include <time.h>
  32. /*!
  33. * \brief Create a new connection
  34. *
  35. * Create a new connection structure in private memory, open the PostgreSQL
  36. * connection and set reference count to 1
  37. * \param id database id
  38. * \return postgres connection structure, 0 on error
  39. */
  40. struct pg_con* db_postgres_new_connection(struct db_id* id)
  41. {
  42. struct pg_con* ptr;
  43. char *ports;
  44. LM_DBG("db_id = %p\n", id);
  45. if (!id) {
  46. LM_ERR("invalid db_id parameter value\n");
  47. return 0;
  48. }
  49. ptr = (struct pg_con*)pkg_malloc(sizeof(struct pg_con));
  50. if (!ptr) {
  51. LM_ERR("failed trying to allocated %lu bytes for connection structure."
  52. "\n", (unsigned long)sizeof(struct pg_con));
  53. return 0;
  54. }
  55. LM_DBG("%p=pkg_malloc(%lu)\n", ptr, (unsigned long)sizeof(struct pg_con));
  56. memset(ptr, 0, sizeof(struct pg_con));
  57. ptr->ref = 1;
  58. if (id->port) {
  59. ports = int2str(id->port, 0);
  60. LM_DBG("opening connection: postgres://xxxx:xxxx@%s:%d/%s\n", ZSW(id->host),
  61. id->port, ZSW(id->database));
  62. } else {
  63. ports = NULL;
  64. LM_DBG("opening connection: postgres://xxxx:xxxx@%s/%s\n", ZSW(id->host),
  65. ZSW(id->database));
  66. }
  67. ptr->con = PQsetdbLogin(id->host, ports, NULL, NULL, id->database, id->username, id->password);
  68. LM_DBG("PQsetdbLogin(%p)\n", ptr->con);
  69. if( (ptr->con == 0) || (PQstatus(ptr->con) != CONNECTION_OK) )
  70. {
  71. LM_ERR("%s\n", PQerrorMessage(ptr->con));
  72. PQfinish(ptr->con);
  73. goto err;
  74. }
  75. ptr->connected = 1;
  76. ptr->timestamp = time(0);
  77. ptr->id = id;
  78. return ptr;
  79. err:
  80. if (ptr) {
  81. LM_ERR("cleaning up %p=pkg_free()\n", ptr);
  82. pkg_free(ptr);
  83. }
  84. return 0;
  85. }
  86. /*!
  87. * \brief Close the connection and release memory
  88. * \param con connection
  89. */
  90. void db_postgres_free_connection(struct pool_con* con)
  91. {
  92. struct pg_con * _c;
  93. if (!con) return;
  94. _c = (struct pg_con*)con;
  95. if (_c->res) {
  96. LM_DBG("PQclear(%p)\n", _c->res);
  97. PQclear(_c->res);
  98. _c->res = 0;
  99. }
  100. if (_c->id) free_db_id(_c->id);
  101. if (_c->con) {
  102. LM_DBG("PQfinish(%p)\n", _c->con);
  103. PQfinish(_c->con);
  104. _c->con = 0;
  105. }
  106. LM_DBG("pkg_free(%p)\n", _c);
  107. pkg_free(_c);
  108. }