]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlv2.h
e50703ea19de36e13c4553418bf30886e0d38ac1
[user/henk/code/inspircd.git] / src / modules / extra / m_sqlv2.h
1 #ifndef INSPIRCD_SQLAPI_2
2 #define INSPIRCD_SQLAPI_2
3
4 #include <string>
5 #include <deque>
6 #include <map>
7 #include "modules.h"
8
9 /* This is the voodoo magic which lets us pass multiple parameters
10  * to the SQLrequest constructor..voodoo...
11  */
12 #define SQLreq(a, b, c, d, e...) SQLrequest(a, b, c, (SQLquery(d), ##e))
13
14 #define SQLREQID "SQLv2 Request"
15 #define SQLRESID "SQLv2 Result"
16 #define SQLSUCCESS "You shouldn't be reading this (success)"
17
18 enum SQLerrorNum { NO_ERROR, BAD_DBID, BAD_CONN, QSEND_FAIL, QREPLY_FAIL };
19 typedef std::deque<std::string> ParamL;
20
21 class SQLexception : public ModuleException
22 {
23 };
24
25 class SQLbadColName : public SQLexception
26 {
27 public:
28         SQLbadColName() { }
29 };
30
31 class SQLerror : public classbase
32 {
33         SQLerrorNum id;
34         std::string str;
35 public:
36         SQLerror(SQLerrorNum i = NO_ERROR, const std::string &s = "")
37         : id(i), str(s)
38         {       
39         }
40         
41         SQLerrorNum Id()
42         {
43                 return id;
44         }
45         
46         SQLerrorNum Id(SQLerrorNum i)
47         {
48                 id = i;
49                 return id;
50         }
51         
52         void Str(const std::string &s)
53         {
54                 str = s;
55         }
56         
57         const char* Str()
58         {
59                 if(str.length())
60                         return str.c_str();
61                 
62                 switch(id)
63                 {
64                         case NO_ERROR:
65                                 return "No error";
66                         case BAD_DBID:
67                                 return "Invalid database ID";
68                         case BAD_CONN:
69                                 return "Invalid connection";
70                         case QSEND_FAIL:
71                                 return "Sending query failed";
72                         case QREPLY_FAIL:
73                                 return "Getting query result failed";
74                         default:
75                                 return "Unknown error";                         
76                 }
77         }
78 };
79
80 class SQLquery
81 {
82 public:
83         std::string q;
84         ParamL p;
85
86         SQLquery(const std::string &query)
87         : q(query)
88         {
89                 log(DEBUG, "SQLquery constructor: %s", q.c_str());
90         }
91
92         SQLquery(const std::string &query, const ParamL &params)
93         : q(query), p(params)
94         {
95                 log(DEBUG, "SQLquery constructor with %d params: %s", p.size(), q.c_str());
96         }       
97         
98         SQLquery& operator,(const std::string &foo)
99         {
100                 p.push_back(foo);
101                 return *this;
102         }
103         
104         SQLquery& operator%(const std::string &foo)
105         {
106                 p.push_back(foo);
107                 return *this;
108         }
109 };
110
111 class SQLrequest : public Request
112 {
113 public:
114         SQLquery query;
115         std::string dbid;
116         bool pri;
117         unsigned long id;
118         SQLerror error;
119         
120         SQLrequest(Module* s, Module* d, const std::string &databaseid, const SQLquery &q)
121         : Request(SQLREQID, s, d), query(q), dbid(databaseid), pri(false), id(0)
122         {
123         }
124         
125         void Priority(bool p = true)
126         {
127                 pri = p;
128         }
129         
130         void SetSource(Module* mod)
131         {
132                 source = mod;
133         }
134 };
135
136 class SQLfield
137 {
138 public:
139         /* The data itself */
140         std::string d;
141
142         /* If the field was null */
143         bool null;
144
145         SQLfield(const std::string &data, bool n)
146         : d(data), null(n)
147         {
148                 
149         }
150 };
151
152 typedef std::vector<SQLfield> SQLfieldList;
153 typedef std::map<std::string, SQLfield> SQLfieldMap;
154
155 class SQLresult : public Request
156 {
157 public:
158         std::string query;
159         std::string dbid;
160         SQLerror error; 
161         unsigned long id;
162
163         SQLresult(Module* s, Module* d, unsigned long i)
164         : Request(SQLRESID, s, d), id(i)
165         {
166         }
167         
168         /* Return the number of rows in the result */
169         virtual int Rows() = 0;
170         
171         /* Return the number of columns in the result */
172         virtual int Cols() = 0;
173         
174         /* Get a string name of the column by an index number */
175         virtual std::string ColName(int column) = 0;
176         
177         /* Get an index number for a column from a string name.
178          * An exception of type SQLbadColName will be thrown if
179          * the name given is invalid.
180          */
181         virtual int ColNum(const std::string &column) = 0;
182         
183         /* Get a string value in a given row and column */
184         virtual SQLfield GetValue(int row, int column) = 0;
185         
186         /* Return a list of values in a row, this should
187          * increment an internal counter so you can repeatedly
188          * call it until it returns an empty vector.
189          * This returns a reference to an internal object,
190          * the same object is used for all calls to this function
191          * and therefore the return value is only valid until
192          * you call this function again. It is also invalid if
193          * the SQLresult object is destroyed.
194          */
195         virtual SQLfieldList& GetRow() = 0;
196         
197         /* As above, but return a map indexed by key name */
198         virtual SQLfieldMap& GetRowMap() = 0;
199         
200         /* Like GetRow(), but returns a pointer to a dynamically
201          * allocated object which must be explicitly freed. For
202          * portability reasons this must be freed with SQLresult::Free()
203          */
204         virtual SQLfieldList* GetRowPtr() = 0;
205         
206         /* As above, but return a map indexed by key name */
207         virtual SQLfieldMap* GetRowMapPtr() = 0;
208         
209         /* Overloaded function for freeing the lists and maps returned
210          * above.
211          */
212         virtual void Free(SQLfieldMap* fm) = 0;
213         virtual void Free(SQLfieldList* fl) = 0;
214 };
215
216 #endif