]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlv2.h
Okay, working PostgreSQL module, API header and example client module in /extra/...
[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
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
87 class SQLfield
88 {
89 public:
90         /* The data itself */
91         std::string d;
92
93         /* If the field was null */
94         bool null;
95
96         SQLfield(const std::string &data, bool n)
97         : d(data), null(n)
98         {
99                 
100         }
101 };
102
103 typedef std::vector<SQLfield> SQLfieldList;
104 typedef std::map<std::string, SQLfield> SQLfieldMap;
105
106 class SQLresult : public Request
107 {
108 public:
109         std::string query;
110         std::string dbid;
111         SQLerror error; 
112
113         SQLresult(Module* s, Module* d)
114         : Request(SQLRESID, s, d)
115         {
116         }
117         
118         /* Return the number of rows in the result */
119         virtual int Rows() = 0;
120         
121         /* Return the number of columns in the result */
122         virtual int Cols() = 0;
123         
124         /* Get a string name of the column by an index number */
125         virtual std::string ColName(int column) = 0;
126         
127         /* Get an index number for a column from a string name.
128          * An exception of type SQLbadColName will be thrown if
129          * the name given is invalid.
130          */
131         virtual int ColNum(const std::string &column) = 0;
132         
133         /* Get a string value in a given row and column */
134         virtual SQLfield GetValue(int row, int column) = 0;
135         
136         /* Return a list of values in a row, this should
137          * increment an internal counter so you can repeatedly
138          * call it until it returns an empty vector.
139          * This returns a reference to an internal object,
140          * the same object is used for all calls to this function
141          * and therefore the return value is only valid until
142          * you call this function again. It is also invalid if
143          * the SQLresult object is destroyed.
144          */
145         virtual SQLfieldList& GetRow() = 0;
146         
147         /* As above, but return a map indexed by key name */
148         virtual SQLfieldMap& GetRowMap() = 0;
149         
150         /* Like GetRow(), but returns a pointer to a dynamically
151          * allocated object which must be explicitly freed. For
152          * portability reasons this must be freed with SQLresult::Free()
153          */
154         virtual SQLfieldList* GetRowPtr() = 0;
155         
156         /* As above, but return a map indexed by key name */
157         virtual SQLfieldMap* GetRowMapPtr() = 0;
158         
159         /* Overloaded function for freeing the lists and maps returned
160          * above.
161          */
162         virtual void Free(SQLfieldMap* fm) = 0;
163         virtual void Free(SQLfieldList* fl) = 0;
164 };
165
166 #endif