diff options
Diffstat (limited to 'src/modules/UserDBBackendMysql/UserDBBackendMysql.cpp')
-rw-r--r-- | src/modules/UserDBBackendMysql/UserDBBackendMysql.cpp | 68 |
1 files changed, 35 insertions, 33 deletions
diff --git a/src/modules/UserDBBackendMysql/UserDBBackendMysql.cpp b/src/modules/UserDBBackendMysql/UserDBBackendMysql.cpp index 7e7711a..46f9d12 100644 --- a/src/modules/UserDBBackendMysql/UserDBBackendMysql.cpp +++ b/src/modules/UserDBBackendMysql/UserDBBackendMysql.cpp @@ -27,6 +27,7 @@ #include <boost/bind.hpp> #include <boost/regex.hpp> #include <boost/thread/locks.hpp> +#include <boost/variant/get.hpp> #include <mysql/mysqld_error.h> @@ -34,7 +35,7 @@ namespace Mad { namespace Modules { namespace UserDBBackendMysql { -const std::string UserDBBackendMysql::name("UserDBBackendMysql"); +const Core::String UserDBBackendMysql::name("UserDBBackendMysql"); bool UserDBBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/) { if(!entry[0].getKey().matches("UserManager")) @@ -48,19 +49,19 @@ bool UserDBBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool if(entry[2].getKey().matches("Host")) { if(entry[3].isEmpty()) - host = entry[2][0].extract(); + host = entry[2][0]; } else if(entry[2].getKey().matches("Username")) { if(entry[3].isEmpty()) - username = entry[2][0].extract(); + username = entry[2][0]; } else if(entry[2].getKey().matches("Password")) { if(entry[3].isEmpty()) - passwd = entry[2][0].extract(); + passwd = entry[2][0]; } else if(entry[2].getKey().matches("Database")) { if(entry[3].isEmpty()) - db = entry[2][0].extract(); + db = entry[2][0]; } else if(entry[2].getKey().matches("Port")) { if(entry[3].isEmpty()) { @@ -77,76 +78,76 @@ bool UserDBBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool } else if(entry[2].getKey().matches("UnixSocket")) { if(entry[3].isEmpty()) - unixSocket = entry[2][0].extract(); + unixSocket = entry[2][0]; } else if(entry[2].getKey().matches("Queries")) { if(entry[3].getKey().matches("ListUsers")) { if(entry[4].isEmpty()) - queryListUsers = entry[3][0].extract(); + queryListUsers = entry[3][0]; } else if(entry[3].getKey().matches("ListGroups")) { if(entry[4].isEmpty()) - queryListGroups = entry[3][0].extract(); + queryListGroups = entry[3][0]; } else if(entry[3].getKey().matches("ListUserGroups")) { if(entry[4].isEmpty()) - queryListUserGroups = entry[3][0].extract(); + queryListUserGroups = entry[3][0]; } else if(entry[3].getKey().matches("ListGroupUsers")) { if(entry[4].isEmpty()) - queryListGroupUsers = entry[3][0].extract(); + queryListGroupUsers = entry[3][0]; } else if(entry[3].getKey().matches("UserById")) { if(entry[4].isEmpty()) - queryUserById = entry[3][0].extract(); + queryUserById = entry[3][0]; } else if(entry[3].getKey().matches("UserByName")) { if(entry[4].isEmpty()) - queryUserByName = entry[3][0].extract(); + queryUserByName = entry[3][0]; } else if(entry[3].getKey().matches("GroupById")) { if(entry[4].isEmpty()) - queryGroupById = entry[3][0].extract(); + queryGroupById = entry[3][0]; } else if(entry[3].getKey().matches("GroupByName")) { if(entry[4].isEmpty()) - queryGroupByName = entry[3][0].extract(); + queryGroupByName = entry[3][0]; } else if(entry[3].getKey().matches("UserGroupTable")) { if(entry[4].isEmpty()) - queryUserGroupTable = entry[3][0].extract(); + queryUserGroupTable = entry[3][0]; } else if(entry[3].getKey().matches("AddUser")) { if(entry[4].isEmpty()) - queryAddUser = entry[3][0].extract(); + queryAddUser = entry[3][0]; } else if(entry[3].getKey().matches("UpdateUser")) { if(entry[4].isEmpty()) - queryUpdateUser = entry[3][0].extract(); + queryUpdateUser = entry[3][0]; } else if(entry[3].getKey().matches("DeleteUser")) { if(entry[4].isEmpty()) - queryDeleteUser = entry[3][0].extract(); + queryDeleteUser = entry[3][0]; } else if(entry[3].getKey().matches("AddGroup")) { if(entry[4].isEmpty()) - queryAddGroup = entry[3][0].extract(); + queryAddGroup = entry[3][0]; } else if(entry[3].getKey().matches("UpdateGroup")) { if(entry[4].isEmpty()) - queryUpdateGroup = entry[3][0].extract(); + queryUpdateGroup = entry[3][0]; } else if(entry[3].getKey().matches("DeleteGroup")) { if(entry[4].isEmpty()) - queryDeleteGroup = entry[3][0].extract(); + queryDeleteGroup = entry[3][0]; } else if(entry[3].getKey().matches("AddUserToGroup")) { if(entry[4].isEmpty()) - queryAddUserToGroup = entry[3][0].extract(); + queryAddUserToGroup = entry[3][0]; } else if(entry[3].getKey().matches("DeleteUserFromGroup")) { if(entry[4].isEmpty()) - queryDeleteUserFromGroup = entry[3][0].extract(); + queryDeleteUserFromGroup = entry[3][0]; } else if(!entry[3].isEmpty()) return false; @@ -158,32 +159,33 @@ bool UserDBBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool } void UserDBBackendMysql::configFinished() { - if(db.empty()) { + if(db.isEmpty()) { application->log(Core::Logger::LOG_ERROR, "UserDBBackendMysql: No database name given"); return; } boost::lock_guard<boost::mutex> lock(mutex); mysql = mysql_init(0); - mysql_real_connect(mysql, host.c_str(), username.c_str(), passwd.c_str(), db.c_str(), port, unixSocket.empty() ? 0 : unixSocket.c_str(), 0); + mysql_real_connect(mysql, host.extract().c_str(), username.extract().c_str(), passwd.extract().c_str(), + db.extract().c_str(), port, unixSocket.isEmpty() ? 0 : unixSocket.extract().c_str(), 0); } -UserDBBackendMysql::Result UserDBBackendMysql::query(const std::string &query, const ArgumentMap &args) throw(Core::Exception) { +UserDBBackendMysql::Result UserDBBackendMysql::query(const Core::String &query, const ArgumentMap &args) throw(Core::Exception) { if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); + std::string queryStr = query.extract(); + if(args.empty()) { - mysql_real_query(mysql, query.c_str(), query.length()); + mysql_real_query(mysql, queryStr.c_str(), queryStr.length()); } else { - std::string queryStr = query; - for(ArgumentMap::const_iterator arg = args.begin(); arg != args.end(); ++arg) { std::string argStr; try { - argStr = boost::get<std::string>(arg->second); + argStr = boost::get<Core::String>(arg->second).extract(); } catch(...) { std::ostringstream stream; @@ -194,7 +196,7 @@ UserDBBackendMysql::Result UserDBBackendMysql::query(const std::string &query, c boost::scoped_array<char> escaped(new char[argStr.length()*2+1]); mysql_real_escape_string(mysql, escaped.get(), argStr.c_str(), argStr.length()); - queryStr = boost::regex_replace(queryStr, boost::regex("\\{" + arg->first + "\\}"), "\"" + std::string(escaped.get()) + "\"", boost::match_default); + queryStr = boost::regex_replace(queryStr, boost::regex("\\{" + arg->first.extract() + "\\}"), "\"" + std::string(escaped.get()) + "\"", boost::match_default); } mysql_real_query(mysql, queryStr.c_str(), queryStr.length()); @@ -273,7 +275,7 @@ boost::shared_ptr<const Common::UserInfo> UserDBBackendMysql::getUserInfo(unsign throw Core::Exception(Core::Exception::NOT_FOUND); } -boost::shared_ptr<const Common::UserInfo> UserDBBackendMysql::getUserInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { +boost::shared_ptr<const Common::UserInfo> UserDBBackendMysql::getUserInfoByName(const Core::String &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { application->getThreadManager()->detach(); boost::unique_lock<boost::mutex> lock(mutex); @@ -400,7 +402,7 @@ boost::shared_ptr<const Common::GroupInfo> UserDBBackendMysql::getGroupInfo(unsi throw Core::Exception(Core::Exception::NOT_FOUND); } -boost::shared_ptr<const Common::GroupInfo> UserDBBackendMysql::getGroupInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { +boost::shared_ptr<const Common::GroupInfo> UserDBBackendMysql::getGroupInfoByName(const Core::String &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { application->getThreadManager()->detach(); boost::unique_lock<boost::mutex> lock(mutex); |