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> |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 22 | |
| 23 | namespace atmos { |
| 24 | namespace util { |
| 25 | |
| 26 | ConnectionDetails::ConnectionDetails(const std::string& serverInput, const std::string& userInput, |
| 27 | const std::string& passwordInput, const std::string& databaseInput) |
| 28 | : server(serverInput), user(userInput), password(passwordInput), database(databaseInput) |
| 29 | { |
| 30 | // empty |
| 31 | } |
| 32 | |
| 33 | |
| 34 | std::shared_ptr<MYSQL> |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame^] | 35 | MySQLConnectionSetup(const ConnectionDetails& details) { |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 36 | MYSQL* conn = mysql_init(NULL); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame^] | 37 | if(!mysql_real_connect(conn, details.server.c_str(), details.user.c_str(), |
| 38 | details.password.c_str(), details.database.c_str(), 0, NULL, 0)) { |
| 39 | throw std::runtime_error(mysql_error(conn)); |
| 40 | } |
| 41 | std::shared_ptr<MYSQL> connection(conn, &mysql_close); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 42 | return connection; |
| 43 | } |
| 44 | |
| 45 | std::shared_ptr<MYSQL_RES> |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame^] | 46 | MySQLPerformQuery(std::shared_ptr<MYSQL> connection, const std::string& sql_query) { |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 47 | switch (mysql_query(connection.get(), sql_query.c_str())) |
| 48 | { |
| 49 | case 0: |
| 50 | { |
| 51 | MYSQL_RES* resultPtr = mysql_store_result(connection.get()); |
| 52 | if (resultPtr != NULL) |
| 53 | { |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame^] | 54 | return std::shared_ptr<MYSQL_RES>(resultPtr, &mysql_free_result); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 55 | } |
| 56 | break; |
| 57 | } |
| 58 | // Various error cases |
| 59 | case CR_COMMANDS_OUT_OF_SYNC: |
| 60 | case CR_SERVER_GONE_ERROR: |
| 61 | case CR_SERVER_LOST: |
| 62 | case CR_UNKNOWN_ERROR: |
| 63 | default: |
| 64 | break; |
| 65 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame^] | 66 | return nullptr; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } // namespace util |
| 70 | } // namespace atmos |