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;
28 * Result of an SQL query. Only valid inside OnResult
30 class SQLResult : public interfacebase
34 * Return the number of rows in the result.
36 * Note that if you have perfomed an INSERT or UPDATE query or other
37 * query which will not return rows, this will return the number of
38 * affected rows. In this case you SHOULD NEVER access any of the result
39 * set rows, as there aren't any!
40 * @returns Number of rows in the result set.
42 virtual int Rows() = 0;
45 * Return a single row (result of the query). The internal row counter
46 * is incremented by one.
48 * @param result Storage for the result data.
49 * @returns true if there was a row, false if no row exists (end of
52 virtual bool GetRow(std::vector<std::string>& result) = 0;
55 /** SQLerror holds the error state of a request.
56 * The error string varies from database software to database software
57 * and should be used to display informational error messages to users.
70 /** Initialize an SQLerror
71 * @param i The error ID to set
72 * @param s The (optional) error string to set
74 SQLerror(SQLerrorNum i, const std::string &s = "")
79 /** Return the error string for an error
89 return "Invalid database ID";
91 return "Invalid connection";
93 return "Sending query failed";
95 return "Getting query result failed";
97 return "Unknown error";
103 * Object representing an SQL query. This should be allocated on the heap and
104 * passed to an SQLProvider, which will free it when the query is complete or
105 * when the querying module is unloaded.
107 * You should store whatever information is needed to have the callbacks work in
108 * this object (UID of user, channel name, etc).
110 class SQLQuery : public classbase
114 const std::string dbid;
115 const std::string query;
117 SQLQuery(Module* Creator, const std::string& db, const std::string& q)
118 : creator(Creator), dbid(db), query(q) {}
119 virtual ~SQLQuery() {}
121 virtual void OnResult(SQLResult& result) = 0;
123 * Called when the query fails
125 virtual void OnError(SQLerror& error) { }
129 * Provider object for SQL servers
131 class SQLProvider : public DataProvider
134 SQLProvider(Module* Creator, const std::string& Name) : DataProvider(Creator, Name) {}
135 /** Submit an asynchronous SQL request
136 * @param dbid The database ID to apply the request to
137 * @param query The query string
138 * @param callback The callback that the result is sent to
140 virtual void submit(SQLQuery* query) = 0;
142 /** Format a parameterized query string using proper SQL escaping.
143 * @param q The query string, with '?' parameters
144 * @param p The parameters to fill in in the '?' slots
146 virtual std::string FormatQuery(std::string q, ParamL p) = 0;
148 /** Format a parameterized query string using proper SQL escaping.
149 * @param q The query string, with '$foo' parameters
150 * @param p The map to look up parameters in
152 virtual std::string FormatQuery(std::string q, ParamM p) = 0;