Manages the connection to the database server. More...
#include <Database.h>
Public Member Functions | |
~Database () | |
Default destructor: disconnect from the database server and destroy the instance of class. | |
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 () |
void | printResult (StoreQueryResult &res) |
Print to standard out the content of the given result set. | |
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) |
Change to a different database managed by the database server we are connected to. | |
Static Public Member Functions | |
static Database & | Instance () |
Protected Member Functions | |
void | printRow (IntVector &widths, Row &row) |
Print to standard out the content of the given row. | |
Private Member Functions | |
Database (string server, string user, string pwd, string db) | |
Init the class specifying the database informations (server to connect to, credentials to authenticate and database name). | |
Database (const Database &) | |
These methods are intentionally empty to prevent copies of the shared instance. | |
void | operator= (Database &) |
Private Attributes | |
Connection * | _conn |
string | _server |
string | _user |
string | _passwd |
string | _db |
Static Private Attributes | |
static Database | sharedInstance |
Friends | |
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).
The key to creating a singleton is to prevent the client programmer from having any control over the lifetime of the object. To do this, all constructors are declared as private, and prevent the compiler from implicitly generating any constructors. Note that the copy constructor and assignment operator (which intentionally have no implementations, since they will never be called) are declared private to prevent any sort of copies being made.
Database::Database | ( | string | server, | |
string | user, | |||
string | pwd, | |||
string | db | |||
) | [private] |
Init the class specifying the database informations (server to connect to, credentials to authenticate and database name).
This private constructor need to be called to initialize static shared instance: it's intentionally declared private to prevent compiler to generate a default contructor and let the client programmer allocated more than one instance.
References _conn, setDB(), setPassword(), setServer(), and setUser().
Database::Database | ( | const Database & | ) | [private] |
These methods are intentionally empty to prevent copies of the shared instance.
Database::~Database | ( | ) |
Default destructor: disconnect from the database server and destroy the instance of class.
References disconnect().
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 | ( | ) |
References _conn.
Referenced by ProductProxy::catalog(), Category::catalog(), Category::categoryByID(), Order::create(), ProductProxy::getProduct(), ManagedObject::initEntity(), User::login(), Order::ordersForUser(), Product::productByID(), Order::products(), Product::showCompatibleProducts(), AdminUser::showMonthlyTrend(), ManagedObject::store(), ManagedObject::update(), User::userByID(), and AdminUser::userList().
Database & Database::Instance | ( | ) | [static] |
References sharedInstance.
Referenced by ProductProxy::catalog(), Category::catalog(), Category::categoryByID(), Order::create(), ProductProxy::getProduct(), ManagedObject::initEntity(), User::login(), main(), 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::operator= | ( | Database & | ) | [private] |
void Database::printResult | ( | StoreQueryResult & | res | ) |
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 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 | ) |
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] |
Database Database::sharedInstance [static, private] |
Referenced by Instance().