km_pg_con.c 2.8 KB

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