From 4a33343730f075018cc7c5424335f2137d11619a Mon Sep 17 00:00:00 2001 From: Huynh Tran Date: Thu, 16 Jun 2005 17:19:27 +0000 Subject: Simplified APIs, change namespace from tmw to tmwserv, implemented MySQL data provider, added unit tests (require CPPUnit) and bug fixes. --- src/dal/dataprovider.h | 130 ++++++++++++++----------------------------------- 1 file changed, 36 insertions(+), 94 deletions(-) (limited to 'src/dal/dataprovider.h') diff --git a/src/dal/dataprovider.h b/src/dal/dataprovider.h index 6975d084..4e897b3c 100644 --- a/src/dal/dataprovider.h +++ b/src/dal/dataprovider.h @@ -21,8 +21,8 @@ */ -#ifndef _TMW_DATA_PROVIDER_H_ -#define _TMW_DATA_PROVIDER_H_ +#ifndef _TMWSERV_DATA_PROVIDER_H_ +#define _TMWSERV_DATA_PROVIDER_H_ #include @@ -31,7 +31,7 @@ #include "recordset.h" -namespace tmw +namespace tmwserv { namespace dal { @@ -41,13 +41,23 @@ namespace dal * Enumeration type for the database backends. */ typedef enum { - MYSQL, - SQLITE + DB_BKEND_MYSQL, + DB_BKEND_SQLITE } DbBackends; /** * An abstract data provider. + * + * Notes: + * - depending on the database backend, the connection to an unexisting + * database may actually create it as a side-effect (e.g. SQLite). + * + * Limitations: + * - this class does not provide APIs for: + * - remote connections, + * - creating new databases, + * - dropping existing databases. */ class DataProvider { @@ -67,143 +77,75 @@ class DataProvider /** - * 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) + * Get the connection status. * - * @exception DbCreationFailure if unsuccessful creation. - * @exception std::exception if unexpected exception. + * @return true if connected. */ - virtual void - createDb(const std::string& dbName, - const std::string& dbPath = "") - throw(DbCreationFailure, - std::exception) = 0; + bool + isConnected(void) const + throw(); /** - * Create a connection to the database. + * Get the name of the database backend. * - * @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. + * @return the database backend name. */ - virtual void - connect(const std::string& dbName, - const std::string& userName, - const std::string& password) - throw(DbConnectionFailure, - std::exception) = 0; + virtual DbBackends + getDbBackend(void) const + throw() = 0; /** * Create a connection to the database. * * @param dbName the database name. - * @param dbPath the database file path. * @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& dbPath, const std::string& userName, - const std::string& password) - throw(DbConnectionFailure, - std::exception) = 0; + const std::string& password) = 0; /** * Execute a SQL query. * * @param sql the SQL query. - * @param refresh if true, refresh the cache (optional) + * @param refresh if true, refresh the cache (default = false). * * @return a recordset. * * @exception DbSqlQueryExecFailure if unsuccessful execution. - * @exception std::exception if unexpected exception. + * @exception std::runtime_error if trying to query a closed database. */ virtual const RecordSet& execSql(const std::string& sql, - const bool refresh = false) - throw(DbSqlQueryExecFailure, - std::exception) = 0; + const bool refresh = false) = 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(); + disconnect(void) = 0; protected: - /** - * The database name. - */ - std::string mDbName; - - - /** - * The database path. - */ - std::string mDbPath; - - - /** - * The connection status. - */ - bool mIsConnected; - - - /** - * Cache the last SQL query. - */ - std::string mSql; - - - /** - * Cache the result of the last SQL query. - */ - RecordSet mRecordSet; + std::string mDbName; /**< the database name */ + bool mIsConnected; /**< the connection status */ + std::string mSql; /**< cache the last SQL query */ + RecordSet mRecordSet; /**< cache the result of the last SQL query */ }; } // namespace dal -} // namespace tmw +} // namespace tmwserv -#endif // _TMW_DATA_PROVIDER_H_ +#endif // _TMWSERV_DATA_PROVIDER_H_ -- cgit v1.2.3-60-g2f50