]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_mysql.cpp
Change the SQLutils and SQL providers to also use interfaces for proper unload order...
[user/henk/code/inspircd.git] / src / modules / extra / m_mysql.cpp
index fe23eee8c60ae3baf4556f28ba5c62ccfdce0fd2..2449b523866292ff0fa606216b37dca05db07b91 100644 (file)
@@ -594,9 +594,19 @@ void ConnectDatabases(InspIRCd* ServerInstance)
        }
 }
 
+void ClearDatabases()
+{
+       ConnMap::iterator i;
+       while ((i = Connections.begin()) != Connections.end())
+       {
+               Connections.erase(i);
+               delete i->second;
+       }
+}
+
 void LoadDatabases(ConfigReader* ThisConf, InspIRCd* ServerInstance)
 {
-       Connections.clear();
+       ClearDatabases();
        for (int j =0; j < ThisConf->Enumerate("database"); j++)
        {
                std::string db = ThisConf->ReadValue("database","name",j);
@@ -715,6 +725,47 @@ class ModuleSQL : public Module
        pthread_t Dispatcher;
        int currid;
 
+       ModuleSQL(InspIRCd* Me)
+       : Module::Module(Me)
+       {
+               ServerInstance->UseInterface("SQLutils");
+
+               Conf = new ConfigReader(ServerInstance);
+               PublicServerInstance = ServerInstance;
+               currid = 0;
+               SQLModule = this;
+
+               MessagePipe = new Notifier(ServerInstance);
+               ServerInstance->Log(DEBUG,"Bound notifier to 127.0.0.1:%d",MessagePipe->GetPort());
+               
+               pthread_attr_t attribs;
+               pthread_attr_init(&attribs);
+               pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_DETACHED);
+               if (pthread_create(&this->Dispatcher, &attribs, DispatcherThread, (void *)this) != 0)
+               {
+                       throw ModuleException("m_mysql: Failed to create dispatcher thread: " + std::string(strerror(errno)));
+               }
+
+               if (!ServerInstance->PublishFeature("SQL", this))
+               {
+                       /* Tell worker thread to exit NOW */
+                       giveup = true;
+                       throw ModuleException("m_mysql: Unable to publish feature 'SQL'");
+               }
+
+               ServerInstance->PublishInterface("SQL", this);
+       }
+
+       virtual ~ModuleSQL()
+       {
+               giveup = true;
+               ClearDatabases();
+               DELETE(Conf);
+               ServerInstance->UnpublishInterface("SQL", this);
+               ServerInstance->DoneWithInterface("SQLutils");
+       }
+
+
        void Implements(char* List)
        {
                List[I_OnRehash] = List[I_OnRequest] = 1;
@@ -764,38 +815,6 @@ class ModuleSQL : public Module
                return NULL;
        }
 
-       ModuleSQL(InspIRCd* Me)
-               : Module::Module(Me)
-       {
-               
-               Conf = new ConfigReader(ServerInstance);
-               PublicServerInstance = ServerInstance;
-               currid = 0;
-               SQLModule = this;
-
-               MessagePipe = new Notifier(ServerInstance);
-               ServerInstance->Log(DEBUG,"Bound notifier to 127.0.0.1:%d",MessagePipe->GetPort());
-               
-               pthread_attr_t attribs;
-               pthread_attr_init(&attribs);
-               pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_DETACHED);
-               if (pthread_create(&this->Dispatcher, &attribs, DispatcherThread, (void *)this) != 0)
-               {
-                       throw ModuleException("m_mysql: Failed to create dispatcher thread: " + std::string(strerror(errno)));
-               }
-               if (!ServerInstance->PublishFeature("SQL", this))
-               {
-                       /* Tell worker thread to exit NOW */
-                       giveup = true;
-                       throw ModuleException("m_mysql: Unable to publish feature 'SQL'");
-               }
-       }
-       
-       virtual ~ModuleSQL()
-       {
-               DELETE(Conf);
-       }
-       
        virtual void OnRehash(const std::string &parameter)
        {
                /* TODO: set rehash bool here, which makes the dispatcher thread rehash at next opportunity */