# A library for MySQL that implements a simpler framework for MySQL Connector++ A small framework for basic MySQL database operations via MySQL/Connector++ ## Features - Object oriented - Active connection pool - Retries to connect - Native C++ containers: vector, tuple - Response object - Thread safe - Exceptions and log error callback - Support my Asynco wrapper around Boost ASIO and support threads ## Installation First install dependency MySQL/Connector++ ``` sudo apt install libmysqlcppconn-dev ``` If you are going to use with an Asynco wrapper, download the archive from the profile and install the Boost dependencies ``` sudo apt install libboost-all-dev ``` Just download the latest release and unzip it into your project. You can turn it on with: ``` #include "mysql/lib/mysql.hpp" using namespace marcelb; ``` Compiling ``` # use with my Asynco lib g++ -DMYSQL_USE_ASYNCO test.cpp ../src/* ../../asynco/src/* -o test.o -lmysqlcppconn -lpthread # or use without asnyco (in multithread) g++ test.cpp ../src/* ../../asynco/src/* -o test.o -lmysqlcppconn -lpthread ``` ## 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++ #include "../lib/mysql.hpp" using namespace marcelb::mysql; /** * Init */ 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 ------------------ */ | | ------------------ | try { | | | | // execute | | | | auto response = mydb.exec("SELECT id,domain FROM records WHERE enabled = 0;"); // response is MySQL_Res type // check is execute cout << response.affected << " " << response.have_result << endl; cout << response.rows << " " << response.columns << endl; for (auto row : response) { // row is tuple type cout << get<0>(row) << " " << get<1>(row) << endl; } // access columns name for (auto column_name : response.columns_name) { cout << column_name << endl; } } catch (const string err) { cout << err << endl; } ``` ### 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++ /** * You can call multiple queries asynchronously */ auto a1 = async_ ( [&mydb] () { try { auto response = mydb.exec("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 = async_ ( [&mydb] () { try { auto response = mydb.exec("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 = async_ ( [&mydb] () { try { auto response = mydb.exec("SELECT username,email FROM users WHERE enabled = 1;"); for (auto row : response) { cout << get<0>(row) << " " << get<1>(row) << endl; } } catch (const string err) { cout << err << endl; } }); await(a1); await(a2); await(a3); ``` ## License [APACHE 2.0](http://www.apache.org/licenses/LICENSE-2.0/) ## Support & Feedback For support and any feedback, contact the address: marcelb96@yahoo.com. ## Contributing Contributions are always welcome! Feel free to fork and start working with or without a later pull request. Or contact for suggest and request an option.