Tuesday, May 20, 2008

I am alive...

Sorry for the delay... a bunch of stuff that I had put on the back burner all came to a boil at the same time, and I haven't had a lot of time for anything other than work of late. I still plan to get the SQLite persistence class a little more stable and then release it into the wild, but I wouldn't feel right putting it out in its current state.

19 days until WWDC, hope to see you there!

Thursday, May 1, 2008

SQLite Adventures

With the release of OS X 10.4 (Tiger), Apple gave us developers something pretty cool called "Core Data". Core Data is an ORM tool based (at least in concept, if not in actual code) on Enterprise Objects Framework (or EOF) which was NeXT's revolutionary tool for persisting object-oriented data to a database: revolutionary, that is, back in 1993.

Now, I don't mean to slight EOF or Core Data in that statement. Both are cool tools, and are vastly superior to embedding SQL calls to a procedural API in your object-oriented code. In 1993, strongly-typed languages ruled the desktop development roost and "web development" was yet to even really exist as a defined form of programming since, for all practical purposes, 1993 was the year the web was born1.

 C and C++ were the indisputed champions of desktop application development and the arrival of Java two years later would cement the dominance of strongly-typed languages for quite some time. In 1993, weakly-typed languages had been around for a while but, for a number of reasons, were largely considered ill-suited to writing "serious" applications. NeXTStep's use of Objective-C was definitely an odd-man out in terms of desktop application development.

Things have changed quite a lot since 1993, though. Computers have gotten faster, dynamic languages like PERL, Ruby, and Python have become increasingly popular, and even most of the statically typed languages — including C++ — have gained some level of runtime introspection capability.

Probably one of the coolest approaches to object-relational mapping is the ActiveRecord design pattern. A good ActiveRecord implementation requires a dynamic language with robust type introspection which allows the object to dynamically add variables and methods to itself based on the contents of a database table.

But ActiveRecord, as cool as it is, is not ideally suited for desktop development with an embedded database because it is database table-driven. What would be ideal for embedded work is a sort-of "reverse ActiveRecord" where tables are created based on the existing attributes of objects. So, if you added a string variable to your object, it would know to add a varchar column to the table and would also know to save the contents of the variable in that column.  If there is a name for this approach, please let me know it; I've never heard one, but I suspect one exists.

Anyway, I do have a point here, and it has to do with the iPhone and SQLite. I realized yesterday while writing an article proposal on SQLite that Objective-C's dynamic nature would allow this theoretical "reverse ActiveRecord" idea to be implemented and, in fact, that it could probably be encapsulated almost entirely in a single abstract super class. Well, not technically abstract, because Objective-C doesn't support abstract classes, but that's neither here nor there.

Yesterday, I wrote a proof-of-concept version of just such a class. It has methods to create tables, insert, update, and delete data from the database as well as rollback to the last values stored in the database. It's capable of using foreign key relationships for one-to-one associations between objects and cross-reference tables for one-to-many associations represented in Objective-C by the use of arrays and dictionaries.

My code is still very rough around the edges and is definitely not production ready, but I think the concept is sound and with Core Data not being available on the iPhone, I think it's a great approach to avoid having to write tons of custom embedded SQL for each object. In fact, all you do is create a subclass, add instance variables and property declarations and you're done. You don't actually have to write any SQL to handle the persistence and there's even a mechanism to specify indices for your object's table.

I will clean up my code a little bit and put it out there for comments and feedback in the next day or so.

1 - Yes, I know that, in theory the web dates back to 1989 and in implementation dates back to 1990, but CERN announced on April 30, 1993 that the World Wide Web would be free for everyone to use, and that date (in my mind) marks the real beginning of the web as we know it.