2015-12-17 21:04:38 +00:00
|
|
|
#if defined(CONF_SQL)
|
|
|
|
|
|
|
|
#include <base/system.h>
|
|
|
|
|
|
|
|
#include "sql_connector.h"
|
|
|
|
|
2016-01-28 16:46:51 +00:00
|
|
|
CSqlServer** CSqlConnector::ms_ppSqlReadServers = 0;
|
|
|
|
CSqlServer** CSqlConnector::ms_ppSqlWriteServers = 0;
|
|
|
|
|
|
|
|
int CSqlConnector::ms_ReachableReadServer = 0;
|
|
|
|
int CSqlConnector::ms_ReachableWriteServer = 0;
|
|
|
|
|
2015-12-17 21:04:38 +00:00
|
|
|
CSqlConnector::CSqlConnector() :
|
|
|
|
m_pSqlServer(0),
|
|
|
|
m_NumReadRetries(0),
|
|
|
|
m_NumWriteRetries(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool CSqlConnector::ConnectSqlServer(bool ReadOnly)
|
|
|
|
{
|
|
|
|
ReadOnly ? ++m_NumReadRetries : ++m_NumWriteRetries;
|
2016-01-28 16:46:51 +00:00
|
|
|
int& ReachableServer = ReadOnly ? ms_ReachableReadServer : ms_ReachableWriteServer;
|
|
|
|
int NumServers = ReadOnly ? CSqlServer::ms_NumReadServer : CSqlServer::ms_NumWriteServer;
|
2015-12-17 21:04:38 +00:00
|
|
|
|
2016-01-28 16:46:51 +00:00
|
|
|
for (int i = ReachableServer, ID = ReachableServer; i < ReachableServer + NumServers && SqlServer(i % NumServers, ReadOnly); i++, ID = i % NumServers)
|
2015-12-17 21:04:38 +00:00
|
|
|
{
|
2016-01-28 16:46:51 +00:00
|
|
|
if (SqlServer(ID, ReadOnly) && SqlServer(ID, ReadOnly)->Connect())
|
2015-12-17 21:04:38 +00:00
|
|
|
{
|
2016-01-28 16:46:51 +00:00
|
|
|
m_pSqlServer = SqlServer(ID, ReadOnly);
|
|
|
|
ReachableServer = ID;
|
2015-12-17 21:04:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-01-28 16:46:51 +00:00
|
|
|
if (SqlServer(ID, ReadOnly))
|
|
|
|
dbg_msg("SQL", "Warning: Unable to connect to Sql%sServer %d ('%s'), trying next...", ReadOnly ? "Read" : "Write", ID, SqlServer(ID, ReadOnly)->GetIP());
|
2015-12-17 21:04:38 +00:00
|
|
|
}
|
2016-01-28 16:46:51 +00:00
|
|
|
dbg_msg("SQL", "FATAL ERROR: No Sql%sServers available", ReadOnly ? "Read" : "Write");
|
2015-12-17 21:04:38 +00:00
|
|
|
m_pSqlServer = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|