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 | |
Chengyu Fan | 31737f1 | 2016-01-12 21:08:50 -0700 | [diff] [blame^] | 34 | std::shared_ptr<ConnectionPool_T> |
| 35 | zdbConnectionSetup(const ConnectionDetails& details) |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 36 | { |
Chengyu Fan | 31737f1 | 2016-01-12 21:08:50 -0700 | [diff] [blame^] | 37 | std::string dbConnStr("mysql://"); |
| 38 | dbConnStr += details.user; |
| 39 | dbConnStr += ":"; |
| 40 | dbConnStr += details.password; |
| 41 | dbConnStr += "@"; |
| 42 | dbConnStr += details.server; |
| 43 | dbConnStr += ":3306/"; |
| 44 | dbConnStr += details.database; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 45 | |
Chengyu Fan | 31737f1 | 2016-01-12 21:08:50 -0700 | [diff] [blame^] | 46 | URL_T url = URL_new(dbConnStr.c_str()); |
| 47 | |
| 48 | ConnectionPool_T dbConnPool = ConnectionPool_new(url); |
| 49 | ConnectionPool_setMaxConnections(dbConnPool, MAX_DB_CONNECTIONS); |
| 50 | ConnectionPool_setReaper(dbConnPool, 1); |
| 51 | ConnectionPool_start(dbConnPool); |
| 52 | auto sharedPool = std::make_shared<ConnectionPool_T>(dbConnPool); |
| 53 | return sharedPool; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | } // namespace util |
| 57 | } // namespace atmos |