]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sql.h
2e185d978e243bdfee954ee271b36636d744296d
[user/henk/code/inspircd.git] / src / modules / extra / m_sql.h
1 #ifndef __M_SQL_H__
2 #define __M_SQL_H__
3
4 using namespace std;
5
6 #include <string>
7 #include <vector>
8
9 #define SQL_RESULT 1
10 #define SQL_COUNT  2
11 #define SQL_ROW    3
12 #define SQL_ERROR  4
13 #define SQL_END    5
14 #define SQL_DONE   6
15 #define SQL_OK     7
16
17 // SQLRequest is inherited from a basic Request object
18 // so that we can neatly pass information around the
19 // system.
20
21 class SQLRequest
22 {
23  protected:
24         long conn_id;
25         int request_type;
26         std::string thisquery;
27  public:
28         SQLRequest(int qt, long cid, std::string query)
29         {
30                 this->SetQueryType(qt);
31                 this->SetConnID(cid);
32                 this->SetQuery(query);
33         }
34
35         void SetConnID(long id)
36         {
37                 conn_id = id;
38         }
39
40         long GetConnID()
41         {
42                 return conn_id;
43         }
44
45         void SetQueryType(int t)
46         {
47                 request_type = t;
48         }
49
50         int GetQueryType()
51         {
52                 return request_type;
53         }
54
55         void SetQuery(std::string query)
56         {
57                 thisquery = query;
58         }
59
60         std::string GetQuery()
61         {
62                 return thisquery;
63         }
64 };
65
66 // Upon completion, an SQLRequest returns an SQLResponse.
67
68 class SQLResult
69 {
70  protected:
71         int resptype;
72         long count;
73         std::string error;
74         std::map<std::string,std::string> row;
75  public:
76
77         void SetRow(std::map<std::string,std::string> r)
78         {
79                 row = r;
80         }
81
82         std::string GetField(std::string field)
83         {
84                 std::map<std::string,std::string>::iterator iter = row.find(field);
85                 if (iter == row.end()) return "";
86                 return iter->second;
87         }
88
89         void SetType(int rt)
90         {
91                 resptype = rt;
92         }
93
94         void SetError(std::string err)
95         {
96                 error = err;
97         }
98
99         int GetType()
100         {
101                 return resptype;
102         }
103
104         std::string GetError()
105         {
106                 return error;
107         }
108
109         void SetCount(long c)
110         {
111                 count = c;
112         }
113
114         /* This will return a negative value of the SQL server is down */
115         long GetCount()
116         {
117                 return count;
118         }
119 };
120
121 class SQLQuery
122 {
123   private:
124         SQLRequest* rowrequest;
125         SQLRequest* query;
126         SQLResult* result;
127         SQLResult* rowresult;
128         Request* rowquery;
129         unsigned long dbid;
130         Module* parent;
131         Module* SQLModule;
132         Server* Srv;
133
134
135         bool MakeQueryGoNow(std::string qry)
136         {
137                 // Insert Lack of More Original Name here.
138                 Request queryrequest((char*)query, parent, SQLModule);
139                 result = (SQLResult*)queryrequest.Send();
140                 if (result->GetType() != SQL_ERROR)
141                 {
142                         // Query Is fine.. Prepare to get first row...
143                         rowrequest = new SQLRequest(SQL_ROW,dbid,"");
144                         rowquery = new Request((char*)rowrequest, parent, SQLModule);
145                         return true;
146                 }
147                 // Query Failed. - Coder Fucked up! (Probably me too :/)
148                 Srv->Log(DEBUG, " ============= SQL Error, Query And Error Follow. ============= ");
149                 Srv->Log(DEBUG, "Query: "+ qry);
150                 Srv->Log(DEBUG, "Error: "+ result->GetError());
151                 Srv->Log(DEBUG, " ============================================================== ");
152                 // Destroy Variables that were set..
153                 delete query;
154                 query = NULL;
155                 result = NULL;
156                 return false;
157         }
158
159   public:
160
161         SQLQuery(Server* S) : Srv(S)
162         {
163         }
164
165         SQLQuery(Module* a, unsigned long b, Server* S) : dbid(b), parent(a), Srv(S)
166         {
167                 // Make a few useful variables..
168                 SQLModule = Srv->FindModule("m_sql.so");
169         }
170
171         ~SQLQuery()
172         {
173         }
174
175         bool Query(std::string qry)
176         {
177                 query = new SQLRequest(SQL_RESULT, dbid, qry);
178                 return MakeQueryGoNow(qry);
179         }
180
181         bool QueryCount(std::string qry)
182         {
183                 query = new SQLRequest(SQL_COUNT, dbid, qry);
184                 return MakeQueryGoNow(qry);
185         }
186
187         bool GetRow()
188         {
189                 rowresult = (SQLResult*)rowquery->Send();
190                 if (rowresult->GetType() == SQL_ROW)
191                 {
192                         // We have got a row.. thats all for now.
193                         return true;
194                 }
195                 // No Row, Error, or end. KILL CALLER! *BANG*
196                 return false;
197         }
198
199         std::string GetField(std::string fname)
200         {
201                 return rowresult->GetField(fname);
202         }
203
204         long GetCount()
205         {
206                 rowresult = (SQLResult*)rowquery->Send();
207                 if (rowresult->GetType() == SQL_COUNT)
208                 {
209                         return rowresult->GetCount();
210                 }
211                 else
212                 {
213                         return 0;
214                 }
215         }
216
217         void SQLDone()
218         {
219                 // Tell m_sql we are finished..
220                 query->SetQueryType(SQL_DONE);
221                 query->SetConnID(dbid);
222                 Request donerequest((char*)query, parent, SQLModule);
223                 donerequest.Send();
224
225                 // Do Some Clearing up.
226                 delete query;
227                 delete rowrequest;
228                 // Null the variables, so they can be re-used without confusion..
229                 result = NULL;
230                 query = NULL;
231                 rowrequest = NULL;
232                 rowresult = NULL;
233         }
234
235         static std::string Sanitise(const std::string& crap)
236         {
237                 std::string temp = "";
238                 for (unsigned int q = 0; q < crap.length(); q++)
239                 {
240                         if (crap[q] == '\'')
241                         {
242                                 temp = temp + "\'";
243                         }
244                         else if (crap[q] == '"')
245                         {
246                                 temp = temp + "\\\"";
247                         }
248                         else
249                                 temp = temp + crap[q];
250                 }
251                 return temp;
252         }
253 };
254
255
256 #endif