diff options
Diffstat (limited to 'src/account-server/dalstorage.cpp')
-rw-r--r-- | src/account-server/dalstorage.cpp | 226 |
1 files changed, 58 insertions, 168 deletions
diff --git a/src/account-server/dalstorage.cpp b/src/account-server/dalstorage.cpp index 72a61821..a7350333 100644 --- a/src/account-server/dalstorage.cpp +++ b/src/account-server/dalstorage.cpp @@ -101,7 +101,6 @@ DALStorage::DALStorage() * Destructor. */ DALStorage::~DALStorage() - throw() { if (mDb->isConnected()) { close(); @@ -115,8 +114,7 @@ DALStorage::~DALStorage() /** * Connect to the database and initialize it if necessary. */ -void -DALStorage::open(void) +void DALStorage::open() { // Do nothing if already connected. if (mDb->isConnected()) { @@ -204,42 +202,27 @@ DALStorage::open(void) /** * Disconnect from the database. */ -void -DALStorage::close(void) +void DALStorage::close() { mDb->disconnect(); mIsOpen = mDb->isConnected(); } -/** - * Get an account by user name. - */ -AccountPtr -DALStorage::getAccount(const std::string& userName) +AccountPtr DALStorage::getAccountBySQL(std::string const &query) { // connect to the database (if not connected yet). open(); - // look for the account in the list first. - Accounts::iterator it_end = mAccounts.end(), - it = std::find_if(mAccounts.begin(), it_end, account_by_name(userName)); - - if (it != it_end) - return it->second; - using namespace dal; - // the account was not in the list, look for it in the database. try { - std::ostringstream sql; - sql << "select * from " << ACCOUNTS_TBL_NAME << " where username = \"" - << userName << "\";"; - const RecordSet& accountInfo = mDb->execSql(sql.str()); + const RecordSet& accountInfo = mDb->execSql(query); // if the account is not even in the database then // we have no choice but to return nothing. - if (accountInfo.isEmpty()) { + if (accountInfo.isEmpty()) + { return AccountPtr(NULL); } @@ -257,9 +240,9 @@ DALStorage::getAccount(const std::string& userName) mAccounts.insert(std::make_pair(id, account)); // load the characters associated with the account. - sql.str(std::string()); + std::ostringstream sql; sql << "select id from " << CHARACTERS_TBL_NAME << " where user_id = '" - << accountInfo(0, 0) << "';"; + << id << "';"; RecordSet const &charInfo = mDb->execSql(sql.str()); if (!charInfo.isEmpty()) @@ -267,8 +250,7 @@ DALStorage::getAccount(const std::string& userName) int size = charInfo.rows(); Characters characters; - LOG_DEBUG(userName << "'s account has " << size - << " character(s) in database."); + LOG_DEBUG("Account "<< id << " has " << size << " character(s) in database."); // Two steps: it seems like multiple requests cannot be alive at the same time. std::vector< unsigned > characterIDs; @@ -293,109 +275,56 @@ DALStorage::getAccount(const std::string& userName) } } + /** - * Get an account by ID. + * Get an account by user name. */ AccountPtr -DALStorage::getAccountByID(int accountID) +DALStorage::getAccount(const std::string& userName) { - // connect to the database (if not connected yet). - open(); - // look for the account in the list first. - Accounts::iterator it = mAccounts.find(accountID); + Accounts::iterator it_end = mAccounts.end(), + it = std::find_if(mAccounts.begin(), it_end, account_by_name(userName)); - if (it != mAccounts.end()) + if (it != it_end) return it->second; - using namespace dal; - // the account was not in the list, look for it in the database. - try { - std::ostringstream sql; - sql << "select * from " << ACCOUNTS_TBL_NAME << " where id = '" - << accountID << "';"; - const RecordSet& accountInfo = mDb->execSql(sql.str()); - - // if the account is not even in the database then - // we have no choice but to return nothing. - if (accountInfo.isEmpty()) { - return AccountPtr(NULL); - } - - // specialize the string_to functor to convert - // a string to an unsigned int. - string_to< unsigned > toUint; - unsigned id = toUint(accountInfo(0, 0)); - - // create an Account instance - // and initialize it with information about the user. - AccountPtr account(new Account(accountInfo(0, 1), - accountInfo(0, 2), - accountInfo(0, 3), id)); - - mAccounts.insert(std::make_pair(id, account)); - - // load the characters associated with the account. - sql.str(std::string()); - sql << "select id from " << CHARACTERS_TBL_NAME << " where user_id = '" - << accountInfo(0, 0) << "';"; - RecordSet const &charInfo = mDb->execSql(sql.str()); - - if (!charInfo.isEmpty()) - { - int size = charInfo.rows(); - Characters characters; + std::ostringstream sql; + sql << "select * from " << ACCOUNTS_TBL_NAME << " where username = \"" << userName << "\";"; + return getAccountBySQL(sql.str()); +} - LOG_DEBUG("AccountID: "<< accountID << "; has " << size - << " character(s) in database."); - // Two steps: it seems like multiple requests cannot be alive at the same time. - std::vector< unsigned > characterIDs; - for (int k = 0; k < size; ++k) - { - characterIDs.push_back(toUint(charInfo(k, 0))); - } - - for (int k = 0; k < size; ++k) - { - characters.push_back(getCharacter(characterIDs[k])); - } +/** + * Get an account by ID. + */ +AccountPtr +DALStorage::getAccountByID(int accountID) +{ + // look for the account in the list first. + Accounts::iterator it = mAccounts.find(accountID); - account->setCharacters(characters); - } + if (it != mAccounts.end()) + return it->second; - return account; - } - catch (const DbSqlQueryExecFailure& e) - { - return AccountPtr(NULL); // TODO: Throw exception here - } + // the account was not in the list, look for it in the database. + std::ostringstream sql; + sql << "select * from " << ACCOUNTS_TBL_NAME << " where id = '" << accountID << "';"; + return getAccountBySQL(sql.str()); } -/** - * Gets a character by database ID. - */ -CharacterPtr DALStorage::getCharacter(int id) + +CharacterPtr DALStorage::getCharacterBySQL(std::string const &query) { // connect to the database (if not connected yet). open(); - // look for the character in the list first. - Characters::iterator it_end = mCharacters.end(), - it = std::find_if(mCharacters.begin(), it_end, character_by_id(id)); - - if (it != it_end) - return *it; - using namespace dal; // the account was not in the list, look for it in the database. try { - std::ostringstream sql; - sql << "select * from " << CHARACTERS_TBL_NAME << " where id = '" - << id << "';"; - RecordSet const &charInfo = mDb->execSql(sql.str()); + RecordSet const &charInfo = mDb->execSql(query); // if the character is not even in the database then // we have no choice but to return nothing. @@ -449,14 +378,30 @@ CharacterPtr DALStorage::getCharacter(int id) } } + +/** + * Gets a character by database ID. + */ +CharacterPtr DALStorage::getCharacter(int id) +{ + // look for the character in the list first. + Characters::iterator it_end = mCharacters.end(), + it = std::find_if(mCharacters.begin(), it_end, character_by_id(id)); + + if (it != it_end) + return *it; + + // the account was not in the list, look for it in the database. + std::ostringstream sql; + sql << "select * from " << CHARACTERS_TBL_NAME << " where id = '" << id << "';"; + return getCharacterBySQL(sql.str()); +} + /** * Gets a character by character name. */ CharacterPtr DALStorage::getCharacter(const std::string &name) { - // connect to the database (if not connected yet). - open(); - // look for the character in the list first. Characters::iterator it_end = mCharacters.end(), it = std::find_if(mCharacters.begin(), it_end, character_by_name(name)); @@ -464,65 +409,10 @@ CharacterPtr DALStorage::getCharacter(const std::string &name) if (it != it_end) return *it; - using namespace dal; - // the account was not in the list, look for it in the database. - try { - std::ostringstream sql; - sql << "select * from " << CHARACTERS_TBL_NAME << " where name = '" - << name << "';"; - RecordSet const &charInfo = mDb->execSql(sql.str()); - - // if the character is not even in the database then - // we have no choice but to return nothing. - if (charInfo.isEmpty()) - { - return CharacterPtr(NULL); - } - - // specialize the string_to functor to convert - // a string to an unsigned int. - string_to< unsigned > toUint; - - // specialize the string_to functor to convert - // a string to an unsigned short. - string_to< unsigned short > toUshort; - - CharacterData *character = new CharacterData(charInfo(0, 2), - toUint(charInfo(0, 0))); - character->setAccountID(toUint(charInfo(0, 1))); - character->setGender(toUshort(charInfo(0, 3))); - character->setHairStyle(toUshort(charInfo(0, 4))); - character->setHairColor(toUshort(charInfo(0, 5))); - character->setLevel(toUshort(charInfo(0, 6))); - character->setMoney(toUint(charInfo(0, 7))); - Point pos(toUshort(charInfo(0, 8)), toUshort(charInfo(0, 9))); - character->setPosition(pos); - for (int i = 0; i < NB_BASE_ATTRIBUTES; ++i) - { - character->setBaseAttribute(i, toUshort(charInfo(0, 11 + i))); - } - - int mapId = toUint(charInfo(0, 10)); - if (mapId > 0) - { - character->setMapId(mapId); - } - else - { - // Set character to default map and one of the default location - // Default map is to be 1, as not found return value will be 0. - character->setMapId((int)config.getValue("defaultMap", 1)); - } - - CharacterPtr ptr(character); - mCharacters.push_back(ptr); - return ptr; - } - catch (const DbSqlQueryExecFailure& e) - { - return CharacterPtr(NULL); // TODO: Throw exception here - } + std::ostringstream sql; + sql << "select * from " << CHARACTERS_TBL_NAME << " where name = '" << name << "';"; + return getCharacterBySQL(sql.str()); } /** |