13 #include <database/db_messages.h>
16 #include <boost/algorithm/string.hpp>
17 #include <boost/foreach.hpp>
25 const time_t DatabaseConnection::MAX_DB_TIME = 2147483647;
28 DatabaseConnection::getParameter(
const std::string& name)
const {
29 ParameterMap::const_iterator param = parameters_.find(name);
30 if (param == parameters_.end()) {
33 return (param->second);
37 DatabaseConnection::parse(
const std::string& dbaccess) {
40 if (!dbaccess.empty()) {
47 boost::split(
tokens, dbaccess, boost::is_any_of(
string(
"\t ")));
48 BOOST_FOREACH(std::string token,
tokens) {
49 size_t pos = token.find(
"=");
50 if (pos != string::npos) {
51 string name = token.substr(0, pos);
52 string value = token.substr(pos + 1);
53 mapped_tokens.insert(make_pair(name, value));
57 <<
", expected format is name=value");
62 return (mapped_tokens);
66 DatabaseConnection::redactedAccessString(
const ParameterMap& parameters) {
70 for (DatabaseConnection::ParameterMap::const_iterator i = parameters.begin();
71 i != parameters.end(); ++i) {
74 if (!access.empty()) {
84 if (i->first == std::string(
"password")) {
95 DatabaseConnection::configuredReadOnly()
const {
96 std::string readonly_value =
"false";
98 readonly_value = getParameter(
"readonly");
99 boost::algorithm::to_lower(readonly_value);
105 if ((readonly_value !=
"false") && (readonly_value !=
"true")) {
107 <<
"' specified for boolean parameter 'readonly'");
110 return (readonly_value ==
"true");
114 DatabaseConnection::makeReconnectCtl()
const {
116 string type =
"unknown";
117 unsigned int retries = 0;
118 unsigned int interval = 0;
122 type = getParameter(
"type");
127 std::string parm_str;
129 parm_str = getParameter(
"max-reconnect-tries");
130 retries = boost::lexical_cast<unsigned int>(parm_str);
136 parm_str = getParameter(
"reconnect-wait-time");
137 interval = boost::lexical_cast<unsigned int>(parm_str);
147 DatabaseConnection::invokeDbLostCallback()
const {
148 if (DatabaseConnection::db_lost_callback) {
150 return (DatabaseConnection::db_lost_callback)(makeReconnectCtl());
160 for (
auto param: params) {
161 std::string keyword = param.first;
162 std::string value = param.second;
164 if ((keyword ==
"lfc-interval") ||
165 (keyword ==
"connect-timeout") ||
166 (keyword ==
"request-timeout") ||
167 (keyword ==
"port") ||
168 (keyword ==
"max-reconnect-tries") ||
169 (keyword ==
"reconnect-wait-time") ||
170 (keyword ==
"tcp-keepalive")) {
175 int_value = boost::lexical_cast<int64_t>(value);
179 .arg(
"integer").arg(keyword).arg(value);
181 }
else if ((keyword ==
"persist") ||
182 (keyword ==
"readonly") ||
183 (keyword ==
"tcp-nodelay")) {
185 if (value ==
"true") {
187 }
else if (value ==
"false") {
191 .arg(
"boolean").arg(keyword).arg(value);
193 }
else if ((keyword ==
"type") ||
194 (keyword ==
"user") ||
195 (keyword ==
"password") ||
196 (keyword ==
"host") ||
197 (keyword ==
"name") ||
198 (keyword ==
"contact-points") ||
199 (keyword ==
"keyspace")) {
203 .arg(
"unknown").arg(keyword).arg(value);
211 DatabaseConnection::toElementDbAccessString(
const std::string& dbaccess) {
213 return (toElement(params));
217 DatabaseConnection::db_lost_callback = 0;