Manages the connection to the database server. More...
#include <Database.h>
Public Member Functions | |
virtual | ~Database () |
Default destructor. | |
bool | isConnected () |
Test connection status against the database. | |
bool | connect () |
Connect to database after object is created. | |
void | disconnect () |
Drop the connection to the database server. | |
Connection * | getConnection () |
Return a connection to the database. | |
void | printResult (StoreQueryResult &res) |
Print a result set to stdou. | |
void | setServer (string aValue) |
Change the server we going to connect to. | |
void | setUser (string aValue) |
Change the username we are using to connect to the database server. | |
void | setPassword (string aValue) |
Change the password we are using to connect to the database server. | |
void | setDB (string aValue) |
Database selection. | |
Protected Member Functions | |
Database () | |
Default constructor. | |
void | printRow (IntVector &widths, Row &row) |
Print a generic row to stdout. | |
Private Attributes | |
Connection * | _conn |
string | _server |
string | _user |
string | _passwd |
string | _db |
Friends | |
class | Singleton< Database > |
ostream & | operator<< (ostream &, Database &) |
Manages the connection to the database server.
The class is intented to be used as singleton: call method instance() to get an instance of Database: for this reason, Database needs to be initialized early, such as in your main, with default parameter (use private constructor).
Database::Database | ( | ) | [protected] |
Default constructor.
Init the class specifying the database informations (server to connect to, credentials to authenticate and database name).
References _conn, LOG_CTOR, setDB(), setPassword(), setServer(), and setUser().
Database::~Database | ( | ) | [virtual] |
Default destructor.
Disconnect from the database server and destroy the instance of class.
References disconnect(), and LOG_DTOR.
bool Database::connect | ( | ) |
Connect to database after object is created.
If you call this method on an object that is already connected to a database server, the previous connection is dropped and a new connection is established.
References _conn, _db, _passwd, _user, and disconnect().
Referenced by main().
void Database::disconnect | ( | ) |
Drop the connection to the database server.
References _conn.
Referenced by connect(), setPassword(), setServer(), setUser(), and ~Database().
Connection * Database::getConnection | ( | ) |
Return a connection to the database.
References _conn.
Referenced by ProductProxy::catalog(), Category::catalog(), Category::categoryByID(), Order::create(), AdminUser::deleteProduct(), ProductProxy::getProduct(), DataModel::keysForEntity(), User::login(), Order::ordersForUser(), Product::productByID(), Order::products(), Product::showCompatibleProducts(), AdminUser::showMonthlyTrend(), ManagedObject::store(), ManagedObject::update(), User::userByID(), and AdminUser::userList().
bool Database::isConnected | ( | ) |
Test connection status against the database.
References _conn.
Referenced by setDB(), setPassword(), setServer(), and setUser().
void Database::printResult | ( | StoreQueryResult & | res | ) |
Print a result set to stdou.
Print to standard out the content of the given result set. A vector with all column widths is built and passed to printRow().
[in] | res | The result set |
References printRow().
Referenced by Product::showCompatibleProducts(), and AdminUser::showMonthlyTrend().
void Database::printRow | ( | IntVector & | widths, | |
Row & | row | |||
) | [protected] |
Print a generic row to stdout.
Print to standard out the content of the given row. A vector with all column widths need to be specified.
[in] | widths | Vector of column widths |
[in] | row | The row to be printed |
Referenced by printResult().
void Database::setDB | ( | string | aValue | ) |
Database selection.
Change to a different database managed by the database server we are connected to.
References _conn, _db, and isConnected().
Referenced by Database().
void Database::setPassword | ( | string | aValue | ) |
Change the password we are using to connect to the database server.
If we're already connected to the server, connection is dropped: a new call to connect() need to be called.
[in] | aValue | The new password |
References _passwd, disconnect(), and isConnected().
Referenced by Database(), and main().
void Database::setServer | ( | string | aValue | ) |
Change the server we going to connect to.
If we're already connected to the server, connection is dropped: a new call to connect() need to be called.
[in] | aValue | The new server to connect to |
References _server, disconnect(), and isConnected().
Referenced by Database(), and main().
void Database::setUser | ( | string | aValue | ) |
Change the username we are using to connect to the database server.
If we're already connected to the server, connection is dropped: a new call to connect() need to be called.
[in] | aValue | The new username |
References _user, disconnect(), and isConnected().
Referenced by Database(), and main().
ostream& operator<< | ( | ostream & | aStream, | |
Database & | d | |||
) | [friend] |
Connection* Database::_conn [private] |
Referenced by connect(), Database(), disconnect(), getConnection(), isConnected(), operator<<(), and setDB().
string Database::_db [private] |
string Database::_passwd [private] |
Referenced by connect(), and setPassword().
string Database::_server [private] |
Referenced by setServer().
string Database::_user [private] |