The abstract class ManagedObject is a generic class that implements all the basic behaviour of a table of the database. More...
#include <ManagedObject.h>
Public Member Functions | |
ManagedObject (string anEntityName) | |
Default constructor. | |
ManagedObject (string anEntityName, Row &aRow) | |
Construct an instance from a fetched record. | |
virtual | ~ManagedObject () |
Default destructor. | |
void | setBoolForKey (string aKey, bool aValue) |
Set a boolean value. | |
void | setFloatForKey (string aKey, float aValue) |
Set a float value. | |
void | setIntForKey (string aKey, int aValue) |
Set an integer value. | |
void | setValueForKey (string aKey, string aValue) throw (InvalidArgument) |
Sets the specified property of the receiver to the specified string value. | |
float | floatForKey (string aKey) throw (InvalidArgument) |
Returns the value as float for the property identified by a given key. | |
int | intForKey (string aKey) throw (InvalidArgument) |
Returns the value as integer for the property identified by a given key. | |
bool | boolForKey (string aKey) throw (InvalidArgument) |
Returns the value as boolean for the property identified by a given key. | |
string | valueForKey (string aKey) throw (InvalidArgument) |
Get an attribute value. | |
ulonglong | getLastInsertID () const |
Returns the ID of the autoincrement of the last query (INSERT). | |
bool | store () |
Add this istance to database. | |
bool | update () |
Make changes persistent to database. | |
virtual string | primaryKey ()=0 |
Protected Attributes | |
set< string > | _keys |
List of possible keys associated with this entity. | |
set< string > | _updatedKeys |
List of the updated keys to update. | |
map< string, string > | _fields |
Map of key/value associated to the entity. | |
string | _entityName |
The entity name. | |
bool | _fault |
Fault state: if true the entity need to be serialized. | |
Private Member Functions | |
void | initEntity (string anEntityName) |
Internal init. | |
Private Attributes | |
ulonglong | _lastInsertID |
The abstract class ManagedObject is a generic class that implements all the basic behaviour of a table of the database.
So, a managed object is associated with an entity of the database ; this information must be passed to the constructor of the class: when an instance of the class is created, a connection to the database is automatically requested in order to fetch all the available columns of the linked table.
ManagedObject::ManagedObject | ( | string | anEntityName | ) |
Default constructor.
Construct an instance of ManagedObject linking with a corresponding table on database named "anEntityName". The list of columns is fetched automatically.
References initEntity(), and LOG_CTOR.
ManagedObject::ManagedObject | ( | string | anEntityName, | |
Row & | aRow | |||
) |
Construct an instance from a fetched record.
Construct an instance of ManagedObject linking with a corresponding table on database named "anEntityName" and reading data from mysqlpp::Row.
References _fault, _keys, _updatedKeys, initEntity(), LOG_CTOR, and setValueForKey().
ManagedObject::~ManagedObject | ( | ) | [virtual] |
Default destructor.
References LOG_DTOR.
bool ManagedObject::boolForKey | ( | string | aKey | ) | throw (InvalidArgument) |
Returns the value as boolean for the property identified by a given key.
[in] | aKey | The key to read from |
InvalidArgument | Thrown if the specified key doesn't belong to this entity |
float ManagedObject::floatForKey | ( | string | aKey | ) | throw (InvalidArgument) |
Returns the value as float for the property identified by a given key.
[in] | aKey | The key to read from |
InvalidArgument | Thrown if the specified key doesn't belong to this entity |
Referenced by Product::getPrice().
ulonglong ManagedObject::getLastInsertID | ( | ) | const |
Returns the ID of the autoincrement of the last query (INSERT).
References _lastInsertID.
Referenced by Order::create().
void ManagedObject::initEntity | ( | string | anEntityName | ) | [private] |
Internal init.
Build the list of keys (columns) for the specified entity (table)
References _entityName, _fault, _keys, _lastInsertID, Singleton< DataModel >::instance(), and DataModel::keysForEntity().
Referenced by ManagedObject().
int ManagedObject::intForKey | ( | string | aKey | ) | throw (InvalidArgument) |
Returns the value as integer for the property identified by a given key.
[in] | aKey | The key to read from |
InvalidArgument | Thrown if the specified key doesn't belong to this entity |
Referenced by Basket::addProduct(), ProductProxy::getAvailability(), Product::getAvailability(), Product::getCategory(), Order::Order(), NormalUser::placeOrder(), Basket::removeProduct(), and User::uniqueID().
virtual string ManagedObject::primaryKey | ( | ) | [pure virtual] |
void ManagedObject::setBoolForKey | ( | string | aKey, | |
bool | aValue | |||
) |
Set a boolean value.
Sets the specified property of the receiver to the specified boolean value.
[in] | aKey | The name of one of the receiver's properties. |
[in] | aValue | The new value for the property specified by key. |
References setValueForKey().
Referenced by Product::factory().
void ManagedObject::setFloatForKey | ( | string | aKey, | |
float | aValue | |||
) |
Set a float value.
Sets the specified property of the receiver to the specified float value.
[in] | aKey | The name of one of the receiver's properties. |
[in] | aValue | The new value for the property specified by key. |
References setValueForKey().
Referenced by Order::create(), Product::factory(), and setIntForKey().
void ManagedObject::setIntForKey | ( | string | aKey, | |
int | aValue | |||
) |
Set an integer value.
Sets the specified property of the receiver to the specified integer value.
[in] | aKey | The name of one of the receiver's properties. |
[in] | aValue | The new value for the property specified by key. |
References setFloatForKey().
Referenced by Category::categoryByID(), Order::create(), User::factory(), Product::factory(), and Category::factory().
void ManagedObject::setValueForKey | ( | string | aKey, | |
string | aValue | |||
) | throw (InvalidArgument) |
Sets the specified property of the receiver to the specified string value.
[in] | aKey | The name of one of the receiver's properties. |
[in] | aValue | The new value for the property specified by key. |
InvalidArgument | Thrown if the specified key doesn't belong to this entity |
Referenced by Category::catalog(), Category::categoryByID(), UserMenu::changeProductDetail(), AdminUser::changeUserPassword(), Order::create(), User::factory(), Product::factory(), Category::factory(), ManagedObject(), setBoolForKey(), and setFloatForKey().
bool ManagedObject::store | ( | ) |
Add this istance to database.
Attempts to commit unsaved changes to the persistent store: this function builds a REPLACE statement. Fault state is reset if update was successful.
Iterate on all key/value in order to create the VALUE() part of REPLACE statement; we also fill a SQLQueryParams to pass to mysqlpp::query::execute.
We don't make use of valueMerge template method becuase we need to know on which field we're iterating, in order to properly fill "qp" and "values".
References _entityName, _fault, _fields, _keys, _lastInsertID, Database::getConnection(), Singleton< Database >::instance(), and LOG.
Referenced by UserMenu::addNewCategory(), UserMenu::addNewProduct(), Order::create(), and User::factory().
bool ManagedObject::update | ( | ) |
Make changes persistent to database.
Attempts to update unsaved changes to the persistent store. Fault state is reset if update is successful.
We don't make use of valueMerge template method becuase we need to know on which field we're iterating, in order to properly fill "qp" and "values".
References _entityName, _fault, _fields, _updatedKeys, Database::getConnection(), Singleton< Database >::instance(), LOG, primaryKey(), and valueMerge().
Referenced by UserMenu::changeProductDetail(), and AdminUser::changeUserPassword().
string ManagedObject::valueForKey | ( | string | aKey | ) | throw (InvalidArgument) |
Get an attribute value.
Returns the value as string for the property identified by a given key.
[in] | aKey | The key to read from |
InvalidArgument | Thrown if the specified key doesn't belong to this entity |
Referenced by User::fullName(), ProductProxy::getDescr(), ProductProxy::getName(), Category::getName(), User::loginName(), operator<<(), and Order::products().
string ManagedObject::_entityName [protected] |
The entity name.
Referenced by initEntity(), store(), and update().
bool ManagedObject::_fault [protected] |
Fault state: if true the entity need to be serialized.
Referenced by initEntity(), ManagedObject(), store(), and update().
map<string, string> ManagedObject::_fields [protected] |
set<string> ManagedObject::_keys [protected] |
List of possible keys associated with this entity.
Referenced by initEntity(), ManagedObject(), and store().
ulonglong ManagedObject::_lastInsertID [private] |
Referenced by getLastInsertID(), initEntity(), and store().
set<string> ManagedObject::_updatedKeys [protected] |
List of the updated keys to update.
Referenced by ManagedObject(), and update().