Full smart sql -> sql algorithm ported to objective-c (along with tests) - NB: all lookups are done directly in the database, there is no caching unlike the java implementation