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_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 interfacebase
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
130 const std::string dbid;
131 const std::string query;
133 SQLQuery(Module* Creator, const std::string& db, const std::string& q)
134 : creator(Creator), dbid(db), query(q) {}
135 virtual ~SQLQuery() {}
137 virtual void OnResult(SQLResult& result) = 0;
139 * Called when the query fails
141 virtual void OnError(SQLerror& error) { }
145 * Provider object for SQL servers
147 class SQLProvider : public DataProvider
150 SQLProvider(Module* Creator, const std::string& Name) : DataProvider(Creator, Name) {}
151 /** Submit an asynchronous SQL request
152 * @param dbid The database ID to apply the request to
153 * @param query The query string
154 * @param callback The callback that the result is sent to
156 virtual void submit(SQLQuery* query) = 0;
158 /** Format a parameterized query string using proper SQL escaping.
159 * @param q The query string, with '?' parameters
160 * @param p The parameters to fill in in the '?' slots
162 virtual std::string FormatQuery(const std::string& q, const ParamL& p) = 0;
164 /** Format a parameterized query string using proper SQL escaping.
165 * @param q The query string, with '$foo' parameters
166 * @param p The map to look up parameters in
168 virtual std::string FormatQuery(const std::string& q, const ParamM& p) = 0;
170 /** Convenience function */
171 void PopulateUserInfo(User* user, ParamM& userinfo)
173 userinfo["nick"] = user->nick;
174 userinfo["host"] = user->host;
175 userinfo["ip"] = user->GetIPString();
176 userinfo["gecos"] = user->fullname;
177 userinfo["ident"] = user->ident;
178 userinfo["server"] = user->server;
179 userinfo["uuid"] = user->uuid;