Search code examples
iosormdbaccess

How to drop the table with DBAccess iOS ORM


We are planning to use the DBAccess with our iOS app. We couldn't figure out a way to drop the table while app is running.

Our actual use case is to figure out when DB got updated and then drop or alter some tables. DBAccess also doesn't seems to give version number to db.


Solution

  • That question is slightly convoluted to answer, but here goes:

    How can we actually drop a table with DBAccess?

    The very short answer is, you can't directly. To explain this a little bit further, we used to remove all tables that were not derived from a DBObject class. This caused outrage and hatred amongst our users who we found out, were using DBAccess but with existing databases or pre-populated files, some of the tables were not used as classes but were used for subqueries and the like. So we removed the feature, with what would seem to be at least a 50/50 split of opinion.

    You can issue SQL commands to drop unused tables directly if you wish, just add this category and fill your boots, just pass nil for the database name.

    @interface DBAccess (execSQL)
    
    +(void)executeSQL:(NSString*)sql inDatabase:(NSString*)dbName;
    
    @end
    

    However both points 1&2 are kind of dealt with by the next "feature". We have a revision system built into DBAccess to enable migration and upgrades. It has remained hidden, because it is quite laborious to use and we are planning a much better and GUI based editor for use in the future. But the two method calls that you can add to your objects are as follows:

    + (void)setRevision:(int)revision 
    + (void)entityAtRevision:(int)revision
    

    Your entity will get called on entityAtRevision: with the current revision of the object, and you can then use the executeSQL: method to make changes to the existing data. Importantly, this method is called when the new/renamed column has been added but before the original column is removed so the existing data is there ready. But, before you exit that method you then must call setRevision: with a new revision number.

    As I say, this has not been published because we are wanting to develop a far superior implementation (even if the underlying model remains the same) as this is quite an obvious weak spot for the framework.

    Hope this helps, Adrian