summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2005-04-20 21:17:24 +0000
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2005-04-20 21:17:24 +0000
commitb5666d9baea4bec8035d46f582661a5e8b74ca0a (patch)
tree24caf85380961462d9db4653705d72bd6210d2a0
parent8a50e43133b7c25ad7c975737a872d5b4ad64620 (diff)
Added preliminary m_sql.cpp
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@1141 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r--src/modules/extra/m_sql.cpp201
1 files changed, 201 insertions, 0 deletions
diff --git a/src/modules/extra/m_sql.cpp b/src/modules/extra/m_sql.cpp
new file mode 100644
index 000000000..62d4096eb
--- /dev/null
+++ b/src/modules/extra/m_sql.cpp
@@ -0,0 +1,201 @@
+/* +------------------------------------+
+ * | Inspire Internet Relay Chat Daemon |
+ * +------------------------------------+
+ *
+ * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
+ * E-mail:
+ * <brain@chatspike.net>
+ * <Craig@chatspike.net>
+ *
+ * Written by Craig Edwards, Craig McLure, and others.
+ * This program is free but copyrighted software; see
+ * the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
+
+#include <stdio.h>
+#include <string>
+#include <mysql/mysql.h>
+#include "users.h"
+#include "channels.h"
+#include "modules.h"
+
+/* $ModDesc: m_filter with regexps */
+/* $CompileFlags: -I/usr/local/include -L/usr/local/lib/mysql -L/usr/lib/mysql -lmysqlclient */
+
+/** SQLConnection represents one mysql session.
+ * Each session has its own persistent connection to the database.
+ */
+class SQLConnection
+{
+ protected:
+
+ MYSQL connection;
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ std::string host;
+ std::string user;
+ std::string pass;
+ std::string db;
+ std::map<std::string,std::string> thisrow;
+
+ public:
+
+ // This constructor creates an SQLConnection object with the given credentials, and creates the underlying
+ // MYSQL struct, but does not connect yet.
+ SQLConnection(std::string thishost, std::string thisuser, std::string thispass, std::string thisdb)
+ {
+ this->host = thishost;
+ this->user = thisuser;
+ this->pass = thispass;
+ this->db = thisdb;
+ mysql_init(&connection);
+ }
+
+ // This method connects to the database using the credentials supplied to the constructor, and returns
+ // true upon success.
+ bool Connect()
+ {
+ return mysql_real_connect(&connection, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, NULL, 0);
+ }
+
+ // This method issues a query that expects multiple rows of results. Use GetRow() and QueryDone() to retrieve
+ // multiple rows.
+ bool QueryResult(std::string query)
+ {
+ char escaped_query[query.length()+1];
+ mysql_real_escape_string(&connection, escaped_query, query.c_str(), query.length());
+ int r = mysql_query(&connection, escaped_query);
+ if (!r)
+ {
+ res = mysql_use_result(&connection);
+ }
+ return (!r);
+ }
+
+ // 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)
+ {
+ char escaped_query[query.length()+1];
+ mysql_real_escape_string(&connection, escaped_query, query.c_str(), query.length());
+ int r = mysql_query(&connection, escaped_query);
+ 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
+ // using QueryResult() first! The row's values are returned as a map of std::string
+ // where each item is keyed by the column name.
+ std::map<std::string,std::string> GetRow()
+ {
+ thisrow.clear();
+ if (res)
+ {
+ row = mysql_fetch_row(res);
+ if (row)
+ {
+ unsigned int field_count = 0;
+ if(mysql_field_count(&connection) == 0)
+ return thisrow;
+ MYSQL_FIELD *fields = mysql_fetch_fields(res);
+ while (field_count < mysql_field_count(&connection))
+ {
+ thisrow[std::string(fields[field_count].name)] = std::string(row[field_count]);
+ field_count++;
+ }
+ return thisrow;
+ }
+ }
+ return thisrow;
+ }
+
+ bool QueryDone()
+ {
+ if (res)
+ {
+ mysql_free_result(res);
+ return true;
+ }
+ else return false;
+ }
+
+ std::string GetError()
+ {
+ return mysql_error(&connection);
+ }
+
+ ~SQLConnection()
+ {
+ mysql_close(&connection);
+ }
+};
+
+
+class ModuleSQL : public Module
+{
+ Server *Srv;
+ ConfigReader *Conf;
+
+ public:
+ ModuleSQL()
+ {
+ }
+
+ virtual ~ModuleSQL()
+ {
+ }
+
+ virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
+ {
+ }
+
+ virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
+ {
+ }
+
+ virtual void OnRehash()
+ {
+ }
+
+ virtual Version GetVersion()
+ {
+ return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
+ }
+
+};
+
+// stuff down here is the module-factory stuff. For basic modules you can ignore this.
+
+class ModuleSQLFactory : public ModuleFactory
+{
+ public:
+ ModuleSQLFactory()
+ {
+ }
+
+ ~ModuleSQLFactory()
+ {
+ }
+
+ virtual Module * CreateModule()
+ {
+ return new ModuleSQL;
+ }
+
+};
+
+
+extern "C" void * init_module( void )
+{
+ return new ModuleSQLFactory;
+}
+