catalog: implement catalog driver and facade
This commit also refactories the QueryAdapter's code, adds corresponding
unit-test. Catalog-adapter and catalog do not use template, so the definition
are moved to corresponding cpp files.
refs: #2599, #2600
Change-Id: I2be492ec3c2538e865bfa7c09ac8cd49e2a9527d
diff --git a/catalog/src/util/mysql-util.cpp b/catalog/src/util/mysql-util.cpp
index 62225ba..fb58665 100644
--- a/catalog/src/util/mysql-util.cpp
+++ b/catalog/src/util/mysql-util.cpp
@@ -17,8 +17,8 @@
**/
#include "util/mysql-util.hpp"
-
-#include "mysql/errmsg.h"
+#include <mysql/errmsg.h>
+#include <stdexcept>
namespace atmos {
namespace util {
@@ -32,18 +32,18 @@
std::shared_ptr<MYSQL>
-MySQLConnectionSetup(ConnectionDetails& details) {
+MySQLConnectionSetup(const ConnectionDetails& details) {
MYSQL* conn = mysql_init(NULL);
- mysql_real_connect(conn, details.server.c_str(), details.user.c_str(),
- details.password.c_str(), details.database.c_str(), 0, NULL, 0);
- std::shared_ptr<MYSQL> connection(conn);
+ if(!mysql_real_connect(conn, details.server.c_str(), details.user.c_str(),
+ 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);
return connection;
}
std::shared_ptr<MYSQL_RES>
-PerformQuery(std::shared_ptr<MYSQL> connection, const std::string& sql_query) {
- std::shared_ptr<MYSQL_RES> result(NULL);
-
+MySQLPerformQuery(std::shared_ptr<MYSQL> connection, const std::string& sql_query) {
switch (mysql_query(connection.get(), sql_query.c_str()))
{
case 0:
@@ -51,7 +51,7 @@
MYSQL_RES* resultPtr = mysql_store_result(connection.get());
if (resultPtr != NULL)
{
- result.reset(resultPtr);
+ return std::shared_ptr<MYSQL_RES>(resultPtr, &mysql_free_result);
}
break;
}
@@ -63,7 +63,7 @@
default:
break;
}
- return result;
+ return nullptr;
}
} // namespace util