Core/DBLayer: Store MySQL connection details in a struct and print relevant data in sql driver messages

--HG--
branch : trunk
This commit is contained in:
Machiavelli
2010-09-29 08:48:06 +02:00
parent dcbe4026c3
commit bf72fa749f
9 changed files with 59 additions and 52 deletions

View File

@@ -61,7 +61,6 @@ class DatabaseWorkerPool
m_queue(new ACE_Activation_Queue(new ACE_Message_Queue<ACE_MT_SYNCH>)),
m_connections(0)
{
m_infoString = "";
m_connections.resize(IDX_SIZE);
mysql_library_init(-1, NULL, NULL);
@@ -70,20 +69,22 @@ class DatabaseWorkerPool
~DatabaseWorkerPool()
{
sLog.outSQLDriver("~DatabaseWorkerPool for '%s'.", "missingname");
sLog.outSQLDriver("~DatabaseWorkerPool for '%s'.", m_connectionDetails.database.c_str());
mysql_library_end();
}
bool Open(const std::string& infoString, uint8 async_threads, uint8 synch_threads)
{
sLog.outSQLDriver("Opening databasepool '%s'. Async threads: %u, synch threads: %u", "nonameyet", async_threads, synch_threads);
m_connectionDetails = MySQLConnectionInfo(infoString);
sLog.outSQLDriver("Opening databasepool '%s'. Async threads: %u, synch threads: %u", m_connectionDetails.database.c_str(), async_threads, synch_threads);
/// Open asynchronous connections (delayed operations)
m_connections[IDX_ASYNC].resize(async_threads);
for (uint8 i = 0; i < async_threads; ++i)
{
T* t = new T(m_queue);
t->Open(infoString);
t->Open(m_connectionDetails);
m_connections[IDX_ASYNC][i] = t;
++m_connectionCount;
}
@@ -93,21 +94,18 @@ class DatabaseWorkerPool
for (uint8 i = 0; i < synch_threads; ++i)
{
T* t = new T();
t->Open(infoString);
t->Open(m_connectionDetails);
m_connections[IDX_SYNCH][i] = t;
++m_connectionCount;
}
/// TODO: Connection details in a struct
m_infoString = infoString;
sLog.outSQLDriver("Databasepool opened succesfuly. %u connections running.", (uint32)m_connectionCount.value());
return true;
}
void Close()
{
sLog.outSQLDriver("Closing down databasepool '%s'.", "missingname");
sLog.outSQLDriver("Closing down databasepool '%s'.", m_connectionDetails.database.c_str());
/// Shuts down delaythreads for this connection pool.
m_queue->queue()->deactivate();
@@ -120,7 +118,7 @@ class DatabaseWorkerPool
--m_connectionCount;
}
sLog.outSQLDriver("Asynchronous connections on databasepool '%s' terminated. Proceeding with synchronous connections.", "missingname");
sLog.outSQLDriver("Asynchronous connections on databasepool '%s' terminated. Proceeding with synchronous connections.", m_connectionDetails.database.c_str());
/// Shut down the synchronous connections
for (uint8 i = 0; i < m_connections[IDX_SYNCH].size(); ++i)
@@ -132,7 +130,7 @@ class DatabaseWorkerPool
--m_connectionCount;
}
sLog.outSQLDriver("All connections on databasepool 'missingname' closed.");
sLog.outSQLDriver("All connections on databasepool %s closed.", m_connectionDetails.database.c_str());
}
void Execute(const char* sql)
@@ -362,7 +360,7 @@ class DatabaseWorkerPool
ACE_Activation_Queue* m_queue; //! Queue shared by async worker threads.
std::vector< std::vector<T*> > m_connections;
AtomicUInt m_connectionCount; //! Counter of MySQL connections;
std::string m_infoString; //! Infostring that is passed on to child connections.
MySQLConnectionInfo m_connectionDetails;
};
#endif

View File

@@ -18,9 +18,9 @@
#include "CharacterDatabase.h"
bool CharacterDatabaseConnection::Open(const std::string& infoString)
bool CharacterDatabaseConnection::Open(const MySQLConnectionInfo& connInfo)
{
if (!MySQLConnection::Open(infoString))
if (!MySQLConnection::Open(connInfo))
return false;
m_stmts.resize(MAX_CHARACTERDATABASE_STATEMENTS);

View File

@@ -30,7 +30,7 @@ class CharacterDatabaseConnection : public MySQLConnection
CharacterDatabaseConnection(ACE_Activation_Queue* q) : MySQLConnection(q) {}
//- Loads databasetype specific prepared statements
bool Open(const std::string& infoString);
bool Open(const MySQLConnectionInfo& connInfo);
};
typedef DatabaseWorkerPool<CharacterDatabaseConnection> CharacterDatabaseWorkerPool;

View File

@@ -18,9 +18,9 @@
#include "LoginDatabase.h"
bool LoginDatabaseConnection::Open(const std::string& infoString)
bool LoginDatabaseConnection::Open(const MySQLConnectionInfo& connInfo)
{
if (!MySQLConnection::Open(infoString))
if (!MySQLConnection::Open(connInfo))
return false;
m_stmts.resize(MAX_LOGINDATABASE_STATEMENTS);

View File

@@ -30,7 +30,7 @@ class LoginDatabaseConnection : public MySQLConnection
LoginDatabaseConnection(ACE_Activation_Queue* q) : MySQLConnection(q) {}
//- Loads databasetype specific prepared statements
bool Open(const std::string& infoString);
bool Open(const MySQLConnectionInfo& connInfo);
};
typedef DatabaseWorkerPool<LoginDatabaseConnection> LoginDatabaseWorkerPool;

View File

@@ -18,9 +18,9 @@
#include "WorldDatabase.h"
bool WorldDatabaseConnection::Open(const std::string& infoString)
bool WorldDatabaseConnection::Open(const MySQLConnectionInfo& connInfo)
{
if (!MySQLConnection::Open(infoString))
if (!MySQLConnection::Open(connInfo))
return false;
m_stmts.resize(MAX_WORLDDATABASE_STATEMENTS);

View File

@@ -30,7 +30,7 @@ class WorldDatabaseConnection : public MySQLConnection
WorldDatabaseConnection(ACE_Activation_Queue* q) : MySQLConnection(q) {}
//- Loads databasetype specific prepared statements
bool Open(const std::string& infoString);
bool Open(const MySQLConnectionInfo& connInfo);
};
typedef DatabaseWorkerPool<WorldDatabaseConnection> WorldDatabaseWorkerPool;

View File

@@ -30,7 +30,6 @@
#include "SQLOperation.h"
#include "PreparedStatement.h"
#include "DatabaseWorker.h"
#include "Util.h"
#include "Timer.h"
MySQLConnection::MySQLConnection() :
@@ -65,7 +64,7 @@ void MySQLConnection::Close()
delete this;
}
bool MySQLConnection::Open(const std::string& infoString)
bool MySQLConnection::Open(const MySQLConnectionInfo& connInfo)
{
MYSQL *mysqlInit;
mysqlInit = mysql_init(NULL);
@@ -75,30 +74,12 @@ bool MySQLConnection::Open(const std::string& infoString)
return false;
}
Tokens tokens = StrSplit(infoString, ";");
Tokens::iterator iter;
std::string host, port_or_socket, user, password, database;
int port;
char const* unix_socket;
iter = tokens.begin();
if (iter != tokens.end())
host = *iter++;
if (iter != tokens.end())
port_or_socket = *iter++;
if (iter != tokens.end())
user = *iter++;
if (iter != tokens.end())
password = *iter++;
if (iter != tokens.end())
database = *iter++;
char* unix_socket;
mysql_options(mysqlInit, MYSQL_SET_CHARSET_NAME, "utf8");
#ifdef _WIN32
if (host==".") // named pipe use option (Windows)
if (connInfo.host == ".") // named pipe use option (Windows)
{
unsigned int opt = MYSQL_PROTOCOL_PIPE;
mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt);
@@ -107,17 +88,17 @@ bool MySQLConnection::Open(const std::string& infoString)
}
else // generic case
{
port = atoi(port_or_socket.c_str());
port = atoi(connInfo.port_or_socket.c_str());
unix_socket = 0;
}
#else
if (host==".") // socket use option (Unix/Linux)
if (connInfo.host == ".") // socket use option (Unix/Linux)
{
unsigned int opt = MYSQL_PROTOCOL_SOCKET;
mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt);
host = "localhost";
connInfo.host = "localhost";
port = 0;
unix_socket = port_or_socket.c_str();
unix_socket = connInfo.port_or_socket.c_str();
}
else // generic case
{
@@ -126,8 +107,8 @@ bool MySQLConnection::Open(const std::string& infoString)
}
#endif
m_Mysql = mysql_real_connect(mysqlInit, host.c_str(), user.c_str(),
password.c_str(), database.c_str(), port, unix_socket, 0);
m_Mysql = mysql_real_connect(mysqlInit, connInfo.host.c_str(), connInfo.user.c_str(),
connInfo.password.c_str(), connInfo.database.c_str(), port, unix_socket, 0);
if (m_Mysql)
{
@@ -136,7 +117,7 @@ bool MySQLConnection::Open(const std::string& infoString)
if (mysql_get_server_version(m_Mysql) != mysql_get_client_version())
sLog.outSQLDriver("[WARNING] MySQL client/server version mismatch; may conflict with behaviour of prepared statements.");
sLog.outDetail("Connected to MySQL database at %s", host.c_str());
sLog.outDetail("Connected to MySQL database at %s", connInfo.host.c_str());
if (!mysql_autocommit(m_Mysql, 1))
sLog.outSQLDriver("AUTOCOMMIT SUCCESSFULLY SET TO 1");
else
@@ -160,7 +141,7 @@ bool MySQLConnection::Open(const std::string& infoString)
}
else
{
sLog.outError("Could not connect to MySQL database at %s: %s\n", host.c_str(), mysql_error(mysqlInit));
sLog.outError("Could not connect to MySQL database at %s: %s\n", connInfo.host.c_str(), mysql_error(mysqlInit));
mysql_close(mysqlInit);
return false;
}

View File

@@ -18,6 +18,7 @@
#include <ace/Activation_Queue.h>
#include "DatabaseWorkerPool.h"
#include "Util.h"
#ifndef _MYSQLCONNECTION_H
#define _MYSQLCONNECTION_H
@@ -27,6 +28,33 @@ class PreparedStatement;
class MySQLPreparedStatement;
class PingOperation;
struct MySQLConnectionInfo
{
MySQLConnectionInfo() {}
MySQLConnectionInfo(const std::string& infoString)
{
Tokens tokens = StrSplit(infoString, ";");
Tokens::iterator iter = tokens.begin();
if (iter != tokens.end())
host = *iter++;
if (iter != tokens.end())
port_or_socket = *iter++;
if (iter != tokens.end())
user = *iter++;
if (iter != tokens.end())
password = *iter++;
if (iter != tokens.end())
database = *iter++;
}
std::string user;
std::string password;
std::string database;
std::string host;
std::string port_or_socket;
};
class MySQLConnection
{
template <class T> friend class DatabaseWorkerPool;
@@ -37,7 +65,7 @@ class MySQLConnection
MySQLConnection(ACE_Activation_Queue* queue); //! Constructor for asynchroneous connections.
~MySQLConnection();
virtual bool Open(const std::string& infoString); //! Connection details.
virtual bool Open(const MySQLConnectionInfo& connInfo); //! Connection details.
void Close();
public: