diff options
author | Huynh Tran <nthuynh75@gmail.com> | 2005-06-13 17:44:49 +0000 |
---|---|---|
committer | Huynh Tran <nthuynh75@gmail.com> | 2005-06-13 17:44:49 +0000 |
commit | 57d846f38d510ab37e706f0bd42fd740a5cbfea0 (patch) | |
tree | 343efae315e830dc961db6b251eb65a595810a04 | |
parent | 7c7a1348faa3867f6f2e5fcaf2b671774b1b018a (diff) | |
download | manaserv-57d846f38d510ab37e706f0bd42fd740a5cbfea0.tar.gz manaserv-57d846f38d510ab37e706f0bd42fd740a5cbfea0.tar.bz2 manaserv-57d846f38d510ab37e706f0bd42fd740a5cbfea0.tar.xz manaserv-57d846f38d510ab37e706f0bd42fd740a5cbfea0.zip |
Initial release of the Database Abstraction Layer
-rw-r--r-- | src/dal/dalexcept.h | 212 | ||||
-rw-r--r-- | src/dal/dataprovider.cpp | 67 | ||||
-rw-r--r-- | src/dal/dataprovider.h | 175 | ||||
-rw-r--r-- | src/dal/dataproviderfactory.cpp | 80 | ||||
-rw-r--r-- | src/dal/dataproviderfactory.h | 86 | ||||
-rw-r--r-- | src/dal/mysqldataprovider.cpp | 122 | ||||
-rw-r--r-- | src/dal/mysqldataprovider.h | 136 | ||||
-rw-r--r-- | src/dal/recordset.cpp | 198 | ||||
-rw-r--r-- | src/dal/recordset.h | 208 | ||||
-rw-r--r-- | src/dal/sqlitedataprovider.cpp | 122 | ||||
-rw-r--r-- | src/dal/sqlitedataprovider.h | 136 |
11 files changed, 1542 insertions, 0 deletions
diff --git a/src/dal/dalexcept.h b/src/dal/dalexcept.h new file mode 100644 index 00000000..e11ec9d1 --- /dev/null +++ b/src/dal/dalexcept.h @@ -0,0 +1,212 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + + +#ifndef _TMW_DAL_EXCEPT_H_ +#define _TMW_DAL_EXCEPT_H_ + + +#include <string> + + +namespace tmw +{ +namespace dal +{ + + +/** + * Default database exception. + */ +class DbException: public std::exception +{ + public: + /** + * Constructor. + * + * @param msg the error message. + */ + DbException(const std::string& msg) + throw() + : mMsg(msg) + { + // NOOP + } + + + /** + * Destructor. + */ + ~DbException(void) + throw() + { + // NOOP + } + + + /** + * Get the error message. + * + * @return the error message. + */ + virtual const char* + what(void) + throw() + { + return mMsg.c_str(); + } + + + private: + std::string mMsg; +}; + + +/** + * Database creation failure. + */ +class DbCreationFailure: public DbException +{ + public: + /** + * Default constructor. + */ + DbCreationFailure(void) + throw() + : DbException("") + { + // NOOP + } + + + /** + * Constructor. + * + * @param msg the error message. + */ + DbCreationFailure(const std::string& msg) + throw() + : DbException(msg) + { + // NOOP + } +}; + + +/** + * Database connection failure. + */ +class DbConnectionFailure: public DbException +{ + public: + /** + * Default constructor. + */ + DbConnectionFailure(void) + throw() + : DbException("") + { + // NOOP + } + + + /** + * Constructor. + * + * @param msg the error message. + */ + DbConnectionFailure(const std::string& msg) + throw() + : DbException(msg) + { + // NOOP + } +}; + + +/** + * Database disconnection failure. + */ +class DbDisconnectionFailure: public DbException +{ + public: + /** + * Default constructor. + */ + DbDisconnectionFailure(void) + throw() + : DbException("") + { + // NOOP + } + + + /** + * Constructor. + * + * @param msg the error message. + */ + DbDisconnectionFailure(const std::string& msg) + throw() + : DbException(msg) + { + // NOOP + } +}; + + +/** + * SQL query execution failure. + */ +class DbSqlQueryExecFailure: public DbException +{ + public: + /** + * Default constructor. + */ + DbSqlQueryExecFailure(void) + throw() + : DbException("") + { + // NOOP + } + + + /** + * Constructor. + * + * @param msg the error message. + */ + DbSqlQueryExecFailure(const std::string& msg) + throw() + : DbException(msg) + { + // NOOP + } +}; + + +} // namespace dal +} // namespace tmw + + +#endif // _TMW_DAL_EXCEPT_H_ diff --git a/src/dal/dataprovider.cpp b/src/dal/dataprovider.cpp new file mode 100644 index 00000000..c72ce50d --- /dev/null +++ b/src/dal/dataprovider.cpp @@ -0,0 +1,67 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + + +#include "dataprovider.h" + + +namespace tmw +{ +namespace dal +{ + + +/** + * Constructor. + */ +DataProvider::DataProvider(void) + throw() + : mIsConnected(false), + mRecordSet(RecordSet()) +{ + // NOOP +} + + +/** + * Destructor. + */ +DataProvider::~DataProvider(void) + throw() +{ + // NOOP +} + + +/** + * Get the connection status. + */ +bool +DataProvider::isConnected(void) const + throw() +{ + return mIsConnected; +} + + +} // namespace dal +} // namespace tmw diff --git a/src/dal/dataprovider.h b/src/dal/dataprovider.h new file mode 100644 index 00000000..b5542e49 --- /dev/null +++ b/src/dal/dataprovider.h @@ -0,0 +1,175 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + + +#ifndef _TMW_DATA_PROVIDER_H_ +#define _TMW_DATA_PROVIDER_H_ + + +#include <string> + +#include "dalexcept.h" +#include "recordset.h" + + +namespace tmw +{ +namespace dal +{ + + +/** + * Enumeration type for the database backends. + */ +typedef enum { + MYSQL, + SQLITE +} DbBackends; + + +/** + * An abstract data provider. + */ +class DataProvider +{ + public: + /** + * Constructor. + */ + DataProvider(void) + throw(); + + + /** + * Destructor. + */ + ~DataProvider(void) + throw(); + + + /** + * Get the database backend name. + * + * @return the database backend name. + */ + virtual DbBackends + getDbBackend(void) const + throw() = 0; + + + /** + * Create a new database. + * + * @param dbName the database name. + * @param dbPath the database file path (optional) + * + * @exception DbCreationFailure if unsuccessful creation. + * @exception std::exception if unexpected exception. + */ + virtual void + createDb(const std::string& dbName, + const std::string& dbPath = "") + throw(DbCreationFailure, + std::exception) = 0; + + + /** + * Create a connection to the database. + * + * @param dbName the database name. + * @param userName the user name. + * @param password the user password. + * + * @exception DbConnectionFailure if unsuccessful connection. + * @exception std::exception if unexpected exception. + */ + virtual void + connect(const std::string& dbName, + const std::string& userName, + const std::string& password) + throw(DbConnectionFailure, + std::exception) = 0; + + + /** + * Execute a SQL query. + * + * @param sql the SQL query. + * + * @return a recordset. + * + * @exception DbSqlQueryExecFailure if unsuccessful execution. + * @exception std::exception if unexpected exception. + */ + virtual const RecordSet& + execSql(const std::string& sql) + throw(DbSqlQueryExecFailure, + std::exception) = 0; + + + /** + * Close the connection to the database. + * + * @exception DbDisconnectionFailure if unsuccessful disconnection. + * @exception std::exception if unexpected exception. + */ + virtual void + disconnect(void) + throw(DbDisconnectionFailure, + std::exception) = 0; + + + /** + * Get the connection status. + * + * @return true if connected. + */ + bool + isConnected(void) const + throw(); + + + protected: + /** + * The connection status. + */ + bool mIsConnected; + + + /** + * Cache the last SQL query. + */ + std::string mSql; + + + /** + * Cache the result of the last SQL query. + */ + RecordSet mRecordSet; +}; + + +} // namespace dal +} // namespace tmw + + +#endif // _TMW_DATA_PROVIDER_H_ diff --git a/src/dal/dataproviderfactory.cpp b/src/dal/dataproviderfactory.cpp new file mode 100644 index 00000000..252104af --- /dev/null +++ b/src/dal/dataproviderfactory.cpp @@ -0,0 +1,80 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + + +#include "dataproviderfactory.h" + +#include "mysqldataprovider.h" +#include "sqlitedataprovider.h" + + +namespace tmw +{ +namespace dal +{ + + +/** + * Default constructor. + */ +DataProviderFactory::DataProviderFactory(void) + throw() +{ + // NOOP +} + + +/** + * Destructor. + */ +DataProviderFactory::~DataProviderFactory(void) + throw() +{ + // NOOP +} + + +/** + * Create a data provider. + */ +DataProvider* +DataProviderFactory::createDataProvider(void) + throw(std::runtime_error) +{ +#ifdef USE_MYSQL + MySqlDataProvider* provider = new MySqlDataProvider; + return provider; +#endif + +#ifdef USE_SQLITE + SqLiteDataProvider* provider = new SqLiteDataProvider; + return provider; +#endif + + // a data provider cannot be created as the server has been compiled + // without support for any databases. + throw std::runtime_error("missing database backend support"); +} + + +} // namespace dal +} // namespace tmw diff --git a/src/dal/dataproviderfactory.h b/src/dal/dataproviderfactory.h new file mode 100644 index 00000000..e6ae3cf3 --- /dev/null +++ b/src/dal/dataproviderfactory.h @@ -0,0 +1,86 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + + +#ifndef _TMW_DATA_PROVIDER_FACTORY_H_ +#define _TMW_DATA_PROVIDER_FACTORY_H_ + + +#include <stdexcept> + +#include "dataprovider.h" + + +namespace tmw +{ +namespace dal +{ + + +/** + * A factory to create data providers. + */ +class DataProviderFactory +{ + public: + /** + * Create a new data provider. + */ + static DataProvider* + createDataProvider(void) + throw(std::runtime_error); + + + private: + /** + * Default constructor. + */ + DataProviderFactory(void) + throw(); + + + /** + * Destructor. + */ + ~DataProviderFactory(void) + throw(); + + + /** + * Copy constructor. + */ + DataProviderFactory(const DataProviderFactory& rhs); + + + /** + * Assignment operator. + */ + DataProviderFactory& + operator=(const DataProviderFactory& rhs); +}; + + +} // namespace dal +} // namespace tmw + + +#endif // _TMW_DATA_PROVIDER_FACTORY_H_ diff --git a/src/dal/mysqldataprovider.cpp b/src/dal/mysqldataprovider.cpp new file mode 100644 index 00000000..6ddabe07 --- /dev/null +++ b/src/dal/mysqldataprovider.cpp @@ -0,0 +1,122 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + + +#include "mysqldataprovider.h" + + +namespace tmw +{ +namespace dal +{ + + +/** + * Constructor. + */ +MySqlDataProvider::MySqlDataProvider(void) + throw() +{ + // NOOP +} + + +/** + * Destructor. + */ +MySqlDataProvider::~MySqlDataProvider(void) + throw() +{ + // NOOP +} + + +/** + * Get the database backend name. + */ +DbBackends +MySqlDataProvider::getDbBackend(void) const + throw() +{ + return MYSQL; +} + + +/** + * Create a new database. + */ +void +MySqlDataProvider::createDb(const std::string& dbName, + const std::string& dbPath) + throw(DbCreationFailure, + std::exception) +{ + // TODO +} + + +/** + * Create a connection to the database. + */ +void +MySqlDataProvider::connect(const std::string& dbName, + const std::string& userName, + const std::string& password) + throw(DbConnectionFailure, + std::exception) +{ + // TODO +} + + +/** + * Execute a SQL query. + */ +const RecordSet& +MySqlDataProvider::execSql(const std::string& sql) + throw(DbSqlQueryExecFailure, + std::exception) +{ + // do something only if the query is different from the previous + // otherwise just return the recordset from cache. + if (sql != mSql) { + // TODO + } + + return mRecordSet; +} + + +/** + * Close the connection to the database. + */ +void +MySqlDataProvider::disconnect(void) + throw(DbDisconnectionFailure, + std::exception) +{ + // TODO +} + + +} // namespace dal +} // namespace tmw diff --git a/src/dal/mysqldataprovider.h b/src/dal/mysqldataprovider.h new file mode 100644 index 00000000..687ed38f --- /dev/null +++ b/src/dal/mysqldataprovider.h @@ -0,0 +1,136 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + + +#ifndef _TMW_MYSQL_DATA_PROVIDER_H_ +#define _TMW_MYSQL_DATA_PROVIDER_H_ + + +#include <string> + +#include "dataprovider.h" + + +namespace tmw +{ +namespace dal +{ + + +/** + * A MySQL Data Provider. + */ +class MySqlDataProvider: public DataProvider +{ + public: + /** + * Constructor. + */ + MySqlDataProvider(void) + throw(); + + + /** + * Destructor. + */ + ~MySqlDataProvider(void) + throw(); + + + /** + * Get the database backend name. + * + * @return the database backend name. + */ + DbBackends + getDbBackend(void) const + throw(); + + + /** + * Create a new database. + * + * @param dbName the database name. + * @param dbPath the database file path (optional) + * + * @exception DbCreationFailure if unsuccessful creation. + * @exception std::exception if unexpected exception. + */ + void + createDb(const std::string& dbName, + const std::string& dbPath = "") + throw(DbCreationFailure, + std::exception); + + + /** + * Create a connection to the database. + * + * @param dbName the database name. + * @param userName the user name. + * @param password the user password. + * + * @exception DbConnectionFailure if unsuccessful connection. + * @exception std::exception if unexpected exception. + */ + void + connect(const std::string& dbName, + const std::string& userName, + const std::string& password) + throw(DbConnectionFailure, + std::exception); + + + /** + * Execute a SQL query. + * + * @param sql the SQL query. + * + * @return a recordset. + * + * @exception DbSqlQueryExecFailure if unsuccessful execution. + * @exception std::exception if unexpected exception. + */ + const RecordSet& + execSql(const std::string& sql) + throw(DbSqlQueryExecFailure, + std::exception); + + + /** + * Close the connection to the database. + * + * @exception DbDisconnectionFailure if unsuccessful disconnection. + * @exception std::exception if unexpected exception. + */ + void + disconnect(void) + throw(DbDisconnectionFailure, + std::exception); +}; + + +} // namespace dal +} // namespace tmw + + +#endif // _TMW_MYSQL_DATA_PROVIDER_H_ diff --git a/src/dal/recordset.cpp b/src/dal/recordset.cpp new file mode 100644 index 00000000..fdaf6614 --- /dev/null +++ b/src/dal/recordset.cpp @@ -0,0 +1,198 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + + +#include <sstream> + +#include "recordset.h" + + +namespace tmw +{ +namespace dal +{ + + +/** + * Default constructor. + */ +Record::Record(void) + throw() +{ + // NOOP +} + + +/** + * Destructor. + */ +Record::~Record(void) + throw() +{ + // NOOP +} + + +/** + * Add a new field. + */ +void +Record::addField(const std::string& name, + const std::string& value) + throw() +{ + mFields.insert(Fields::value_type(name, value)); +} + + +/** + * Add a new field. + */ +void +Record::addField(const std::string& name, + const double value) + throw() +{ + // convert the number into a string. + std::ostringstream os; + os << value << std::ends; + + mFields.insert(Fields::value_type(name, os.str())); +} + + +/** + * Get the field value. + */ +const std::string& +Record::get(const std::string& name) const + throw(std::invalid_argument) +{ + return getAsString(name); +} + + +/** + * Get the field value as string. + */ +const std::string& +Record::getAsString(const std::string& name) const + throw(std::invalid_argument) +{ + Fields::const_iterator it = mFields.find(name); + + if (it == mFields.end()) { + std::ostringstream msg; + msg << "unknown field name: " << name << std::ends; + + throw std::invalid_argument(msg.str()); + } + + return it->second; +} + + +/** + * Get the field value as a number. + */ +const double +Record::getAsNumber(const std::string& name) const + throw(std::invalid_argument) +{ + std::istringstream is(getAsString(name)); + double doubleValue; + + // convert the string to a number. + is >> doubleValue; + + return doubleValue; +} + + +/** + * Default constructor. + */ +RecordSet::RecordSet(void) + throw() +{ + // NOOP +} + + +/** + * Destructor. + */ +RecordSet::~RecordSet(void) + throw() +{ + Rows::iterator it = mRows.begin(); + Rows::iterator it_end = mRows.end(); + + for(; it != it_end; ++it) { + delete *it; + } +} + + +/** + * Add a new Record. + */ +void +RecordSet::addRecord(const Record* record) + throw() +{ + mRows.push_back(record); +} + + +/** + * Get all the rows. + */ +const Rows& +RecordSet::getRows(void) const + throw() +{ + return mRows; +} + + +/** + * Get a particular row. + */ +const Record& +RecordSet::getRow(const unsigned int row) const + throw(std::out_of_range) +{ + if (row >= mRows.size()) { + std::ostringstream msg; + msg << "index out of range: " << row << "; " + << "num. of records: " << mRows.size() << std::ends; + + throw std::out_of_range(msg.str()); + } + + return *(mRows[row]); +} + + +} // namespace dal +} // namespace tmw diff --git a/src/dal/recordset.h b/src/dal/recordset.h new file mode 100644 index 00000000..8804588c --- /dev/null +++ b/src/dal/recordset.h @@ -0,0 +1,208 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + + +#ifndef _TMW_RECORDSET_H_ +#define _TMW_RECORDSET_H_ + + +#include <map> +#include <vector> +#include <stdexcept> + + +namespace tmw +{ +namespace dal +{ + + +/** + * A Record. + */ +class Record +{ + public: + /** + * Default constructor. + */ + Record(void) + throw(); + + + /** + * Destructor. + */ + ~Record(void) + throw(); + + + /** + * Add a new field. + * + * @param name the field name. + * @param value the field value + */ + void + addField(const std::string& name, + const std::string& value) + throw(); + + + /** + * Add a new field. + * + * @param name the field name. + * @param value the field value + */ + void + addField(const std::string& name, + const double value) + throw(); + + + /** + * Get the field value. + * + * This method is an alias of getAsString(). + * + * @param name the field name. + * + * @return the value as string. + * + * @exception std::invalid_argument if the field is not found. + */ + const std::string& + get(const std::string& name) const + throw(std::invalid_argument); + + + /** + * Get the field value as string. + * + * @param name the field name. + * + * @return the value as string. + * + * @exception std::invalid_argument if the field is not found. + */ + const std::string& + getAsString(const std::string& name) const + throw(std::invalid_argument); + + + /** + * Get the field value as a number. + * + * @param name the field name. + * + * @return the value as float. + * + * @exception std::invalid_argument if the field is not found. + */ + const double + getAsNumber(const std::string& name) const + throw(std::invalid_argument); + + + private: + /** + * A map to hold the field names and their associated values. + */ + typedef std::map<std::string, std::string> Fields; + Fields mFields; +}; + + +/** + * A list of Records. + */ +typedef std::vector<const Record*> Rows; + + +/** + * A RecordSet to store the result of a SQL query. + */ +class RecordSet +{ + public: + /** + * Default constructor. + */ + RecordSet(void) + throw(); + + + /** + * Destructor. + */ + ~RecordSet(void) + throw(); + + + /** + * Add a new Record. + * + * @param record the new record. + */ + void + addRecord(const Record*) + throw(); + + + /** + * Get all the rows. + * + * @return all the rows of the RecordSet. + */ + const Rows& + getRows(void) const + throw(); + + + /** + * Get a particular row. + * + * @param row the row index. + * + * @return the Record at the specified row index. + * + * @exception std::out_of_range + */ + const Record& + getRow(const unsigned int row) const + throw(std::out_of_range); + + + private: + /** + * A list of Records. + */ + Rows mRows; +}; + + +} // namespace dal +} // namespace tmw + + +#endif // _TMW_RECORDSET_H_ diff --git a/src/dal/sqlitedataprovider.cpp b/src/dal/sqlitedataprovider.cpp new file mode 100644 index 00000000..03e7acf7 --- /dev/null +++ b/src/dal/sqlitedataprovider.cpp @@ -0,0 +1,122 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + + +#include "sqlitedataprovider.h" + + +namespace tmw +{ +namespace dal +{ + + +/** + * Constructor. + */ +SqLiteDataProvider::SqLiteDataProvider(void) + throw() +{ + // NOOP +} + + +/** + * Destructor. + */ +SqLiteDataProvider::~SqLiteDataProvider(void) + throw() +{ + // NOOP +} + + +/** + * Get the database backend name. + */ +DbBackends +SqLiteDataProvider::getDbBackend(void) const + throw() +{ + return SQLITE; +} + + +/** + * Create a new database. + */ +void +SqLiteDataProvider::createDb(const std::string& dbName, + const std::string& dbPath) + throw(DbCreationFailure, + std::exception) +{ + // TODO +} + + +/** + * Create a connection to the database. + */ +void +SqLiteDataProvider::connect(const std::string& dbName, + const std::string& userName, + const std::string& password) + throw(DbConnectionFailure, + std::exception) +{ + // TODO +} + + +/** + * Execute a SQL query. + */ +const RecordSet& +SqLiteDataProvider::execSql(const std::string& sql) + throw(DbSqlQueryExecFailure, + std::exception) +{ + // do something only if the query is different from the previous + // otherwise just return the recordset from cache. + if (sql != mSql) { + // TODO + } + + return mRecordSet; +} + + +/** + * Close the connection to the database. + */ +void +SqLiteDataProvider::disconnect(void) + throw(DbDisconnectionFailure, + std::exception) +{ + // TODO +} + + +} // namespace dal +} // namespace tmw diff --git a/src/dal/sqlitedataprovider.h b/src/dal/sqlitedataprovider.h new file mode 100644 index 00000000..4780bfbf --- /dev/null +++ b/src/dal/sqlitedataprovider.h @@ -0,0 +1,136 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + + +#ifndef _TMW_SQLITE_DATA_PROVIDER_H_ +#define _TMW_SQLITE_DATA_PROVIDER_H_ + + +#include <string> + +#include "dataprovider.h" + + +namespace tmw +{ +namespace dal +{ + + +/** + * A SQLite Data Provider. + */ +class SqLiteDataProvider: public DataProvider +{ + public: + /** + * Constructor. + */ + SqLiteDataProvider(void) + throw(); + + + /** + * Destructor. + */ + ~SqLiteDataProvider(void) + throw(); + + + /** + * Get the database backend name. + * + * @return the database backend name. + */ + DbBackends + getDbBackend(void) const + throw(); + + + /** + * Create a new database. + * + * @param dbName the database name. + * @param dbPath the database file path (optional) + * + * @exception DbCreationFailure if unsuccessful creation. + * @exception std::exception if unexpected exception. + */ + void + createDb(const std::string& dbName, + const std::string& dbPath = "") + throw(DbCreationFailure, + std::exception); + + + /** + * Create a connection to the database. + * + * @param dbName the database name. + * @param userName the user name. + * @param password the user password. + * + * @exception DbConnectionFailure if unsuccessful connection. + * @exception std::exception if unexpected exception. + */ + void + connect(const std::string& dbName, + const std::string& userName, + const std::string& password) + throw(DbConnectionFailure, + std::exception); + + + /** + * Execute a SQL query. + * + * @param sql the SQL query. + * + * @return a recordset. + * + * @exception DbSqlQueryExecFailure if unsuccessful execution. + * @exception std::exception if unexpected exception. + */ + const RecordSet& + execSql(const std::string& sql) + throw(DbSqlQueryExecFailure, + std::exception); + + + /** + * Close the connection to the database. + * + * @exception DbDisconnectionFailure if unsuccessful disconnection. + * @exception std::exception if unexpected exception. + */ + void + disconnect(void) + throw(DbDisconnectionFailure, + std::exception); +}; + + +} // namespace dal +} // namespace tmw + + +#endif // _TMW_SQLITE_DATA_PROVIDER_H_ |