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) | |
Construct an instance of ManagedObject linking with a corresponding table on database named "anEntityName". | |
ManagedObject (string anEntityName, Row &aRow) | |
Construct an instance of ManagedObject linking with a corresponding table on database named "anEntityName" and reading data from mysqlpp::Row. | |
~ManagedObject () | |
void | setBoolForKey (string aKey, bool aValue) |
Sets the specified property of the receiver to the specified boolean value. | |
void | setFloatForKey (string aKey, float aValue) |
Sets the specified property of the receiver to the specified float value. | |
void | setIntForKey (string aKey, int aValue) |
Sets the specified property of the receiver to the specified 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) |
Returns the value as string for the property identified by a given key. | |
ulonglong | getLastInsertID () const |
Returns the ID of the autoincrement of the last query (INSERT). | |
bool | store () |
Attempts to commit unsaved changes to the persistent store: this function builds a REPLACE statement. | |
bool | update () |
Attempts to update unsaved changes to the persistent store. | |
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) |
Build the list of keys (columns) for the specified entity (table). | |
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 | ) |
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 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 | ( | ) |
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 belog 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 belog 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] |
Build the list of keys (columns) for the specified entity (table).
References _entityName, _fault, _keys, Database::getConnection(), Database::Instance(), lastInsertID, and LOG.
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 belog to this entity |
Referenced by Basket::addProduct(), ProductProxy::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 | |||
) |
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 | |||
) |
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 UserMenu::changeProductDetail(), Order::create(), Product::factory(), and setIntForKey().
void ManagedObject::setIntForKey | ( | string | aKey, | |
int | aValue | |||
) |
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 belog 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 | ( | ) |
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.
References _entityName, _fault, _fields, _keys, Database::getConnection(), Database::Instance(), lastInsertID, and LOG.
Referenced by UserMenu::addNewCategory(), UserMenu::addNewProduct(), Order::create(), and User::factory().
bool ManagedObject::update | ( | ) |
Attempts to update unsaved changes to the persistent store.
Fault state is reset if update is successful.
References _entityName, _fault, _fields, _updatedKeys, Database::getConnection(), Database::Instance(), LOG, primaryKey(), and valueMerge().
Referenced by UserMenu::changeProductDetail(), and AdminUser::changeUserPassword().
string ManagedObject::valueForKey | ( | string | aKey | ) | throw (InvalidArgument) |
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 belog to this entity |
Referenced by User::fullName(), ProductProxy::getDescr(), ProductProxy::getName(), Category::getName(), 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().
set<string> ManagedObject::_updatedKeys [protected] |
List of the updated keys to update.
Referenced by ManagedObject(), and update().
ulonglong ManagedObject::lastInsertID [private] |
Referenced by getLastInsertID(), initEntity(), and store().