diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/modules/extra/m_sql.cpp | 66 | ||||
-rw-r--r-- | src/modules/extra/m_sql.h | 108 |
2 files changed, 174 insertions, 0 deletions
diff --git a/src/modules/extra/m_sql.cpp b/src/modules/extra/m_sql.cpp index f7df3c27e..489f2ddd4 100644 --- a/src/modules/extra/m_sql.cpp +++ b/src/modules/extra/m_sql.cpp @@ -21,6 +21,7 @@ #include "users.h" #include "channels.h" #include "modules.h" +#include "m_sql.h" /* $ModDesc: m_filter with regexps */ /* $CompileFlags: -I/usr/local/include -I/usr/include -L/usr/local/lib/mysql -L/usr/lib/mysql -lmysqlclient */ @@ -215,6 +216,71 @@ class ModuleSQL : public Module ConnectDatabases(); } + void ResultType(SQLRequest *r, SQLResult *res) + { + for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++) + { + if ((i->GetID() == r->GetConnID()) && (i->Enabled())) + { + bool xr = i->QueryResult(r->GetQuery()); + if (!xr) + { + res->SetType(SQL_ERROR); + res->SetError(r->GetError()); + return; + } + } + } + } + + void CountType(SQLRequest *r, SQLResult* res) + { + for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++) + { + if ((i->GetID() == r->GetConnID()) && (i->Enabled())) + { + res->SetType(SQL_COUNT); + res->SetCount(i->QueryCount(r->GetQuery())); + return; + } + } + } + + void RowType(SQLRequest *r, SQLResult* res) + { + for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++) + { + if ((i->GetID() == r->GetConnID()) && (i->Enabled())) + { + std::map<std::string,std::string> row = i->GetRow(); + res->SetRow(row); + res->SetType(SQL_ROW); + if (!row.size()) + res->SetType(SQL_END); + return; + } + } + } + + char* OnRequest(Request* request) + { + SQLResult Result = new SQLResult(); + SQLRequest *r = (SQLRequest*)request; + switch (r->GetRequest()) + { + case SQL_RESULT: + ResultType(r,Result); + break; + case SQL_COUNT: + CountType(r,Result); + break; + case SQL_ROW: + RowType(r,Result); + break; + } + return Result; + } + ModuleSQL() { Srv = new Server(); diff --git a/src/modules/extra/m_sql.h b/src/modules/extra/m_sql.h new file mode 100644 index 000000000..768551bf3 --- /dev/null +++ b/src/modules/extra/m_sql.h @@ -0,0 +1,108 @@ +#ifndef __M_SQL_H__ +#define __M_SQL_H__ + +#include <string> +#include <vector> + +#define SQL_RESULT 1 +#define SQL_COUNT 2 +#define SQL_ROW 3 +#define SQL_ERROR 4 +#define SQL_END 5 + +// SQLRequest is inherited from a basic Request object +// so that we can neatly pass information around the +// system. + +class SQLRequest : public Request +{ + protected: + long conn_id; + int request_type; + std::string thisquery; + public: + void SetConnID(long id) + { + conn_id = id; + } + + long GetConnID(long id) + { + return conn_id; + } + + void SetQueryType(int t) + { + request_type = t; + } + + int GetQueryType(int t) + { + return request_type; + } + + void SetQuery(std::string query) + { + thisquery = query; + } + + std::string GetQuery() + { + return thisquery; + } +}; + +// Upon completion, an SQLRequest returns an SQLResponse. + +class SQLResponse +{ + protected: + int resptype; + unsigned long count; + std::string error; + std::map<std::string,std::string> row; + public: + void SetRow(std::map<std::string,std::string> r) + { + row = r; + } + + std::string GetField(std::string field) + { + std::map<std::string,std::string>::iterator iter = row.find(field); + if (iter == row.end()) return ""; + return iter->second; + } + + void SetType(int rt) + { + resptype = rt; + } + + void SetError(std::string err) + { + error = err; + } + + int GetType() + { + return restype; + } + + std::string GetError() + { + return error; + } + + void SetCount(unsigned long c) + { + count = c; + } + + unsigned long GetCount() + { + return count; + } +}; + +#endif |