Database Class Reference

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

#include <Database.h>

Inheritance diagram for Database:
Inheritance graph
[legend]

List of all members.

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

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

See also:
Singleton

Constructor & Destructor Documentation

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.


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

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

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

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]
friend class Singleton< Database > [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().


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

Generated by  doxygen 1.6.2