Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 1 | /** 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 Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 20 | #include <mysql/errmsg.h> |
| 21 | #include <stdexcept> |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 22 | #include <iostream> |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 23 | |
| 24 | namespace atmos { |
| 25 | namespace util { |
| 26 | |
| 27 | ConnectionDetails::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 | |
| 35 | std::shared_ptr<MYSQL> |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 36 | MySQLConnectionSetup(const ConnectionDetails& details) |
| 37 | { |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 38 | MYSQL* conn = mysql_init(NULL); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 39 | my_bool reconnect = 1; |
| 40 | mysql_options(conn, MYSQL_OPT_RECONNECT, &reconnect); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 41 | if(!mysql_real_connect(conn, details.server.c_str(), details.user.c_str(), |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 42 | details.password.c_str(), details.database.c_str(), 0, NULL, 0)) { |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 43 | throw std::runtime_error(mysql_error(conn)); |
| 44 | } |
| 45 | std::shared_ptr<MYSQL> connection(conn, &mysql_close); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 46 | return connection; |
| 47 | } |
| 48 | |
| 49 | std::shared_ptr<MYSQL_RES> |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 50 | MySQLPerformQuery(std::shared_ptr<MYSQL> connection, |
| 51 | const std::string& sql_query, |
| 52 | DatabaseOperation op, |
| 53 | bool& success, |
| 54 | std::string& errMsg) |
| 55 | { |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 56 | switch (mysql_query(connection.get(), sql_query.c_str())) |
| 57 | { |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 58 | case 0: |
| 59 | { |
| 60 | success = true; |
| 61 | if (op == QUERY) { |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 62 | MYSQL_RES* resultPtr = mysql_store_result(connection.get()); |
| 63 | if (resultPtr != NULL) |
| 64 | { |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 65 | return std::shared_ptr<MYSQL_RES>(resultPtr, &mysql_free_result); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 66 | } |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 67 | } |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 68 | //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 Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 81 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 82 | return nullptr; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace util |
| 86 | } // namespace atmos |