blob: 38c0f3a6d8f20594ff0622c6891960e6dd64a408 [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
Chengyu Fan31737f12016-01-12 21:08:50 -070034std::shared_ptr<ConnectionPool_T>
35zdbConnectionSetup(const ConnectionDetails& details)
Chengyu Fan46398212015-08-11 11:23:13 -060036{
Chengyu Fan31737f12016-01-12 21:08:50 -070037 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 Craig2a4d5282015-04-10 12:00:02 -060045
Chengyu Fan31737f12016-01-12 21:08:50 -070046 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 Craig2a4d5282015-04-10 12:00:02 -060054}
55
56} // namespace util
57} // namespace atmos