Updating with the Adaptor Level
Description
The adaptor level is a server-independent interface for working with the relational database consisting of an EOAdaptor, an EOAdaptorContext and an EOAdaptorChannel. In this level, the database rows are represented as instances of NSDictionary.
The EOAdaptor manages the EOModel, a model that maps the tables and relationships in the database to EOF objects. The EOAdaptor is also responsible for instantiating the correct subclass for the database specific adaptor layer. The EOAdaptorContext manages the database transactions. The EOAdaptorChannel does all the inserts, updates, and deletes, as well as executing SQL. Before you send any SQL to the database, you need to:
· Create an EOAdaptor for the database.
· Create an EOAdaptorContext from the EOAdaptor.
To update the database at the adaptor level, the row should be stored in a NSDictionary where the keys are NSStrings containing the database-dependent attributes to be updated, and the values are their corresponding values.
The keys representing the attributes depend on the database. In ODBC, the keys are the column names. For example, in Oracle, the first column to which the SQL UPDATE refers has the key "Attribute0", the second column has the key "Attribute1" and so on.
An EOQualifier specifies which row to update.
The following code updates a row using the adaptor level.
Figure 1. Objective-C Code
- (void) rawUpdate:(EOEditingContext *)ec forModelNamed:(NSString *)
modelName forEntityNamed:(NSString *)entityName withQualifierString:
(NSString *)qualString dictionary:(NSDictionary *)dict
{
EOModel *myModel;
EOEntity *myEntity;
EOQualifier *myQualifier;
EOAdaptor *myAdaptor;
EOAdaptorContext *myAdaptorContext;
EOAdaptorChannel *myAdaptorChannel;
// get the model, entity, and qualifier describing row to update
myModel = [[ec modelGroup] modelNamed:modelName];
myEntity = [ec entityNamed:entityName];
myQualifier = [EOQualifier qualifierWithQualifierFormat:qualString];
// get the adaptor, adaptor context, and adaptor channel
myAdaptor = [EOAdaptor adaptorWithModel:myModel];
myAdaptorContext = [myAdaptor createAdaptorContext];
myAdaptorChannel = [myAdaptorContext createAdaptorChannel];
// update the database
[myAdaptorChannel openChannel];
[myAdaptorChannel updateValues:dict
inRowDescribedByQualifier:myQualifier entity:myEntity];
[myAdaptorChannel closeChannel];
}