PATH |
Deleting with the Adaptor Sublayer
Description
There are three ways to delete a row into the database with the adaptor sublayer. You can use raw SQL employing the adaptor channel's evaluateExpression method, the adaptor channel's deleteRowDescribedByQualifier method, or a stored procedure.
In this topic we describe how to delete a row using the deleteRowDescribedByQualifier method. This method eliminates the overhead associated with snapshots, uniquing, and fault creation. Furthermore, the source code does not depend on the particular database you use.
If you are working with an editing context and deleting using the adaptor sublayer at the same time, EOF does not realize that an objected has been deleted. You need to synchronize any enterprise objects with the changes to the database.
To delete a row using the adaptor channel's deleteRowDescribedByQualifier method, you need to provide
- An EOModel mapping entities to database tables.
- An adaptor channel that is connected to the database. (See the programming topics Accessing Adaptor Sublayer Objects , Creating Adaptor Sublayer Objects and Connecting Them to the Server , and Accessing Schema Information from the Database Server for more information about getting an adaptor channel.
- The name of the entity from which you wish to delete, for example, " Movie ".
- An EOQualifier specifying which row to delete. See the programming topic Creating EOQualifiers Programmatically for more information about creating qualifiers.
The following code deletes a row using the adaptor sublayer.
EOAdaptorChannel myAdaptorChannel; // assume exists String modelName = "movies"; String entityName = "Movie"; EOQualifier myQualifier = EOQualifier.qualifierWithQualifierFormat ("title = 'EOF III: The Multithreading'"); EOModel myModel = EOModelGroup.defaultGroup().modelNamed(modelName) EOEntity myEntity = myModel.entityNamed(entityName); // delete row myAdaptorChannel.openChannel(); myAdaptorChannel.deleteRowsDescribedByQualifier(myQualifier,myEntity); myAdaptorChannel.closeChannel();
The code first creates the qualifier that identifies the row you wish to delete.
The code then finds the EOModel corresponding to the model name in the default model group. The default model group, accessed using EOModelGroup.defaultGroup() is a collection of EOModel objects corresponding to the models in the project's Resources suitcase. If you already have the EOModel, you don't need this step. Next, the code determines the EOEntity corresponding to the given entity name from the EOModel.
Before performing deleteRowsDescribedByQualifier with the adaptor channel, the code establishes a connection to the database using the openChannel method. The closeChannel method invocation disconnects the channel from the database.
Deleting Multiple Rows
The deleteRowDescribedByQualifier method deletes a single row from the database. If the qualifier matches more than one row, the method deletes the matching rows and raises an exception. If you need to delete more than one row, use the deleteRowsDescribedByQualifier which will not raise an exception when multiple rows match the qualifier. This method also returns the number of rows that were deleted.
See Also
- Accessing Adaptor Sublayer Objects
- Creating Adaptor Sublayer Objects and Connecting Them to the Server
- Accessing Schema Information from the Database Server
- Inserting with the Adaptor Sublayer
- Updating with the Adaptor Sublayer
- Executing Arbitrary SQL Statements
- EOQualifier class specification in the Enterprise Objects Framework Reference
- EOAdaptor class specification in the Enterprise Objects Framework Reference
- EOAdaptorContext class specification in the Enterprise Objects Framework Reference
- EOAdaptorChannel class specification in the Enterprise Objects Framework Reference
Revision History
22 July, 1998. Seejo Pylappan. First Draft.
13 November, 1998. Clif Liu. Second Draft.
16 March, 1999. Clif Liu. Third Draft.
© 1999 Apple Computer, Inc.