]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_sql.cpp
Updated copyrights in headers etc using perl inplace edit
[user/henk/code/inspircd.git] / src / modules / extra / m_sql.cpp
index 2148ec3de7423340fd190f0723c59f49521625cb..de96c5a9309898e66d453cfa792fc24b0c18ec35 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
+ *  InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
  *                       E-mail:
  *                <brain@chatspike.net>
  *               <Craig@chatspike.net>
  * ---------------------------------------------------
  */
 
+using namespace std;
 
 #include <stdio.h>
 #include <string>
-#include <mysql/mysql.h>
+#include <mysql.h>
 #include "users.h"
 #include "channels.h"
 #include "modules.h"
+#include "helperfuncs.h"
 #include "m_sql.h"
 
 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
-/* $CompileFlags: -I/usr/local/include -I/usr/include -L/usr/local/lib/mysql -L/usr/lib/mysql -lmysqlclient */
+/* $CompileFlags: -I/usr/local/include/mysql -I/usr/include/mysql -I/usr/local/include -I/usr/include */
+/* $LinkerFlags: -L/usr/local/lib/mysql -Wl,--rpath -Wl,/usr/local/lib/mysql -L/usr/lib/mysql -Wl,--rpath -Wl,/usr/lib/mysql -lmysqlclient */
 
 /** SQLConnection represents one mysql session.
  * Each session has its own persistent connection to the database.
  */
+
+#if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
+#define mysql_field_count mysql_num_fields
+#endif
+
 class SQLConnection
 {
  protected:
@@ -56,7 +64,9 @@ class SQLConnection
                this->pass = thispass;
                this->db = thisdb;
                this->id = myid;
+               unsigned int timeout = 1;
                mysql_init(&connection);
+               mysql_options(&connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
        }
 
        // This method connects to the database using the credentials supplied to the constructor, and returns
@@ -105,15 +115,20 @@ class SQLConnection
                        if (row)
                        {
                                unsigned int field_count = 0;
+                               MYSQL_FIELD *fields = mysql_fetch_fields(res);
                                if(mysql_field_count(&connection) == 0)
                                        return thisrow;
-                               MYSQL_FIELD *fields = mysql_fetch_fields(res);
-                               while (field_count < mysql_field_count(&connection))
+                               if (fields && mysql_field_count(&connection))
                                {
-                                       thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
-                                       field_count++;
+                                       while (field_count < mysql_field_count(&connection))
+                                       {
+                                               std::string a = (fields[field_count].name ? fields[field_count].name : "");
+                                               std::string b = (row[field_count] ? row[field_count] : "");
+                                               thisrow[a] = b;
+                                               field_count++;
+                                       }
+                                       return thisrow;
                                }
-                               return thisrow;
                        }
                }
                return thisrow;
@@ -159,10 +174,6 @@ class SQLConnection
                return Enabled;
        }
 
-       ~SQLConnection()
-       {
-               mysql_close(&connection);
-       }
 };
 
 typedef std::vector<SQLConnection> ConnectionList;
@@ -191,22 +202,25 @@ class ModuleSQL : public Module
                }
        }
 
-       void LoadDatabases(ConfigReader* Conf)
+       void LoadDatabases(ConfigReader* ThisConf)
        {
                Srv->Log(DEFAULT,"SQL: Loading database settings");
                Connections.clear();
-               for (int j =0; j < Conf->Enumerate("database"); j++)
+               Srv->Log(DEBUG,"Cleared connections");
+               for (int j =0; j < ThisConf->Enumerate("database"); j++)
                {
-                       std::string db = Conf->ReadValue("database","name",j);
-                       std::string user = Conf->ReadValue("database","username",j);
-                       std::string pass = Conf->ReadValue("database","password",j);
-                       std::string host = Conf->ReadValue("database","hostname",j);
-                       std::string id = Conf->ReadValue("database","id",j);
+                       std::string db = ThisConf->ReadValue("database","name",j);
+                       std::string user = ThisConf->ReadValue("database","username",j);
+                       std::string pass = ThisConf->ReadValue("database","password",j);
+                       std::string host = ThisConf->ReadValue("database","hostname",j);
+                       std::string id = ThisConf->ReadValue("database","id",j);
+                       Srv->Log(DEBUG,"Read database settings");
                        if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
                        {
                                SQLConnection ThisSQL(host,user,pass,db,atoi(id.c_str()));
                                Srv->Log(DEFAULT,"Loaded database: "+ThisSQL.GetHost());
                                Connections.push_back(ThisSQL);
+                               Srv->Log(DEBUG,"Pushed back connection");
                        }
                }
                ConnectDatabases();
@@ -277,6 +291,11 @@ class ModuleSQL : public Module
                }
        }
 
+       void Implements(char* List)
+       {
+               List[I_OnRehash] = List[I_OnRequest] = 1;
+       }
+
        char* OnRequest(Request* request)
        {
                if (request)
@@ -303,9 +322,10 @@ class ModuleSQL : public Module
                return NULL;
        }
 
-       ModuleSQL()
+       ModuleSQL(Server* Me)
+               : Module::Module(Me)
        {
-               Srv = new Server();
+               Srv = Me;
                Conf = new ConfigReader();
                LoadDatabases(Conf);
        }
@@ -314,10 +334,9 @@ class ModuleSQL : public Module
        {
                Connections.clear();
                delete Conf;
-               delete Srv;
        }
        
-       virtual void OnRehash()
+       virtual void OnRehash(std::string parameter)
        {
                delete Conf;
                Conf = new ConfigReader();
@@ -344,9 +363,9 @@ class ModuleSQLFactory : public ModuleFactory
        {
        }
        
-       virtual Module * CreateModule()
+       virtual Module * CreateModule(Server* Me)
        {
-               return new ModuleSQL;
+               return new ModuleSQL(Me);
        }
        
 };