MySQL mydb("tcp://192.168.2.10:3306", "user_nm", "passss", "my_db", 5);
mydb.on_error = [](const string& error) {
cout <<error<<endl;
};
mydb.on_connect = []() {
cout << "Init all pool connection done" <<endl;
};
/**
* Use ------------------
*/ | |
@ -72,34 +93,16 @@ try { | | | |
}
```
### External engine
### Run async with Asynco
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, time_loop_type::external);
periodic mysql_maintenance ( [&mydb] () {
mydb.periodic_maintenance();
}, MYSQL_PERIODIC_INTERNAL_TIME);
mydb.set_on_error( [](const string& error) {
cout <<error<<endl;//printorlog
});
/**
* You can call multiple queries asynchronously
*/
auto a1 = atask ( [&mydb] () {
auto a1 = async_ ( [&mydb] () {
try {
auto response = mydb.exec<int,string>("SELECT id,domain FROM records WHERE enabled = 1;");
for (auto row : response) {
@ -110,7 +113,7 @@ auto a1 = atask ( [&mydb] () {
}
});
auto a2 = atask ( [&mydb] () {
auto a2 = async_ ( [&mydb] () {
try {
auto response = mydb.exec<string,string>("SELECT zonename,auth_key FROM zones;");
for (auto row : response) {
@ -121,7 +124,7 @@ auto a2 = atask ( [&mydb] () {
}
});
auto a3 = atask ( [&mydb] () {
auto a3 = async_ ( [&mydb] () {
try {
auto response = mydb.exec<string,string>("SELECT username,email FROM users WHERE enabled = 1;");