From 27074f92694cdc9d712cef5efd128d16b0af379f Mon Sep 17 00:00:00 2001 From: mbandic Date: Thu, 12 Sep 2024 13:42:33 +0000 Subject: [PATCH] Add on error callback --- .vscode/settings.json | 4 +++- lib/mysql.hpp | 6 ++++++ src/mysql.cpp | 38 +++++++++++++++++++++++++------------- test/test.cpp | 4 ++++ 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 7b6a465..20cccac 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -59,6 +59,8 @@ "stop_token": "cpp", "streambuf": "cpp", "cinttypes": "cpp", - "typeinfo": "cpp" + "typeinfo": "cpp", + "any": "cpp", + "variant": "cpp" } } \ No newline at end of file diff --git a/lib/mysql.hpp b/lib/mysql.hpp index 5e21ae8..04e8b1b 100644 --- a/lib/mysql.hpp +++ b/lib/mysql.hpp @@ -106,6 +106,7 @@ class MySQL { bool run_engin = true; future periodic_engin; periodical_engine engine_type; + function on_error; /** * Open one database server connection @@ -165,6 +166,11 @@ class MySQL { */ void set_connect_trys(const uint32_t _trys); + /** + * Set callback on error + */ + void set_on_error(function _on_error); + /** * Execute the SQL statement */ diff --git a/src/mysql.cpp b/src/mysql.cpp index fcf724b..d0e76b3 100644 --- a/src/mysql.cpp +++ b/src/mysql.cpp @@ -42,8 +42,9 @@ Connection* marcelb::mysql::MySQL::create_connection() { } } catch (const SQLException &error) { - cout << error.what() << endl; - // on_error -- ako se ikad impelementira pozovi ga ovdje! + if (on_error) { + on_error(error.what() + string(", SQL state: ") + error.getSQLState() + string(", Error code: ") + to_string(error.getErrorCode())); + } usleep(reconnectSleep); connect_trys == unlimited ? trys : trys++; } @@ -78,7 +79,9 @@ bool marcelb::mysql::MySQL::disconnect_connection(Connection* connection) { status = !connection->isClosed(); } catch (const SQLException &error) { - cout << error.what() << endl; + if (on_error) { + on_error(error.what() + string(", SQL state: ") + error.getSQLState() + string(", Error code: ") + to_string(error.getErrorCode())); + } status = true; } } @@ -101,6 +104,10 @@ void marcelb::mysql::MySQL::set_connect_trys(const uint32_t _trys) { connect_trys = _trys; } +void marcelb::mysql::MySQL::set_on_error(function _on_error) { + on_error = _on_error; +} + Connection* marcelb::mysql::MySQL::occupy_connection() { unique_lock lock(io); while (connection_pool.empty()) { @@ -136,8 +143,6 @@ marcelb::mysql::MySQL::~MySQL() { run_engin = false; periodic_engin.get(); } else { - // ne bi bilo loše ubiti periodic nekako?! - // iako disconnecta može periodic connect napraviti!!! run_engin = false; } @@ -147,15 +152,22 @@ marcelb::mysql::MySQL::~MySQL() { void marcelb::mysql::MySQL::periodic_maintenance() { for (size_t i = 0; i < pool_size && run_engin; i++) { - Connection *conn = occupy_connection(); - if (conn->isValid()) { - release_connection(conn); - } else { - if (!conn->isClosed()){ - conn->close(); + try { + + Connection *conn = occupy_connection(); + if (conn->isValid()) { + release_connection(conn); + } else { + if (!conn->isClosed()){ + conn->close(); + } + Connection *n_conn = create_connection(); + release_connection(n_conn); + } + } catch (const SQLException &error) { + if (on_error) { + on_error(error.what() + string(", SQL state: ") + error.getSQLState() + string(", Error code: ") + to_string(error.getErrorCode())); } - Connection *n_conn = create_connection(); - release_connection(n_conn); } } } \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp index 13466f7..4856e52 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -17,6 +17,10 @@ int main() { // MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 10, periodical_engine::external); MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5); + mydb.set_on_error( [](const string& error) { + cout << error << endl; + }); + // periodic mysql_maintenance ( [&mydb] () { // mydb.periodic_maintenance(); // }, MYSQL_PERIODIC_INTERNAL_TIME);