Author: baufo
Date: Sun Mar 23 18:48:52 2008
New Revision: 25026

URL: http://svn.gna.org/viewcvs/wesnoth?rev=25026&view=rev
Log:
Check if user generated queries succeed

Modified:
    branches/mp_registration/src/server/sqlite.cpp
    branches/mp_registration/src/server/user_handler.cpp
    branches/mp_registration/src/server/user_handler.hpp

Modified: branches/mp_registration/src/server/sqlite.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/mp_registration/src/server/sqlite.cpp?rev=25026&r1=25025&r2=25026&view=diff
==============================================================================
--- branches/mp_registration/src/server/sqlite.cpp (original)
+++ branches/mp_registration/src/server/sqlite.cpp Sun Mar 23 18:48:52 2008
@@ -37,7 +37,7 @@
             }
         }
     } else {
-        std::cerr << errmsg << std::endl;
+        std::cerr << "Error executing SQL query: " << errmsg << std::endl;
     }
 
     sqlite3_free_table(result);

Modified: branches/mp_registration/src/server/user_handler.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/mp_registration/src/server/user_handler.cpp?rev=25026&r1=25025&r2=25026&view=diff
==============================================================================
--- branches/mp_registration/src/server/user_handler.cpp (original)
+++ branches/mp_registration/src/server/user_handler.cpp Sun Mar 23 18:48:52 
2008
@@ -226,24 +226,19 @@
         }
     }
 
+    std::string now = lexical_cast_default<std::string>(time(NULL));
+
+    sql_query("insert into users 
(name,password,email,realname,registration_date,last_login) values ('" +
+            name + "','" + password + "','" + mail + "','','" + now + "','" + 
now + "')");
+
     users_.insert(std::pair<std::string,std::string*>(name,NULL));
     users_[name] = new std::string[uh::MAX_VALUE];
-
-    for(std::map<std::string,std::string*>::iterator a = users_.begin(); a!= 
users_.end(); ++a) {
-        std::cout << a->first << std::endl;
-        std::cout << a->second << std::endl;
-    }
-
-    std::string now = lexical_cast_default<std::string>(time(NULL));
 
     users_[name][uh::PASSWORD] = password;
     users_[name][uh::EMAIL]    = mail;
     users_[name][uh::REGISTRATION_DATE] = now;
     users_[name][uh::LAST_LOGIN] = now;
 
-    user_data_.exec("insert into users 
(name,password,email,realname,registration_date,last_login) values ('" +
-            name + "','" + password + "','" + mail + "','','" + now + "','" + 
now + "')");
-
     //I don't think we need to send the user details via email,
     //we don't require any account activation anyways.
 
@@ -294,9 +289,9 @@
         throw error("Could not remove user. No user with the name '" + name + 
"' exists.");
     }
 
+    sql_query("delete from users where name='" + name + "'");
+
     users_.erase(users_.find(name));
-
-    user_data_.exec("delete from users where name='" + name + "'");
 
     std::cout << "Removed user '" << name << "'\n";
 }
@@ -319,6 +314,8 @@
         throw error("Could not set attribute '" + attribute  + "' for user '" 
+ name +
         "'. No user with the name with this name exists.");
     }
+
+    sql_query("update users set " + attribute + "='" + value + "' where 
name='" + name + "'");
 
     std::string* u = users_[name];
 
@@ -333,8 +330,6 @@
     } else {
         std::cerr << "Call of set_user_attribute() with unknown attribute '" 
<< attribute << "'.\n";
     }
-
-    user_data_.exec("update users set " + attribute + "='" + value + "' where 
name='" + name + "'");
 }
 
 bool user_handler::user_exists(const std::string& name) {
@@ -370,6 +365,13 @@
 void user_handler::check_mail(const std::string& mail) {
     if(!(mail.empty() ||utils::isvalid_email(mail))) {
         throw error("The email adress '" + mail + "' appears to be invalid.");
+    }
+}
+
+void user_handler::sql_query(const std::string query, 
std::vector<std::string>* data) {
+    int r = user_data_.exec(query);
+    if(r != SQLITE_OK) {
+        throw error("Error executing SQL query.");
     }
 }
 

Modified: branches/mp_registration/src/server/user_handler.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/mp_registration/src/server/user_handler.hpp?rev=25026&r1=25025&r2=25026&view=diff
==============================================================================
--- branches/mp_registration/src/server/user_handler.hpp (original)
+++ branches/mp_registration/src/server/user_handler.hpp Sun Mar 23 18:48:52 
2008
@@ -90,14 +90,18 @@
         void check_mail(const std::string& mail);
         void check_password(const std::string& password);
 
+        //! A wrapper for user_data_.exec()
+        //! Throws an exception if the the query fails
+        //! User generated querys (e.g. registration of
+        //! a new nick, etc.) should use this.
+        void sql_query(const std::string query, std::vector<std::string>* data 
=NULL);
+
         std::string users_file_;
         unsigned short mail_port_;
         unsigned short username_expiration_limit_;
 
         config cfg_;
-
         sqlite_database user_data_;
-
         std::map<std::string,std::string*> users_;
 };
 


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

Reply via email to