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