]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sql.h
Extra checks to not set the ssl marker twice on re-handshake (nonfatal but wasteful)
[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         unsigned long count;
73         std::string error;
74         std::map<std::string,std::string> row;
75  public:
76         void SetRow(std::map<std::string,std::string> r)
77         {
78                 row = r;
79         }
80
81         std::string GetField(std::string field)
82         {
83                 std::map<std::string,std::string>::iterator iter = row.find(field);
84                 if (iter == row.end()) return "";
85                 return iter->second;
86         }
87
88         void SetType(int rt)
89         {
90                 resptype = rt;
91         }
92
93         void SetError(std::string err)
94         {
95                 error = err;
96         }
97
98         int GetType()
99         {
100                 return resptype;
101         }
102
103         std::string GetError()
104         {
105                 return error;
106         }
107
108         void SetCount(unsigned long c)
109         {
110                 count = c;
111         }
112
113         unsigned long GetCount()
114         {
115                 return count;
116         }
117 };
118
119 class SQLQuery
120 {
121   private:
122         SQLRequest* rowrequest;
123         SQLRequest* query;
124         SQLResult* result;
125         SQLResult* rowresult;
126         Request* rowquery;
127         unsigned long dbid;
128         Module* parent;
129         Module* SQLModule;
130         Server* Srv;
131
132
133         bool MakeQueryGoNow(std::string qry)
134         {
135                 // Insert Lack of More Original Name here.
136                 Request queryrequest((char*)query, parent, SQLModule);
137                 result = (SQLResult*)queryrequest.Send();
138                 if (result->GetType() != SQL_ERROR)
139                 {
140                         // Query Is fine.. Prepare to get first row...
141                         rowrequest = new SQLRequest(SQL_ROW,dbid,"");
142                         rowquery = new Request((char*)rowrequest, parent, SQLModule);
143                         return true;
144                 }
145                 // Query Failed. - Coder Fucked up! (Probably me too :/)
146                 Srv->Log(DEBUG, " ============= SQL Error, Query And Error Follow. ============= ");
147                 Srv->Log(DEBUG, "Query: "+ qry);
148                 Srv->Log(DEBUG, "Error: "+ result->GetError());
149                 Srv->Log(DEBUG, " ============================================================== ");
150                 // Destroy Variables that were set..
151                 delete query;
152                 query = NULL;
153                 result = NULL;
154                 return false;
155         }
156
157   public:
158
159         SQLQuery(Server* S) : Srv(S)
160         {
161         }
162
163         SQLQuery(Module* a, unsigned long b, Server* S) : dbid(b), parent(a), Srv(S)
164         {
165                 // Make a few useful variables..
166                 SQLModule = Srv->FindModule("m_sql.so");
167         }
168
169         ~SQLQuery()
170         {
171         }
172
173         bool Query(std::string qry)
174         {
175                 query = new SQLRequest(SQL_RESULT, dbid, qry);
176                 return MakeQueryGoNow(qry);
177         }
178
179         bool QueryCount(std::string qry)
180         {
181                 query = new SQLRequest(SQL_COUNT, dbid, qry);
182                 return MakeQueryGoNow(qry);
183         }
184
185         bool GetRow()
186         {
187                 rowresult = (SQLResult*)rowquery->Send();
188                 if (rowresult->GetType() == SQL_ROW)
189                 {
190                         // We have got a row.. thats all for now.
191                         return true;
192                 }
193                 // No Row, Error, or end. KILL CALLER! *BANG*
194                 return false;
195         }
196
197         std::string GetField(std::string fname)
198         {
199                 return rowresult->GetField(fname);
200         }
201
202         int GetCount()
203         {
204                 rowresult = (SQLResult*)rowquery->Send();
205                 if (rowresult->GetType() == SQL_COUNT)
206                 {
207                         return rowresult->GetCount();
208                 }
209                 else
210                 {
211                         return 0;
212                 }
213         }
214
215         void SQLDone()
216         {
217                 // Tell m_sql we are finished..
218                 query->SetQueryType(SQL_DONE);
219                 query->SetConnID(dbid);
220                 Request donerequest((char*)query, parent, SQLModule);
221                 donerequest.Send();
222
223                 // Do Some Clearing up.
224                 delete query;
225                 delete rowrequest;
226                 // Null the variables, so they can be re-used without confusion..
227                 result = NULL;
228                 query = NULL;
229                 rowrequest = NULL;
230                 rowresult = NULL;
231         }
232
233         std::string Sanitise(std::string crap)
234         {
235                 std::string temp = "";
236                 for (unsigned int q = 0; q < crap.length(); q++)
237                 {
238                         if (crap[q] == '\'')
239                         {
240                                 temp = temp + "\'";
241                         }
242                         else if (crap[q] == '"')
243                         {
244                                 temp = temp + "\\\"";
245                         }
246                         else
247                                 temp = temp + crap[q];
248                 }
249                 return temp;
250         }
251 };
252
253
254 #endif