catalog: Implement Sync Update Executor
Change-Id: I88a0c6935d7b8094dfb587e83270a607e753ea31
refs: #2611
diff --git a/catalog/src/util/mysql-util.cpp b/catalog/src/util/mysql-util.cpp
index fb58665..9df8b8e 100644
--- a/catalog/src/util/mysql-util.cpp
+++ b/catalog/src/util/mysql-util.cpp
@@ -19,6 +19,7 @@
#include "util/mysql-util.hpp"
#include <mysql/errmsg.h>
#include <stdexcept>
+#include <iostream>
namespace atmos {
namespace util {
@@ -32,10 +33,13 @@
std::shared_ptr<MYSQL>
-MySQLConnectionSetup(const ConnectionDetails& details) {
+MySQLConnectionSetup(const ConnectionDetails& details)
+{
MYSQL* conn = mysql_init(NULL);
+ my_bool reconnect = 1;
+ mysql_options(conn, MYSQL_OPT_RECONNECT, &reconnect);
if(!mysql_real_connect(conn, details.server.c_str(), details.user.c_str(),
- details.password.c_str(), details.database.c_str(), 0, NULL, 0)) {
+ details.password.c_str(), details.database.c_str(), 0, NULL, 0)) {
throw std::runtime_error(mysql_error(conn));
}
std::shared_ptr<MYSQL> connection(conn, &mysql_close);
@@ -43,25 +47,37 @@
}
std::shared_ptr<MYSQL_RES>
-MySQLPerformQuery(std::shared_ptr<MYSQL> connection, const std::string& sql_query) {
+MySQLPerformQuery(std::shared_ptr<MYSQL> connection,
+ const std::string& sql_query,
+ DatabaseOperation op,
+ bool& success,
+ std::string& errMsg)
+{
switch (mysql_query(connection.get(), sql_query.c_str()))
{
- case 0:
- {
+ case 0:
+ {
+ success = true;
+ if (op == QUERY) {
MYSQL_RES* resultPtr = mysql_store_result(connection.get());
if (resultPtr != NULL)
{
return std::shared_ptr<MYSQL_RES>(resultPtr, &mysql_free_result);
}
- break;
}
- // Various error cases
- case CR_COMMANDS_OUT_OF_SYNC:
- case CR_SERVER_GONE_ERROR:
- case CR_SERVER_LOST:
- case CR_UNKNOWN_ERROR:
- default:
- break;
+ //for add, remove, we don't need the results, may be log the events
+ break;
+ }
+ // Various error cases
+ case CR_COMMANDS_OUT_OF_SYNC:
+ case CR_SERVER_GONE_ERROR:
+ case CR_SERVER_LOST:
+ case CR_UNKNOWN_ERROR:
+ default:
+ {
+ errMsg.assign(mysql_error(connection.get()));
+ break;
+ }
}
return nullptr;
}
diff --git a/catalog/src/util/mysql-util.hpp b/catalog/src/util/mysql-util.hpp
index ee45f97..75e5997 100644
--- a/catalog/src/util/mysql-util.hpp
+++ b/catalog/src/util/mysql-util.hpp
@@ -26,6 +26,7 @@
namespace atmos {
namespace util {
+enum DatabaseOperation {CREATE, UPDATE, ADD, REMOVE, QUERY};
struct ConnectionDetails {
public:
std::string server;
@@ -41,7 +42,11 @@
MySQLConnectionSetup(const ConnectionDetails& details);
std::shared_ptr<MYSQL_RES>
-MySQLPerformQuery(std::shared_ptr<MYSQL> connection, const std::string& sql_query);
+MySQLPerformQuery(std::shared_ptr<MYSQL> connection,
+ const std::string& sql_query,
+ DatabaseOperation op,
+ bool& success,
+ std::string& errMsg);
} // namespace util
} // namespace atmos