- Inherits From:
- NSObject
- Declared In:
- MCPObject.h
Synopsis:
typedef enum {
MCPDBReturnUnknown = 0,
MCPDBReturnDeleted = 1,
MCPDBReturnUsed = 2,
MCPDBReturnNone = 3,
MCPDBReturnNew = 4,
MCPDBReturnUpdated = 5,
MCPDBReturnIncompleteKey = 6,
MCPDBReturnMultiple = 7,
MCPDBReturnNoIdentity = 8,
MCPDBReturnNoKey = 9,
MCPDBReturnNoConnection = 10,
MCPDBReturnOK = 100
} MCPDBReturnCode;
Description:
Possible return code on some operations of the database interaction.
MCPClassDescription *classDescription;
MCPConnection *connection;
classDescription No description. connection No description.
Life of the objectAccessor(s)
- - init
- - dealloc
- - setAttributesToDefault
Database interface
- - classDescription
- - connection
- - setConnection:
Utiliy method
- - readFromDBRow:withTableName:
- - setPrimaryKey:andFetchFromDB:
- - checkDBId
- - saveInDB
- - getAutoGenerated
- - updateInDB
- - deleteInDB
- + deleteInDBUsingConnection:withId:
Output :
- - defaultValueForKey:
- - primaryKey
Anti-crash method...
- - description
- - setNilValueForKey:
+ (MCPDBReturnCode)deleteInDBUsingConnection:(MCPConnection *)iConnection withId:(id)iId
Uses the connection to remove from the DB the entry corresponding to the primary key id given by iId. If any part of the primary key is missing, nothing is done.
- (NSDictionary *)checkDBId
Using the identity properties of the class, this method will check if self already exists in the DB, in which case it will set the primary key attributes to match the DB entry. If the object is not present in the DB, the primary key attributes are set to null.
The returned dictionary contains the values of the primary key attributes. It also contains one entry with key MCPDBReturnCode, which contains the result of the operation (was the object in DB?).
If the identityAttributes of the class description is empty, the entry will always be considered not to be in the DB, AND the primary key attributes of the object will be left unchnaged and returned as they are at call time.
- (MCPClassDescription *)classDescription
No method description.
- (MCPConnection *)connection
No method description.
- (void)dealloc
Deallocating the class description, then passes the message to super.
- (id)defaultValueForKey:(NSString *)iKey
This method will return the default value (object) for the given key.
- (MCPDBReturnCode)deleteInDB
Uses the connection to delete the object from the DB. In this process the autoGenerated attributes are set to null. If the primary key is not completely set, this method calls checkDBId... method to try to complete it (if the identityAttributes of the class description is not empty).
- (NSString *)description
No method description.
- (MCPDBReturnCode)getAutoGenerated
This method will use the Identity attributes of the object to retrieve the autogenerated attributes from the database.
If the identity is not defined for this class/entity, then it will try to use the primary key to get the auto-generated values.
Obviously this might generate a bug if one of the Identity attributes is autogenerated. This will create a trouble if some object does not have an identity defined but still contains some auto-generated attributes.
- (id)init
Taking care of getting the class description for self (after passing the message up).
- (NSDictionary *)primaryKey
Returns a dictionary with the values of the primary key.
- (id)readFromDBRow:(NSDictionary *)iDictionary withTableName:(NSString *)iTableName
Uses a query result row (described as a NSDictionary) to set the instance variables of self. If the result contains columns from multiple tables, the iTableName can be used to specify the alias used for the table name corresponding to the class.
If iTableName == nil, the columns will be searched first without table name (column_name) and if not found then with the table name in front (from the class description : table_name.column_name).
Otherwise, the search will be performed in the following order : iTableName.column_name, column_name, table_name.column_name.
- (NSDictionary *)saveInDB
First will use the checkDBIdWithConnection method to check if the entry is already in the database. If this is the case, it will use the updateInDB... method to update the current DB instance. Otherwise it will make an insert into the database to save the entry, than it makes a select to get the value of autogenerated column, and use them (if necessary) to return the primary key (as NSDictionary).
As for checkDBId... the returned dictionary also have a MCPDBReturnCode key with a NSNumber as value containing the return code of the operation.
- (void)setAttributesToDefault
Set all the attributes to default values, except for auto-generated and primary key attributes, which are set to NULL.
NOTE : !! In the current version the auto-generated and key are ALSO set to default values!!.
- (void)setConnection:(MCPConnection *)iConnection
No method description.
- (void)setNilValueForKey:(NSString *)iKey
No method description.
- (MCPDBReturnCode)setPrimaryKey:(id)iDictionary andFetchFromDB:(MCPConnection *)iConnection
This method is used to retrieve an object from the DB given its precise primary key. It will return self. If the object is not found in the DB, then all instance variable are set to the default (and autogenerated/primary-key attributes are set to null).
- (MCPDBReturnCode)updateInDB
This method will use the primary key value held by the object to modify the object that is saved in the DB.
(NO YET IMPLEMENTED) If the primary key is not complete (some of the attributes are to null or 0) and the identityAttributes (from class description) is not an empty list then it will perform a checkDBId... to try to retrieve the missing part of the primary key.