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 };
19 typedef std::vector<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 "Unknown error";
84 SQLquery(const std::string &query)
87 log(DEBUG, "SQLquery constructor: %s", q.c_str());
90 SQLquery(const std::string &query, const ParamL ¶ms)
93 log(DEBUG, "SQLquery constructor with %d params: %s", p.size(), q.c_str());
96 SQLquery& operator,(const std::string &foo)
102 SQLquery& operator%(const std::string &foo)
109 class SQLrequest : public Request
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)
123 void Priority(bool p = true)
128 void SetSource(Module* mod)
137 /* The data itself */
140 /* If the field was null */
143 SQLfield(const std::string &data, bool n)
150 typedef std::vector<SQLfield> SQLfieldList;
151 typedef std::map<std::string, SQLfield> SQLfieldMap;
153 class SQLresult : public Request
160 SQLresult(Module* s, Module* d)
161 : Request(SQLRESID, s, d)
165 /* Return the number of rows in the result */
166 virtual int Rows() = 0;
168 /* Return the number of columns in the result */
169 virtual int Cols() = 0;
171 /* Get a string name of the column by an index number */
172 virtual std::string ColName(int column) = 0;
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.
178 virtual int ColNum(const std::string &column) = 0;
180 /* Get a string value in a given row and column */
181 virtual SQLfield GetValue(int row, int column) = 0;
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.
192 virtual SQLfieldList& GetRow() = 0;
194 /* As above, but return a map indexed by key name */
195 virtual SQLfieldMap& GetRowMap() = 0;
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()
201 virtual SQLfieldList* GetRowPtr() = 0;
203 /* As above, but return a map indexed by key name */
204 virtual SQLfieldMap* GetRowMapPtr() = 0;
206 /* Overloaded function for freeing the lists and maps returned
209 virtual void Free(SQLfieldMap* fm) = 0;
210 virtual void Free(SQLfieldList* fl) = 0;