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 };
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)
93 /* If the field was null */
96 SQLfield(const std::string &data, bool n)
103 typedef std::vector<SQLfield> SQLfieldList;
104 typedef std::map<std::string, SQLfield> SQLfieldMap;
106 class SQLresult : public Request
113 SQLresult(Module* s, Module* d)
114 : Request(SQLRESID, s, d)
118 /* Return the number of rows in the result */
119 virtual int Rows() = 0;
121 /* Return the number of columns in the result */
122 virtual int Cols() = 0;
124 /* Get a string name of the column by an index number */
125 virtual std::string ColName(int column) = 0;
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.
131 virtual int ColNum(const std::string &column) = 0;
133 /* Get a string value in a given row and column */
134 virtual SQLfield GetValue(int row, int column) = 0;
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.
145 virtual SQLfieldList& GetRow() = 0;
147 /* As above, but return a map indexed by key name */
148 virtual SQLfieldMap& GetRowMap() = 0;
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()
154 virtual SQLfieldList* GetRowPtr() = 0;
156 /* As above, but return a map indexed by key name */
157 virtual SQLfieldMap* GetRowMapPtr() = 0;
159 /* Overloaded function for freeing the lists and maps returned
162 virtual void Free(SQLfieldMap* fm) = 0;
163 virtual void Free(SQLfieldList* fl) = 0;