Author: baufo
Date: Sun Sep 14 17:31:20 2008
New Revision: 29460
URL: http://svn.gna.org/viewcvs/wesnoth?rev=29460&view=rev
Log:
Put data written to the database by wesnothd into a seperate table
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=29460&r1=29459&r2=29460&view=diff
==============================================================================
--- trunk/src/server/forum_user_handler.cpp (original)
+++ trunk/src/server/forum_user_handler.cpp Sun Sep 14 17:31:20 2008
@@ -25,6 +25,7 @@
db_user_ = c["db_user"];
db_password_ = c["db_password"];
db_users_table_ = c["db_users_table"];
+ db_extra_table_ = c["db_extra_table"];
// Connect to the database
try {
@@ -35,11 +36,25 @@
}
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) {
- return std::string("UPDATE " + db_users_table_ + " SET " + detail +
"='" + value + "' WHERE username='" + name + "'");
+ return db_query_to_string("SELECT " + detail + " FROM " +
db_users_table_ + " WHERE username='" + name + "'");
+}
+
+std::string fuh::get_writable_detail_for_user(const std::string& name, const
std::string& detail) {
+ if(!extra_row_exists(name)) return "";
+ return db_query_to_string("SELECT " + detail + " FROM " +
db_extra_table_ + " WHERE username='" + name + "'");
+}
+
+void fuh::set_detail_for_user(const std::string& name, const std::string&
detail, const std::string& value) {
+ try {
+ // Check if we do already have a row for this user in the extra
table
+ if(!extra_row_exists(name)) {
+ // If not create the row
+ db_query("INSERT INTO " + db_extra_table_ + " VALUES('"
+ name + "','" + value + "')");
+ }
+ db_query("UPDATE " + db_extra_table_ + " SET " + detail + "='"
+ value + "' WHERE username='" + name + "'");
+ } catch (error e) {
+ ERR_UH << "Could not set detail for user '" << name << "': " <<
e.message << std::endl;
+ }
}
void fuh::add_user(const std::string& name, const std::string& mail, const
std::string& password) {
@@ -68,12 +83,18 @@
db_interface_.connect(db_name_.c_str(),
db_host_.c_str(), db_user_.c_str(), db_password_.c_str());
} catch(...) {
ERR_UH << "Could not connect to database: " <<
db_interface_.error() << std::endl;
+ throw error("Could not connect to database.");
}
}
- mysqlpp::Query query = db_interface_.query();
- query << sql;
- return query.store();
+ try {
+ mysqlpp::Query query = db_interface_.query();
+ query << sql;
+ return query.store();
+ } catch(...) {
+ ERR_UH << "Could not connect to database: " <<
db_interface_.error() << std::endl;
+ throw error("Error querying database.");
+ }
}
std::string fuh::db_query_to_string(const std::string& query) {
@@ -166,14 +187,25 @@
// Make a test query for this username
try {
- return db_query(get_detail_for_user(name,
"username")).num_rows() > 0;
- } catch (error e) {
- ERR_UH << "Could not execute test query for user '" <<
e.message << std::endl;
+ return db_query("SELECT username FROM " + db_users_table_ + "
WHERE username='" + name + "'").num_rows() > 0;
+ } catch (error e) {
+ ERR_UH << "Could not execute test query for user '" << name <<
"' :" << e.message << std::endl;
// If the database is down just let all usernames log in
return false;
}
}
+bool fuh::extra_row_exists(const std::string& name) {
+
+ // Make a test query for this username
+ try {
+ return db_query("SELECT username FROM " + db_extra_table_ + "
WHERE username='" + name + "'").num_rows() > 0;
+ } catch (error e) {
+ ERR_UH << "Could not execute test query for user '" << name <<
"' :" << e.message << std::endl;
+ return false;
+ }
+}
+
void fuh::user_logged_in(const std::string& name) {
set_lastlogin(name, time(NULL));
}
@@ -189,26 +221,26 @@
ss << lastlogin;
try {
- db_query(set_detail_for_user(user, "user_lastvisit", ss.str()));
- } catch (error e) {
- ERR_UH << "Could not set last visit for user '" << e.message <<
std::endl;
+ set_detail_for_user(user, "user_lastvisit", ss.str());
+ } catch (error e) {
+ ERR_UH << "Could not set last visit for user '" << user << "'
:" << e.message << std::endl;
}
}
std::string fuh::get_hash(const std::string& user) {
try {
- return db_query_to_string(get_detail_for_user(user,
"user_password"));
- } catch (error e) {
- ERR_UH << "Could not retrieve password for user '" << e.message
<< std::endl;
+ return get_detail_for_user(user, "user_password");
+ } catch (error e) {
+ ERR_UH << "Could not retrieve password for user '" << user <<
"' :" << e.message << std::endl;
return time_t(0);
}
}
std::string fuh::get_mail(const std::string& user) {
try {
- return db_query_to_string(get_detail_for_user(user,
"user_email"));
- } catch (error e) {
- ERR_UH << "Could not retrieve email for user '" << e.message <<
std::endl;
+ return get_detail_for_user(user, "user_email");
+ } catch (error e) {
+ ERR_UH << "Could not retrieve email for user '" << user << "'
:" << e.message << std::endl;
return time_t(0);
}
}
@@ -271,20 +303,20 @@
time_t fuh::get_lastlogin(const std::string& user) {
try {
- int time_int =
atoi(db_query_to_string(get_detail_for_user(user, "user_lastvisit")).c_str());
+ int time_int = atoi(get_writable_detail_for_user(user,
"user_lastvisit").c_str());
return time_t(time_int);
} catch (error e) {
- ERR_UH << "Could not retrieve last visit for user '" <<
e.message << std::endl;
+ ERR_UH << "Could not retrieve last visit for user '" << user <<
"' :" << e.message << std::endl;
return time_t(0);
}
}
time_t fuh::get_registrationdate(const std::string& user) {
try {
- int time_int =
atoi(db_query_to_string(get_detail_for_user(user, "user_regdate")).c_str());
+ int time_int = atoi(get_detail_for_user(user,
"user_regdate").c_str());
return time_t(time_int);
} catch (error e) {
- ERR_UH << "Could not retrieve registration date for user '" <<
e.message << std::endl;
+ ERR_UH << "Could not retrieve registration date for user '" <<
user << "' :" << e.message << std::endl;
return time_t(0);
}
}
Modified: trunk/src/server/forum_user_handler.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/server/forum_user_handler.hpp?rev=29460&r1=29459&r2=29460&view=diff
==============================================================================
--- trunk/src/server/forum_user_handler.hpp (original)
+++ trunk/src/server/forum_user_handler.hpp Sun Sep 14 17:31:20 2008
@@ -33,7 +33,8 @@
// db_host=localhost
// db_user=root
// db_password=secret
-// db_table=users
+// db_users_table=users
+// db_extra_table=extra_data
//[/user_handler]
@@ -75,17 +76,21 @@
void set_lastlogin(const std::string& user, const time_t&
lastlogin);
- std::string db_name_, db_host_, db_user_, db_password_,
db_users_table_;
+ std::string db_name_, db_host_, db_user_, db_password_,
db_users_table_, db_extra_table_;
mysqlpp::Result 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
+ // Query a detail for a particular user from the database
std::string get_detail_for_user(const std::string& name, const
std::string& detail);
+ std::string get_writable_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);
+ // Write something to the write table
+ void set_detail_for_user(const std::string& name, const
std::string& detail, const std::string& value);
+
+ // Same as user_exists() but checks if we have a row for this
user in the extra table
+ bool extra_row_exists(const std::string& name);
};
#endif //FORUM_USER_HANDLER_HPP_INCLUDED
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits