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-11-16 19:18:08 +01:00
|
|
|
#include <mysql/errmsg.h>
|
2010-09-04 21:33:53 +02:00
|
|
|
|
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) */
|
|
|
|
|
2017-05-01 14:41:55 +02:00
|
|
|
PRIVATE bool
|
2010-10-01 19:32:34 +02:00
|
|
|
__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*
|
2017-05-01 14:41:55 +02:00
|
|
|
__mysql_do_init ( MYSQL **__DB, bool is_out )
|
2010-10-01 19:32:34 +02:00
|
|
|
{
|
|
|
|
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-11-20 16:47:57 +01:00
|
|
|
{
|
2010-09-04 21:33:53 +02:00
|
|
|
return NULL;
|
2010-11-20 16:47:57 +01:00
|
|
|
}
|
2010-09-04 21:33:53 +02:00
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
if ( !( mysql_init ( *__DB )))
|
2010-11-20 16:47:57 +01:00
|
|
|
{
|
2010-09-04 21:33:53 +02:00
|
|
|
return NULL;
|
2010-11-20 16:47:57 +01:00
|
|
|
}
|
2010-09-04 21:33:53 +02:00
|
|
|
|
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 ))
|
2010-11-20 16:47:57 +01:00
|
|
|
{
|
2010-10-01 19:32:34 +02:00
|
|
|
return NULL;
|
2010-11-20 16:47:57 +01:00
|
|
|
}
|
2010-10-01 19:32:34 +02:00
|
|
|
|
|
|
|
if ( mysql_select_db ( *__DB, config->outdbname ))
|
2010-11-20 16:47:57 +01:00
|
|
|
{
|
2010-10-01 19:32:34 +02:00
|
|
|
return NULL;
|
2010-11-20 16:47:57 +01:00
|
|
|
}
|
2010-10-01 19:32:34 +02:00
|
|
|
} 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-11-23 18:42:20 +01:00
|
|
|
PRIVATE void
|
|
|
|
__mysql_do_close ( MYSQL **__DB )
|
|
|
|
{
|
|
|
|
if ( *__DB )
|
|
|
|
mysql_close ( *__DB );
|
|
|
|
|
|
|
|
free ( *__DB );
|
|
|
|
*__DB = NULL;
|
|
|
|
}
|
|
|
|
|
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-11-23 18:42:20 +01:00
|
|
|
{
|
2010-09-04 21:33:53 +02:00
|
|
|
return NULL;
|
2010-11-23 18:42:20 +01:00
|
|
|
}
|
2010-09-04 21:33:53 +02:00
|
|
|
|
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
|
|
|
/* End of private functions */
|
|
|
|
/****************************/
|
|
|
|
|
|
|
|
/********************/
|
|
|
|
/* Public functions */
|
|
|
|
|
2017-05-01 14:41:55 +02:00
|
|
|
bool
|
2010-10-01 19:32:34 +02:00
|
|
|
mysql_is_init ()
|
|
|
|
{
|
|
|
|
return __mysql_is_init ( db );
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
mysql_do_init ()
|
|
|
|
{
|
|
|
|
return __mysql_do_init ( &db, false );
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:41:55 +02:00
|
|
|
bool
|
2010-11-23 18:42:20 +01:00
|
|
|
mysql_is_gone ()
|
|
|
|
{
|
|
|
|
return (( mysql_errno ( db ) == CR_SERVER_GONE_ERROR ) || ( mysql_errno ( db ) == CR_SERVER_LOST ));
|
|
|
|
}
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
MYSQL_RES*
|
|
|
|
mysql_do_query ( const char *query )
|
|
|
|
{
|
2010-11-23 18:42:20 +01:00
|
|
|
if ( !db )
|
|
|
|
mysql_do_init();
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
return __mysql_do_query ( db, query );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
mysql_do_escape_string ( char **to, const char *from, unsigned long length )
|
|
|
|
{
|
2010-11-23 02:31:55 +01:00
|
|
|
if ( !from )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ( strlen ( from ) == 0 )
|
|
|
|
return 0;
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
return mysql_real_escape_string ( db, *to, from, length );
|
|
|
|
}
|
|
|
|
|
2010-11-16 19:18:08 +01:00
|
|
|
const char*
|
|
|
|
mysql_do_error ()
|
|
|
|
{
|
|
|
|
return mysql_error ( db );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-05-01 14:41:55 +02:00
|
|
|
bool
|
2010-10-01 19:32:34 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:41:55 +02:00
|
|
|
bool
|
2010-11-23 18:42:20 +01:00
|
|
|
mysql_is_out_gone ()
|
|
|
|
{
|
|
|
|
return (( mysql_errno ( outdb ) == CR_SERVER_GONE_ERROR ) || ( mysql_errno ( outdb ) == CR_SERVER_LOST ));
|
|
|
|
}
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
MYSQL_RES*
|
|
|
|
mysql_do_out_query ( const char *query )
|
|
|
|
{
|
2010-11-23 18:42:20 +01:00
|
|
|
if ( !outdb )
|
|
|
|
mysql_do_out_init();
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
return __mysql_do_query ( outdb, query );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
mysql_do_out_escape_string ( char **to, const char *from, unsigned long length )
|
|
|
|
{
|
2010-11-23 02:31:55 +01:00
|
|
|
if ( !from )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ( strlen ( from ) == 0 )
|
|
|
|
return 0;
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
return mysql_real_escape_string ( outdb, *to, from, length );
|
|
|
|
}
|
|
|
|
|
2010-11-16 19:18:08 +01:00
|
|
|
const char*
|
|
|
|
mysql_do_out_error ()
|
|
|
|
{
|
|
|
|
return mysql_error ( outdb );
|
|
|
|
}
|
|
|
|
|
2010-10-01 19:32:34 +02:00
|
|
|
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
|