]> 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 3dadd5873e0b8cdb09a7b4916f60cd37f4fd0732..2449b523866292ff0fa606216b37dca05db07b91 100644 (file)
@@ -2,20 +2,15 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
- *                    E-mail:
- *             <brain@chatspike.net>
- *               <Craig@chatspike.net>
- *     
- * Written by Craig Edwards, Craig McLure, and others.
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
  * This program is free but copyrighted software; see
- *         the file COPYING for details.
+ *            the file COPYING for details.
  *
  * ---------------------------------------------------
  */
 
-using namespace std;
-
 #include <stdio.h>
 #include <string>
 #include <mysql.h>
@@ -31,6 +26,7 @@ using namespace std;
 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
 /* $CompileFlags: `mysql_config --include` */
 /* $LinkerFlags: `mysql_config --libs_r` `perl extra/mysql_rpath.pl` */
+/* $ModDep: m_sqlv2.h */
 
 /* THE NONBLOCKING MYSQL API!
  * 
@@ -199,6 +195,8 @@ pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 pthread_mutex_t results_mutex = PTHREAD_MUTEX_INITIALIZER;
 
+pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 /** Represents a mysql result set
  */
 class MySQLresult : public SQLresult
@@ -530,6 +528,8 @@ class SQLConnection : public classbase
                 * Pass them this connection id as what to examine
                 */
 
+               delete[] query;
+
                NotifyMainThread(this);
        }
 
@@ -583,24 +583,30 @@ void ConnectDatabases(InspIRCd* ServerInstance)
        for (ConnMap::iterator i = Connections.begin(); i != Connections.end(); i++)
        {
                i->second->SetEnable(true);
-               if (i->second->Connect())
-               {
-                       ServerInstance->Log(DEFAULT,"SQL: Successfully connected database "+i->second->GetHost());
-               }
-               else
+               if (!i->second->Connect())
                {
+                       /* XXX: MUTEX */
+                       pthread_mutex_lock(&logging_mutex);
                        ServerInstance->Log(DEFAULT,"SQL: Failed to connect database "+i->second->GetHost()+": Error: "+i->second->GetError());
                        i->second->SetEnable(false);
+                       pthread_mutex_unlock(&logging_mutex);
                }
        }
 }
 
+void ClearDatabases()
+{
+       ConnMap::iterator i;
+       while ((i = Connections.begin()) != Connections.end())
+       {
+               Connections.erase(i);
+               delete i->second;
+       }
+}
 
 void LoadDatabases(ConfigReader* ThisConf, InspIRCd* ServerInstance)
 {
-       ServerInstance->Log(DEFAULT,"SQL: Loading database settings");
-       Connections.clear();
-       ServerInstance->Log(DEBUG,"Cleared connections");
+       ClearDatabases();
        for (int j =0; j < ThisConf->Enumerate("database"); j++)
        {
                std::string db = ThisConf->ReadValue("database","name",j);
@@ -608,13 +614,10 @@ void LoadDatabases(ConfigReader* ThisConf, InspIRCd* ServerInstance)
                std::string pass = ThisConf->ReadValue("database","password",j);
                std::string host = ThisConf->ReadValue("database","hostname",j);
                std::string id = ThisConf->ReadValue("database","id",j);
-               ServerInstance->Log(DEBUG,"Read database settings");
                if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
                {
                        SQLConnection* ThisSQL = new SQLConnection(host,user,pass,db,id);
-                       ServerInstance->Log(DEFAULT,"Loaded database: "+ThisSQL->GetHost());
                        Connections[id] = ThisSQL;
-                       ServerInstance->Log(DEBUG,"Pushed back connection");
                }
        }
        ConnectDatabases(ServerInstance);
@@ -722,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;
@@ -771,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 */