1 #ifndef INSPIRCD_SQLAPI_2
2 #define INSPIRCD_SQLAPI_2
4 #define SQLREQID "SQLv2 Request"
5 #define SQLRESID "SQLv2 Result"
6 #define SQLSUCCESS "You shouldn't be reading this (success)"
13 enum SQLerrorNum { NO_ERROR, BAD_DBID, BAD_CONN, QSEND_FAIL };
15 class SQLexception : public ModuleException
19 class SQLbadColName : public SQLexception
25 class SQLerror : public classbase
30 SQLerror(SQLerrorNum i = NO_ERROR, const std::string &s = "")
40 SQLerrorNum Id(SQLerrorNum i)
46 void Str(const std::string &s)
61 return "Invalid database ID";
63 return "Invalid connection";
65 return "Sending query failed";
67 return "Unknown error";
72 class SQLrequest : public Request
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)
86 void SetSource(Module* mod)
98 /* If the field was null */
101 SQLfield(const std::string &data, bool n)
108 typedef std::vector<SQLfield> SQLfieldList;
109 typedef std::map<std::string, SQLfield> SQLfieldMap;
111 class SQLresult : public Request
118 SQLresult(Module* s, Module* d)
119 : Request(SQLRESID, s, d)
123 /* Return the number of rows in the result */
124 virtual int Rows() = 0;
126 /* Return the number of columns in the result */
127 virtual int Cols() = 0;
129 /* Get a string name of the column by an index number */
130 virtual std::string ColName(int column) = 0;
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.
136 virtual int ColNum(const std::string &column) = 0;
138 /* Get a string value in a given row and column */
139 virtual SQLfield GetValue(int row, int column) = 0;
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.
150 virtual SQLfieldList& GetRow() = 0;
152 /* As above, but return a map indexed by key name */
153 virtual SQLfieldMap& GetRowMap() = 0;
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()
159 virtual SQLfieldList* GetRowPtr() = 0;
161 /* As above, but return a map indexed by key name */
162 virtual SQLfieldMap* GetRowMapPtr() = 0;
164 /* Overloaded function for freeing the lists and maps returned
167 virtual void Free(SQLfieldMap* fm) = 0;
168 virtual void Free(SQLfieldList* fl) = 0;