2015-11-23 15:25:19 +00:00
# if defined(CONF_SQL)
2015-11-21 23:52:12 +00:00
# include <base/system.h>
# include <engine/shared/protocol.h>
# include <engine/shared/config.h>
2015-11-23 15:25:19 +00:00
# include "sql_server.h"
2015-11-21 23:52:12 +00:00
2016-01-28 16:46:51 +00:00
int CSqlServer : : ms_NumReadServer = 0 ;
int CSqlServer : : ms_NumWriteServer = 0 ;
2018-08-27 12:49:17 +00:00
CSqlServer : : CSqlServer ( const char * pDatabase , const char * pPrefix , const char * pUser , const char * pPass , const char * pIp , int Port , lock * pGlobalLock , bool ReadOnly , bool SetUpDb ) :
2015-12-20 15:07:32 +00:00
m_Port ( Port ) ,
2018-08-27 07:42:34 +00:00
m_SetUpDB ( SetUpDb ) ,
2018-08-27 12:49:17 +00:00
m_SqlLock ( ) ,
m_pGlobalLock ( pGlobalLock )
2015-11-21 23:52:12 +00:00
{
2015-11-23 18:19:44 +00:00
str_copy ( m_aDatabase , pDatabase , sizeof ( m_aDatabase ) ) ;
str_copy ( m_aPrefix , pPrefix , sizeof ( m_aPrefix ) ) ;
str_copy ( m_aUser , pUser , sizeof ( m_aUser ) ) ;
str_copy ( m_aPass , pPass , sizeof ( m_aPass ) ) ;
str_copy ( m_aIp , pIp , sizeof ( m_aIp ) ) ;
2015-11-21 23:52:12 +00:00
m_pDriver = 0 ;
m_pConnection = 0 ;
m_pResults = 0 ;
m_pStatement = 0 ;
2016-01-28 14:21:57 +00:00
2016-01-28 16:46:51 +00:00
ReadOnly ? ms_NumReadServer + + : ms_NumWriteServer + + ;
2015-11-21 23:52:12 +00:00
}
CSqlServer : : ~ CSqlServer ( )
{
2018-08-27 12:49:17 +00:00
scope_lock LockScope ( & m_SqlLock ) ;
2015-11-21 23:52:12 +00:00
try
{
if ( m_pResults )
delete m_pResults ;
if ( m_pConnection )
2018-12-20 08:26:45 +00:00
{
2015-11-21 23:52:12 +00:00
delete m_pConnection ;
2018-12-20 08:26:45 +00:00
m_pConnection = 0 ;
}
2016-05-03 21:07:24 +00:00
dbg_msg ( " sql " , " SQL connection disconnected " ) ;
2015-11-21 23:52:12 +00:00
}
catch ( sql : : SQLException & e )
{
2017-07-08 12:14:50 +00:00
dbg_msg ( " sql " , " ERROR: No SQL connection: %s " , e . what ( ) ) ;
}
catch ( const std : : exception & ex )
{
dbg_msg ( " sql " , " ERROR: No SQL connection: %s " , ex . what ( ) ) ;
}
catch ( const std : : string & ex )
{
dbg_msg ( " sql " , " ERROR: No SQL connection: %s " , ex . c_str ( ) ) ;
}
catch ( . . . )
{
dbg_msg ( " sql " , " Unknown Error cause by the MySQL/C++ Connector " ) ;
2015-11-21 23:52:12 +00:00
}
}
2015-12-20 15:07:32 +00:00
bool CSqlServer : : Connect ( )
2015-11-21 23:52:12 +00:00
{
2018-08-27 12:49:17 +00:00
m_SqlLock . take ( ) ;
2015-11-24 17:44:08 +00:00
2015-11-21 23:52:12 +00:00
if ( m_pDriver ! = NULL & & m_pConnection ! = NULL )
{
try
{
// Connect to specific database
2015-11-23 18:19:44 +00:00
m_pConnection - > setSchema ( m_aDatabase ) ;
2017-07-08 12:14:50 +00:00
return true ;
2015-11-21 23:52:12 +00:00
}
catch ( sql : : SQLException & e )
{
2016-06-15 09:52:40 +00:00
dbg_msg ( " sql " , " MySQL Error: %s " , e . what ( ) ) ;
2015-11-21 23:52:12 +00:00
}
2017-07-08 12:14:50 +00:00
catch ( const std : : exception & ex )
{
dbg_msg ( " sql " , " MySQL Error: %s " , ex . what ( ) ) ;
}
catch ( const std : : string & ex )
{
dbg_msg ( " sql " , " MySQL Error: %s " , ex . c_str ( ) ) ;
}
catch ( . . . )
{
dbg_msg ( " sql " , " Unknown Error cause by the MySQL/C++ Connector " ) ;
}
2018-08-27 12:49:17 +00:00
m_SqlLock . release ( ) ;
2018-08-27 07:42:34 +00:00
dbg_msg ( " sql " , " ERROR: SQL connection failed " ) ;
2017-07-08 12:14:50 +00:00
return false ;
2015-11-21 23:52:12 +00:00
}
try
{
m_pDriver = 0 ;
m_pConnection = 0 ;
m_pStatement = 0 ;
sql : : ConnectOptionsMap connection_properties ;
2015-11-23 18:19:44 +00:00
connection_properties [ " hostName " ] = sql : : SQLString ( m_aIp ) ;
2015-11-21 23:52:12 +00:00
connection_properties [ " port " ] = m_Port ;
2015-11-23 18:19:44 +00:00
connection_properties [ " userName " ] = sql : : SQLString ( m_aUser ) ;
connection_properties [ " password " ] = sql : : SQLString ( m_aPass ) ;
2016-01-28 16:46:51 +00:00
connection_properties [ " OPT_CONNECT_TIMEOUT " ] = 10 ;
2016-05-16 13:37:51 +00:00
connection_properties [ " OPT_READ_TIMEOUT " ] = 10 ;
connection_properties [ " OPT_WRITE_TIMEOUT " ] = 20 ;
2015-11-21 23:52:12 +00:00
connection_properties [ " OPT_RECONNECT " ] = true ;
2017-08-26 08:12:47 +00:00
connection_properties [ " OPT_CHARSET_NAME " ] = sql : : SQLString ( " utf8mb4 " ) ;
2015-11-21 23:52:12 +00:00
// Create connection
2018-08-27 07:42:34 +00:00
{
2018-08-27 12:49:17 +00:00
scope_lock GlobalLockScope ( m_pGlobalLock ) ;
2018-08-27 07:42:34 +00:00
m_pDriver = get_driver_instance ( ) ;
}
2015-11-21 23:52:12 +00:00
m_pConnection = m_pDriver - > connect ( connection_properties ) ;
2019-01-06 09:59:29 +00:00
// Create Statement
2015-11-21 23:52:12 +00:00
m_pStatement = m_pConnection - > createStatement ( ) ;
2015-12-20 15:07:32 +00:00
if ( m_SetUpDB )
2015-11-23 18:19:44 +00:00
{
2015-11-23 21:49:18 +00:00
char aBuf [ 128 ] ;
2015-11-23 18:19:44 +00:00
// create database
2017-08-26 08:12:47 +00:00
str_format ( aBuf , sizeof ( aBuf ) , " CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8mb4 " , m_aDatabase ) ;
2015-11-23 18:19:44 +00:00
m_pStatement - > execute ( aBuf ) ;
}
2015-11-21 23:52:12 +00:00
// Connect to specific database
2015-11-23 18:19:44 +00:00
m_pConnection - > setSchema ( m_aDatabase ) ;
2016-05-03 21:07:24 +00:00
dbg_msg ( " sql " , " sql connection established " ) ;
2015-11-21 23:52:12 +00:00
return true ;
}
catch ( sql : : SQLException & e )
{
2016-06-15 09:52:40 +00:00
dbg_msg ( " sql " , " MySQL Error: %s " , e . what ( ) ) ;
2015-11-21 23:52:12 +00:00
}
catch ( const std : : exception & ex )
{
2017-07-08 12:14:50 +00:00
dbg_msg ( " sql " , " MySQL Error: %s " , ex . what ( ) ) ;
2015-11-21 23:52:12 +00:00
}
catch ( const std : : string & ex )
{
2017-07-08 12:14:50 +00:00
dbg_msg ( " sql " , " MySQL Error: %s " , ex . c_str ( ) ) ;
2015-11-21 23:52:12 +00:00
}
catch ( . . . )
{
2017-07-08 12:14:50 +00:00
dbg_msg ( " sql " , " Unknown Error cause by the MySQL/C++ Connector " ) ;
2015-11-21 23:52:12 +00:00
}
2017-07-08 12:14:50 +00:00
dbg_msg ( " sql " , " ERROR: sql connection failed " ) ;
2018-08-27 12:49:17 +00:00
m_SqlLock . release ( ) ;
2015-11-21 23:52:12 +00:00
return false ;
}
void CSqlServer : : Disconnect ( )
{
2018-08-27 12:49:17 +00:00
m_SqlLock . release ( ) ;
2015-11-21 23:52:12 +00:00
}
void CSqlServer : : CreateTables ( )
{
2015-12-20 15:07:32 +00:00
if ( ! Connect ( ) )
2015-11-23 21:49:18 +00:00
return ;
2015-11-21 23:52:12 +00:00
try
{
char aBuf [ 1024 ] ;
// create tables
2018-02-07 00:10:02 +00:00
str_format ( aBuf , sizeof ( aBuf ) , " CREATE TABLE IF NOT EXISTS %s_race (Map VARCHAR(128) BINARY NOT NULL, Name VARCHAR(%d) BINARY NOT NULL, Timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , Time FLOAT DEFAULT 0, Server CHAR(4), cp1 FLOAT DEFAULT 0, cp2 FLOAT DEFAULT 0, cp3 FLOAT DEFAULT 0, cp4 FLOAT DEFAULT 0, cp5 FLOAT DEFAULT 0, cp6 FLOAT DEFAULT 0, cp7 FLOAT DEFAULT 0, cp8 FLOAT DEFAULT 0, cp9 FLOAT DEFAULT 0, cp10 FLOAT DEFAULT 0, cp11 FLOAT DEFAULT 0, cp12 FLOAT DEFAULT 0, cp13 FLOAT DEFAULT 0, cp14 FLOAT DEFAULT 0, cp15 FLOAT DEFAULT 0, cp16 FLOAT DEFAULT 0, cp17 FLOAT DEFAULT 0, cp18 FLOAT DEFAULT 0, cp19 FLOAT DEFAULT 0, cp20 FLOAT DEFAULT 0, cp21 FLOAT DEFAULT 0, cp22 FLOAT DEFAULT 0, cp23 FLOAT DEFAULT 0, cp24 FLOAT DEFAULT 0, cp25 FLOAT DEFAULT 0, GameID VARCHAR(64), KEY (Map, Name)) CHARACTER SET utf8mb4; " , m_aPrefix , MAX_NAME_LENGTH ) ;
2015-11-21 23:52:12 +00:00
executeSql ( aBuf ) ;
2018-02-07 15:43:36 +00:00
str_format ( aBuf , sizeof ( aBuf ) , " CREATE TABLE IF NOT EXISTS %s_teamrace (Map VARCHAR(128) BINARY NOT NULL, Name VARCHAR(%d) BINARY NOT NULL, Timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, Time FLOAT DEFAULT 0, ID VARBINARY(16) NOT NULL, GameID VARCHAR(64), KEY Map (Map)) CHARACTER SET utf8mb4; " , m_aPrefix , MAX_NAME_LENGTH ) ;
2015-11-21 23:52:12 +00:00
executeSql ( aBuf ) ;
2018-01-14 13:11:52 +00:00
str_format ( aBuf , sizeof ( aBuf ) , " CREATE TABLE IF NOT EXISTS %s_maps (Map VARCHAR(128) BINARY NOT NULL, Server VARCHAR(32) BINARY NOT NULL, Mapper VARCHAR(128) BINARY NOT NULL, Points INT DEFAULT 0, Stars INT DEFAULT 0, Timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY Map (Map)) CHARACTER SET utf8mb4; " , m_aPrefix ) ;
2015-11-21 23:52:12 +00:00
executeSql ( aBuf ) ;
2017-08-26 07:00:33 +00:00
str_format ( aBuf , sizeof ( aBuf ) , " CREATE TABLE IF NOT EXISTS %s_saves (Savegame TEXT CHARACTER SET utf8mb4 BINARY NOT NULL, Map VARCHAR(128) BINARY NOT NULL, Code VARCHAR(128) BINARY NOT NULL, Timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, Server CHAR(4), UNIQUE KEY (Map, Code)) CHARACTER SET utf8mb4; " , m_aPrefix ) ;
2015-11-21 23:52:12 +00:00
executeSql ( aBuf ) ;
2017-08-26 07:00:33 +00:00
str_format ( aBuf , sizeof ( aBuf ) , " CREATE TABLE IF NOT EXISTS %s_points (Name VARCHAR(%d) BINARY NOT NULL, Points INT DEFAULT 0, UNIQUE KEY Name (Name)) CHARACTER SET utf8mb4; " , m_aPrefix , MAX_NAME_LENGTH ) ;
2015-11-21 23:52:12 +00:00
executeSql ( aBuf ) ;
2016-05-03 21:07:24 +00:00
dbg_msg ( " sql " , " Tables were created successfully " ) ;
2015-11-21 23:52:12 +00:00
}
catch ( sql : : SQLException & e )
{
2016-06-15 09:52:40 +00:00
dbg_msg ( " sql " , " MySQL Error: %s " , e . what ( ) ) ;
2015-11-21 23:52:12 +00:00
}
2015-11-23 21:49:18 +00:00
Disconnect ( ) ;
2015-11-21 23:52:12 +00:00
}
void CSqlServer : : executeSql ( const char * pCommand )
{
m_pStatement - > execute ( pCommand ) ;
}
void CSqlServer : : executeSqlQuery ( const char * pQuery )
{
if ( m_pResults )
2016-05-03 22:27:03 +00:00
delete m_pResults ;
2016-05-09 21:35:23 +00:00
2019-01-06 09:59:29 +00:00
// set it to 0, so exceptions raised from executeQuery can not make m_pResults point to invalid memory
m_pResults = 0 ;
2015-11-21 23:52:12 +00:00
m_pResults = m_pStatement - > executeQuery ( pQuery ) ;
}
2015-11-23 15:25:19 +00:00
# endif