#include "gwlib/gwlib.h"#include "gwlib/dbpool.h"#include "dlr_p.h"#include <libpq-fe.h>Include dependency graph for dlr_pgsql.c:

Go to the source code of this file.
Functions | |
| int | pgsql_update (const Octstr *sql) |
| List * | pgsql_select (const Octstr *sql) |
| void | dlr_pgsql_shutdown () |
| void | dlr_pgsql_add (struct dlr_entry *entry) |
| dlr_entry * | dlr_pgsql_get (const Octstr *smsc, const Octstr *ts, const Octstr *dst) |
| void | dlr_pgsql_remove (const Octstr *smsc, const Octstr *ts, const Octstr *dst) |
| void | dlr_pgsql_update (const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status) |
| long | dlr_pgsql_messages (void) |
| void | dlr_pgsql_flush (void) |
| dlr_storage * | dlr_init_pgsql (Cfg *cfg) |
Variables | |
| DBPool * | pool = NULL |
| dlr_db_fields * | fields = NULL |
| dlr_storage | handles |
|
|
Definition at line 298 of file dlr_pgsql.c. References cfg_get, cfg_get_integer(), cfg_get_multi_group(), cfg_get_single_group(), PgSQLConf::database, dbpool_conn_count(), dbpool_create(), DBPOOL_PGSQL, dlr_db_fields_create(), fields, gw_assert, gwlist_destroy(), gwlist_extract_first(), PgSQLConf::host, octstr_compare(), octstr_destroy(), octstr_get_cstr, octstr_imm(), panic, PgSQLConf::password, DBConf::pgsql, pool, PgSQLConf::port, and PgSQLConf::username. Referenced by dlr_init(). 00299 {
00300 CfgGroup *grp;
00301 List *grplist;
00302 Octstr *pgsql_host, *pgsql_user, *pgsql_pass, *pgsql_db, *pgsql_id;
00303 long pgsql_port = 0;
00304 Octstr *p = NULL;
00305 long pool_size;
00306 DBConf *db_conf = NULL;
00307
00308 /*
00309 * check for all mandatory directives that specify the field names
00310 * of the table used
00311 */
00312 if (!(grp = cfg_get_single_group(cfg, octstr_imm("dlr-db"))))
00313 panic(0, "DLR: PgSQL: group 'dlr-db' is not specified!");
00314
00315 if (!(pgsql_id = cfg_get(grp, octstr_imm("id"))))
00316 panic(0, "DLR: PgSQL: directive 'id' is not specified!");
00317
00318 fields = dlr_db_fields_create(grp);
00319 gw_assert(fields != NULL);
00320
00321 /*
00322 * now grap the required information from the 'pgsql-connection' group
00323 * with the pgsql-id we just obtained
00324 *
00325 * we have to loop through all available PostgreSQL connection definitions
00326 * and search for the one we are looking for
00327 */
00328
00329 grplist = cfg_get_multi_group(cfg, octstr_imm("pgsql-connection"));
00330 while (grplist && (grp = gwlist_extract_first(grplist)) != NULL) {
00331 p = cfg_get(grp, octstr_imm("id"));
00332 if (p != NULL && octstr_compare(p, pgsql_id) == 0) {
00333 goto found;
00334 }
00335 if (p != NULL)
00336 octstr_destroy(p);
00337 }
00338 panic(0, "DLR: PgSQL: connection settings for id '%s' are not specified!",
00339 octstr_get_cstr(pgsql_id));
00340
00341 found:
00342 octstr_destroy(p);
00343 gwlist_destroy(grplist, NULL);
00344
00345 if (cfg_get_integer(&pool_size, grp, octstr_imm("max-connections")) == -1 || pool_size == 0)
00346 pool_size = 1;
00347
00348 if (!(pgsql_host = cfg_get(grp, octstr_imm("host"))))
00349 panic(0, "DLR: PgSQL: directive 'host' is not specified!");
00350 if (!(pgsql_user = cfg_get(grp, octstr_imm("username"))))
00351 panic(0, "DLR: PgSQL: directive 'username' is not specified!");
00352 if (!(pgsql_pass = cfg_get(grp, octstr_imm("password"))))
00353 panic(0, "DLR: PgSQL: directive 'password' is not specified!");
00354 if (!(pgsql_db = cfg_get(grp, octstr_imm("database"))))
00355 panic(0, "DLR: PgSQL: directive 'database' is not specified!");
00356 cfg_get_integer(&pgsql_port, grp, octstr_imm("port")); /* optional */
00357
00358 /*
00359 * ok, ready to connect to the database
00360 */
00361 db_conf = gw_malloc(sizeof(DBConf));
00362 gw_assert(db_conf != NULL);
00363
00364 db_conf->pgsql = gw_malloc(sizeof(PgSQLConf));
00365 gw_assert(db_conf->pgsql != NULL);
00366
00367 db_conf->pgsql->host = pgsql_host;
00368 db_conf->pgsql->port = pgsql_port;
00369 db_conf->pgsql->username = pgsql_user;
00370 db_conf->pgsql->password = pgsql_pass;
00371 db_conf->pgsql->database = pgsql_db;
00372
00373 pool = dbpool_create(DBPOOL_PGSQL, db_conf, pool_size);
00374 gw_assert(pool != NULL);
00375
00376 /*
00377 * XXX should a failing connect throw panic?!
00378 */
00379 if (dbpool_conn_count(pool) == 0)
00380 panic(0,"DLR: PgSQL: database pool has no connections!");
00381
00382 octstr_destroy(pgsql_id);
00383
00384 return &handles;
00385 }
|
Here is the call graph for this function:

|
Here is the call graph for this function:

|
|
Definition at line 275 of file dlr_pgsql.c. References fields, octstr_destroy(), octstr_format(), octstr_get_cstr, pgsql_update(), and dlr_db_fields::table. 00276 {
00277 Octstr *sql;
00278
00279 sql = octstr_format("DELETE FROM %s;", octstr_get_cstr(fields->table));
00280
00281 pgsql_update(sql);
00282 octstr_destroy(sql);
00283 }
|
Here is the call graph for this function:

|
||||||||||||||||
Here is the call graph for this function:

|
|
Definition at line 250 of file dlr_pgsql.c. References error(), fields, gwlist_destroy(), gwlist_extract_first(), gwlist_get(), gwlist_len(), octstr_destroy(), octstr_destroy_item(), octstr_format(), octstr_get_cstr, pgsql_select(), res, and dlr_db_fields::table. 00251 {
00252 Octstr *sql;
00253 long ret;
00254 List *res;
00255
00256 sql = octstr_format("SELECT count(*) FROM %s;", octstr_get_cstr(fields->table));
00257
00258 res = pgsql_select(sql);
00259 octstr_destroy(sql);
00260
00261 if (res == NULL || gwlist_len(res) < 1) {
00262 error(0, "PGSQL: Could not get count of DLR table");
00263 ret = -1;
00264 } else {
00265 ret = atol(octstr_get_cstr(gwlist_get(gwlist_get(res, 0), 0)));
00266 }
00267
00268 gwlist_destroy(gwlist_extract_first(res), octstr_destroy_item);
00269 gwlist_destroy(res, NULL);
00270
00271 return ret;
00272 }
|
Here is the call graph for this function:

|
||||||||||||||||
|
Definition at line 218 of file dlr_pgsql.c. References debug(), dlr_db_fields::field_smsc, dlr_db_fields::field_ts, fields, octstr_destroy(), octstr_format(), octstr_get_cstr, pgsql_update(), and dlr_db_fields::table. 00219 {
00220 Octstr *sql;
00221
00222 debug("dlr.pgsql", 0, "removing DLR from database");
00223 sql = octstr_format("DELETE FROM %s WHERE oid = (SELECT oid FROM %s WHERE %s='%s' AND %s='%s' LIMIT 1);",
00224 octstr_get_cstr(fields->table), octstr_get_cstr(fields->table),
00225 octstr_get_cstr(fields->field_smsc),
00226 octstr_get_cstr(smsc), octstr_get_cstr(fields->field_ts), octstr_get_cstr(ts));
00227
00228
00229 pgsql_update(sql);
00230 octstr_destroy(sql);
00231 }
|
Here is the call graph for this function:

|
|
Definition at line 133 of file dlr_pgsql.c. References dbpool_destroy(), dlr_db_fields_destroy(), fields, and pool. 00134 {
00135 dbpool_destroy(pool);
00136 dlr_db_fields_destroy(fields);
00137 }
|
Here is the call graph for this function:

|
||||||||||||||||||||
|
Definition at line 234 of file dlr_pgsql.c. References debug(), dlr_db_fields::field_smsc, dlr_db_fields::field_status, dlr_db_fields::field_ts, fields, octstr_destroy(), octstr_format(), octstr_get_cstr, pgsql_update(), and dlr_db_fields::table. 00235 {
00236 Octstr *sql;
00237
00238 debug("dlr.pgsql", 0, "updating DLR status in database");
00239 sql = octstr_format("UPDATE %s SET %s=%d WHERE oid = (SELECT oid FROM %s WHERE %s='%s' AND %s='%s' LIMIT 1);",
00240 octstr_get_cstr(fields->table),
00241 octstr_get_cstr(fields->field_status), status,
00242 octstr_get_cstr(fields->table),
00243 octstr_get_cstr(fields->field_smsc), octstr_get_cstr(smsc),
00244 octstr_get_cstr(fields->field_ts), octstr_get_cstr(ts));
00245 pgsql_update(sql);
00246 octstr_destroy(sql);
00247 }
|
Here is the call graph for this function:

|
|
Definition at line 110 of file dlr_pgsql.c. References dbpool_conn_consume(), dbpool_conn_produce(), dbpool_conn_select(), debug(), error(), octstr_get_cstr, and pool. Referenced by dlr_pgsql_get(), and dlr_pgsql_messages(). 00111 {
00112 DBPoolConn *pc;
00113 List *ret = NULL;
00114
00115 #if defined(DLR_TRACE)
00116 debug("dlr.pgsql", 0, "sql: %s", octstr_get_cstr(sql));
00117 #endif
00118
00119 pc = dbpool_conn_consume(pool);
00120 if (pc == NULL) {
00121 error(0, "PGSQL: Database pool got no connection! DB operation failed!");
00122 return NULL;
00123 }
00124
00125 if (dbpool_conn_select(pc, sql, NULL, &ret) == -1)
00126 error(0, "PGSQL: Select failed!");
00127
00128 dbpool_conn_produce(pc);
00129 return ret;
00130 }
|
Here is the call graph for this function:

|
|
Definition at line 87 of file dlr_pgsql.c. References dbpool_conn_consume(), dbpool_conn_produce(), dbpool_conn_update(), debug(), error(), octstr_get_cstr, and pool. Referenced by dlr_pgsql_add(), dlr_pgsql_flush(), dlr_pgsql_remove(), and dlr_pgsql_update(). 00088 {
00089 DBPoolConn *pc;
00090 int ret = 0;
00091
00092 #if defined(DLR_TRACE)
00093 debug("dlr.pgsql", 0, "sql: %s", octstr_get_cstr(sql));
00094 #endif
00095
00096 pc = dbpool_conn_consume(pool);
00097 if (pc == NULL) {
00098 error(0, "PGSQL: Database pool got no connection! DB update failed!");
00099 return -1;
00100 }
00101
00102 if ((ret = dbpool_conn_update(pc, sql, NULL)) == -1)
00103 error(0, "PGSQL: DB update failed!");
00104
00105 dbpool_conn_produce(pc);
00106 return ret;
00107 }
|
Here is the call graph for this function:

|
|
Definition at line 84 of file dlr_pgsql.c. Referenced by dlr_init_pgsql(), dlr_pgsql_add(), dlr_pgsql_flush(), dlr_pgsql_get(), dlr_pgsql_messages(), dlr_pgsql_remove(), dlr_pgsql_shutdown(), and dlr_pgsql_update(). |
|
|
Initial value: {
.type = "pgsql",
.dlr_add = dlr_pgsql_add,
.dlr_get = dlr_pgsql_get,
.dlr_update = dlr_pgsql_update,
.dlr_remove = dlr_pgsql_remove,
.dlr_shutdown = dlr_pgsql_shutdown,
.dlr_messages = dlr_pgsql_messages,
.dlr_flush = dlr_pgsql_flush
}
Definition at line 286 of file dlr_pgsql.c. |
|
|
Definition at line 79 of file dlr_pgsql.c. Referenced by dlr_init_pgsql(), dlr_pgsql_shutdown(), pgsql_select(), and pgsql_update(). |