summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-07-02 19:49:54 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-07-02 19:49:54 +0000
commite0ec0277143055e84ca0a5ebdf7e7a255f07c09a (patch)
tree1da65b334973b3b9924a76a63381543c63784677 /src
parent7020d891b341d577fed97659e80de293bf27b062 (diff)
downloadmanaserv-e0ec0277143055e84ca0a5ebdf7e7a255f07c09a.tar.gz
manaserv-e0ec0277143055e84ca0a5ebdf7e7a255f07c09a.tar.bz2
manaserv-e0ec0277143055e84ca0a5ebdf7e7a255f07c09a.tar.xz
manaserv-e0ec0277143055e84ca0a5ebdf7e7a255f07c09a.zip
Cleaned prototypes and factored out duplicate code.
Diffstat (limited to 'src')
-rw-r--r--src/account-server/dalstorage.cpp226
-rw-r--r--src/account-server/dalstorage.hpp32
-rw-r--r--src/account-server/storage.cpp82
-rw-r--r--src/account-server/storage.hpp35
4 files changed, 99 insertions, 276 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());
}
/**
diff --git a/src/account-server/dalstorage.hpp b/src/account-server/dalstorage.hpp
index a99bc5d2..b84b1aa7 100644
--- a/src/account-server/dalstorage.hpp
+++ b/src/account-server/dalstorage.hpp
@@ -47,15 +47,13 @@ class DALStorage: public Storage
/**
* Connect to the database and initialize it if necessary.
*/
- void
- open(void);
+ void open();
/**
* Disconnect from the database.
*/
- void
- close(void);
+ void close();
/**
@@ -209,14 +207,13 @@ class DALStorage: public Storage
/**
* Constructor.
*/
- DALStorage(void);
+ DALStorage();
/**
* Destructor.
*/
- ~DALStorage(void)
- throw();
+ ~DALStorage();
/**
@@ -245,7 +242,26 @@ class DALStorage: public Storage
const std::string& sql);
- private:
+ /**
+ * Gets an account by using a SQL query string.
+ *
+ * @param query the query for the account
+ *
+ * @return the account found by the query
+ */
+ AccountPtr getAccountBySQL(std::string const &query);
+
+
+ /**
+ * Gets a character by character name.
+ *
+ * @param query the query for the character
+ *
+ * @return the character found by the query
+ */
+ CharacterPtr getCharacterBySQL(const std::string &query);
+
+
std::auto_ptr<dal::DataProvider> mDb; /**< the data provider */
};
diff --git a/src/account-server/storage.cpp b/src/account-server/storage.cpp
index a02ec010..efa6ca91 100644
--- a/src/account-server/storage.cpp
+++ b/src/account-server/storage.cpp
@@ -31,26 +31,6 @@ std::string Storage::mPassword("");
/**
- * Constructor.
- */
-Storage::Storage(void)
- throw()
-{
- // NOOP
-}
-
-
-/**
- * Destructor.
- */
-Storage::~Storage(void)
- throw()
-{
- // NOOP
-}
-
-
-/**
* Create an instance of Storage.
*/
Storage&
@@ -66,12 +46,11 @@ Storage::instance(const std::string& name)
return (*mInstance);
}
-
/**
* Delete the instance.
*/
void
-Storage::destroy(void)
+Storage::destroy()
{
if (mInstance != 0) {
delete mInstance;
@@ -84,62 +63,3 @@ Storage::destroy(void)
mPassword = "";
}
-
-/**
- * Check if the storage is open.
- */
-bool
-Storage::isOpen(void) const
-{
- return mIsOpen;
-}
-
-
-/**
- * Get the storage name.
- */
-const std::string&
-Storage::getName(void) const
-{
- return mName;
-}
-
-
-/**
- * Set a user name for the storage.
- */
-void
-Storage::setUser(const std::string& userName)
-{
- mUser = userName;
-}
-
-
-/**
- * Get the user name.
- */
-const std::string&
-Storage::getUser(void) const
-{
- return mUser;
-}
-
-
-/**
- * Set a user password for the storage.
- */
-void
-Storage::setPassword(const std::string& password)
-{
- mPassword = password;
-}
-
-
-/**
- * Get the user password.
- */
-const std::string&
-Storage::getPassword(void) const
-{
- return mPassword;
-}
diff --git a/src/account-server/storage.hpp b/src/account-server/storage.hpp
index c0abf004..fbd2400f 100644
--- a/src/account-server/storage.hpp
+++ b/src/account-server/storage.hpp
@@ -75,7 +75,7 @@ class Storage
* Delete the storage.
*/
static void
- destroy(void);
+ destroy();
/**
@@ -86,7 +86,7 @@ class Storage
* database.
*/
virtual void
- open(void) = 0;
+ open() = 0;
/**
@@ -105,8 +105,8 @@ class Storage
*
* @return true if the storage is open.
*/
- bool
- isOpen(void) const;
+ bool isOpen() const
+ { return mIsOpen; }
/**
@@ -114,8 +114,8 @@ class Storage
*
* @return the storage name.
*/
- const std::string&
- getName(void) const;
+ std::string const &getName() const
+ { return mName; }
/**
@@ -127,8 +127,8 @@ class Storage
*
* @param userName the user name.
*/
- void
- setUser(const std::string& userName);
+ void setUser(const std::string& userName)
+ { mUser = userName; }
/**
@@ -137,8 +137,8 @@ class Storage
* @return the user name (it may be an empty string if not set
* previously).
*/
- const std::string&
- getUser(void) const;
+ std::string const &getUser() const
+ { return mUser; }
/**
@@ -150,8 +150,8 @@ class Storage
*
* @param password the user password.
*/
- void
- setPassword(const std::string& password);
+ void setPassword(const std::string& password)
+ { mPassword = password; }
/**
@@ -160,8 +160,8 @@ class Storage
* @return the user password (it may be an empty string if not set
* previously).
*/
- const std::string&
- getPassword(void) const;
+ std::string const &getPassword() const
+ { return mPassword; }
/**
@@ -324,16 +324,13 @@ class Storage
/**
* Default constructor.
*/
- Storage(void)
- throw();
+ Storage() {}
/**
* Destructor.
*/
- virtual
- ~Storage(void)
- throw();
+ virtual ~Storage() {}
/**