diff options
author | Huynh Tran <nthuynh75@gmail.com> | 2005-06-21 19:35:52 +0000 |
---|---|---|
committer | Huynh Tran <nthuynh75@gmail.com> | 2005-06-21 19:35:52 +0000 |
commit | d3970eb90caa538c88e44609130d9e159e01d60f (patch) | |
tree | d267111cd1edc03866fa079ba9a158830eff96ed /src | |
parent | d344056b9bc3fc1d7ced6f18fde4dc19f3a3dd5f (diff) | |
download | manaserv-d3970eb90caa538c88e44609130d9e159e01d60f.tar.gz manaserv-d3970eb90caa538c88e44609130d9e159e01d60f.tar.bz2 manaserv-d3970eb90caa538c88e44609130d9e159e01d60f.tar.xz manaserv-d3970eb90caa538c88e44609130d9e159e01d60f.zip |
Made sure that pointers are initalized and reset to null after deallocation and some reformatting.
Diffstat (limited to 'src')
-rw-r--r-- | src/dal/dataprovider.h | 3 | ||||
-rw-r--r-- | src/dal/dataproviderfactory.cpp | 36 | ||||
-rw-r--r-- | src/dal/mysqldataprovider.cpp | 14 | ||||
-rw-r--r-- | src/dal/pqdataprovider.cpp | 18 | ||||
-rw-r--r-- | src/dal/pqdataprovider.h | 155 | ||||
-rw-r--r-- | src/dal/sqlitedataprovider.cpp | 7 |
6 files changed, 129 insertions, 104 deletions
diff --git a/src/dal/dataprovider.h b/src/dal/dataprovider.h index a3ea8cc5..2789571a 100644 --- a/src/dal/dataprovider.h +++ b/src/dal/dataprovider.h @@ -73,7 +73,8 @@ class DataProvider /** * Destructor. */ - virtual ~DataProvider(void) + virtual + ~DataProvider(void) throw(); diff --git a/src/dal/dataproviderfactory.cpp b/src/dal/dataproviderfactory.cpp index f0f951dd..19e0cc1a 100644 --- a/src/dal/dataproviderfactory.cpp +++ b/src/dal/dataproviderfactory.cpp @@ -23,22 +23,14 @@ #include "dataproviderfactory.h" -#ifdef MYSQL_SUPPORT +#if defined (MYSQL_SUPPORT) #include "mysqldataprovider.h" -#else - -#ifdef SQLITE_SUPPORT -#include "sqlitedataprovider.h" -#else - -#ifdef POSTGRE_SUPPORT +#elif defined (POSTGRE_SUPPORT) #include "pqdataprovider.h" +#elif defined (SQLITE_SUPPORT) +#include "sqlitedataprovider.h" #else - -#error "Database not specified" - -#endif -#endif +#error "no database backend defined" #endif @@ -74,24 +66,16 @@ DataProviderFactory::~DataProviderFactory(void) DataProvider* DataProviderFactory::createDataProvider(void) { -#ifdef MYSQL_SUPPORT +#if defined (MYSQL_SUPPORT) MySqlDataProvider* provider = new MySqlDataProvider; return provider; -#endif - -#ifdef SQLITE_SUPPORT - SqLiteDataProvider* provider = new SqLiteDataProvider; - return provider; -#endif - -#ifdef POSTGRE_SUPPORT +#elif defined (POSTGRE_SUPPORT) PqDataProvider *provider = new PqDataProvider; return provider; +#else // SQLITE_SUPPORT + SqLiteDataProvider* provider = new SqLiteDataProvider; + return provider; #endif - - // a data provider cannot be created as the server has been compiled - // without support for any database. - throw std::runtime_error("missing database backend support."); } diff --git a/src/dal/mysqldataprovider.cpp b/src/dal/mysqldataprovider.cpp index 20478161..1813fddd 100644 --- a/src/dal/mysqldataprovider.cpp +++ b/src/dal/mysqldataprovider.cpp @@ -37,6 +37,7 @@ namespace dal */ MySqlDataProvider::MySqlDataProvider(void) throw() + : mDb(0) { // NOOP } @@ -53,7 +54,9 @@ MySqlDataProvider::~MySqlDataProvider(void) // make sure that the database is closed. // disconnect() calls mysql_close() which takes care of freeing // the memory allocated for the handle. - disconnect(); + if (mIsConnected) { + disconnect(); + } } @@ -76,6 +79,10 @@ MySqlDataProvider::connect(const std::string& dbName, const std::string& userName, const std::string& password) { + if (mIsConnected) { + return; + } + // allocate and initialize a new MySQL object suitable // for mysql_real_connect(). mDb = mysql_init(NULL); @@ -170,17 +177,18 @@ MySqlDataProvider::execSql(const std::string& sql, void MySqlDataProvider::disconnect(void) { - if (!isConnected()) { + if (!mIsConnected) { return; } // mysql_close() closes the connection and deallocates the connection - // handle as it was allocated by mysql_init(). + // handle allocated by mysql_init(). mysql_close(mDb); // deinitialize the MySQL client library. mysql_library_end(); + mDb = 0; mIsConnected = false; } diff --git a/src/dal/pqdataprovider.cpp b/src/dal/pqdataprovider.cpp index 3fa8db45..dd91f276 100644 --- a/src/dal/pqdataprovider.cpp +++ b/src/dal/pqdataprovider.cpp @@ -20,30 +20,39 @@ * $Id$ */ + #include "pqdataprovider.h" + namespace tmwserv { namespace dal { + /** * Constructor */ PqDataProvider::PqDataProvider(void) throw() + : mDb(0) { + // NOOP } + /** * Destructor */ PqDataProvider::~PqDataProvider(void) throw() { - disconnect(); + if (mIsConnected) { + disconnect(); + } } + /** * Get the database backend name. */ @@ -54,6 +63,7 @@ PqDataProvider::getDbBackend(void) const return DB_BKEND_POSTGRESQL; } + /** * Create a connection to the database. */ @@ -82,6 +92,7 @@ PqDataProvider::connect(const std::string& dbName, mIsConnected = true; } + /** * Execute a SQL query. */ @@ -131,6 +142,7 @@ PqDataProvider::execSql(const std::string& sql, } } + /** * Close connection to database. */ @@ -141,11 +153,13 @@ PqDataProvider::disconnect(void) return; } - // finish up with Postgre + // finish up with Postgre. PQfinish(mDb); + mDb = 0; mIsConnected = false; } + } // namespace dal } // namespace tmwserv diff --git a/src/dal/pqdataprovider.h b/src/dal/pqdataprovider.h index 069228d5..8b0c0bf6 100644 --- a/src/dal/pqdataprovider.h +++ b/src/dal/pqdataprovider.h @@ -20,88 +20,101 @@ * $Id$ */ -#ifndef PQDATAPROVIDER_H -#define PQDATAPROVIDER_H + +#ifndef _TMWSERV_PQDATAPROVIDER_H_ +#define _TMWSERV_PQDATAPROVIDER_H_ + #include <string> + +#include <libpq-fe.h> + #include "dataprovider.h" -#include "libpq-fe.h" + namespace tmwserv { namespace dal { -class PqDataProvider : public DataProvider -{ - public: - /** - * Constructor - */ - PqDataProvider() - throw(); - - /** - * Destructor - */ - ~PqDataProvider() - throw(); - - /** - * Get name of the database backend - * - * @return the database backend name - */ - DbBackends - getDbBackend(void) const - throw(); - - /** - * 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. - */ - void - connect(const std::string& dbName, - const std::string& userName, - const std::string& password); - - - /** - * Execute a SQL query. - * - * @param sql the SQL query. - * @param refresh if true, refresh the cache (default = false). - * - * @return a recordset. - * - * @exception DbSqlQueryExecFailure if unsuccessful execution. - * @exception std::runtime_error if trying to query a closed database. - */ - const RecordSet& - execSql(const std::string& sql, - const bool refresh = false); - - - /** - * Close the connection to the database. - * - * @exception DbDisconnectionFailure if unsuccessful disconnection. - */ - void - disconnect(void); - - protected: - PGconn *mDb; /**< Database connection handle */ +/** + * A PostgreSQL Data Provider. + */ +class PqDataProvider: public DataProvider +{ + public: + /** + * Constructor + */ + PqDataProvider(void) + throw(); + + + /** + * Destructor + */ + ~PqDataProvider(void) + throw(); + + + /** + * Get name of the database backend + * + * @return the database backend name + */ + DbBackends + getDbBackend(void) const + throw(); + + + /** + * 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. + */ + void + connect(const std::string& dbName, + const std::string& userName, + const std::string& password); + + + /** + * Execute a SQL query. + * + * @param sql the SQL query. + * @param refresh if true, refresh the cache (default = false). + * + * @return a recordset. + * + * @exception DbSqlQueryExecFailure if unsuccessful execution. + * @exception std::runtime_error if trying to query a closed database. + */ + const RecordSet& + execSql(const std::string& sql, + const bool refresh = false); + + + /** + * Close the connection to the database. + * + * @exception DbDisconnectionFailure if unsuccessful disconnection. + */ + void + disconnect(void); + + + private: + PGconn *mDb; /**< Database connection handle */ }; -} -} +} // namespace dal +} // namespace tmwserv + -#endif +#endif // _TMWSERV_PQDATAPROVIDER_H_ diff --git a/src/dal/sqlitedataprovider.cpp b/src/dal/sqlitedataprovider.cpp index 38001acb..8018098f 100644 --- a/src/dal/sqlitedataprovider.cpp +++ b/src/dal/sqlitedataprovider.cpp @@ -51,7 +51,9 @@ SqLiteDataProvider::~SqLiteDataProvider(void) // make sure that the database is closed. // disconnect() calls sqlite3_close() which takes care of freeing // the memory allocated for the handle. - disconnect(); + if (mIsConnected) { + disconnect(); + } } catch (...) { // ignore @@ -169,10 +171,13 @@ SqLiteDataProvider::disconnect(void) return; } + // sqlite3_close() closes the connection and deallocates the connection + // handle. if (sqlite3_close(mDb) != SQLITE_OK) { throw DbDisconnectionFailure(sqlite3_errmsg(mDb)); } + mDb = 0; mIsConnected = false; } |