Database Class Reference

Manages the connection to the database server. More...

#include <Database.h>

Collaboration diagram for Database:
Collaboration graph
[legend]

List of all members.

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 DatabaseInstance ()

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 &)

Detailed Description

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.

See also:
Database(), Instance()

Constructor & Destructor Documentation

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().


Member Function Documentation

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.

Returns:
True if connection was estabilished successfully.

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 (  ) 
Database & Database::Instance (  )  [static]
bool Database::isConnected (  ) 

Test connection status against the database.

Returns:
True if connection was established successfully.

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().

Parameters:
[in] res The result set
See also:
printRow()

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.

Parameters:
[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.

Parameters:
[in] aValue The new password
See also:
connect(), disconnect()

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.

Parameters:
[in] aValue The new server to connect to
See also:
connect(), disconnect()

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.

Parameters:
[in] aValue The new username
See also:
connect(), disconnect()

References _user, disconnect(), and isConnected().

Referenced by Database(), and main().


Friends And Related Function Documentation

ostream& operator<< ( ostream &  aStream,
Database d 
) [friend]

Member Data Documentation

Connection* Database::_conn [private]
string Database::_db [private]

Referenced by connect(), and setDB().

string Database::_passwd [private]

Referenced by connect(), and setPassword().

string Database::_server [private]

Referenced by setServer().

string Database::_user [private]

Referenced by connect(), and setUser().

Referenced by Instance().


The documentation for this class was generated from the following files:

Generated by  doxygen 1.6.2