2010-12-18 21:30:16 +00:00
# include "quakedef.h"
2012-11-27 03:23:19 +00:00
//SQLITE:
//should probably be compiled with -DSQLITE_OMIT_ATTACH -DSQLITE_OMIT_LOAD_EXTENSION which would mean we don't need to override authorisation.
2010-12-18 21:30:16 +00:00
# ifdef SQL
# include "sv_sql.h"
2012-11-27 03:23:19 +00:00
# ifdef USE_MYSQL
2010-12-18 21:30:16 +00:00
static void ( VARGS * qmysql_library_end ) ( void ) ;
static int ( VARGS * qmysql_library_init ) ( int argc , char * * argv , char * * groups ) ;
static my_ulonglong ( VARGS * qmysql_affected_rows ) ( MYSQL * mysql ) ;
static void ( VARGS * qmysql_close ) ( MYSQL * sock ) ;
static void ( VARGS * qmysql_data_seek ) ( MYSQL_RES * result , my_ulonglong offset ) ;
static unsigned int ( VARGS * qmysql_errno ) ( MYSQL * mysql ) ;
static const char * ( VARGS * qmysql_error ) ( MYSQL * mysql ) ;
static MYSQL_FIELD * ( VARGS * qmysql_fetch_field_direct ) ( MYSQL_RES * res , unsigned int fieldnr ) ;
static MYSQL_ROW ( VARGS * qmysql_fetch_row ) ( MYSQL_RES * result ) ;
2014-03-30 08:55:06 +00:00
static unsigned long * ( VARGS * qmysql_fetch_lengths ) ( MYSQL_RES * result ) ;
2010-12-18 21:30:16 +00:00
static unsigned int ( VARGS * qmysql_field_count ) ( MYSQL * mysql ) ;
static void ( VARGS * qmysql_free_result ) ( MYSQL_RES * result ) ;
static const char * ( VARGS * qmysql_get_client_info ) ( void ) ;
static MYSQL * ( VARGS * qmysql_init ) ( MYSQL * mysql ) ;
static MYSQL_RES * ( VARGS * qmysql_store_result ) ( MYSQL * mysql ) ;
static unsigned int ( VARGS * qmysql_num_fields ) ( MYSQL_RES * res ) ;
static my_ulonglong ( VARGS * qmysql_num_rows ) ( MYSQL_RES * res ) ;
static int ( VARGS * qmysql_options ) ( MYSQL * mysql , enum mysql_option option , const char * arg ) ;
static int ( VARGS * qmysql_query ) ( MYSQL * mysql , const char * q ) ;
static MYSQL * ( VARGS * qmysql_real_connect ) ( MYSQL * mysql , const char * host , const char * user , const char * passwd , const char * db , unsigned int port , const char * unix_socket , unsigned long clientflag ) ;
static unsigned long ( VARGS * qmysql_real_escape_string ) ( MYSQL * mysql , char * to , const char * from , unsigned long length ) ;
static void ( VARGS * qmysql_thread_end ) ( void ) ;
static my_bool ( VARGS * qmysql_thread_init ) ( void ) ;
static unsigned int ( VARGS * qmysql_thread_safe ) ( void ) ;
static dllfunction_t mysqlfuncs [ ] =
{
{ ( void * ) & qmysql_library_end , " mysql_server_end " } , /* written as a define alias in mysql.h */
{ ( void * ) & qmysql_library_init , " mysql_server_init " } , /* written as a define alias in mysql.h */
{ ( void * ) & qmysql_affected_rows , " mysql_affected_rows " } ,
{ ( void * ) & qmysql_close , " mysql_close " } ,
{ ( void * ) & qmysql_data_seek , " mysql_data_seek " } ,
{ ( void * ) & qmysql_errno , " mysql_errno " } ,
{ ( void * ) & qmysql_error , " mysql_error " } ,
{ ( void * ) & qmysql_fetch_field_direct , " mysql_fetch_field_direct " } ,
{ ( void * ) & qmysql_fetch_row , " mysql_fetch_row " } ,
2014-03-30 08:55:06 +00:00
{ ( void * ) & qmysql_fetch_lengths , " mysql_fetch_lengths " } ,
2010-12-18 21:30:16 +00:00
{ ( void * ) & qmysql_field_count , " mysql_field_count " } ,
{ ( void * ) & qmysql_free_result , " mysql_free_result " } ,
{ ( void * ) & qmysql_get_client_info , " mysql_get_client_info " } ,
{ ( void * ) & qmysql_init , " mysql_init " } ,
{ ( void * ) & qmysql_store_result , " mysql_store_result " } ,
{ ( void * ) & qmysql_num_fields , " mysql_num_fields " } ,
{ ( void * ) & qmysql_num_rows , " mysql_num_rows " } ,
{ ( void * ) & qmysql_options , " mysql_options " } ,
{ ( void * ) & qmysql_query , " mysql_query " } ,
{ ( void * ) & qmysql_real_connect , " mysql_real_connect " } ,
{ ( void * ) & qmysql_real_escape_string , " mysql_real_escape_string " } ,
{ ( void * ) & qmysql_thread_end , " mysql_thread_end " } ,
{ ( void * ) & qmysql_thread_init , " mysql_thread_init " } ,
{ ( void * ) & qmysql_thread_safe , " mysql_thread_safe " } ,
{ NULL }
} ;
2012-11-29 13:37:48 +00:00
dllhandle_t * mysqlhandle ;
2012-11-27 03:23:19 +00:00
# endif
# ifdef USE_SQLITE
2012-12-04 19:37:57 +00:00
# include "sqlite3.h"
2012-11-27 03:23:19 +00:00
SQLITE_API int ( QDECL * qsqlite3_open ) ( const char * zFilename , sqlite3 * * ppDb ) ;
SQLITE_API const char * ( QDECL * qsqlite3_libversion ) ( void ) ;
SQLITE_API int ( QDECL * qsqlite3_set_authorizer ) ( sqlite3 * , int ( QDECL * xAuth ) ( void * , int , const char * , const char * , const char * , const char * ) , void * pUserData ) ;
SQLITE_API int ( QDECL * qsqlite3_enable_load_extension ) ( sqlite3 * db , int onoff ) ;
SQLITE_API const char * ( QDECL * qsqlite3_errmsg ) ( sqlite3 * db ) ;
SQLITE_API int ( QDECL * qsqlite3_close ) ( sqlite3 * db ) ;
SQLITE_API int ( QDECL * qsqlite3_prepare ) ( sqlite3 * db , const char * zSql , int nBytes , sqlite3_stmt * * ppStmt , const char * * pzTail ) ;
SQLITE_API int ( QDECL * qsqlite3_column_count ) ( sqlite3_stmt * pStmt ) ;
SQLITE_API const char * ( QDECL * qsqlite3_column_name ) ( sqlite3_stmt * pStmt , int N ) ;
SQLITE_API int ( QDECL * qsqlite3_step ) ( sqlite3_stmt * pStmt ) ;
SQLITE_API const unsigned char * ( QDECL * qsqlite3_column_text ) ( sqlite3_stmt * pStmt , int i ) ;
2014-03-30 08:55:06 +00:00
SQLITE_API int ( QDECL * qsqlite3_column_bytes ) ( sqlite3_stmt * , int iCol ) ;
2012-11-27 03:23:19 +00:00
SQLITE_API int ( QDECL * qsqlite3_finalize ) ( sqlite3_stmt * pStmt ) ;
static dllfunction_t sqlitefuncs [ ] =
{
{ ( void * ) & qsqlite3_open , " sqlite3_open " } , /* written as a define alias in mysql.h */
{ ( void * ) & qsqlite3_libversion , " sqlite3_libversion " } , /* written as a define alias in mysql.h */
{ ( void * ) & qsqlite3_set_authorizer , " sqlite3_set_authorizer " } ,
{ ( void * ) & qsqlite3_enable_load_extension , " sqlite3_enable_load_extension " } ,
{ ( void * ) & qsqlite3_errmsg , " sqlite3_errmsg " } ,
{ ( void * ) & qsqlite3_close , " sqlite3_close " } ,
{ ( void * ) & qsqlite3_prepare , " sqlite3_prepare " } ,
{ ( void * ) & qsqlite3_column_count , " sqlite3_column_count " } ,
{ ( void * ) & qsqlite3_column_name , " sqlite3_column_name " } ,
{ ( void * ) & qsqlite3_step , " sqlite3_step " } ,
{ ( void * ) & qsqlite3_column_text , " sqlite3_column_text " } ,
2014-03-30 08:55:06 +00:00
{ ( void * ) & qsqlite3_column_bytes , " sqlite3_column_bytes " } ,
2012-11-27 03:23:19 +00:00
{ ( void * ) & qsqlite3_finalize , " sqlite3_finalize " } ,
{ NULL }
} ;
2012-11-29 13:37:48 +00:00
dllhandle_t * sqlitehandle ;
2012-11-27 03:23:19 +00:00
# endif
2010-12-18 21:30:16 +00:00
2016-09-08 19:04:35 +00:00
cvar_t sql_driver = CVARF ( " sv_sql_driver " , " " , CVAR_NOUNSAFEEXPAND ) ;
cvar_t sql_host = CVARF ( " sv_sql_host " , " 127.0.0.1 " , CVAR_NOUNSAFEEXPAND ) ;
cvar_t sql_username = CVARF ( " sv_sql_username " , " " , CVAR_NOUNSAFEEXPAND ) ;
cvar_t sql_password = CVARF ( " sv_sql_password " , " " , CVAR_NOUNSAFEEXPAND ) ;
cvar_t sql_defaultdb = CVARF ( " sv_sql_defaultdb " , " " , CVAR_NOUNSAFEEXPAND ) ;
2010-12-18 21:30:16 +00:00
void SQL_PushResult ( sqlserver_t * server , queryresult_t * qres )
{
Sys_LockMutex ( server - > resultlock ) ;
qres - > next = NULL ;
if ( ! server - > resultslast )
server - > results = server - > resultslast = qres ;
else
server - > resultslast = server - > resultslast - > next = qres ;
Sys_UnlockMutex ( server - > resultlock ) ;
}
queryresult_t * SQL_PullResult ( sqlserver_t * server )
{
queryresult_t * qres ;
Sys_LockMutex ( server - > resultlock ) ;
qres = server - > results ;
if ( qres )
{
server - > results = qres - > next ;
if ( ! server - > results )
server - > resultslast = NULL ;
}
Sys_UnlockMutex ( server - > resultlock ) ;
return qres ;
}
2013-03-31 04:21:08 +00:00
//called by main thread
2010-12-18 21:30:16 +00:00
void SQL_PushRequest ( sqlserver_t * server , queryrequest_t * qreq )
{
2013-03-31 04:21:08 +00:00
qreq - > state = SR_PENDING ;
2010-12-18 21:30:16 +00:00
Sys_LockConditional ( server - > requestcondv ) ;
2013-03-31 04:21:08 +00:00
qreq - > nextqueue = NULL ;
2010-12-18 21:30:16 +00:00
if ( ! server - > requestslast )
2013-03-31 04:21:08 +00:00
server - > requestqueue = server - > requestslast = qreq ;
2010-12-18 21:30:16 +00:00
else
2013-03-31 04:21:08 +00:00
server - > requestslast = server - > requestslast - > nextqueue = qreq ;
2010-12-18 21:30:16 +00:00
Sys_UnlockConditional ( server - > requestcondv ) ;
}
2013-03-31 04:21:08 +00:00
//called by sql worker thread
2010-12-18 21:30:16 +00:00
queryrequest_t * SQL_PullRequest ( sqlserver_t * server , qboolean lock )
{
queryrequest_t * qreq ;
if ( lock )
Sys_LockConditional ( server - > requestcondv ) ;
2013-03-31 04:21:08 +00:00
qreq = server - > requestqueue ;
2010-12-18 21:30:16 +00:00
if ( qreq )
{
2013-03-31 04:21:08 +00:00
server - > requestqueue = qreq - > nextqueue ;
if ( ! server - > requestqueue )
2010-12-18 21:30:16 +00:00
server - > requestslast = NULL ;
}
Sys_UnlockConditional ( server - > requestcondv ) ;
return qreq ;
}
sqlserver_t * * sqlservers ;
int sqlservercount ;
2012-11-27 03:23:19 +00:00
int sqlavailable ;
2016-08-25 00:12:14 +00:00
int sqlinited ;
2012-11-27 03:23:19 +00:00
# ifdef USE_SQLITE
//this is to try to sandbox sqlite so it can only edit the file its originally opened with.
int QDECL mysqlite_authorizer ( void * ctx , int action , const char * detail0 , const char * detail1 , const char * detail2 , const char * detail3 )
{
if ( action = = SQLITE_PRAGMA )
{
Sys_Printf ( " SQL: Rejecting pragma \" %s \" \n " , detail0 ) ;
return SQLITE_DENY ;
}
if ( action = = SQLITE_ATTACH )
{
Sys_Printf ( " SQL: Rejecting attach to \" %s \" \n " , detail0 ) ;
return SQLITE_DENY ;
}
return SQLITE_OK ;
}
# endif
2010-12-18 21:30:16 +00:00
int sql_serverworker ( void * sref )
{
sqlserver_t * server = ( sqlserver_t * ) sref ;
2012-11-27 03:23:19 +00:00
const char * error = NULL ;
2013-05-11 05:03:07 +00:00
int i ;
2010-12-18 21:30:16 +00:00
qboolean needlock = false ;
2012-11-29 13:37:48 +00:00
qboolean allokay = true ;
2013-05-11 05:03:07 +00:00
# ifdef USE_MYSQL
int tinit = - 1 ;
# endif
2010-12-18 21:30:16 +00:00
2012-11-27 03:23:19 +00:00
switch ( server - > driver )
{
# ifdef USE_MYSQL
case SQLDRV_MYSQL :
2010-12-18 21:30:16 +00:00
{
2012-11-27 03:23:19 +00:00
my_bool reconnect = 1 ;
if ( ! qmysql_thread_init )
error = " MYSQL library not available " ;
else if ( tinit = qmysql_thread_init ( ) )
error = " MYSQL thread init failed " ;
else if ( ! ( server - > mysql = qmysql_init ( NULL ) ) )
error = " MYSQL init failed " ;
else if ( qmysql_options ( server - > mysql , MYSQL_OPT_RECONNECT , & reconnect ) )
error = " MYSQL reconnect options set failed " ;
else
{
int port = 0 ;
char * colon ;
2010-12-18 21:30:16 +00:00
2012-11-27 03:23:19 +00:00
colon = strchr ( server - > connectparams [ 0 ] , ' : ' ) ;
if ( colon )
{
* colon = ' \0 ' ;
port = atoi ( colon + 1 ) ;
}
2010-12-18 21:30:16 +00:00
2012-11-27 03:23:19 +00:00
if ( ! ( server - > mysql = qmysql_real_connect ( server - > mysql , server - > connectparams [ 0 ] , server - > connectparams [ 1 ] , server - > connectparams [ 2 ] , server - > connectparams [ 3 ] , port , 0 , 0 ) ) )
error = " MYSQL initial connect attempt failed " ;
2010-12-18 21:30:16 +00:00
2012-11-27 03:23:19 +00:00
if ( colon )
* colon = ' : ' ;
}
}
break ;
# endif
# ifdef USE_SQLITE
case SQLDRV_SQLITE :
if ( qsqlite3_open ( server - > connectparams [ 3 ] , & server - > sqlite ) )
{
error = qsqlite3_errmsg ( server - > sqlite ) ;
}
else
{
//disable extension loading, set up an authorizer hook.
qsqlite3_enable_load_extension ( server - > sqlite , false ) ;
qsqlite3_set_authorizer ( server - > sqlite , mysqlite_authorizer , server ) ;
}
break ;
# endif
default :
error = " That driver is not enabled in this build. " ;
break ;
}
2010-12-18 21:30:16 +00:00
if ( error )
2012-11-29 13:37:48 +00:00
allokay = false ;
2010-12-18 21:30:16 +00:00
2012-11-29 13:37:48 +00:00
while ( allokay )
2010-12-18 21:30:16 +00:00
{
Sys_LockConditional ( server - > requestcondv ) ;
2013-03-31 04:21:08 +00:00
if ( ! server - > requestqueue ) // this is needed for thread startup and to catch any "lost" changes
2011-01-27 01:34:08 +00:00
Sys_ConditionWait ( server - > requestcondv ) ;
2010-12-18 21:30:16 +00:00
needlock = false ; // so we don't try to relock first round
while ( 1 )
{
queryrequest_t * qreq = NULL ;
queryresult_t * qres ;
int columns = - 1 ;
if ( ! ( qreq = SQL_PullRequest ( server , needlock ) ) )
2012-11-29 13:37:48 +00:00
{
if ( ! server - > active )
allokay = false ;
2010-12-18 21:30:16 +00:00
break ;
2012-11-29 13:37:48 +00:00
}
2010-12-18 21:30:16 +00:00
// pullrequest makes sure our condition is unlocked but we'll need
// a lock next round
needlock = true ;
2012-11-27 03:23:19 +00:00
switch ( server - > driver )
2010-12-18 21:30:16 +00:00
{
2013-05-11 05:03:07 +00:00
default :
error = " Bad database driver " ;
allokay = false ;
break ;
2012-11-27 03:23:19 +00:00
# ifdef USE_MYSQL
case SQLDRV_MYSQL :
2010-12-18 21:30:16 +00:00
{
2013-05-11 05:03:07 +00:00
void * res = NULL ;
int qesize = 0 ;
int rows = - 1 ;
const char * qerror = NULL ;
// perform the query and fill out the result structure
if ( qmysql_query ( server - > mysql , qreq - > query ) )
qerror = qmysql_error ( server - > mysql ) ;
else // query succeeded
2012-11-27 03:23:19 +00:00
{
2013-05-11 05:03:07 +00:00
res = qmysql_store_result ( server - > mysql ) ;
if ( res ) // result set returned
{
rows = qmysql_num_rows ( res ) ;
columns = qmysql_num_fields ( res ) ;
}
else if ( qmysql_field_count ( server - > mysql ) = = 0 ) // no result set
{
rows = qmysql_affected_rows ( server - > mysql ) ;
if ( rows < 0 )
rows = 0 ;
columns = 0 ;
}
else // error
qerror = qmysql_error ( server - > mysql ) ;
2012-11-27 03:23:19 +00:00
}
2010-12-18 21:30:16 +00:00
2012-11-27 03:23:19 +00:00
if ( qerror )
2013-05-11 05:03:07 +00:00
qesize = Q_strlen ( qerror ) ;
qres = ( queryresult_t * ) ZF_Malloc ( sizeof ( queryresult_t ) + qesize ) ;
if ( qres )
{
if ( qerror )
Q_strncpy ( qres - > error , qerror , qesize ) ;
qres - > result = res ;
qres - > rows = rows ;
qres - > columns = columns ;
qres - > request = qreq ;
qres - > eof = true ; // store result has no more rows to read afterwards
SQL_PushResult ( server , qres ) ;
}
else // we're screwed here so bomb out
{
server - > active = false ;
error = " MALLOC ERROR! Unable to allocate query result! " ;
break ;
}
2012-11-27 03:23:19 +00:00
}
break ;
# endif
# ifdef USE_SQLITE
case SQLDRV_SQLITE :
{
int rc ;
sqlite3_stmt * pStmt ;
const char * trailingstring ;
char * statementstring = qreq - > query ;
2014-03-30 08:55:06 +00:00
sqliteresult_t * mat ;
2012-11-27 03:23:19 +00:00
int rowspace ;
2012-12-04 19:37:57 +00:00
int totalrows = 0 ;
2012-11-27 03:23:19 +00:00
qboolean keeplooping = true ;
2013-04-13 07:53:21 +00:00
// Sys_Printf("processing %s\n", statementstring);
2012-11-27 03:23:19 +00:00
// qsqlite3_mutex_enter(server->sqlite->mutex);
// while(*statementstring)
// {
if ( qsqlite3_prepare ( server - > sqlite , statementstring , - 1 , & pStmt , & trailingstring ) = = SQLITE_OK )
{ //sql statement is valid, apparently.
columns = qsqlite3_column_count ( pStmt ) ;
rc = qsqlite3_step ( pStmt ) ;
while ( keeplooping )
{
rowspace = 65 ;
2014-03-30 08:55:06 +00:00
qres = ( queryresult_t * ) ZF_Malloc ( sizeof ( queryresult_t ) + columns * sizeof ( sqliteresult_t ) * rowspace ) ;
mat = ( sqliteresult_t * ) ( qres + 1 ) ;
2012-11-27 03:23:19 +00:00
if ( qres )
{
qres - > result = mat ;
qres - > rows = 0 ;
qres - > columns = columns ;
qres - > request = qreq ;
2012-12-04 19:37:57 +00:00
qres - > firstrow = totalrows ;
2012-11-27 03:23:19 +00:00
qres - > eof = false ;
2013-03-31 04:21:08 +00:00
qreq - > nextqueue = NULL ;
2012-11-27 03:23:19 +00:00
//headers technically take a row.
for ( i = 0 ; i < columns ; i + + )
{
2014-03-30 08:55:06 +00:00
mat [ i ] . ptr = strdup ( qsqlite3_column_name ( pStmt , i ) ) ;
mat [ i ] . len = 0 ;
2012-11-27 03:23:19 +00:00
}
rowspace - - ;
mat + = columns ;
while ( 1 )
{
if ( rc = = SQLITE_ROW )
{
if ( ! rowspace )
break ;
//generate the row info
for ( i = 0 ; i < columns ; i + + )
2014-03-30 08:55:06 +00:00
{
const char * data = qsqlite3_column_text ( pStmt , i ) ;
mat [ i ] . len = qsqlite3_column_bytes ( pStmt , i ) ;
mat [ i ] . ptr = malloc ( mat [ i ] . len + 1 ) ;
memcpy ( mat [ i ] . ptr , data , mat [ i ] . len ) ;
mat [ i ] . ptr [ mat [ i ] . len ] = 0 ; //make sure blobs are null terminated, in case someone reads them as a string.
}
2012-11-27 03:23:19 +00:00
qres - > rows + + ;
2012-12-04 19:37:57 +00:00
totalrows + + ;
2012-11-27 03:23:19 +00:00
rowspace - - ;
mat + = columns ;
}
else if ( rc = = SQLITE_DONE )
{
//no more data to get.
keeplooping = false ;
qres - > eof = true ; //this one was the ender.
break ;
}
2012-11-29 13:37:48 +00:00
else
{
Sys_Printf ( " sqlite error code %i: %s \n " , rc , statementstring ) ;
keeplooping = false ;
qres - > eof = true ; //this one was the ender.
if ( ! qres - > columns )
qres - > columns = - 1 ;
break ;
}
2012-11-27 03:23:19 +00:00
rc = qsqlite3_step ( pStmt ) ;
}
}
else
keeplooping = false ;
SQL_PushResult ( server , qres ) ;
}
qsqlite3_finalize ( pStmt ) ;
}
else
{
2012-12-04 19:37:57 +00:00
qres = ( queryresult_t * ) ZF_Malloc ( sizeof ( queryresult_t ) + 18 + strlen ( statementstring ) ) ;
2012-11-27 03:23:19 +00:00
if ( qres )
{
2012-12-04 19:37:57 +00:00
strcpy ( qres - > error , " Bad SQL statement " ) ;
strcpy ( qres - > error + 18 , statementstring ) ;
2012-11-27 03:23:19 +00:00
qres - > result = NULL ;
qres - > rows = 0 ;
qres - > columns = - 1 ;
2012-12-04 19:37:57 +00:00
qres - > firstrow = totalrows ;
2012-11-27 03:23:19 +00:00
qres - > request = qreq ;
qres - > eof = true ;
2013-03-31 04:21:08 +00:00
qreq - > nextqueue = NULL ;
2012-11-27 03:23:19 +00:00
SQL_PushResult ( server , qres ) ;
}
break ;
}
// statementstring = trailingstring;
// }
// qsqlite3_mutex_leave(server->sqlite->mutex);
}
2010-12-18 21:30:16 +00:00
break ;
2012-11-27 03:23:19 +00:00
# endif
2010-12-18 21:30:16 +00:00
}
}
}
2012-11-29 13:37:48 +00:00
server - > active = false ;
2010-12-18 21:30:16 +00:00
2012-11-27 03:23:19 +00:00
switch ( server - > driver )
{
# ifdef USE_MYSQL
case SQLDRV_MYSQL :
if ( server - > mysql )
qmysql_close ( server - > mysql ) ;
break ;
# endif
# ifdef USE_SQLITE
case SQLDRV_SQLITE :
qsqlite3_close ( server - > sqlite ) ;
server - > sqlite = NULL ;
break ;
# endif
2013-05-11 05:03:07 +00:00
default :
break ;
2012-11-27 03:23:19 +00:00
}
2010-12-18 21:30:16 +00:00
// if we have a server error we still need to put it on the queue
if ( error )
{
int esize = Q_strlen ( error ) ;
queryresult_t * qres = ( queryresult_t * ) Z_Malloc ( sizeof ( queryresult_t ) + esize ) ;
if ( qres )
{ // hopefully the qmysql_close gained us some memory otherwise we're pretty screwed
qres - > rows = qres - > columns = - 1 ;
Q_strncpy ( qres - > error , error , esize ) ;
SQL_PushResult ( server , qres ) ;
}
}
2012-11-27 03:23:19 +00:00
# ifdef USE_MYSQL
2010-12-18 21:30:16 +00:00
if ( ! tinit )
qmysql_thread_end ( ) ;
2012-11-27 03:23:19 +00:00
# endif
2010-12-18 21:30:16 +00:00
2012-11-27 03:23:19 +00:00
server - > terminated = true ;
2010-12-18 21:30:16 +00:00
return 0 ;
}
sqlserver_t * SQL_GetServer ( int serveridx , qboolean inactives )
{
if ( serveridx < 0 | | serveridx > = sqlservercount )
return NULL ;
2012-11-27 03:23:19 +00:00
if ( ! sqlservers [ serveridx ] )
return NULL ;
2010-12-18 21:30:16 +00:00
if ( ! inactives & & sqlservers [ serveridx ] - > active = = false )
return NULL ;
return sqlservers [ serveridx ] ;
}
2013-03-31 04:21:08 +00:00
queryrequest_t * SQL_GetQueryRequest ( sqlserver_t * server , int queryidx )
2010-12-18 21:30:16 +00:00
{
2013-03-31 04:21:08 +00:00
queryrequest_t * qreq ;
for ( qreq = server - > requests ; qreq ; qreq = qreq - > nextreq )
{
if ( qreq - > num = = queryidx )
return qreq ;
}
return NULL ;
}
2010-12-18 21:30:16 +00:00
2013-03-31 04:21:08 +00:00
queryresult_t * SQL_GetQueryResult ( sqlserver_t * server , int queryidx , int row )
{
queryresult_t * qres ;
queryrequest_t * qreq ;
2010-12-18 21:30:16 +00:00
2013-03-31 04:21:08 +00:00
qreq = SQL_GetQueryRequest ( server , queryidx ) ;
for ( qres = qreq - > results ; qres ; qres = qres - > next )
2014-02-13 23:54:57 +00:00
if ( qres - > request & & qres - > request - > num = = queryidx & & ( row > = qres - > firstrow | | ( row = = - 1 & & ! qres - > firstrow ) ) & & row < qres - > firstrow + qres - > rows )
2010-12-18 21:30:16 +00:00
return qres ;
return NULL ;
}
2012-11-27 03:23:19 +00:00
static void SQL_DeallocResult ( sqlserver_t * server , queryresult_t * qres )
2010-12-18 21:30:16 +00:00
{
// deallocate current result
2012-11-27 03:23:19 +00:00
switch ( server - > driver )
{
2013-05-11 05:03:07 +00:00
default :
break ;
2012-11-27 03:23:19 +00:00
# ifdef USE_MYSQL
case SQLDRV_MYSQL :
if ( qres - > result )
qmysql_free_result ( qres - > result ) ;
break ;
# endif
# ifdef USE_SQLITE
case SQLDRV_SQLITE :
if ( qres - > result )
{
2014-12-08 21:18:32 +00:00
sqliteresult_t * mat = qres - > result ;
2012-11-27 03:23:19 +00:00
int i ;
for ( i = 0 ; i < qres - > columns * ( qres - > rows + 1 ) ; i + + )
2014-12-08 21:18:32 +00:00
free ( mat [ i ] . ptr ) ;
2012-11-27 03:23:19 +00:00
}
break ;
# endif
}
2010-12-18 21:30:16 +00:00
Z_Free ( qres ) ;
}
2013-03-31 04:21:08 +00:00
static void SQL_CloseRequestResult ( sqlserver_t * server , queryresult_t * qres )
2010-12-18 21:30:16 +00:00
{
queryresult_t * prev , * cur ;
2013-03-31 04:21:08 +00:00
prev = qres - > request - > results ;
2010-12-18 21:30:16 +00:00
if ( prev = = qres )
{
2013-03-31 04:21:08 +00:00
qres - > request - > results = prev - > next ;
2012-11-27 03:23:19 +00:00
SQL_DeallocResult ( server , prev ) ;
2010-12-18 21:30:16 +00:00
return ;
}
for ( cur = prev - > next ; cur ; prev = cur , cur = prev - > next )
{
if ( cur = = qres )
{
prev = cur - > next ;
2012-11-27 03:23:19 +00:00
SQL_DeallocResult ( server , cur ) ;
2010-12-18 21:30:16 +00:00
return ;
}
}
}
2013-03-31 04:21:08 +00:00
//MT only. flush a result without discarding the request. will result in gaps.
2010-12-18 21:30:16 +00:00
void SQL_CloseResult ( sqlserver_t * server , queryresult_t * qres )
{
if ( ! qres )
return ;
2013-03-31 04:21:08 +00:00
/* if (qres == server->currentresult)
2010-12-18 21:30:16 +00:00
{
2012-11-27 03:23:19 +00:00
SQL_DeallocResult ( server , server - > currentresult ) ;
2010-12-18 21:30:16 +00:00
server - > currentresult = NULL ;
return ;
}
2013-03-31 04:21:08 +00:00
*/
2010-12-18 21:30:16 +00:00
// else we have a persistant query
2013-03-31 04:21:08 +00:00
SQL_CloseRequestResult ( server , qres ) ;
}
//MT only. releases the request.
void SQL_CloseRequest ( sqlserver_t * server , queryrequest_t * qreq , qboolean force )
{
while ( qreq - > results )
{
SQL_CloseResult ( server , qreq - > results ) ;
}
//if the worker thread is still active with it for whatever reason, flag it as aborted but keep it otherwise valid. actually close it later on when we get the results back.
2013-04-13 07:53:21 +00:00
if ( qreq - > state ! = SR_FINISHED & & qreq - > state ! = SR_NEW & & ! force )
2013-03-31 04:21:08 +00:00
qreq - > state = SR_ABORTED ;
else
{
queryrequest_t * prev , * cur ;
//unlink and free it now that its complete.
prev = server - > requests ;
if ( prev = = qreq )
server - > requests = prev - > nextreq ;
else
{
for ( cur = prev - > nextreq ; cur ; prev = cur , cur = prev - > nextreq )
{
if ( cur = = qreq )
{
prev - > nextreq = cur - > nextreq ;
break ;
}
}
}
Z_Free ( qreq ) ;
}
2010-12-18 21:30:16 +00:00
}
2013-03-31 04:21:08 +00:00
void SQL_CloseAllRequests ( sqlserver_t * server )
2010-12-18 21:30:16 +00:00
{
queryresult_t * oldqres , * qres ;
2013-03-31 04:21:08 +00:00
//we assume lock is either held or the thread has already died. this function isn't (worker)thread safe.
// close pending results
2010-12-18 21:30:16 +00:00
qres = server - > results ;
while ( qres )
{
oldqres = qres ;
qres = qres - > next ;
2012-11-27 03:23:19 +00:00
SQL_DeallocResult ( server , oldqres ) ;
2010-12-18 21:30:16 +00:00
}
2013-03-31 04:21:08 +00:00
//now terminate all the requests
while ( server - > requests )
2010-12-18 21:30:16 +00:00
{
2013-03-31 04:21:08 +00:00
SQL_CloseRequest ( server , server - > requests , true ) ;
2010-12-18 21:30:16 +00:00
}
2013-03-31 04:21:08 +00:00
2010-12-18 21:30:16 +00:00
// close server result
if ( server - > serverresult )
{
2012-11-27 03:23:19 +00:00
SQL_DeallocResult ( server , server - > serverresult ) ;
2010-12-18 21:30:16 +00:00
server - > serverresult = NULL ;
}
}
2014-03-30 08:55:06 +00:00
char * SQL_ReadField ( sqlserver_t * server , queryresult_t * qres , int row , int col , qboolean fields , size_t * resultsize )
2010-12-18 21:30:16 +00:00
{
2014-03-30 08:55:06 +00:00
if ( resultsize )
* resultsize = 0 ;
2013-03-31 04:21:08 +00:00
if ( ! qres - > result )
2010-12-18 21:30:16 +00:00
return NULL ;
else
{ // store_result query
2013-03-31 04:21:08 +00:00
row - = qres - > firstrow ;
2010-12-18 21:30:16 +00:00
if ( qres - > rows < row | | qres - > columns < col | | col < 0 )
return NULL ;
if ( row < 0 )
{ // fetch field name
if ( fields ) // but only if we asked for them
{
2012-11-27 03:23:19 +00:00
switch ( server - > driver )
{
# ifdef USE_MYSQL
case SQLDRV_MYSQL :
{
MYSQL_FIELD * field = qmysql_fetch_field_direct ( qres - > result , col ) ;
2010-12-18 21:30:16 +00:00
2014-03-30 08:55:06 +00:00
if ( resultsize )
* resultsize = 0 ;
2012-11-27 03:23:19 +00:00
if ( ! field )
return NULL ;
else
return field - > name ;
}
# endif
# ifdef USE_SQLITE
case SQLDRV_SQLITE :
{
2014-03-30 08:55:06 +00:00
sqliteresult_t * mat = qres - > result ;
2012-11-27 03:23:19 +00:00
if ( mat )
2014-03-30 08:55:06 +00:00
{
if ( resultsize )
* resultsize = mat [ col ] . len ;
return mat [ col ] . ptr ;
}
2012-11-27 03:23:19 +00:00
}
2010-12-18 21:30:16 +00:00
return NULL ;
2012-11-27 03:23:19 +00:00
# endif
default :
return NULL ;
}
2010-12-18 21:30:16 +00:00
}
else
return NULL ;
}
else
{ // fetch data
2012-11-27 03:23:19 +00:00
switch ( server - > driver )
{
# ifdef USE_MYSQL
case SQLDRV_MYSQL :
{
MYSQL_ROW sqlrow ;
2014-03-30 08:55:06 +00:00
unsigned long * lengths ;
2010-12-18 21:30:16 +00:00
2012-11-27 03:23:19 +00:00
qmysql_data_seek ( qres - > result , row ) ;
sqlrow = qmysql_fetch_row ( qres - > result ) ;
2014-03-30 08:55:06 +00:00
lengths = qmysql_fetch_lengths ( qres - > result ) ;
if ( resultsize )
* resultsize = lengths ? lengths [ col ] : 0 ;
2012-11-27 03:23:19 +00:00
if ( ! sqlrow | | ! sqlrow [ col ] )
return NULL ;
else
return sqlrow [ col ] ;
}
# endif
# ifdef USE_SQLITE
case SQLDRV_SQLITE :
{
2014-03-30 08:55:06 +00:00
sqliteresult_t * mat = qres - > result ;
2012-11-27 03:23:19 +00:00
col + = qres - > columns * ( row + 1 ) ;
if ( mat )
2014-03-30 08:55:06 +00:00
{
if ( resultsize )
* resultsize = mat [ col ] . len ;
return mat [ col ] . ptr ;
}
2012-11-27 03:23:19 +00:00
}
return NULL ;
# endif
default :
2010-12-18 21:30:16 +00:00
return NULL ;
2012-11-27 03:23:19 +00:00
}
2010-12-18 21:30:16 +00:00
}
}
}
2012-11-27 03:23:19 +00:00
void SQL_CleanupServer ( sqlserver_t * server )
{
int i ;
queryrequest_t * qreq , * oldqreq ;
server - > active = false ; // set thread to kill itself
if ( server - > requestcondv )
Sys_ConditionBroadcast ( server - > requestcondv ) ; // force condition check
if ( server - > thread )
Sys_WaitOnThread ( server - > thread ) ; // wait on thread to die
// server resource deallocation (TODO: should this be done in the thread itself?)
if ( server - > requestcondv )
Sys_DestroyConditional ( server - > requestcondv ) ;
if ( server - > resultlock )
Sys_DestroyMutex ( server - > resultlock ) ;
// close orphaned requests
2013-03-31 04:21:08 +00:00
qreq = server - > requestqueue ;
2012-11-27 03:23:19 +00:00
while ( qreq )
{
oldqreq = qreq ;
2013-03-31 04:21:08 +00:00
qreq = qreq - > nextqueue ;
2012-11-27 03:23:19 +00:00
Z_Free ( oldqreq ) ;
}
2013-03-31 04:21:08 +00:00
SQL_CloseAllRequests ( server ) ;
2012-11-27 03:23:19 +00:00
for ( i = SQL_CONNECT_STRUCTPARAMS ; i < SQL_CONNECT_PARAMS ; i + + )
Z_Free ( server - > connectparams [ i ] ) ;
if ( server - > connectparams )
BZ_Free ( server - > connectparams ) ;
Z_Free ( server ) ;
}
2014-03-30 08:55:06 +00:00
int SQL_NewServer ( const char * driver , const char * * paramstr )
2010-12-18 21:30:16 +00:00
{
sqlserver_t * server ;
int serverref ;
int drvchoice ;
int paramsize [ SQL_CONNECT_PARAMS ] ;
2012-11-27 03:23:19 +00:00
char nativepath [ MAX_OSPATH ] ;
2010-12-18 21:30:16 +00:00
int i , tsize ;
2012-12-04 19:37:57 +00:00
//name matches
2010-12-18 21:30:16 +00:00
if ( Q_strcasecmp ( driver , " mysql " ) = = 0 )
drvchoice = SQLDRV_MYSQL ;
else if ( Q_strcasecmp ( driver , " sqlite " ) = = 0 )
2012-11-27 03:23:19 +00:00
drvchoice = SQLDRV_SQLITE ;
2012-12-04 19:37:57 +00:00
else if ( ! * driver & & ( sqlavailable & ( 1u < < SQLDRV_SQLITE ) ) )
drvchoice = SQLDRV_SQLITE ;
else if ( ! * driver & & ( sqlavailable & ( 1u < < SQLDRV_MYSQL ) ) )
drvchoice = SQLDRV_MYSQL ;
2010-12-18 21:30:16 +00:00
else // invalid driver choice so we bomb out
return - 1 ;
2016-08-25 00:12:14 +00:00
if ( ! SQL_Available ( ) ) //also makes sure the drivers are actually loaded.
return - 1 ;
2012-11-27 03:23:19 +00:00
if ( ! ( sqlavailable & ( 1u < < drvchoice ) ) )
return - 1 ;
if ( drvchoice = = SQLDRV_SQLITE )
{
2012-12-04 19:37:57 +00:00
//sqlite can be sandboxed.
2012-11-27 03:23:19 +00:00
//explicitly allow 'temp' and 'memory' databases
if ( * paramstr [ 3 ] & & strcmp ( paramstr [ 3 ] , " :memory: " ) )
{
//anything else is sandboxed into a subdir/database.
2012-12-04 19:37:57 +00:00
char * qname = va ( " sqlite/%s.db " , paramstr [ 3 ] ) ;
2012-11-27 03:23:19 +00:00
if ( ! FS_NativePath ( qname , FS_GAMEONLY , nativepath , sizeof ( nativepath ) ) )
return - 1 ;
paramstr [ 3 ] = nativepath ;
FS_CreatePath ( qname , FS_GAMEONLY ) ;
}
}
for ( i = 0 ; i < SQL_CONNECT_PARAMS ; i + + )
paramsize [ i ] = Q_strlen ( paramstr [ i ] ) ;
2010-12-18 21:30:16 +00:00
// alloc or realloc sql servers array
if ( sqlservers = = NULL )
{
serverref = 0 ;
sqlservercount = 1 ;
sqlservers = ( sqlserver_t * * ) BZ_Malloc ( sizeof ( sqlserver_t * ) ) ;
}
else
{
serverref = sqlservercount ;
sqlservercount + + ;
sqlservers = ( sqlserver_t * * ) BZ_Realloc ( sqlservers , sizeof ( sqlserver_t * ) * sqlservercount ) ;
}
// assemble server structure
tsize = 0 ;
for ( i = 0 ; i < SQL_CONNECT_STRUCTPARAMS ; i + + )
tsize + = paramsize [ i ] + 1 ; // allocate extra space for host and user only
server = ( sqlserver_t * ) Z_Malloc ( sizeof ( sqlserver_t ) + tsize ) ;
server - > connectparams = ( char * * ) BZ_Malloc ( sizeof ( char * ) * SQL_CONNECT_PARAMS ) ;
tsize = 0 ;
for ( i = 0 ; i < SQL_CONNECT_STRUCTPARAMS ; i + + )
{
server - > connectparams [ i ] = ( ( char * ) ( server + 1 ) ) + tsize ;
Q_strncpy ( server - > connectparams [ i ] , paramstr [ i ] , paramsize [ i ] ) ;
// string should be null-terminated due to Z_Malloc
tsize + = paramsize [ i ] + 1 ;
}
for ( i = SQL_CONNECT_STRUCTPARAMS ; i < SQL_CONNECT_PARAMS ; i + + )
{
server - > connectparams [ i ] = ( char * ) Z_Malloc ( sizeof ( char ) * ( paramsize [ i ] + 1 ) ) ;
Q_strncpy ( server - > connectparams [ i ] , paramstr [ i ] , paramsize [ i ] ) ;
// string should be null-terminated due to Z_Malloc
}
sqlservers [ serverref ] = server ;
server - > driver = ( sqldrv_t ) drvchoice ;
server - > querynum = 1 ;
server - > active = true ;
server - > requestcondv = Sys_CreateConditional ( ) ;
server - > resultlock = Sys_CreateMutex ( ) ;
if ( ! server - > requestcondv | | ! server - > resultlock )
{
if ( server - > requestcondv )
Sys_DestroyConditional ( server - > requestcondv ) ;
if ( server - > resultlock )
Sys_DestroyMutex ( server - > resultlock ) ;
Z_Free ( server ) ;
sqlservercount - - ;
return - 1 ;
}
2012-11-29 13:37:48 +00:00
server - > thread = Sys_CreateThread ( " sqlworker " , sql_serverworker , ( void * ) server , THREADP_NORMAL , 1024 ) ;
2010-12-18 21:30:16 +00:00
if ( ! server - > thread )
{
Z_Free ( server ) ;
sqlservercount - - ;
return - 1 ;
}
return serverref ;
}
2017-08-14 16:38:44 +00:00
int SQL_NewQuery ( sqlserver_t * server , qboolean ( * callback ) ( queryrequest_t * req , int firstrow , int numrows , int numcols , qboolean eof ) , const char * str , queryrequest_t * * reqout )
2010-12-18 21:30:16 +00:00
{
int qsize = Q_strlen ( str ) ;
queryrequest_t * qreq ;
int querynum ;
qreq = ( queryrequest_t * ) ZF_Malloc ( sizeof ( queryrequest_t ) + qsize ) ;
if ( qreq )
{
2013-03-31 04:21:08 +00:00
qreq - > state = SR_NEW ;
2010-12-18 21:30:16 +00:00
querynum = qreq - > num = server - > querynum ;
// prevent the reference num from getting too big to prevent FP problems
2013-03-31 04:21:08 +00:00
while ( 1 )
{
if ( + + server - > querynum > ( 1 < < 21 ) )
server - > querynum = 1 ;
if ( ! SQL_GetQueryRequest ( server , server - > querynum ) )
break ;
}
qreq - > callback = callback ;
2010-12-18 21:30:16 +00:00
Q_strncpy ( qreq - > query , str , qsize ) ;
2013-03-31 04:21:08 +00:00
qreq - > nextreq = server - > requests ;
server - > requests = qreq ;
2010-12-18 21:30:16 +00:00
SQL_PushRequest ( server , qreq ) ;
Sys_ConditionSignal ( server - > requestcondv ) ;
2013-03-31 04:21:08 +00:00
* reqout = qreq ;
2010-12-18 21:30:16 +00:00
return querynum ;
}
2013-03-31 04:21:08 +00:00
* reqout = NULL ;
2010-12-18 21:30:16 +00:00
return - 1 ;
}
void SQL_Disconnect ( sqlserver_t * server )
{
server - > active = false ;
// force the threads to reiterate requests and hopefully terminate
Sys_ConditionBroadcast ( server - > requestcondv ) ;
}
2014-03-30 08:55:06 +00:00
void SQL_Escape ( sqlserver_t * server , const char * src , char * dst , int dstlen )
2010-12-18 21:30:16 +00:00
{
switch ( server - > driver )
{
2012-11-27 03:23:19 +00:00
# ifdef USE_MYSQL
2010-12-18 21:30:16 +00:00
case SQLDRV_MYSQL :
2012-11-27 03:23:19 +00:00
{
int srclen = strlen ( dst ) ;
if ( srclen > ( dstlen / 2 - 1 ) )
dst [ 0 ] = ' \0 ' ;
else
qmysql_real_escape_string ( server - > mysql , dst , src , srclen ) ;
}
break ;
# endif
# ifdef USE_SQLITE
case SQLDRV_SQLITE :
{
dstlen - - ;
while ( dstlen > 2 & & * src )
{
if ( * src = = ' \' ' )
{
* dst + + = * src ;
dstlen - - ;
}
* dst + + = * src + + ;
dstlen - - ;
}
* dst = ' \0 ' ;
}
2010-12-18 21:30:16 +00:00
break ;
2012-11-27 03:23:19 +00:00
# endif
2010-12-18 21:30:16 +00:00
default :
dst [ 0 ] = ' \0 ' ;
}
}
2012-11-27 03:23:19 +00:00
const char * SQL_Info ( sqlserver_t * server )
2010-12-18 21:30:16 +00:00
{
switch ( server - > driver )
{
2012-11-27 03:23:19 +00:00
# ifdef USE_MYSQL
2010-12-18 21:30:16 +00:00
case SQLDRV_MYSQL :
2012-11-27 03:23:19 +00:00
if ( qmysql_get_client_info )
return va ( " mysql: %s " , qmysql_get_client_info ( ) ) ;
else
return " ERROR: mysql library not loaded " ;
2010-12-18 21:30:16 +00:00
break ;
2012-11-27 03:23:19 +00:00
# endif
# ifdef USE_SQLITE
case SQLDRV_SQLITE :
if ( qsqlite3_libversion )
return va ( " sqlite: %s " , qsqlite3_libversion ( ) ) ;
else
return " ERROR: sqlite library not loaded " ;
# endif
2010-12-18 21:30:16 +00:00
default :
return " unknown " ;
}
}
qboolean SQL_Available ( void )
{
2016-08-25 00:12:14 +00:00
if ( ! sqlinited )
{
sqlinited = true ;
# ifdef USE_MYSQL
//mysql pokes network etc. there's no sandbox. people can use quake clients to pry upon private databases.
if ( COM_CheckParm ( " -mysql " ) )
if ( SQL_MYSQLInit ( ) )
sqlavailable | = 1u < < SQLDRV_MYSQL ;
# endif
# ifdef USE_SQLITE
//our sqlite implementation is sandboxed. we block database attachments, and restrict the master database name.
2016-09-09 18:46:08 +00:00
# ifdef _WIN32
2016-08-25 00:12:14 +00:00
sqlitehandle = Sys_LoadLibrary ( " sqlite3 " , sqlitefuncs ) ;
2016-09-09 18:46:08 +00:00
# else
sqlitehandle = Sys_LoadLibrary ( " libsqlite3.so.0 " , sqlitefuncs ) ; //at least on debian.
# endif
2016-08-25 00:12:14 +00:00
if ( sqlitehandle )
{
sqlavailable | = 1u < < SQLDRV_SQLITE ;
}
# endif
}
2012-11-27 03:23:19 +00:00
return ! ! sqlavailable ;
2010-12-18 21:30:16 +00:00
}
/* SQL related commands */
void SQL_Status_f ( void )
{
int i ;
2013-03-31 04:21:08 +00:00
char * stat ;
2010-12-18 21:30:16 +00:00
2016-08-25 00:12:14 +00:00
SQL_Available ( ) ; //also ensures any drivers are loaded.
# ifdef USE_MYSQL
if ( ! COM_CheckParm ( " -mysql " ) )
Con_Printf ( " mysql: %s \n " , " requires -mysql cmdline argument " ) ;
2012-11-27 03:23:19 +00:00
else
2016-08-25 00:12:14 +00:00
Con_Printf ( " mysql: %s \n " , ( sqlavailable & ( 1u < < SQLDRV_MYSQL ) ) ? " loaded " : " unavailable " ) ;
# else
Con_Printf ( " mysql: %s \n " , " disabled at compile time " ) ;
# endif
# ifdef USE_SQLITE
Con_Printf ( " sqlite: %s \n " , ( sqlavailable & ( 1u < < SQLDRV_SQLITE ) ) ? " loaded " : " unavailable " ) ;
# else
Con_Printf ( " sqlite: %s \n " , " disabled at compile time " ) ;
# endif
Con_Printf ( " %i connections \n " , sqlservercount ) ;
2010-12-18 21:30:16 +00:00
for ( i = 0 ; i < sqlservercount ; i + + )
{
int reqnum = 0 ;
int resnum = 0 ;
queryrequest_t * qreq ;
queryresult_t * qres ;
sqlserver_t * server = sqlservers [ i ] ;
2012-11-27 03:23:19 +00:00
if ( ! server )
continue ;
2010-12-18 21:30:16 +00:00
Sys_LockMutex ( server - > resultlock ) ;
Sys_LockConditional ( server - > requestcondv ) ;
2013-03-31 04:21:08 +00:00
for ( qreq = server - > requests ; qreq ; qreq = qreq - > nextreq )
2010-12-18 21:30:16 +00:00
reqnum + + ;
for ( qres = server - > results ; qres ; qres = qres - > next )
resnum + + ;
2012-11-27 03:23:19 +00:00
switch ( server - > driver )
{
case SQLDRV_MYSQL :
Con_Printf ( " #%i %s@%s: %s \n " ,
i ,
server - > connectparams [ 1 ] ,
server - > connectparams [ 0 ] ,
server - > active ? " active " : " inactive " ) ;
break ;
case SQLDRV_SQLITE :
Con_Printf ( " #%i %s: %s \n " ,
i ,
server - > connectparams [ 3 ] ,
server - > active ? " active " : " inactive " ) ;
break ;
2013-05-11 05:03:07 +00:00
default :
Con_Printf ( " Bad driver \n " ) ;
break ;
2012-11-27 03:23:19 +00:00
}
2010-12-18 21:30:16 +00:00
if ( reqnum )
{
2013-03-31 04:21:08 +00:00
Con_Printf ( " - %i requests \n " , reqnum ) ;
for ( qreq = server - > requests ; qreq ; qreq = qreq - > nextqueue )
2010-12-18 21:30:16 +00:00
{
2013-03-31 04:21:08 +00:00
switch ( qreq - > state )
{
case SR_NEW : stat = " new " ; break ;
case SR_PENDING : stat = " pending " ; break ;
case SR_PARTIAL : stat = " partial " ; break ;
case SR_FINISHED : stat = " finished " ; break ;
case SR_ABORTED : stat = " aborted " ; break ;
default : stat = " ??? " ; break ;
}
Con_Printf ( " query #%i (%s): %s \n " ,
2010-12-18 21:30:16 +00:00
qreq - > num ,
2013-03-31 04:21:08 +00:00
stat ,
2010-12-18 21:30:16 +00:00
qreq - > query ) ;
// TODO: function lookup?
}
}
if ( resnum )
{
2013-03-31 04:21:08 +00:00
Con_Printf ( " - %i pending results \n " , resnum ) ;
2010-12-18 21:30:16 +00:00
for ( qres = server - > results ; qres ; qres = qres - > next )
{
Con_Printf ( " * %i rows, %i columns " ,
qres - > rows ,
qres - > columns ) ;
if ( qres - > error [ 0 ] )
Con_Printf ( " , error %s \n " , qres - > error ) ;
else
Con_Printf ( " \n " ) ;
// TODO: request info?
}
}
if ( server - > serverresult )
Con_Printf ( " server result: error %s \n " , server - > serverresult - > error ) ;
// TODO: list all requests, results here
Sys_UnlockMutex ( server - > resultlock ) ;
Sys_UnlockConditional ( server - > requestcondv ) ;
}
}
void SQL_Kill_f ( void )
{
sqlserver_t * server ;
if ( Cmd_Argc ( ) < 2 )
{
Con_Printf ( " Syntax: %s serverid \n " , Cmd_Argv ( 0 ) ) ;
return ;
}
server = SQL_GetServer ( atoi ( Cmd_Argv ( 1 ) ) , false ) ;
if ( server )
{
server - > active = false ;
Sys_ConditionBroadcast ( server - > requestcondv ) ;
return ;
}
}
void SQL_Killall_f ( void )
{
SQL_KillServers ( ) ;
}
2013-03-31 04:21:08 +00:00
void SQL_ServerCycle ( void )
2010-12-18 21:30:16 +00:00
{
int i ;
for ( i = 0 ; i < sqlservercount ; i + + )
{
sqlserver_t * server = sqlservers [ i ] ;
queryresult_t * qres ;
2013-03-31 04:21:08 +00:00
queryrequest_t * qreq ;
2010-12-18 21:30:16 +00:00
2012-11-27 03:23:19 +00:00
if ( ! server )
continue ;
2013-05-11 05:03:07 +00:00
while ( ( qres = SQL_PullResult ( server ) ) )
2010-12-18 21:30:16 +00:00
{
2013-03-31 04:21:08 +00:00
qreq = qres - > request ;
2010-12-18 21:30:16 +00:00
qres - > next = NULL ;
2013-03-31 04:21:08 +00:00
if ( qreq & & qreq - > callback )
2010-12-18 21:30:16 +00:00
{
2013-03-31 04:21:08 +00:00
qboolean persist ;
2010-12-18 21:30:16 +00:00
2013-03-31 04:21:08 +00:00
//save it for later.
qres - > next = qreq - > results ;
qreq - > results = qres ;
2010-12-18 21:30:16 +00:00
2014-03-30 08:55:06 +00:00
if ( developer . ival )
if ( qres - > error )
Con_Printf ( " %s \n " , qres - > error ) ;
2013-03-31 04:21:08 +00:00
if ( qreq - > state = = SR_ABORTED )
{
persist = false ;
if ( qres - > eof )
SQL_CloseRequest ( server , qreq , true ) ;
}
else
{
if ( qreq - > state = = SR_FINISHED )
Sys_Error ( " SQL: Results after finished! \n " ) ;
if ( qreq - > state = = SR_PENDING )
qreq - > state = SR_PARTIAL ;
if ( qres - > eof )
qreq - > state = SR_FINISHED ;
persist = qreq - > callback ( qreq , qres - > firstrow , qres - > rows , qres - > columns , qres - > eof ) ;
}
if ( ! persist )
{
SQL_CloseResult ( server , qres ) ;
if ( qreq - > state = = SR_FINISHED )
SQL_CloseRequest ( server , qreq , false ) ;
2010-12-18 21:30:16 +00:00
}
}
else // error or server-only result
{
if ( server - > serverresult )
2013-03-31 04:21:08 +00:00
SQL_CloseResult ( server , server - > serverresult ) ;
2010-12-18 21:30:16 +00:00
server - > serverresult = qres ;
}
}
2013-03-31 04:21:08 +00:00
if ( server - > terminated )
{
sqlservers [ i ] = NULL ;
SQL_CleanupServer ( server ) ;
continue ;
}
2010-12-18 21:30:16 +00:00
}
}
2012-11-27 03:23:19 +00:00
# ifdef USE_MYSQL
2012-12-04 19:37:57 +00:00
qboolean SQL_MYSQLInit ( void )
2010-12-18 21:30:16 +00:00
{
2012-12-04 19:37:57 +00:00
if ( ( mysqlhandle = Sys_LoadLibrary ( " libmysql " , mysqlfuncs ) ) )
2010-12-18 21:30:16 +00:00
{
2012-12-04 19:37:57 +00:00
if ( qmysql_thread_safe ( ) )
2010-12-18 21:30:16 +00:00
{
2012-12-04 19:37:57 +00:00
if ( ! qmysql_library_init ( 0 , NULL , NULL ) )
{
Con_Printf ( " MYSQL backend loaded \n " ) ;
return true ;
}
else
Con_Printf ( " MYSQL library init failed! \n " ) ;
2010-12-18 21:30:16 +00:00
}
else
2012-12-04 19:37:57 +00:00
Con_Printf ( " MYSQL client is not thread safe! \n " ) ;
Sys_CloseLibrary ( mysqlhandle ) ;
2010-12-18 21:30:16 +00:00
}
else
2012-12-04 19:37:57 +00:00
{
Con_Printf ( " mysql client didn't load \n " ) ;
}
return false ;
2010-12-18 21:30:16 +00:00
}
2012-11-27 03:23:19 +00:00
# endif
2010-12-18 21:30:16 +00:00
void SQL_Init ( void )
{
2012-12-04 19:37:57 +00:00
sqlavailable = 0 ;
2012-11-27 03:23:19 +00:00
2010-12-18 21:30:16 +00:00
Cmd_AddCommand ( " sqlstatus " , SQL_Status_f ) ;
Cmd_AddCommand ( " sqlkill " , SQL_Kill_f ) ;
Cmd_AddCommand ( " sqlkillall " , SQL_Killall_f ) ;
Cvar_Register ( & sql_driver , SQLCVAROPTIONS ) ;
Cvar_Register ( & sql_host , SQLCVAROPTIONS ) ;
Cvar_Register ( & sql_username , SQLCVAROPTIONS ) ;
Cvar_Register ( & sql_password , SQLCVAROPTIONS ) ;
Cvar_Register ( & sql_defaultdb , SQLCVAROPTIONS ) ;
}
void SQL_KillServers ( void )
{
int i ;
for ( i = 0 ; i < sqlservercount ; i + + )
{
sqlserver_t * server = sqlservers [ i ] ;
2012-11-27 03:23:19 +00:00
sqlservers [ i ] = NULL ;
if ( ! server )
continue ;
SQL_CleanupServer ( server ) ;
2010-12-18 21:30:16 +00:00
}
if ( sqlservers )
Z_Free ( sqlservers ) ;
sqlservers = NULL ;
sqlservercount = 0 ;
}
void SQL_DeInit ( void )
{
2012-11-29 13:37:48 +00:00
sqlavailable = 0 ;
2010-12-18 21:30:16 +00:00
SQL_KillServers ( ) ;
2016-08-25 00:12:14 +00:00
sqlinited = false ;
2010-12-18 21:30:16 +00:00
2012-11-27 03:23:19 +00:00
# ifdef USE_MYSQL
if ( qmysql_library_end )
qmysql_library_end ( ) ;
2010-12-18 21:30:16 +00:00
2013-03-12 23:05:47 +00:00
if ( mysqlhandle )
Sys_CloseLibrary ( mysqlhandle ) ;
2012-11-27 03:23:19 +00:00
# endif
2012-11-29 13:37:48 +00:00
# ifdef USE_SQLITE
2013-03-12 23:05:47 +00:00
if ( sqlitehandle )
Sys_CloseLibrary ( sqlitehandle ) ;
2012-11-29 13:37:48 +00:00
# endif
2010-12-18 21:30:16 +00:00
}
# endif