2010-09-04 21:33:53 +02:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: mysql.c
|
|
|
|
*
|
|
|
|
* Description: Interface to a MySQL database
|
|
|
|
*
|
|
|
|
* Version: 0.1
|
|
|
|
* Created: 04/09/2010 20:16:47
|
|
|
|
* 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!
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
|
2010-09-11 02:12:39 +02:00
|
|
|
#include "spp_ai.h"
|
2010-09-14 19:24:03 +02:00
|
|
|
#ifdef HAVE_LIBMYSQLCLIENT
|
2010-09-05 23:54:22 +02:00
|
|
|
|
2010-09-04 21:33:53 +02:00
|
|
|
#include <mysql/mysql.h>
|
|
|
|
|
2010-09-11 02:12:39 +02:00
|
|
|
/** \defgroup mysql Module for the interface with a MySQL DBMS
|
|
|
|
* @{ */
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
/***************************/
|
|
|
|
/* Database descriptors */
|
|
|
|
PRIVATE MYSQL *db = NULL;
|
|
|
|
PRIVATE MYSQL *outdb = NULL;
|
|
|
|
/***************************/
|
2010-09-04 21:33:53 +02:00
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
/*************************************************************/
|
|
|
|
/* Private functions (operating on the database descriptors) */
|
|
|
|
|
|
|
|
PRIVATE BOOL
|
|
|
|
__mysql_is_init ( MYSQL *__DB )
|
2010-09-04 21:33:53 +02:00
|
|
|
{
|
2010-10-01 19:32:34 +02:00
|
|
|
return ( __DB != NULL );
|
|
|
|
}
|
2010-09-04 21:33:53 +02:00
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
PRIVATE void*
|
|
|
|
__mysql_do_init ( MYSQL **__DB, BOOL is_out )
|
|
|
|
{
|
|
|
|
if ( __mysql_is_init ( *__DB ) )
|
|
|
|
return (void*) *__DB;
|
2010-09-04 21:33:53 +02:00
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
if ( !( *__DB = (MYSQL*) malloc ( sizeof ( MYSQL ))))
|
2010-09-04 21:33:53 +02:00
|
|
|
return NULL;
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
if ( !( mysql_init ( *__DB )))
|
2010-09-04 21:33:53 +02:00
|
|
|
return NULL;
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
if ( is_out )
|
|
|
|
{
|
|
|
|
if ( !mysql_real_connect ( *__DB, config->outdbhost, config->outdbuser, config->outdbpass, NULL, 0, NULL, 0 ))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ( mysql_select_db ( *__DB, config->outdbname ))
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
if ( !mysql_real_connect ( *__DB, config->dbhost, config->dbuser, config->dbpass, NULL, 0, NULL, 0 ))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ( mysql_select_db ( *__DB, config->dbname ))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (void*) *__DB;
|
2010-09-04 21:33:53 +02:00
|
|
|
}
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
PRIVATE MYSQL_RES*
|
|
|
|
__mysql_do_query ( MYSQL *__DB, const char *query )
|
2010-09-04 21:33:53 +02:00
|
|
|
{
|
|
|
|
MYSQL_RES *res = NULL;
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
if ( mysql_query ( __DB, query ))
|
2010-09-04 21:33:53 +02:00
|
|
|
return NULL;
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
if ( !( res = mysql_store_result ( __DB )))
|
2010-09-04 21:33:53 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
PRIVATE void
|
|
|
|
__mysql_do_close ( MYSQL **__DB )
|
|
|
|
{
|
|
|
|
if ( *__DB )
|
|
|
|
mysql_close ( *__DB );
|
|
|
|
|
|
|
|
free ( *__DB );
|
|
|
|
*__DB = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of private functions */
|
|
|
|
/****************************/
|
|
|
|
|
|
|
|
/********************/
|
|
|
|
/* Public functions */
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
mysql_is_init ()
|
|
|
|
{
|
|
|
|
return __mysql_is_init ( db );
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
mysql_do_init ()
|
|
|
|
{
|
|
|
|
return __mysql_do_init ( &db, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES*
|
|
|
|
mysql_do_query ( const char *query )
|
|
|
|
{
|
|
|
|
return __mysql_do_query ( db, query );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
mysql_do_escape_string ( char **to, const char *from, unsigned long length )
|
|
|
|
{
|
|
|
|
return mysql_real_escape_string ( db, *to, from, length );
|
|
|
|
}
|
|
|
|
|
2010-09-04 21:33:53 +02:00
|
|
|
void
|
|
|
|
mysql_do_close ()
|
|
|
|
{
|
2010-10-01 19:32:34 +02:00
|
|
|
__mysql_do_close ( &db );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output database functions */
|
2010-09-04 21:33:53 +02:00
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
BOOL
|
|
|
|
mysql_is_out_init ()
|
|
|
|
{
|
|
|
|
return __mysql_is_init ( outdb );
|
2010-09-04 21:33:53 +02:00
|
|
|
}
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
void*
|
|
|
|
mysql_do_out_init ()
|
|
|
|
{
|
|
|
|
return __mysql_do_init ( &outdb, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES*
|
|
|
|
mysql_do_out_query ( const char *query )
|
|
|
|
{
|
|
|
|
return __mysql_do_query ( outdb, query );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
mysql_do_out_escape_string ( char **to, const char *from, unsigned long length )
|
|
|
|
{
|
|
|
|
return mysql_real_escape_string ( outdb, *to, from, length );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mysql_do_out_close ()
|
|
|
|
{
|
|
|
|
__mysql_do_close ( &outdb );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of public functions */
|
|
|
|
/***************************/
|
|
|
|
|
2010-09-11 02:12:39 +02:00
|
|
|
/** @} */
|
|
|
|
|
2010-09-05 23:54:22 +02:00
|
|
|
#endif
|
|
|
|
|