mirror of
https://github.com/SapphireServer/Sapphire.git
synced 2025-05-23 18:17:46 +00:00
Added c++MysqlConnector
This commit is contained in:
parent
e65902e20f
commit
f298cee118
5 changed files with 134 additions and 6 deletions
|
@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 2.6)
|
|||
project(Sapphire)
|
||||
|
||||
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/sapphire/datReader/")
|
||||
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/external/MySQLpp/include/")
|
||||
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/external/ChaiScript-6.0.0/include/")
|
||||
|
||||
if(UNIX)
|
||||
|
@ -23,6 +24,9 @@ if(UNIX)
|
|||
else()
|
||||
add_definitions(-D_WIN32_WINNT=0x601)
|
||||
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/external/MySQL/")
|
||||
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/external/MySQLpp/")
|
||||
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/external/MySQLpp/includes/")
|
||||
message(STATUS "Using boost in /libraries/external")
|
||||
message(STATUS "Setting MSVC flags")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHc")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
|
||||
|
|
44
src/servers/Server_Common/Database/DatabaseNew.cpp
Normal file
44
src/servers/Server_Common/Database/DatabaseNew.cpp
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include "DatabaseNew.h"
|
||||
|
||||
#include "src/servers/Server_Common/Logging/Logger.h"
|
||||
|
||||
extern Core::Logger g_log;
|
||||
|
||||
Core::Db::DatabaseNew::DatabaseNew()
|
||||
{
|
||||
m_driver = get_driver_instance();
|
||||
}
|
||||
|
||||
Core::Db::DatabaseNew::~DatabaseNew()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool Core::Db::DatabaseNew::initialize( const DbParams& params )
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
g_log.info( "Database: Connecting to " + params.hostname + ", database " + params.databaseName + "..." );
|
||||
|
||||
m_pConnections = new DbConnection[params.connectionCount];
|
||||
for( i = 0; i < params.connectionCount; ++i )
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string host = "tcp://" + params.hostname + ":" + std::to_string( params.port );
|
||||
m_pConnections[i].conn = m_driver->connect( host.c_str(), params.username.c_str(), params.password.c_str() );
|
||||
m_pConnections[i].conn->setSchema( params.databaseName.c_str() );
|
||||
bool myTrue = true;
|
||||
m_pConnections[i].conn->setClientOption( "OPT_RECONNECT", &myTrue );
|
||||
m_pConnections[i].conn->setClientOption( "SET_CHARSET_NAME", "utf8" );
|
||||
}
|
||||
catch( sql::SQLException &e )
|
||||
{
|
||||
g_log.fatal( "Database: Connection failed: " + std::string( e.what() ) );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
60
src/servers/Server_Common/Database/DatabaseNew.h
Normal file
60
src/servers/Server_Common/Database/DatabaseNew.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
#ifndef SAPPHIRE_DATABASENEW_H
|
||||
#define SAPPHIRE_DATABASENEW_H
|
||||
|
||||
#include <mutex>
|
||||
|
||||
#include "mysql_connection.h"
|
||||
|
||||
#include <cppconn/driver.h>
|
||||
#include <cppconn/exception.h>
|
||||
#include <cppconn/resultset.h>
|
||||
#include <cppconn/statement.h>
|
||||
|
||||
namespace Core
|
||||
{
|
||||
namespace Db
|
||||
{
|
||||
|
||||
struct DbParams
|
||||
{
|
||||
std::string hostname;
|
||||
std::string username;
|
||||
std::string password;
|
||||
std::string databaseName;
|
||||
uint16_t port;
|
||||
uint32_t bufferSize;
|
||||
uint32_t connectionCount;
|
||||
};
|
||||
|
||||
struct DbConnection
|
||||
{
|
||||
std::mutex lock;
|
||||
sql::Connection *conn;
|
||||
};
|
||||
|
||||
class DatabaseNew
|
||||
{
|
||||
private:
|
||||
sql::Driver * m_driver;
|
||||
sql::Connection *m_con;
|
||||
sql::Statement *m_stmt;
|
||||
sql::ResultSet *m_res;
|
||||
|
||||
DbConnection *m_pConnections;
|
||||
|
||||
public:
|
||||
DatabaseNew();
|
||||
~DatabaseNew();
|
||||
|
||||
bool initialize( const DbParams& params );
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#endif //SAPPHIRE_DATABASENEW_H
|
|
@ -4,8 +4,9 @@ cmake_policy(SET CMP0014 OLD)
|
|||
|
||||
project(Sapphire_Zone)
|
||||
|
||||
include_directories("../../libraries/external/ChaiScript-6.0.0/include/")
|
||||
include_directories("../../libraries/sapphire/datReader/")
|
||||
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/external/ChaiScript-6.0.0/include/")
|
||||
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/sapphire/datReader/")
|
||||
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/external/MySQLpp/include/")
|
||||
include_directories("../")
|
||||
|
||||
file(GLOB SERVER_PUBLIC_INCLUDE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/* ${CMAKE_CURRENT_SOURCE_DIR}/Script/*)
|
||||
|
@ -77,6 +78,7 @@ include_directories(${Boost_INCLUDE_DIR})
|
|||
link_directories(${BOOST_LIBRARYDIR})
|
||||
link_directories(${SERVER_COMMON_DIR})
|
||||
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/sapphire/datReader)
|
||||
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/external/MySQLpp/lib/opt)
|
||||
|
||||
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
|
||||
# 32 bit link
|
||||
|
@ -106,7 +108,7 @@ set_target_properties(server_zone PROPERTIES
|
|||
if (UNIX)
|
||||
target_link_libraries ( server_zone Common xivdat pthread mysqlclient dl z )
|
||||
else()
|
||||
target_link_libraries (server_zone Common xivdat libmysql zlib1)
|
||||
target_link_libraries ( server_zone Common xivdat libmysql zlib1 mysqlcppconn )
|
||||
endif()
|
||||
|
||||
target_link_libraries(server_zone ${Boost_LIBRARIES} ${Boost_LIBRARIES})
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <src/servers/Server_Common/Logging/Logger.h>
|
||||
#include <src/servers/Server_Common/Config/XMLConfig.h>
|
||||
#include <src/servers/Server_Common/Database/Database.h>
|
||||
#include <src/servers/Server_Common/Database/DatabaseNew.h>
|
||||
|
||||
#include <src/servers/Server_Common/Network/Connection.h>
|
||||
#include <src/servers/Server_Common/Network/Hive.h>
|
||||
|
@ -33,6 +34,7 @@
|
|||
|
||||
Core::Logger g_log;
|
||||
Core::Db::Database g_database;
|
||||
Core::Db::DatabaseNew g_dbNew;
|
||||
Core::DebugCommandHandler g_gameCommandMgr;
|
||||
Core::Scripting::ScriptManager g_scriptMgr;
|
||||
Core::Data::ExdData g_exdData;
|
||||
|
@ -164,6 +166,22 @@ bool Core::ServerZone::loadSettings( int32_t argc, char* argv[] )
|
|||
params.port = m_pConfig->getValue< uint16_t >( "Settings.General.Mysql.Port", 3306 );
|
||||
params.username = m_pConfig->getValue< std::string >( "Settings.General.Mysql.Username", "root" );
|
||||
|
||||
Db::DbParams params1;
|
||||
params1.bufferSize = 16384;
|
||||
params1.connectionCount = 3;
|
||||
params1.databaseName = m_pConfig->getValue< std::string >( "Settings.General.Mysql.Database", "sapphire" );
|
||||
params1.hostname = m_pConfig->getValue< std::string >( "Settings.General.Mysql.Host", "127.0.0.1" );
|
||||
params1.password = m_pConfig->getValue< std::string >( "Settings.General.Mysql.Pass", "" );
|
||||
params1.port = m_pConfig->getValue< uint16_t >( "Settings.General.Mysql.Port", 3306 );
|
||||
params1.username = m_pConfig->getValue< std::string >( "Settings.General.Mysql.Username", "root" );
|
||||
|
||||
|
||||
if( !g_dbNew.initialize( params1 ) )
|
||||
{
|
||||
std::this_thread::sleep_for( std::chrono::milliseconds( 5000 ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
if( !g_database.initialize( params ) )
|
||||
{
|
||||
std::this_thread::sleep_for( std::chrono::milliseconds( 5000 ) );
|
||||
|
|
Loading…
Add table
Reference in a new issue