@ -13,6 +13,7 @@ A small framework for basic MySQL database operations via MySQL/Connector++
- Response object
- Response object
- Thread safe
- Thread safe
- Exceptions
- Exceptions
- Can use external periodic maintenance for connection management
## Installation
## Installation
@ -31,7 +32,15 @@ using namespace marcelb;
## Usage
## Usage
### Internal engine
It internally initializes a single thread that periodically checks the states of the connection pool, adds new ones as needed, and cleans up inactive ones.
```c++
```c++
#include "../lib/mysql.hpp"
using namespace marcelb::mysql;
/**
/**
* Init
* Init
*/
*/
@ -62,6 +71,69 @@ try { | | | |
cout <<err<<endl;
cout <<err<<endl;
}
}
```
```
### External engine
As I developed quite a few wrappers that have some internal thread, I realized that it was inefficient and made it possible to call the necessary functions periodically outside (one thread per whole application or timer (ASIO), or my asynco wrapper).
```c++
#include "../lib/mysql.hpp"
using namespace marcelb::mysql;
#include "../../asynco/lib/timers.hpp"
using namespace marcelb::asynco;
/**
* Init
*/
MySQL mydb("tcp://192.168.2.10:3306", "user_nm", "passss", "my_db", 5, periodical_engine::external);
periodic mysql_maintenance ( [&mydb] () {
cout << "IZVRŠAVA SE ENGINE" <<endl;
mydb.periodic_maintenance();
}, MYSQL_PERIODIC_INTERNAL_TIME);
/**
* You can call multiple queries asynchronously
*/
auto a1 = atask ( [&mydb] () {
try {
auto response = mydb.exec<int,string>("SELECT id,domain FROM records WHERE enabled = 1;");
for (auto row : response) {
cout <<get<0>(row) << " " <<get<1>(row) <<endl;
}
} catch (const string err) {
cout <<err<<endl;
}
});
auto a2 = atask ( [&mydb] () {
try {
auto response = mydb.exec<string,string>("SELECT zonename,auth_key FROM zones;");
for (auto row : response) {
cout <<get<0>(row) << " " <<get<1>(row) <<endl;
}
} catch (const string err) {
cout <<err<<endl;
}
});
auto a3 = atask ( [&mydb] () {
try {
auto response = mydb.exec<string,string>("SELECT username,email FROM users WHERE enabled = 1;");