diff options
author | Yohann Ferreira <bertram@cegetel.net> | 2005-12-07 23:21:24 +0000 |
---|---|---|
committer | Yohann Ferreira <bertram@cegetel.net> | 2005-12-07 23:21:24 +0000 |
commit | 122a08bb076aa0841646eca0d4d0079b1197fc2f (patch) | |
tree | 6a611325fcf9c7680f86edd30e2a85591495a7e9 | |
parent | 619d958712ac1f370225cd6e1381ad9c1c85a15f (diff) | |
download | manaserv-122a08bb076aa0841646eca0d4d0079b1197fc2f.tar.gz manaserv-122a08bb076aa0841646eca0d4d0079b1197fc2f.tar.bz2 manaserv-122a08bb076aa0841646eca0d4d0079b1197fc2f.tar.xz manaserv-122a08bb076aa0841646eca0d4d0079b1197fc2f.zip |
Improved the getEmailList() function again using an optimized SQL Query.
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | src/accounthandler.cpp | 31 | ||||
-rw-r--r-- | src/dalstorage.cpp | 44 | ||||
-rw-r--r-- | src/dalstorage.h | 9 | ||||
-rw-r--r-- | src/storage.h | 7 |
5 files changed, 75 insertions, 24 deletions
@@ -1,3 +1,11 @@ +2005-12-08 Yohann Ferreira <bertram@cegetel.net> + + * src/dalstorage.cpp, src/dalstorage.h, src/storage.h: + Radically improve the getEmailList() again based on + MrLindejer's good idea. + * src/accounthandler.cpp: Simplify the code of Email check + based on the previous improvement. + 2005-12-06 Yohann Ferreira <bertram@cegetel.net> * src/dalstorage.cpp: Improved the getEmailList() function. diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index 602f5845..b3e89edd 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -117,30 +117,15 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) // checking conditions for having a good account. std::cout << username << " is trying to register." << std::endl; - bool emailValid = true; - // looking the email address already exists. - std::list<std::string> emailList = store.getEmailList(); - std::string upcasedEmail, upcasedIt; - upcasedEmail = email; - std::transform(upcasedEmail.begin(), upcasedEmail.end(), upcasedEmail.begin(), (int(*)(int))std::toupper); - for (std::list<std::string>::const_iterator it = emailList.begin(); it != emailList.end(); it++) + bool emailValid = false; + // Testing Email validity + if (store.doesEmailAlreadyExists(email)) // Search if Email already exists { - // Upcasing both mails for a good comparison - upcasedIt = *it; - std::transform(upcasedIt.begin(), upcasedIt.end(), upcasedIt.begin(), (int(*)(int))std::toupper); - if ( upcasedEmail == upcasedIt ) - { - result.writeShort(SMSG_REGISTER_RESPONSE); - result.writeByte(REGISTER_EXISTS_EMAIL); - std::cout << email << ": Email already exists" << std::endl; - emailValid = false; - break; - } + result.writeShort(SMSG_REGISTER_RESPONSE); + result.writeByte(REGISTER_EXISTS_EMAIL); + std::cout << email << ": Email already exists." << std::endl; + break; } - if (!emailValid) break; - - // Testing Email validity - emailValid = false; if ((email.find_first_of('@') != std::string::npos)) // Searching for an @. { int atpos = email.find_first_of('@'); @@ -177,7 +162,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) { result.writeShort(SMSG_REGISTER_RESPONSE); result.writeByte(REGISTER_INVALID_EMAIL); - std::cout << email << ": Email Invalid (misses @, or . after the @, or maybe there is a ' '.)." << std::endl; + std::cout << email << ": Email Invalid, only a@b.c format is accepted." << std::endl; } else { diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp index 187a9a26..f8b8c2db 100644 --- a/src/dalstorage.cpp +++ b/src/dalstorage.cpp @@ -386,6 +386,50 @@ DALStorage::getEmailList() } /** + * Tells if Email already exists. + */ +bool +DALStorage::doesEmailAlreadyExists(std::string email) +{ + // If not opened already + open(); + + try { + std::string sql("select count(email) from "); + sql += ACCOUNTS_TBL_NAME; + sql += " where upper(email) = upper('"; + sql += email; + sql += "');"; + const dal::RecordSet& accountInfo = mDb->execSql(sql); + + // if the account is empty then + // we have no choice but to return false. + if (accountInfo.isEmpty()) { + return false; + } + + std::stringstream ssStream(accountInfo(0,0)); + int iReturn = -1; + ssStream >> iReturn; + if ( iReturn > 0 ) + { + return true; + } + else + { + return false; + } + } + catch (const dal::DbSqlQueryExecFailure& e) { + // TODO: throw an exception. + LOG_ERROR("SQL query failure: " << e.what()) + } + + return false; +} + + +/** * Save changes to the database permanently. */ void diff --git a/src/dalstorage.h b/src/dalstorage.h index ec69e991..88495307 100644 --- a/src/dalstorage.h +++ b/src/dalstorage.h @@ -96,8 +96,15 @@ class DALStorage: public Storage * Get the list of Emails in the accounts list. * @return the list of Email's Addresses. */ - std::list<std::string> getEmailList(); + std::list<std::string> + getEmailList(); + /** + * Tells if Email already exists. + * @return true if Email is already in database + */ + bool + doesEmailAlreadyExists(std::string email); /** * Save changes to the database permanently. diff --git a/src/storage.h b/src/storage.h index d366aa50..ac3ad88b 100644 --- a/src/storage.h +++ b/src/storage.h @@ -288,6 +288,13 @@ class Storage std::list<std::string> getEmailList() = 0; /** + * Return if an Email is already in account's table. + * @return true if Email already exists. + */ + virtual + bool doesEmailAlreadyExists(std::string email) = 0; + + /** * Saves the changes permanently. */ virtual void |