catalog: initial query module
Change-Id: I0ddd22c5774d5e5d54c1ee80e0f01566fed311fd
refs: #2638
diff --git a/catalog/src/util/mysql-util.cpp b/catalog/src/util/mysql-util.cpp
new file mode 100644
index 0000000..62225ba
--- /dev/null
+++ b/catalog/src/util/mysql-util.cpp
@@ -0,0 +1,70 @@
+/** NDN-Atmos: Cataloging Service for distributed data originally developed
+ * for atmospheric science data
+ * Copyright (C) 2015 Colorado State University
+ *
+ * NDN-Atmos is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * NDN-Atmos is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with NDN-Atmos. If not, see <http://www.gnu.org/licenses/>.
+**/
+
+#include "util/mysql-util.hpp"
+
+#include "mysql/errmsg.h"
+
+namespace atmos {
+namespace util {
+
+ConnectionDetails::ConnectionDetails(const std::string& serverInput, const std::string& userInput,
+ const std::string& passwordInput, const std::string& databaseInput)
+ : server(serverInput), user(userInput), password(passwordInput), database(databaseInput)
+{
+ // empty
+}
+
+
+std::shared_ptr<MYSQL>
+MySQLConnectionSetup(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);
+ 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);
+
+ switch (mysql_query(connection.get(), sql_query.c_str()))
+ {
+ case 0:
+ {
+ MYSQL_RES* resultPtr = mysql_store_result(connection.get());
+ if (resultPtr != NULL)
+ {
+ result.reset(resultPtr);
+ }
+ 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;
+ }
+ return result;
+}
+
+} // namespace util
+} // namespace atmos