2010-09-16 17:11:46 +02:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: postgresql.c
|
|
|
|
*
|
|
|
|
* Description: Interface to a PostgreSQL database
|
|
|
|
*
|
|
|
|
* Version: 0.1
|
|
|
|
* Created: 16/09/2010 00:23:34
|
|
|
|
* Revision: none
|
|
|
|
* Compiler: gcc
|
|
|
|
*
|
|
|
|
* Author: BlackLight (http://0x00.ath.cx), <blacklight@autistici.org>
|
|
|
|
* Licence: GNU GPL v.3
|
|
|
|
* Company: DO WHAT YOU WANT CAUSE A PIRATE IS FREE, YOU ARE A PIRATE!
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spp_ai.h"
|
|
|
|
#ifdef HAVE_LIBPQ
|
|
|
|
|
|
|
|
#include <alloca.h>
|
|
|
|
#include <postgresql/libpq-fe.h>
|
|
|
|
#include "db.h"
|
|
|
|
|
|
|
|
/** \defgroup postgresql Module for the interface with a PostgreSQL DBMS
|
|
|
|
* @{ */
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
/***************************/
|
|
|
|
/* Database descriptors */
|
|
|
|
PRIVATE PGconn *db = NULL;
|
|
|
|
PRIVATE PGconn *outdb = NULL;
|
|
|
|
/***************************/
|
2010-09-16 17:11:46 +02:00
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
/*************************************************************/
|
|
|
|
/* Private functions (operating on the database descriptors) */
|
|
|
|
|
|
|
|
PRIVATE BOOL
|
|
|
|
__postgresql_is_init ( PGconn *__DB )
|
|
|
|
{
|
|
|
|
return ( __DB != NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIVATE void*
|
|
|
|
__postgresql_do_init ( PGconn **__DB, BOOL is_out )
|
2010-09-16 17:11:46 +02:00
|
|
|
{
|
|
|
|
char *conninfo = NULL;
|
|
|
|
int conninfo_max_length =
|
2010-10-01 19:32:34 +02:00
|
|
|
( is_out ?
|
|
|
|
((config->outdbhost) ? strlen ( config->outdbhost ) : 0) +
|
|
|
|
((config->outdbuser) ? strlen ( config->outdbuser ) : 0) +
|
|
|
|
((config->outdbpass) ? strlen ( config->outdbpass ) : 0) +
|
|
|
|
((config->outdbname) ? strlen ( config->outdbname ) : 0) :
|
|
|
|
|
|
|
|
((config->dbhost) ? strlen ( config->dbhost ) : 0) +
|
|
|
|
((config->dbuser) ? strlen ( config->dbuser ) : 0) +
|
|
|
|
((config->dbpass) ? strlen ( config->dbpass ) : 0) +
|
|
|
|
((config->dbname) ? strlen ( config->dbname ) : 0)) + 100;
|
|
|
|
|
|
|
|
if ( postgresql_is_init ( *__DB ))
|
|
|
|
return (void*) *__DB;
|
|
|
|
|
2010-09-16 17:11:46 +02:00
|
|
|
if ( !( conninfo = (char*) alloca ( conninfo_max_length )))
|
|
|
|
_dpd.fatalMsg ( "AIPreproc: Fatal dynamic memory allocation error at %s:%d\n", __FILE__, __LINE__ );
|
|
|
|
|
|
|
|
memset ( conninfo, 0, conninfo_max_length );
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
if ( is_out )
|
|
|
|
{
|
|
|
|
snprintf ( conninfo, conninfo_max_length, "dbname=%s", config->outdbname );
|
|
|
|
|
|
|
|
if ( config->outdbuser )
|
|
|
|
sprintf ( conninfo, "%s user=%s", conninfo, config->outdbuser );
|
|
|
|
|
|
|
|
if ( config->outdbpass )
|
|
|
|
sprintf ( conninfo, "%s password=%s", conninfo, config->outdbpass );
|
2010-09-16 17:11:46 +02:00
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
if ( config->outdbhost )
|
|
|
|
sprintf ( conninfo, "%s hostaddr=%s", conninfo, config->outdbhost );
|
|
|
|
} else {
|
|
|
|
snprintf ( conninfo, conninfo_max_length, "dbname=%s", config->dbname );
|
2010-09-16 17:11:46 +02:00
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
if ( config->dbuser )
|
|
|
|
sprintf ( conninfo, "%s user=%s", conninfo, config->dbuser );
|
|
|
|
|
|
|
|
if ( config->dbpass )
|
|
|
|
sprintf ( conninfo, "%s password=%s", conninfo, config->dbpass );
|
|
|
|
|
|
|
|
if ( config->dbhost )
|
|
|
|
sprintf ( conninfo, "%s hostaddr=%s", conninfo, config->dbhost );
|
|
|
|
}
|
2010-09-16 17:11:46 +02:00
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
if ( PQstatus ( *__DB = PQconnectdb ( conninfo )) != CONNECTION_OK )
|
2010-09-16 17:11:46 +02:00
|
|
|
return NULL;
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
return (void*) *__DB;
|
2010-09-16 17:11:46 +02:00
|
|
|
}
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
PRIVATE PSQL_result*
|
|
|
|
__postgresql_do_query ( PGconn *__DB, const char *query )
|
2010-09-16 17:11:46 +02:00
|
|
|
{
|
|
|
|
int i, j, ntuples, nfields;
|
|
|
|
PSQL_result *res = NULL;
|
|
|
|
|
|
|
|
if ( !( res = (PSQL_result*) malloc ( sizeof ( PSQL_result ))))
|
|
|
|
_dpd.fatalMsg ( "AIPreproc: Fatal dynamic memory allocation error at %s:%d\n", __FILE__, __LINE__ );
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
if ( PQresultStatus ( res->res = PQexec( __DB, query )) != PGRES_TUPLES_OK )
|
2010-09-16 17:11:46 +02:00
|
|
|
{
|
2010-10-01 19:32:34 +02:00
|
|
|
PQfinish ( __DB );
|
2010-09-16 17:11:46 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ntuples = PQntuples ( res->res );
|
|
|
|
res->index = 0;
|
|
|
|
res->rows = NULL;
|
|
|
|
|
|
|
|
if ( !( res->rows = ( char*** ) malloc ( ntuples * sizeof ( char** ))))
|
|
|
|
_dpd.fatalMsg ( "AIPreproc: Fatal dynamic memory allocation error at %s:%d\n", __FILE__, __LINE__ );
|
|
|
|
|
|
|
|
for ( i=0; i < ntuples; i++ )
|
|
|
|
{
|
|
|
|
nfields = PQnfields ( res->res );
|
|
|
|
|
|
|
|
if ( !( res->rows[i] = ( char** ) malloc ( nfields * sizeof ( char* ))))
|
|
|
|
_dpd.fatalMsg ( "AIPreproc: Fatal dynamic memory allocation error at %s:%d\n", __FILE__, __LINE__ );
|
|
|
|
|
|
|
|
for ( j=0; j < nfields; j++ )
|
|
|
|
{
|
|
|
|
res->rows[i][j] = PQgetvalue ( res->res, i, j );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
postgresql_num_rows ( PSQL_result *res )
|
|
|
|
{
|
|
|
|
return PQntuples ( res->res );
|
|
|
|
}
|
|
|
|
|
|
|
|
char**
|
|
|
|
postgresql_fetch_row ( PSQL_result *res )
|
|
|
|
{
|
|
|
|
if ( (res->index++) >= PQntuples ( res->res ))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return res->rows[ res->index - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
postgresql_free_result ( PSQL_result *res )
|
|
|
|
{
|
2010-09-16 17:24:53 +02:00
|
|
|
int i, j, ntuples;
|
|
|
|
|
2010-09-16 17:11:46 +02:00
|
|
|
if ( res )
|
|
|
|
{
|
2010-09-16 17:24:53 +02:00
|
|
|
ntuples = PQntuples ( res->res );
|
|
|
|
|
|
|
|
for ( i=0; i < ntuples; i++ )
|
|
|
|
free ( res->rows[i] );
|
|
|
|
free ( res->rows );
|
|
|
|
|
2010-09-16 17:11:46 +02:00
|
|
|
PQclear ( res->res );
|
|
|
|
free ( res );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
PRIVATE void
|
|
|
|
__postgresql_do_close ( PGconn **__DB )
|
|
|
|
{
|
|
|
|
if ( *__DB )
|
|
|
|
PQfinish ( *__DB );
|
|
|
|
|
|
|
|
*__DB = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of private functions */
|
|
|
|
/****************************/
|
|
|
|
|
|
|
|
/********************/
|
|
|
|
/* Public functions */
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
postgresql_is_init ()
|
|
|
|
{
|
|
|
|
return __postgresql_is_init ( db );
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
postgresql_do_init ()
|
|
|
|
{
|
|
|
|
return __postgresql_do_init ( &db, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
PSQL_result*
|
|
|
|
postgresql_do_query ( const char *query )
|
|
|
|
{
|
|
|
|
return __postgresql_do_query ( db, query );
|
|
|
|
}
|
|
|
|
|
2010-09-16 17:11:46 +02:00
|
|
|
void
|
|
|
|
postgresql_do_close ()
|
|
|
|
{
|
2010-10-01 19:32:34 +02:00
|
|
|
__postgresql_do_close ( &db );
|
|
|
|
}
|
2010-09-16 17:11:46 +02:00
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
/* Output database functions */
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
postgresql_is_out_init ()
|
|
|
|
{
|
|
|
|
return __postgresql_is_init ( outdb );
|
2010-09-16 17:11:46 +02:00
|
|
|
}
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
void*
|
|
|
|
postgresql_do_out_init ()
|
|
|
|
{
|
|
|
|
return __postgresql_do_init ( &outdb, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
PSQL_result*
|
|
|
|
postgresql_do_out_query ( const char *query )
|
|
|
|
{
|
|
|
|
return __postgresql_do_query ( outdb, query );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
postgresql_do_out_close ()
|
|
|
|
{
|
|
|
|
__postgresql_do_close ( &outdb );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of public functions */
|
|
|
|
/***************************/
|
|
|
|
|
2010-09-16 17:11:46 +02:00
|
|
|
/* @} */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|