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::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 "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
161 SQLresult(Module* s, Module* d, unsigned long i)
162 : Request(SQLRESID, s, d), id(i)
166 /* Return the number of rows in the result */
167 virtual int Rows() = 0;
169 /* Return the number of columns in the result */
170 virtual int Cols() = 0;
172 /* Get a string name of the column by an index number */
173 virtual std::string ColName(int column) = 0;
175 /* Get an index number for a column from a string name.
176 * An exception of type SQLbadColName will be thrown if
177 * the name given is invalid.
179 virtual int ColNum(const std::string &column) = 0;
181 /* Get a string value in a given row and column */
182 virtual SQLfield GetValue(int row, int column) = 0;
184 /* Return a list of values in a row, this should
185 * increment an internal counter so you can repeatedly
186 * call it until it returns an empty vector.
187 * This returns a reference to an internal object,
188 * the same object is used for all calls to this function
189 * and therefore the return value is only valid until
190 * you call this function again. It is also invalid if
191 * the SQLresult object is destroyed.
193 virtual SQLfieldList& GetRow() = 0;
195 /* As above, but return a map indexed by key name */
196 virtual SQLfieldMap& GetRowMap() = 0;
198 /* Like GetRow(), but returns a pointer to a dynamically
199 * allocated object which must be explicitly freed. For
200 * portability reasons this must be freed with SQLresult::Free()
202 virtual SQLfieldList* GetRowPtr() = 0;
204 /* As above, but return a map indexed by key name */
205 virtual SQLfieldMap* GetRowMapPtr() = 0;
207 /* Overloaded function for freeing the lists and maps returned
210 virtual void Free(SQLfieldMap* fm) = 0;
211 virtual void Free(SQLfieldList* fl) = 0;