]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_sql.cpp
API header and client module updates for new multi-parameter query request. Needs...
[user/henk/code/inspircd.git] / src / modules / extra / m_sql.cpp
index 28d24bc482626b9680e0138fdaaaa1a7a040365b..654d841918284aebb3f70b6ba8c8496d6df2bb1f 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>
@@ -14,6 +14,7 @@
  * ---------------------------------------------------
  */
 
+using namespace std;
 
 #include <stdio.h>
 #include <string>
@@ -25,7 +26,8 @@
 #include "m_sql.h"
 
 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
-/* $CompileFlags: -I/usr/local/include/mysql -I/usr/include/mysql -I/usr/local/include -I/usr/include -L/usr/local/lib/mysql -L/usr/lib/mysql -L/usr/local/lib -lmysqlclient */
+/* $CompileFlags: `mysql_config --include` */
+/* $LinkerFlags: `mysql_config --libs` `perl ../mysql_rpath.pl` */
 
 /** SQLConnection represents one mysql session.
  * Each session has its own persistent connection to the database.
@@ -35,7 +37,7 @@
 #define mysql_field_count mysql_num_fields
 #endif
 
-class SQLConnection
+class SQLConnection : public classbase
 {
  protected:
 
@@ -62,15 +64,15 @@ 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
        // true upon success.
        bool Connect()
        {
+               unsigned int timeout = 1;
+               mysql_init(&connection);
+               mysql_options(&connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
                return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
        }
 
@@ -78,6 +80,8 @@ class SQLConnection
        // multiple rows.
        bool QueryResult(std::string query)
        {
+               if (!CheckConnection()) return false;
+               
                int r = mysql_query(&connection, query.c_str());
                if (!r)
                {
@@ -88,17 +92,20 @@ class SQLConnection
 
        // This method issues a query that just expects a number of 'effected' rows (e.g. UPDATE or DELETE FROM).
        // the number of effected rows is returned in the return value.
-       unsigned long QueryCount(std::string query)
+       long QueryCount(std::string query)
        {
-               int r = mysql_query(&connection, query.c_str());
-               if (!r)
-               {
-                       res = mysql_store_result(&connection);
-                       unsigned long rows = mysql_affected_rows(&connection);
-                       mysql_free_result(res);
-                       return rows;
-               }
-               return 0;
+               /* If the connection is down, we return a negative value - New to 1.1 */
+               if (!CheckConnection()) return -1;
+
+               int r = mysql_query(&connection, query.c_str());
+               if (!r)
+               {
+                       res = mysql_store_result(&connection);
+                       unsigned long rows = mysql_affected_rows(&connection);
+                       mysql_free_result(res);
+                       return rows;
+               }
+               return 0;
        }
 
        // This method fetches a row, if available from the database. You must issue a query
@@ -113,15 +120,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;
@@ -132,11 +144,28 @@ class SQLConnection
                if (res)
                {
                        mysql_free_result(res);
+                       res = NULL;
                        return true;
                }
                else return false;
        }
 
+       bool ConnectionLost()
+       {
+               if (&connection) {
+                       return (mysql_ping(&connection) != 0);
+               }
+               else return false;
+       }
+
+       bool CheckConnection()
+       {
+               if (ConnectionLost()) {
+                       return Connect();
+               }
+               else return true;
+       }
+
        std::string GetError()
        {
                return mysql_error(&connection);
@@ -284,6 +313,11 @@ class ModuleSQL : public Module
                }
        }
 
+       void Implements(char* List)
+       {
+               List[I_OnRehash] = List[I_OnRequest] = 1;
+       }
+
        char* OnRequest(Request* request)
        {
                if (request)
@@ -310,9 +344,10 @@ class ModuleSQL : public Module
                return NULL;
        }
 
-       ModuleSQL()
+       ModuleSQL(Server* Me)
+               : Module::Module(Me)
        {
-               Srv = new Server();
+               Srv = Me;
                Conf = new ConfigReader();
                LoadDatabases(Conf);
        }
@@ -320,13 +355,12 @@ class ModuleSQL : public Module
        virtual ~ModuleSQL()
        {
                Connections.clear();
-               delete Conf;
-               delete Srv;
+               DELETE(Conf);
        }
        
-       virtual void OnRehash()
+       virtual void OnRehash(const std::string &parameter)
        {
-               delete Conf;
+               DELETE(Conf);
                Conf = new ConfigReader();
                LoadDatabases(Conf);
        }
@@ -351,9 +385,9 @@ class ModuleSQLFactory : public ModuleFactory
        {
        }
        
-       virtual Module * CreateModule()
+       virtual Module * CreateModule(Server* Me)
        {
-               return new ModuleSQL;
+               return new ModuleSQL(Me);
        }
        
 };