MXS-2544 Add utility class for handling SQLite
Preparation and cleanup for adding more data to handle user roles in PAM authenticator.
This commit is contained in:
@ -37,3 +37,77 @@ extern const int NUM_FIELDS;
|
||||
|
||||
extern const char* SQLITE_OPEN_FAIL;
|
||||
extern const char* SQLITE_OPEN_OOM;
|
||||
|
||||
struct sqlite3;
|
||||
|
||||
class SQLite;
|
||||
|
||||
/**
|
||||
* Convenience class for working with SQLite.
|
||||
*/
|
||||
class SQLite
|
||||
{
|
||||
public:
|
||||
SQLite(const SQLite& rhs) = delete;
|
||||
SQLite& operator=(const SQLite& rhs) = delete;
|
||||
|
||||
using SSQLite = std::unique_ptr<SQLite>;
|
||||
|
||||
template<class T>
|
||||
using Callback = int (*)(T* data, int n_columns, char** rows, char** field_names);
|
||||
|
||||
/**
|
||||
* Create a new database handle.
|
||||
*
|
||||
* @param filename The filename/url given to sqlite3_open_v2
|
||||
* @param flags Flags given to sqlite3_open_v2
|
||||
* @return New handle if successful, null otherwise.
|
||||
*/
|
||||
static SSQLite create(const std::string& filename, int flags, std::string* error_out);
|
||||
~SQLite();
|
||||
|
||||
/**
|
||||
* Run a simple query which returns no data.
|
||||
*
|
||||
* @param sql SQL to run
|
||||
* @return True on success
|
||||
*/
|
||||
bool exec(const std::string& sql);
|
||||
|
||||
/**
|
||||
* Run a query which may return data.
|
||||
*
|
||||
* @param sql SQL to run
|
||||
* @param cb Callback given to sqlite3_exec
|
||||
* @param cb_data Data pointer given to sqlite3_exec
|
||||
* @return True on success
|
||||
*/
|
||||
template <class T>
|
||||
bool exec(const std::string& sql, Callback<T> cb, T* cb_data)
|
||||
{
|
||||
return exec_impl(sql, reinterpret_cast<CallbackVoid>(cb), cb_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls sqlite3_busy_timeout.
|
||||
*
|
||||
* @param ms The timeout in ms
|
||||
*/
|
||||
void set_timeout(int ms);
|
||||
|
||||
/**
|
||||
* Get latest error.
|
||||
*
|
||||
* @return Error string
|
||||
*/
|
||||
const char* error() const;
|
||||
|
||||
private:
|
||||
using CallbackVoid = int (*)(void* data, int n_columns, char** rows, char** field_names);
|
||||
bool exec_impl(const std::string& sql, CallbackVoid cb, void* cb_data);
|
||||
|
||||
SQLite(sqlite3* handle);
|
||||
|
||||
sqlite3* m_dbhandle {nullptr};
|
||||
std::string m_errormsg;
|
||||
};
|
||||
|
Reference in New Issue
Block a user