1 #ifndef INSPIRCD_SQLAPI_2
2 #define INSPIRCD_SQLAPI_2
9 /* This is the voodoo magic which lets us pass multiple parameters
10 * to the SQLrequest constructor..voodoo...
12 #define SQLreq(a, b, c, d, e...) SQLrequest(a, b, c, (SQLquery(d), ##e))
14 #define SQLREQID "SQLv2 Request"
15 #define SQLRESID "SQLv2 Result"
16 #define SQLSUCCESS "You shouldn't be reading this (success)"
18 enum SQLerrorNum { NO_ERROR, BAD_DBID, BAD_CONN, QSEND_FAIL, QREPLY_FAIL };
19 typedef std::deque<std::string> ParamL;
21 class SQLexception : public ModuleException
25 class SQLbadColName : public SQLexception
31 class SQLerror : public classbase
36 SQLerror(SQLerrorNum i = NO_ERROR, const std::string &s = "")
46 SQLerrorNum Id(SQLerrorNum i)
52 void Str(const std::string &s)
67 return "Invalid database ID";
69 return "Invalid connection";
71 return "Sending query failed";
73 return "Getting query result failed";
75 return "Unknown error";
86 SQLquery(const std::string &query)
89 log(DEBUG, "SQLquery constructor: %s", q.c_str());
92 SQLquery(const std::string &query, const ParamL ¶ms)
95 log(DEBUG, "SQLquery constructor with %d params: %s", p.size(), q.c_str());
98 SQLquery& operator,(const std::string &foo)
104 SQLquery& operator%(const std::string &foo)
111 class SQLrequest : public Request
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)
125 void Priority(bool p = true)
130 void SetSource(Module* mod)
139 /* The data itself */
142 /* If the field was null */
145 SQLfield(const std::string &data, bool n)
152 typedef std::vector<SQLfield> SQLfieldList;
153 typedef std::map<std::string, SQLfield> SQLfieldMap;
155 class SQLresult : public Request
163 SQLresult(Module* s, Module* d, unsigned long i)
164 : Request(SQLRESID, s, d), id(i)
168 /* Return the number of rows in the result */
169 virtual int Rows() = 0;
171 /* Return the number of columns in the result */
172 virtual int Cols() = 0;
174 /* Get a string name of the column by an index number */
175 virtual std::string ColName(int column) = 0;
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.
181 virtual int ColNum(const std::string &column) = 0;
183 /* Get a string value in a given row and column */
184 virtual SQLfield GetValue(int row, int column) = 0;
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.
195 virtual SQLfieldList& GetRow() = 0;
197 /* As above, but return a map indexed by key name */
198 virtual SQLfieldMap& GetRowMap() = 0;
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()
204 virtual SQLfieldList* GetRowPtr() = 0;
206 /* As above, but return a map indexed by key name */
207 virtual SQLfieldMap* GetRowMapPtr() = 0;
209 /* Overloaded function for freeing the lists and maps returned
212 virtual void Free(SQLfieldMap* fm) = 0;
213 virtual void Free(SQLfieldList* fl) = 0;