blob: 9df8b8e061edd2855a4cbc4b70a7bbf2033998a6 [file] [log] [blame]
Alison Craig2a4d5282015-04-10 12:00:02 -06001/** NDN-Atmos: Cataloging Service for distributed data originally developed
2 * for atmospheric science data
3 * Copyright (C) 2015 Colorado State University
4 *
5 * NDN-Atmos is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * NDN-Atmos is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NDN-Atmos. If not, see <http://www.gnu.org/licenses/>.
17**/
18
19#include "util/mysql-util.hpp"
Chengyu Fanb25835b2015-04-28 17:09:35 -060020#include <mysql/errmsg.h>
21#include <stdexcept>
Chengyu Fan46398212015-08-11 11:23:13 -060022#include <iostream>
Alison Craig2a4d5282015-04-10 12:00:02 -060023
24namespace atmos {
25namespace util {
26
27ConnectionDetails::ConnectionDetails(const std::string& serverInput, const std::string& userInput,
28 const std::string& passwordInput, const std::string& databaseInput)
29 : server(serverInput), user(userInput), password(passwordInput), database(databaseInput)
30{
31 // empty
32}
33
34
35std::shared_ptr<MYSQL>
Chengyu Fan46398212015-08-11 11:23:13 -060036MySQLConnectionSetup(const ConnectionDetails& details)
37{
Alison Craig2a4d5282015-04-10 12:00:02 -060038 MYSQL* conn = mysql_init(NULL);
Chengyu Fan46398212015-08-11 11:23:13 -060039 my_bool reconnect = 1;
40 mysql_options(conn, MYSQL_OPT_RECONNECT, &reconnect);
Chengyu Fanb25835b2015-04-28 17:09:35 -060041 if(!mysql_real_connect(conn, details.server.c_str(), details.user.c_str(),
Chengyu Fan46398212015-08-11 11:23:13 -060042 details.password.c_str(), details.database.c_str(), 0, NULL, 0)) {
Chengyu Fanb25835b2015-04-28 17:09:35 -060043 throw std::runtime_error(mysql_error(conn));
44 }
45 std::shared_ptr<MYSQL> connection(conn, &mysql_close);
Alison Craig2a4d5282015-04-10 12:00:02 -060046 return connection;
47}
48
49std::shared_ptr<MYSQL_RES>
Chengyu Fan46398212015-08-11 11:23:13 -060050MySQLPerformQuery(std::shared_ptr<MYSQL> connection,
51 const std::string& sql_query,
52 DatabaseOperation op,
53 bool& success,
54 std::string& errMsg)
55{
Alison Craig2a4d5282015-04-10 12:00:02 -060056 switch (mysql_query(connection.get(), sql_query.c_str()))
57 {
Chengyu Fan46398212015-08-11 11:23:13 -060058 case 0:
59 {
60 success = true;
61 if (op == QUERY) {
Alison Craig2a4d5282015-04-10 12:00:02 -060062 MYSQL_RES* resultPtr = mysql_store_result(connection.get());
63 if (resultPtr != NULL)
64 {
Chengyu Fanb25835b2015-04-28 17:09:35 -060065 return std::shared_ptr<MYSQL_RES>(resultPtr, &mysql_free_result);
Alison Craig2a4d5282015-04-10 12:00:02 -060066 }
Alison Craig2a4d5282015-04-10 12:00:02 -060067 }
Chengyu Fan46398212015-08-11 11:23:13 -060068 //for add, remove, we don't need the results, may be log the events
69 break;
70 }
71 // Various error cases
72 case CR_COMMANDS_OUT_OF_SYNC:
73 case CR_SERVER_GONE_ERROR:
74 case CR_SERVER_LOST:
75 case CR_UNKNOWN_ERROR:
76 default:
77 {
78 errMsg.assign(mysql_error(connection.get()));
79 break;
80 }
Alison Craig2a4d5282015-04-10 12:00:02 -060081 }
Chengyu Fanb25835b2015-04-28 17:09:35 -060082 return nullptr;
Alison Craig2a4d5282015-04-10 12:00:02 -060083}
84
85} // namespace util
86} // namespace atmos