17 // SQLRequest is inherited from a basic Request object
18 // so that we can neatly pass information around the
21 class SQLRequest : public classbase
26 std::string thisquery;
28 SQLRequest(int qt, long cid, std::string query)
30 this->SetQueryType(qt);
32 this->SetQuery(query);
35 void SetConnID(long id)
45 void SetQueryType(int t)
55 void SetQuery(std::string query)
60 std::string GetQuery()
66 // Upon completion, an SQLRequest returns an SQLResponse.
68 class SQLResult : public classbase
74 std::map<std::string,std::string> row;
77 void SetRow(std::map<std::string,std::string> r)
82 std::string GetField(std::string field)
84 std::map<std::string,std::string>::iterator iter = row.find(field);
85 if (iter == row.end()) return "";
94 void SetError(std::string err)
104 std::string GetError()
109 void SetCount(long c)
114 /* This will return a negative value of the SQL server is down */
121 class SQLQuery : public classbase
124 SQLRequest* rowrequest;
127 SQLResult* rowresult;
135 bool MakeQueryGoNow(std::string qry)
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)
142 // Query Is fine.. Prepare to get first row...
143 rowrequest = new SQLRequest(SQL_ROW,dbid,"");
144 rowquery = new Request((char*)rowrequest, parent, SQLModule);
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 error = result->GetError();
153 // Destroy Variables that were set..
162 SQLQuery(Server* S) : Srv(S)
166 SQLQuery(Module* a, unsigned long b, Server* S) : dbid(b), parent(a), Srv(S)
168 // Make a few useful variables..
169 SQLModule = Srv->FindModule("m_sql.so");
176 bool Query(std::string qry)
178 query = new SQLRequest(SQL_RESULT, dbid, qry);
179 return MakeQueryGoNow(qry);
182 bool QueryCount(std::string qry)
184 query = new SQLRequest(SQL_COUNT, dbid, qry);
185 return MakeQueryGoNow(qry);
190 rowresult = (SQLResult*)rowquery->Send();
191 if (rowresult->GetType() == SQL_ROW)
193 // We have got a row.. thats all for now.
196 // No Row, Error, or end. KILL CALLER! *BANG*
200 std::string GetField(std::string fname)
202 return rowresult->GetField(fname);
207 rowresult = (SQLResult*)rowquery->Send();
208 if (rowresult->GetType() == SQL_COUNT)
210 return rowresult->GetCount();
218 const std::string &GetError()
225 // Tell m_sql we are finished..
226 query->SetQueryType(SQL_DONE);
227 query->SetConnID(dbid);
228 Request donerequest((char*)query, parent, SQLModule);
231 // Do Some Clearing up.
234 // Null the variables, so they can be re-used without confusion..
241 static std::string Sanitise(const std::string& crap)
243 std::string temp = "";
244 for (unsigned int q = 0; q < crap.length(); q++)
250 else if (crap[q] == '"')
254 else if (crap[q] == '\\')