typedef std::map<std::string, SQLConnection*> ConnMap;
typedef std::deque<SQLresult*> ResultQueue;
-unsigned long count(const char * const str, char a)
+static unsigned long count(const char * const str, char a)
{
unsigned long n = 0;
for (const char *p = str; *p; ++p)
class ModuleSQL : public Module
{
public:
-
- ConfigReader *Conf;
int currid;
bool rehashing;
DispatcherThread* Dispatcher;
Mutex ResultsMutex;
Mutex LoggingMutex;
Mutex ConnMutex;
+ ServiceProvider sqlserv;
ModuleSQL();
~ModuleSQL();
return false;
}
-bool HostInConf(ConfigReader* conf, const SQLhost &h)
+bool HostInConf(const SQLhost &h)
{
- for(int i = 0; i < conf->Enumerate("database"); i++)
+ ConfigReader conf;
+ for(int i = 0; i < conf.Enumerate("database"); i++)
{
SQLhost host;
- host.id = conf->ReadValue("database", "id", i);
- host.host = conf->ReadValue("database", "hostname", i);
- host.port = conf->ReadInteger("database", "port", i, true);
- host.name = conf->ReadValue("database", "name", i);
- host.user = conf->ReadValue("database", "username", i);
- host.pass = conf->ReadValue("database", "password", i);
- host.ssl = conf->ReadFlag("database", "ssl", i);
+ host.id = conf.ReadValue("database", "id", i);
+ host.host = conf.ReadValue("database", "hostname", i);
+ host.port = conf.ReadInteger("database", "port", i, true);
+ host.name = conf.ReadValue("database", "name", i);
+ host.user = conf.ReadValue("database", "username", i);
+ host.pass = conf.ReadValue("database", "password", i);
+ host.ssl = conf.ReadFlag("database", "ssl", i);
if (h == host)
return true;
}
return false;
}
-void ClearOldConnections(ConfigReader* conf)
+void ClearOldConnections()
{
ConnMap::iterator i,safei;
for (i = Connections.begin(); i != Connections.end(); i++)
{
- if (!HostInConf(conf, i->second->GetConfHost()))
+ if (!HostInConf(i->second->GetConfHost()))
{
delete i->second;
safei = i;
}
}
-void LoadDatabases(ConfigReader* conf, ModuleSQL* Parent)
+void LoadDatabases(ModuleSQL* Parent)
{
+ ConfigReader conf;
Parent->ConnMutex.Lock();
- ClearOldConnections(conf);
- for (int j =0; j < conf->Enumerate("database"); j++)
+ ClearOldConnections();
+ for (int j =0; j < conf.Enumerate("database"); j++)
{
SQLhost host;
- host.id = conf->ReadValue("database", "id", j);
- host.host = conf->ReadValue("database", "hostname", j);
- host.port = conf->ReadInteger("database", "port", j, true);
- host.name = conf->ReadValue("database", "name", j);
- host.user = conf->ReadValue("database", "username", j);
- host.pass = conf->ReadValue("database", "password", j);
- host.ssl = conf->ReadFlag("database", "ssl", j);
- std::string initquery = conf->ReadValue("database", "initialquery", j);
+ host.id = conf.ReadValue("database", "id", j);
+ host.host = conf.ReadValue("database", "hostname", j);
+ host.port = conf.ReadInteger("database", "port", j, true);
+ host.name = conf.ReadValue("database", "name", j);
+ host.user = conf.ReadValue("database", "username", j);
+ host.pass = conf.ReadValue("database", "password", j);
+ host.ssl = conf.ReadFlag("database", "ssl", j);
+ std::string initquery = conf.ReadValue("database", "initialquery", j);
if (HasHost(host))
continue;
return Connections.end();
}
-class ModuleSQL;
-
class DispatcherThread : public SocketThread
{
private:
virtual void OnNotify();
};
-ModuleSQL::ModuleSQL() : rehashing(false)
+ModuleSQL::ModuleSQL() : rehashing(false), sqlserv(this, "SQL/mysql", SERVICE_DATA)
{
- ServerInstance->Modules->UseInterface("SQLutils");
-
- Conf = new ConfigReader;
currid = 0;
Dispatcher = new DispatcherThread(this);
ServerInstance->Threads->Start(Dispatcher);
- if (!ServerInstance->Modules->PublishFeature("SQL", this))
- {
- Dispatcher->join();
- delete Dispatcher;
- ServerInstance->Modules->DoneWithInterface("SQLutils");
- throw ModuleException("m_mysql: Unable to publish feature 'SQL'");
- }
-
- ServerInstance->Modules->PublishInterface("SQL", this);
Implementation eventlist[] = { I_OnRehash };
ServerInstance->Modules->Attach(eventlist, this, 1);
}
{
delete Dispatcher;
ClearAllConnections();
- delete Conf;
- ServerInstance->Modules->UnpublishInterface("SQL", this);
- ServerInstance->Modules->UnpublishFeature("SQL");
- ServerInstance->Modules->DoneWithInterface("SQLutils");
}
unsigned long ModuleSQL::NewID()
Version ModuleSQL::GetVersion()
{
- return Version("SQL Service Provider module for all other m_sql* modules", VF_VENDOR | VF_SERVICEPROVIDER);
+ return Version("SQL Service Provider module for all other m_sql* modules", VF_VENDOR);
}
void DispatcherThread::Run()
{
- LoadDatabases(Parent->Conf, Parent);
+ LoadDatabases(Parent);
SQLConnection* conn = NULL;
if (Parent->rehashing)
{
Parent->rehashing = false;
- LoadDatabases(Parent->Conf, Parent);
+ LoadDatabases(Parent);
}
conn = NULL;