DPP-MySQL
An extremely lightweight MySQL wrapper for D++ bots
|
Database abstraction layer Abstracts mysql C connector, supporting prepared statements and caching. More...
Classes | |
struct | resultset |
Definition of a result set. Supports iteration and accessing its rows via operator[] and at(). You can also insert new rows with emplace_back and push_back(). More... | |
Typedefs | |
using | row = std::map< std::string, std::string > |
Definition of a row in a result set. More... | |
using | sql_query_callback = std::function< void(const resultset &)> |
A callback which happens when an asynchronous SQL query is completed. More... | |
using | parameter_type = std::variant< float, std::string, uint64_t, int64_t, bool, int32_t, uint32_t, double > |
Possible parameter types for SQL parameters. More... | |
using | paramlist = std::vector< parameter_type > |
A list of database query parameters. These will be translated into prepared statement arguments. More... | |
Functions | |
void | init (dpp::cluster &bot) |
Initialise database connection. More... | |
bool | connect (const std::string &host, const std::string &user, const std::string &pass, const std::string &db, int port=3306, const std::string &socket="") |
Connect to database and set options. More... | |
bool | close () |
Disconnect from database and free query cache. More... | |
resultset | query (const std::string &format, const paramlist ¶meters={}) |
Run a mysql query, with automatic escaping of parameters to prevent SQL injection. More... | |
void | query_callback (const std::string &format, const paramlist ¶meters, const sql_query_callback &cb) |
Run a mysql query asynchronously, with automatic escaping of parameters to prevent SQL injection. Call the callback on completion. More... | |
dpp::async< resultset > | co_query (const std::string &format, const paramlist ¶meters={}) |
Run a mysql query, with automatic escaping of parameters to prevent SQL injection. This is the coroutine asynchronous version of db::query. It will complete its dpp::async once the query has finished without blocking, by means of a separate thread. More... | |
resultset | query (const std::string &format, const paramlist ¶meters, double lifetime) |
Run a mysql query, with automatic escaping of parameters to prevent SQL injection. More... | |
size_t | affected_rows () |
Returns number of affected rows from an UPDATE, INSERT, DELETE. More... | |
const std::string & | error () |
Returns the last error string. More... | |
size_t | cache_size () |
Returns the size of the query cache. More... | |
size_t | query_count () |
Returns total number of queries executed since connection was established. More... | |
void | transaction (std::function< bool()> closure, sql_query_callback callback={}) |
Start an SQL transaction. SQL transactions are atomic in nature, ALL other queries will be forced to wait. The transaction will be inserted into the queue to run as one atomic operation, meaning that db::co_query cannot disrupt it or insert queries in the middle of it, and db::query function calls that are not within the closure will be forced to wait. More... | |
dpp::async< resultset > | co_transaction (std::function< bool()> closure) |
Start an SQL transaction in a coroutine that can be awaited. SQL transactions are atomic in nature, ALL other queries will be forced to wait. The transaction will be inserted into the queue to run as one atomic operation, meaning that db::co_query cannot disrupt it or insert queries in the middle of it, and db::query function calls that are not within the closure will be forced to wait. More... | |
Database abstraction layer Abstracts mysql C connector, supporting prepared statements and caching.
using db::parameter_type = typedef std::variant<float, std::string, uint64_t, int64_t, bool, int32_t, uint32_t, double> |
Possible parameter types for SQL parameters.
using db::paramlist = typedef std::vector<parameter_type> |
A list of database query parameters. These will be translated into prepared statement arguments.
using db::row = typedef std::map<std::string, std::string> |
Definition of a row in a result set.
using db::sql_query_callback = typedef std::function<void(const resultset&)> |
A callback which happens when an asynchronous SQL query is completed.
size_t db::affected_rows | ( | ) |
Returns number of affected rows from an UPDATE, INSERT, DELETE.
size_t db::cache_size | ( | ) |
Returns the size of the query cache.
Prepared statement handles are stored in a std::map along with their metadata, so that they don't have to be re-prepared if they are executed repeatedly. This is a diagnostic and informational function which returns the size of that map.
bool db::close | ( | ) |
Disconnect from database and free query cache.
dpp::async<resultset> db::co_query | ( | const std::string & | format, |
const paramlist & | parameters = {} |
||
) |
Run a mysql query, with automatic escaping of parameters to prevent SQL injection. This is the coroutine asynchronous version of db::query. It will complete its dpp::async once the query has finished without blocking, by means of a separate thread.
format | Format string, where each parameter should be indicated by a ? symbol |
parameters | Parameters to prepare into the query in place of the ?'s |
The parameters given should be a vector of strings. You can instantiate this using "{}". The queries are cached as prepared statements and therefore do not need quote symbols to be placed around parameters in the query. These will be automatically added if required.
For example:
dpp::async<resultset> db::co_transaction | ( | std::function< bool()> | closure | ) |
Start an SQL transaction in a coroutine that can be awaited. SQL transactions are atomic in nature, ALL other queries will be forced to wait. The transaction will be inserted into the queue to run as one atomic operation, meaning that db::co_query cannot disrupt it or insert queries in the middle of it, and db::query function calls that are not within the closure will be forced to wait.
closure | The transactional code to execute. |
bool db::connect | ( | const std::string & | host, |
const std::string & | user, | ||
const std::string & | pass, | ||
const std::string & | db, | ||
int | port = 3306 , |
||
const std::string & | socket = "" |
||
) |
Connect to database and set options.
host | Database hostname |
user | Database username |
pass | Database password |
db | Database schema name |
port | Database port number |
socket | unix socket path |
localhost
. This is a special value in the mysql client and causes a unix socket connection to occur. If you do not want to use unix sockets, keep the value as an empty string. const std::string& db::error | ( | ) |
Returns the last error string.
void db::init | ( | dpp::cluster & | bot | ) |
Initialise database connection.
bot | creating D++ cluster |
Run a mysql query, with automatic escaping of parameters to prevent SQL injection.
format | Format string, where each parameter should be indicated by a ? symbol |
parameters | Parameters to prepare into the query in place of the ?'s |
lifetime | How long to cache this query's resultset in memory for |
The parameters given should be a vector of strings. You can instantiate this using "{}". The queries are cached as prepared statements and therefore do not need quote symbols to be placed around parameters in the query. These will be automatically added if required.
Run a mysql query, with automatic escaping of parameters to prevent SQL injection.
format | Format string, where each parameter should be indicated by a ? symbol |
parameters | Parameters to prepare into the query in place of the ?'s |
The parameters given should be a vector of strings. You can instantiate this using "{}". The queries are cached as prepared statements and therefore do not need quote symbols to be placed around parameters in the query. These will be automatically added if required.
For example:
void db::query_callback | ( | const std::string & | format, |
const paramlist & | parameters, | ||
const sql_query_callback & | cb | ||
) |
Run a mysql query asynchronously, with automatic escaping of parameters to prevent SQL injection. Call the callback on completion.
format | Format string, where each parameter should be indicated by a ? symbol |
parameters | Parameters to prepare into the query in place of the ?'s |
cb | Callback to call on completion of the query. The callback will be passed the resultset as its parameter. |
The parameters given should be a vector of strings. You can instantiate this using "{}". The queries are cached as prepared statements and therefore do not need quote symbols to be placed around parameters in the query. These will be automatically added if required.
size_t db::query_count | ( | ) |
Returns total number of queries executed since connection was established.
void db::transaction | ( | std::function< bool()> | closure, |
sql_query_callback | callback = {} |
||
) |
Start an SQL transaction. SQL transactions are atomic in nature, ALL other queries will be forced to wait. The transaction will be inserted into the queue to run as one atomic operation, meaning that db::co_query cannot disrupt it or insert queries in the middle of it, and db::query function calls that are not within the closure will be forced to wait.
closure | The transactional code to execute. |
callback | Callback to call when the transaction completes |