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