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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
/* ====================================================================
* The Kannel Software License, Version 1.0
*
* Copyright (c) 2001-2018 Kannel Group
* Copyright (c) 1998-2001 WapIT Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Kannel Group (http://www.kannel.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Kannel" and "Kannel Group" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact org@kannel.org.
*
* 5. Products derived from this software may not be called "Kannel",
* nor may "Kannel" appear in their name, without prior written
* permission of the Kannel Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Kannel Group. For more information on
* the Kannel Group, please see <http://www.kannel.org/>.
*
* Portions of this software are based upon software originally written at
* WapIT Ltd., Helsinki, Finland for the Kannel project.
*/
/*
* dbpool_pgsql.c - implement PostgreSQL operations for generic database connection pool
*
* modeled after dbpool_mysql.c
* Martiin Atukunda <matlads@myrealbox.com>
*/
#ifdef HAVE_PGSQL
#include <libpq-fe.h>
#define add1(str, value) \
if (value != NULL && octstr_len(value) > 0) { \
tmp = octstr_format(str, value); \
octstr_append(cs, tmp); \
octstr_destroy(tmp); \
}
static void *pgsql_open_conn(const DBConf *db_conf)
{
PGconn *conn = NULL;
PgSQLConf *conf = db_conf->pgsql; /* make compiler happy */
Octstr *tmp, *cs;
/* sanity check */
if (conf == NULL)
return NULL;
cs = octstr_create("");
add1(" host=%S", conf->host);
/* TODO: add hostaddr support via 'host' directive too.
* This needs an octstr_is_addr(Octstr *os) checking if a given string
* contains a valid IPv4 address. Obviously parsing on our own via gwlib
* functions or using regex. If found, insert hostaddr instead of host
* for the connection string. */
/* add1(" hostaddr=%S", conf->host); */
if (conf->port > 0) { /* add only if user set a value */
octstr_append_cstr(cs, " port=");
octstr_append_decimal(cs, conf->port);
}
add1(" user=%S", conf->username);
add1(" password=%S", conf->password);
add1(" dbname=%S", conf->database);
#if 0
/* TODO: This is very bad to show password in the log file */
info(0, "PGSQL: Using connection string: %s.", octstr_get_cstr(cs));
#endif
conn = PQconnectdb(octstr_get_cstr(cs));
octstr_destroy(cs);
if (conn == NULL)
goto failed;
gw_assert(conn != NULL);
if (PQstatus(conn) == CONNECTION_BAD) {
error(0, "PGSQL: connection to database '%s' failed!", octstr_get_cstr(conf->database));
panic(0, "PGSQL: %s", PQerrorMessage(conn));
goto failed;
}
info(0, "PGSQL: Connected to server at '%s'.", octstr_get_cstr(conf->host));
return conn;
failed:
PQfinish(conn);
return NULL;
}
static void pgsql_close_conn(void *conn)
{
if (conn == NULL)
return;
PQfinish(conn);
return;
}
static int pgsql_check_conn(void *conn)
{
if (conn == NULL)
return -1;
if (PQstatus(conn) == CONNECTION_BAD) {
error(0, "PGSQL: Database check failed!");
error(0, "PGSQL: %s", PQerrorMessage(conn));
return -1;
}
return 0;
}
static void pgsql_conf_destroy(DBConf *db_conf)
{
PgSQLConf *conf = db_conf->pgsql;
octstr_destroy(conf->host);
octstr_destroy(conf->username);
octstr_destroy(conf->password);
octstr_destroy(conf->database);
gw_free(conf);
gw_free(db_conf);
}
static int pgsql_update(void *theconn, const Octstr *sql, List *binds)
{
int rows;
PGresult *res = NULL;
PGconn *conn = (PGconn*) theconn;
res = PQexec(conn, octstr_get_cstr(sql));
if (res == NULL)
return -1;
switch (PQresultStatus(res)) {
case PGRES_BAD_RESPONSE:
case PGRES_NONFATAL_ERROR:
case PGRES_FATAL_ERROR:
error(0, "PGSQL: %s", octstr_get_cstr(sql));
error(0, "PGSQL: %s", PQresultErrorMessage(res));
PQclear(res);
return -1;
default: /* for compiler please */
break;
}
rows = atoi(PQcmdTuples(res));
PQclear(res);
return rows;
}
static int pgsql_select(void *theconn, const Octstr *sql, List *binds, List **list)
{
int nTuples, nFields, row_loop, field_loop;
PGresult *res = NULL;
List *fields;
PGconn *conn = (PGconn*) theconn;
gw_assert(list != NULL);
*list = NULL;
res = PQexec(conn, octstr_get_cstr(sql));
if (res == NULL)
return -1;
switch (PQresultStatus(res)) {
case PGRES_EMPTY_QUERY:
case PGRES_BAD_RESPONSE:
case PGRES_NONFATAL_ERROR:
case PGRES_FATAL_ERROR:
error(0, "PGSQL: %s", octstr_get_cstr(sql));
error(0, "PGSQL: %s", PQresultErrorMessage(res));
PQclear(res);
return -1;
default: /* for compiler please */
break;
}
nTuples = PQntuples(res);
nFields = PQnfields(res);
*list = gwlist_create();
for (row_loop = 0; row_loop < nTuples; row_loop++) {
fields = gwlist_create();
for (field_loop = 0; field_loop < nFields; field_loop++) {
if (PQgetisnull(res, row_loop, field_loop))
gwlist_produce(fields, octstr_create(""));
else
gwlist_produce(fields, octstr_create(PQgetvalue(res, row_loop, field_loop)));
}
gwlist_produce(*list, fields);
}
PQclear(res);
return 0;
}
static struct db_ops pgsql_ops = {
.open = pgsql_open_conn,
.close = pgsql_close_conn,
.check = pgsql_check_conn,
.conf_destroy = pgsql_conf_destroy,
.update = pgsql_update,
.select = pgsql_select
};
#endif /* HAVE_PGSQL */
|