]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlv2.h
75d7eb093e594f586a2103ab16684e26fda0f45c
[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 };
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                         default:
73                                 return "Unknown error";                         
74                 }
75         }
76 };
77
78 class SQLquery
79 {
80 public:
81         std::string q;
82         ParamL p;
83
84         SQLquery(const std::string &query)
85         : q(query)
86         {
87                 log(DEBUG, "SQLquery constructor: %s", q.c_str());
88         }
89
90         SQLquery(const std::string &query, const ParamL &params)
91         : q(query), p(params)
92         {
93                 log(DEBUG, "SQLquery constructor with %d params: %s", p.size(), q.c_str());
94         }       
95         
96         SQLquery& operator,(const std::string &foo)
97         {
98                 p.push_back(foo);
99                 return *this;
100         }
101         
102         SQLquery& operator%(const std::string &foo)
103         {
104                 p.push_back(foo);
105                 return *this;
106         }
107 };
108
109 class SQLrequest : public Request
110 {
111 public:
112         SQLquery query;
113         std::string dbid;
114         bool pri;
115         unsigned long id;
116         SQLerror error;
117         
118         SQLrequest(Module* s, Module* d, const std::string &databaseid, const SQLquery &q)
119         : Request(SQLREQID, s, d), query(q), dbid(databaseid), pri(false), id(0)
120         {
121         }
122         
123         void Priority(bool p = true)
124         {
125                 pri = p;
126         }
127         
128         void SetSource(Module* mod)
129         {
130                 source = mod;
131         }
132 };
133
134 class SQLfield
135 {
136 public:
137         /* The data itself */
138         std::string d;
139
140         /* If the field was null */
141         bool null;
142
143         SQLfield(const std::string &data, bool n)
144         : d(data), null(n)
145         {
146                 
147         }
148 };
149
150 typedef std::vector<SQLfield> SQLfieldList;
151 typedef std::map<std::string, SQLfield> SQLfieldMap;
152
153 class SQLresult : public Request
154 {
155 public:
156         std::string query;
157         std::string dbid;
158         SQLerror error; 
159
160         SQLresult(Module* s, Module* d)
161         : Request(SQLRESID, s, d)
162         {
163         }
164         
165         /* Return the number of rows in the result */
166         virtual int Rows() = 0;
167         
168         /* Return the number of columns in the result */
169         virtual int Cols() = 0;
170         
171         /* Get a string name of the column by an index number */
172         virtual std::string ColName(int column) = 0;
173         
174         /* Get an index number for a column from a string name.
175          * An exception of type SQLbadColName will be thrown if
176          * the name given is invalid.
177          */
178         virtual int ColNum(const std::string &column) = 0;
179         
180         /* Get a string value in a given row and column */
181         virtual SQLfield GetValue(int row, int column) = 0;
182         
183         /* Return a list of values in a row, this should
184          * increment an internal counter so you can repeatedly
185          * call it until it returns an empty vector.
186          * This returns a reference to an internal object,
187          * the same object is used for all calls to this function
188          * and therefore the return value is only valid until
189          * you call this function again. It is also invalid if
190          * the SQLresult object is destroyed.
191          */
192         virtual SQLfieldList& GetRow() = 0;
193         
194         /* As above, but return a map indexed by key name */
195         virtual SQLfieldMap& GetRowMap() = 0;
196         
197         /* Like GetRow(), but returns a pointer to a dynamically
198          * allocated object which must be explicitly freed. For
199          * portability reasons this must be freed with SQLresult::Free()
200          */
201         virtual SQLfieldList* GetRowPtr() = 0;
202         
203         /* As above, but return a map indexed by key name */
204         virtual SQLfieldMap* GetRowMapPtr() = 0;
205         
206         /* Overloaded function for freeing the lists and maps returned
207          * above.
208          */
209         virtual void Free(SQLfieldMap* fm) = 0;
210         virtual void Free(SQLfieldList* fl) = 0;
211 };
212
213 #endif