1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef INSPIRCD_SQLAPI_3
15 #define INSPIRCD_SQLAPI_3
17 /** Defines the error types which SQLerror may be set to
19 enum SQLerrorNum { SQL_NO_ERROR, SQL_BAD_DBID, SQL_BAD_CONN, SQL_QSEND_FAIL, SQL_QREPLY_FAIL };
21 /** A list of format parameters for an SQLquery object.
23 typedef std::vector<std::string> ParamL;
25 typedef std::map<std::string, std::string> ParamM;
32 SQLEntry() : nul(true) {}
33 SQLEntry(const std::string& v) : value(v), nul(false) {}
34 inline operator std::string&() { return value; }
37 typedef std::vector<SQLEntry> SQLEntries;
40 * Result of an SQL query. Only valid inside OnResult
42 class SQLResult : public classbase
46 * Return the number of rows in the result.
48 * Note that if you have perfomed an INSERT or UPDATE query or other
49 * query which will not return rows, this will return the number of
50 * affected rows. In this case you SHOULD NEVER access any of the result
51 * set rows, as there aren't any!
52 * @returns Number of rows in the result set.
54 virtual int Rows() = 0;
57 * Return a single row (result of the query). The internal row counter
58 * is incremented by one.
60 * @param result Storage for the result data.
61 * @returns true if there was a row, false if no row exists (end of
64 virtual bool GetRow(SQLEntries& result) = 0;
66 /** Returns column names for the items in this row
68 virtual void GetCols(std::vector<std::string>& result) = 0;
71 /** SQLerror holds the error state of a request.
72 * The error string varies from database software to database software
73 * and should be used to display informational error messages to users.
86 /** Initialize an SQLerror
87 * @param i The error ID to set
88 * @param s The (optional) error string to set
90 SQLerror(SQLerrorNum i, const std::string &s = "")
95 /** Return the error string for an error
105 return "Invalid database ID";
107 return "Invalid connection";
109 return "Sending query failed";
110 case SQL_QREPLY_FAIL:
111 return "Getting query result failed";
113 return "Unknown error";
119 * Object representing an SQL query. This should be allocated on the heap and
120 * passed to an SQLProvider, which will free it when the query is complete or
121 * when the querying module is unloaded.
123 * You should store whatever information is needed to have the callbacks work in
124 * this object (UID of user, channel name, etc).
126 class SQLQuery : public classbase
131 SQLQuery(Module* Creator) : creator(Creator) {}
132 virtual ~SQLQuery() {}
134 virtual void OnResult(SQLResult& result) = 0;
136 * Called when the query fails
138 virtual void OnError(SQLerror& error) { }
142 * Provider object for SQL servers
144 class SQLProvider : public DataProvider
147 SQLProvider(Module* Creator, const std::string& Name) : DataProvider(Creator, Name) {}
148 /** Submit an asynchronous SQL request
149 * @param callback The result reporting point
150 * @param query The hardcoded query string. If you have parameters to substitute, see below.
152 virtual void submit(SQLQuery* callback, const std::string& query) = 0;
154 /** Submit an asynchronous SQL request
155 * @param callback The result reporting point
156 * @param format The simple parameterized query string ('?' parameters)
157 * @param p Parameters to fill in for the '?' entries
159 virtual void submit(SQLQuery* callback, const std::string& format, const ParamL& p) = 0;
161 /** Submit an asynchronous SQL request.
162 * @param callback The result reporting point
163 * @param format The parameterized query string ('$name' parameters)
164 * @param p Parameters to fill in for the '$name' entries
166 virtual void submit(SQLQuery* callback, const std::string& format, const ParamM& p) = 0;
168 /** Convenience function to prepare a map from a User* */
169 void PopulateUserInfo(User* user, ParamM& userinfo)
171 userinfo["nick"] = user->nick;
172 userinfo["host"] = user->host;
173 userinfo["ip"] = user->GetIPString();
174 userinfo["gecos"] = user->fullname;
175 userinfo["ident"] = user->ident;
176 userinfo["server"] = user->server;
177 userinfo["uuid"] = user->uuid;