2012-11-27 03:23:19 +00:00
# ifndef SV_SQL_H
# define SV_SQL_H
# ifdef USE_MYSQL
# ifdef _WIN32
# include <windows.h>
# endif
# include <mysql/mysql.h>
# endif
2014-03-30 08:55:06 +00:00
# ifdef USE_SQLITE
typedef struct
{
char * ptr ;
int len ;
} sqliteresult_t ;
# endif
2012-11-27 03:23:19 +00:00
2010-12-18 21:30:16 +00:00
# define SQL_CONNECT_STRUCTPARAMS 2
# define SQL_CONNECT_PARAMS 4
typedef enum
{
SQLDRV_MYSQL ,
SQLDRV_SQLITE , /* NOT IN YET */
SQLDRV_INVALID
} sqldrv_t ;
typedef struct queryrequest_s
{
2013-03-31 04:21:08 +00:00
int srvid ;
int num ; /* query number reference */
struct queryrequest_s * nextqueue ; /* next request in queue */
struct queryrequest_s * nextreq ; /* next request in queue */
struct queryresult_s * results ; /* chain of received results */
enum
{
SR_NEW ,
SR_PENDING ,
SR_PARTIAL , //
SR_FINISHED , //waiting for close
SR_ABORTED //don't notify. destroy on finish.
} state ; //maintained by main thread. worker *may* check for aborted state as a way to quickly generate an error.
qboolean ( * callback ) ( struct queryrequest_s * req , int firstrow , int numrows , int numcols , qboolean eof ) ; /* called on main thread once complete */
struct
{
qboolean persistant ; /* persistant query */
int qccallback ; /* callback function reference */
int selfent ; /* self entity on call */
float selfid ; /* self entity id on call */
int otherent ; /* other entity on call */
float otherid ; /* other entity id on call */
void * thread ;
} user ; /* sql code does not write anything in this struct */
2010-12-18 21:30:16 +00:00
char query [ 1 ] ; /* query to run */
} queryrequest_t ;
typedef struct queryresult_s
{
struct queryrequest_s * request ; /* corresponding request */
struct queryresult_s * next ; /* next result in queue */
int rows ; /* rows contained in single result set */
2012-12-04 19:37:57 +00:00
int firstrow ; /* 0 on first result block */
2010-12-18 21:30:16 +00:00
int columns ; /* fields */
qboolean eof ; /* end of query reached */
2012-11-27 03:23:19 +00:00
void * result ; /* result set from mysql */
2010-12-18 21:30:16 +00:00
#if 0
char * * resultset ; /* stored result set from partial fetch */
# endif
char error [ 1 ] ; /* error string, "" if none */
} queryresult_t ;
typedef struct sqlserver_s
{
void * thread ; /* worker thread for server */
sqldrv_t driver ; /* driver type */
2012-11-27 03:23:19 +00:00
# ifdef USE_MYSQL
2010-12-18 21:30:16 +00:00
MYSQL * mysql ; /* mysql server */
2012-11-27 03:23:19 +00:00
# endif
# ifdef USE_SQLITE
2012-12-04 19:37:57 +00:00
struct sqlite3 * sqlite ;
2012-11-27 03:23:19 +00:00
# endif
2010-12-18 21:30:16 +00:00
volatile qboolean active ; /* set to false to kill thread */
2012-11-27 03:23:19 +00:00
volatile qboolean terminated ; /* set by the worker to say that it won't block (for long) and can be joined */
2010-12-18 21:30:16 +00:00
void * requestcondv ; /* lock and conditional variable for queue read/write */
void * resultlock ; /* mutex for queue read/write */
int querynum ; /* next reference number for queries */
2013-03-31 04:21:08 +00:00
queryrequest_t * requests ; /* list of pending and persistant requests */
queryrequest_t * requestqueue ; /* query requests queue */
2010-12-18 21:30:16 +00:00
queryrequest_t * requestslast ; /* query requests queue last link */
queryresult_t * results ; /* query results queue */
queryresult_t * resultslast ; /* query results queue last link */
2013-03-31 04:21:08 +00:00
queryresult_t * serverresult ; /* most recent (orphaned) server error result */
2010-12-18 21:30:16 +00:00
char * * connectparams ; /* connect parameters (0 = host, 1 = user, 2 = pass, 3 = defaultdb) */
} sqlserver_t ;
/* prototypes */
void SQL_Init ( void ) ;
void SQL_KillServers ( void ) ;
void SQL_DeInit ( void ) ;
sqlserver_t * SQL_GetServer ( int serveridx , qboolean inactives ) ;
2013-03-31 04:21:08 +00:00
queryrequest_t * SQL_GetQueryRequest ( sqlserver_t * server , int queryidx ) ;
queryresult_t * SQL_GetQueryResult ( sqlserver_t * server , int queryidx , int row ) ;
2012-11-27 03:23:19 +00:00
//void SQL_DeallocResult(sqlserver_t *server, queryresult_t *qres);
2010-12-18 21:30:16 +00:00
void SQL_ClosePersistantResult ( sqlserver_t * server , queryresult_t * qres ) ;
void SQL_CloseResult ( sqlserver_t * server , queryresult_t * qres ) ;
2013-03-31 04:21:08 +00:00
void SQL_CloseRequest ( sqlserver_t * server , queryrequest_t * qres , qboolean force ) ;
2010-12-18 21:30:16 +00:00
void SQL_CloseAllResults ( sqlserver_t * server ) ;
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 ) ;
int SQL_NewServer ( const char * driver , const char * * paramstr ) ;
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 ) ; //callback will be called on the main thread once the result is back
2010-12-18 21:30:16 +00:00
void SQL_Disconnect ( sqlserver_t * server ) ;
2014-03-30 08:55:06 +00:00
void SQL_Escape ( sqlserver_t * server , const char * src , char * dst , int dstlen ) ;
2012-11-27 03:23:19 +00:00
const char * SQL_Info ( sqlserver_t * server ) ;
2010-12-18 21:30:16 +00:00
qboolean SQL_Available ( void ) ;
2013-03-31 04:21:08 +00:00
void SQL_ServerCycle ( void ) ;
2010-12-18 21:30:16 +00:00
extern cvar_t sql_driver ;
extern cvar_t sql_host ;
extern cvar_t sql_username ;
extern cvar_t sql_password ;
extern cvar_t sql_defaultdb ;
# define SQLCVAROPTIONS "SQL Defaults"
2012-11-29 13:37:48 +00:00
# endif