summaryrefslogtreecommitdiff
path: root/src/modules/extra/m_sql.h
blob: e1a8b0efd0265779ef7a7a911ddb5f883467eeff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#ifndef __M_SQL_H__
#define __M_SQL_H__

using namespace std;

#include <string>
#include <vector>

#define SQL_RESULT 1
#define SQL_COUNT  2
#define SQL_ROW    3
#define SQL_ERROR  4
#define SQL_END    5
#define SQL_DONE   6
#define SQL_OK     7

// SQLRequest is inherited from a basic Request object
// so that we can neatly pass information around the
// system.

class SQLRequest
{
 protected:
	long conn_id;
	int request_type;
	std::string thisquery;
 public:
	SQLRequest(int qt, long cid, std::string query)
	{
		this->SetQueryType(qt);
		this->SetConnID(cid);
		this->SetQuery(query);
	}

	void SetConnID(long id)
	{
		conn_id = id;
	}

	long GetConnID()
	{
		return conn_id;
	}

	void SetQueryType(int t)
	{
		request_type = t;
	}

	int GetQueryType()
	{
		return request_type;
	}

	void SetQuery(std::string query)
	{
		thisquery = query;
	}

	std::string GetQuery()
	{
		return thisquery;
	}
};

// Upon completion, an SQLRequest returns an SQLResponse.

class SQLResult
{
 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 resptype;
	}

	std::string GetError()
	{
		return error;
	}

	void SetCount(unsigned long c)
	{
		count = c;
	}

	unsigned long GetCount()
	{
		return count;
	}
};

class SQLQuery
{
  private:
        SQLRequest* rowrequest;
	SQLRequest* query;
	SQLResult* result;
	SQLResult* rowresult;
        Request* rowquery;
        unsigned long dbid;
        Module* parent;
        Module* SQLModule;
        Server* Srv;


        bool MakeQueryGoNow(std::string qry)
	{
                // Insert Lack of More Original Name here.
                Request queryrequest((char*)query, parent, SQLModule);
                result = (SQLResult*)queryrequest.Send();
                if (result->GetType() != SQL_ERROR)
		{
			// Query Is fine.. Prepare to get first row...
			rowrequest = new SQLRequest(SQL_ROW,dbid,"");
			rowquery = new Request((char*)rowrequest, parent, SQLModule);
			return true;
		}
		// Query Failed. - Coder Fucked up! (Probably me too :/)
		Srv->Log(DEBUG, " ============= SQL Error, Query And Error Follow. ============= ");
		Srv->Log(DEBUG, "Query: "+ qry);
		Srv->Log(DEBUG, "Error: "+ result->GetError());
                Srv->Log(DEBUG, " ============================================================== ");
		// Destroy Variables that were set..
		delete query;
		query = NULL;
		result = NULL;
		return false;
	}

  public:

	SQLQuery(Server* S) : Srv(S)
	{
	}

        SQLQuery(Module* a, unsigned long b, Server* S) : dbid(b), parent(a), Srv(S)
        {
                // Make a few useful variables..
                SQLModule = Srv->FindModule("m_sql.so");
        }

        ~SQLQuery()
        {
        }

        bool Query(std::string qry)
        {
                query = new SQLRequest(SQL_RESULT, dbid, qry);
                return MakeQueryGoNow(qry);
        }

        bool QueryCount(std::string qry)
        {
                query = new SQLRequest(SQL_COUNT, dbid, qry);
                return MakeQueryGoNow(qry);
        }

        bool GetRow()
        {
		rowresult = (SQLResult*)rowquery->Send();
		if (rowresult->GetType() == SQL_ROW)
		{
                	// We have got a row.. thats all for now.
                        return true;
                }
                // No Row, Error, or end. KILL CALLER! *BANG*
                return false;
	}

	std::string GetField(std::string fname)
	{
		return rowresult->GetField(fname);
	}

	int GetCount()
	{
		rowresult = (SQLResult*)rowquery->Send();
                if (rowresult->GetType() == SQL_COUNT)
		{
                	return rowresult->GetCount();
                }
		else
		{
                        return 0;
                }
        }

        void SQLDone()
	{
                // Tell m_sql we are finished..
		query->SetQueryType(SQL_DONE);
                query->SetConnID(dbid);
                Request donerequest((char*)query, parent, SQLModule);
                donerequest.Send();

                // Do Some Clearing up.
                delete query;
                delete rowrequest;
                // Null the variables, so they can be re-used without confusion..
                result = NULL;
                query = NULL;
                rowrequest = NULL;
                rowresult = NULL;
        }

        std::string Sanitise(std::string crap)
        {
                std::string temp = "";
                for (unsigned int q = 0; q < crap.length(); q++)
                {
                        if (crap[q] == '\'')
			{
				temp = temp + "\'";
			}
                        else if (crap[q] == '"')
			{
				temp = temp + "\\\"";
			}
                        else
				temp = temp + crap[q];
                }
                return temp;
        }
};


#endif