-
Notifications
You must be signed in to change notification settings - Fork 564
/
pg_con.c
142 lines (121 loc) · 3.28 KB
/
pg_con.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright (C) 2001-2004 iptel.org
* Copyright (C) 2008 1&1 Internet AG
*
* This file is part of opensips, a free SIP server.
*
* opensips 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
*
* opensips 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
*/
#include "db_postgres.h"
#include "pg_con.h"
#include "../../mem/mem.h"
#include "../../dprint.h"
#include "../../ut.h"
#include <string.h>
#include <time.h>
#define PSQL_PARAMS_MAX 7
/*
* Create a new connection structure,
* open the PostgreSQL connection and set reference count to 1
*/
struct pg_con* db_postgres_new_connection(struct db_id* id)
{
#define PSQL_PARAM(_k, _v) \
do { \
keywords[p] = (_k); \
values[p] = (_v); \
p++; \
} while (0);
struct pg_con* ptr;
const char *keywords[PSQL_PARAMS_MAX];
const char *values[PSQL_PARAMS_MAX];
char *ports;
int p = 0;
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(%zu)\n", ptr, 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));
PSQL_PARAM("port", ports);
} else {
ports = NULL;
LM_DBG("opening connection: postgres://xxxx:xxxx@%s/%s\n", ZSW(id->host),
ZSW(id->database));
}
if (id->host)
PSQL_PARAM("host", id->host);
if (id->username)
PSQL_PARAM("user", id->username);
if (id->password)
PSQL_PARAM("password", id->password);
PSQL_PARAM("dbname", id->database);
/* force the default timeout */
if (pq_timeout > 0)
PSQL_PARAM("connect_timeout", int2str(pq_timeout, 0));
PSQL_PARAM(0, 0);
ptr->con = PQconnectdbParams(keywords, values, 1);
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;
#undef PSQL_PARAM
}
/*
* Close the connection and release memory
*/
void db_postgres_free_connection(struct pool_con* con)
{
if (!con) return;
struct pg_con * _c;
_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);
}