Author: baufo
Date: Wed Sep 10 20:12:57 2008
New Revision: 29384

URL: http://svn.gna.org/viewcvs/wesnoth?rev=29384&view=rev
Log:
Some code cleanup + softwiring the database table name

Modified:
    trunk/src/server/forum_user_handler.cpp
    trunk/src/server/forum_user_handler.hpp

Modified: trunk/src/server/forum_user_handler.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/server/forum_user_handler.cpp?rev=29384&r1=29383&r2=29384&view=diff
==============================================================================
--- trunk/src/server/forum_user_handler.cpp (original)
+++ trunk/src/server/forum_user_handler.cpp Wed Sep 10 20:12:57 2008
@@ -10,6 +10,7 @@
        db_host_ = c["db_host"];
        db_user_ = c["db_user"];
        db_password_ = c["db_password"];
+       db_users_table_ = c["db_users_table"];
 
        // Connect to the database
        try {
@@ -17,6 +18,15 @@
        } catch(...) {
                 std::cerr << "FUH: ERROR: Could not connect to database: " << 
db_interface_.error() << std::endl;
        }
+}
+
+std::string fuh::get_detail_for_user(const std::string& name, const 
std::string& detail) {
+       return std::string("SELECT " + detail + " FROM " + db_users_table_ + " 
WHERE username='" + name + "'");
+}
+
+std::string fuh::set_detail_for_user(const std::string& name, const 
std::string& detail, const std::string& value) {
+       std::cout <<  std::string("UPDATE " + db_users_table_ + " SET " + 
detail + "='" + name + "' WHERE username='" + name + "'") << std::endl;
+       return std::string("UPDATE " + db_users_table_ + " SET " + detail + 
"='" + value + "' WHERE username='" + name + "'");
 }
 
 void fuh::add_user(const std::string& name, const std::string& mail, const 
std::string& password) {
@@ -141,12 +151,8 @@
 bool fuh::user_exists(const std::string& name) {
 
        // Make a test query for this username
-       std::string sql("SELECT username FROM phpbb_users WHERE username='");
-       sql.append(name);
-       sql.append("'");
-
-       try {
-               return db_query(sql).num_rows() > 0;
+       try {
+               return db_query(get_detail_for_user(name, 
"username")).num_rows() > 0;
        } catch (error e) {
                std::cerr << "FUH: ERROR: " << e.message << std::endl;
                // If the database is down just let all usernames log in
@@ -165,32 +171,19 @@
 
 void fuh::set_lastlogin(const std::string& user, const time_t& lastlogin) {
 
-       // Disabled for now
-
-       /*
        std::stringstream ss;
        ss << lastlogin;
 
-       std::string sql("UPDATE phpbb_users set user_lastvisit='");
-       sql.append(ss.str());
-       sql.append("' where username='");
-       sql.append(user);
-       sql.append("'");
-
-       try {
-       db_query(sql);
-       } catch (error e) {
-               std::cerr << "FUH: ERROR: " << e.message << std::endl;
-       }*/
+       try {
+       db_query(set_detail_for_user(user, "user_lastvisit", ss.str()));
+       } catch (error e) {
+               std::cerr << "FUH: ERROR: " << e.message << std::endl;
+       }
 }
 
 std::string fuh::get_hash(const std::string& user) {
-       std::string sql("SELECT user_password FROM phpbb_users WHERE 
username='");
-       sql.append(user);
-       sql.append("'");
-
-       try {
-               return db_query_to_string(sql);
+       try {
+               return db_query_to_string(get_detail_for_user(user, 
"user_password"));
        } catch (error e) {
                std::cerr << "FUH: ERROR: " << e.message << std::endl;
                return time_t(0);
@@ -198,18 +191,15 @@
 }
 
 std::string fuh::get_mail(const std::string& user) {
-       std::string sql("SELECT user_email FROM phpbb_users WHERE username='");
-       sql.append(user);
-       sql.append("'");
-
-       try {
-               return db_query_to_string(sql);
+       try {
+               return db_query_to_string(get_detail_for_user(user, 
"user_email"));
        } catch (error e) {
                std::cerr << "FUH: ERROR: " << e.message << std::endl;
                return time_t(0);
        }
 }
 
+/*
 std::vector<std::string> fuh::get_friends(const std::string& user) {
        std::string sql("SELECT user_id FROM phpbb_users WHERE username='");
        sql.append(user);
@@ -263,14 +253,11 @@
 
        return ignores;
 }
+*/
 
 time_t fuh::get_lastlogin(const std::string& user) {
-       std::string sql("SELECT user_lastvisit FROM phpbb_users WHERE 
username='");
-       sql.append(user);
-       sql.append("'");
-
-       try {
-               int time_int = atoi(db_query_to_string(sql).c_str());
+       try {
+               int time_int = 
atoi(db_query_to_string(get_detail_for_user(user, "user_lastvisit")).c_str());
                return time_t(time_int);
        } catch (error e) {
                std::cerr << "FUH: ERROR: " << e.message << std::endl;
@@ -279,12 +266,8 @@
 }
 
 time_t fuh::get_registrationdate(const std::string& user) {
-       std::string sql("SELECT user_regdate FROM phpbb_users WHERE 
username='");
-       sql.append(user);
-       sql.append("'");
-
-       try {
-               int time_int = atoi(db_query_to_string(sql).c_str());
+       try {
+               int time_int = 
atoi(db_query_to_string(get_detail_for_user(user, "user_regdate")).c_str());
                return time_t(time_int);
        } catch (error e) {
                std::cerr << "FUH: ERROR: " << e.message << std::endl;

Modified: trunk/src/server/forum_user_handler.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/server/forum_user_handler.hpp?rev=29384&r1=29383&r2=29384&view=diff
==============================================================================
--- trunk/src/server/forum_user_handler.hpp (original)
+++ trunk/src/server/forum_user_handler.hpp Wed Sep 10 20:12:57 2008
@@ -19,6 +19,7 @@
 //     db_host=localhost
 //     db_user=root
 //     db_password=secret
+//     db_table=users
 //[/user_handler]
 
 
@@ -53,18 +54,24 @@
        private:
                std::string get_hash(const std::string& user);
                std::string get_mail(const std::string& user);
-               std::vector<std::string> get_friends(const std::string& user);
-               std::vector<std::string> get_ignores(const std::string& user);
+               /*std::vector<std::string> get_friends(const std::string& user);
+               std::vector<std::string> get_ignores(const std::string& user);*/
                time_t get_lastlogin(const std::string& user);
                time_t get_registrationdate(const std::string& user);
 
                void set_lastlogin(const std::string& user, const time_t& 
lastlogin);
 
-               std::string db_name_, db_host_, db_user_, db_password_;
+               std::string db_name_, db_host_, db_user_, db_password_, 
db_users_table_;
 
                mysqlpp::StoreQueryResult db_query(const std::string& query);
                std::string db_query_to_string(const std::string& query);
                mysqlpp::Connection db_interface_;
+
+               // A helper function to create the SQL to query a detail for a 
particular user
+               std::string get_detail_for_user(const std::string& name, const 
std::string& detail);
+
+               // A helper function to create the SQL to set a detail for a 
particular user
+               std::string set_detail_for_user(const std::string& name, const 
std::string& detail, const std::string& value);
 };
 
 #endif //FORUM_USER_HANDLER_HPP_INCLUDED


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to