marcoarment / FCModel

An alternative to Core Data for people who like having direct SQL access.
MIT License
1.65k stars 173 forks source link

Add support for free-form read queries #102

Closed ivanzoid closed 10 years ago

ivanzoid commented 10 years ago

Useful if you want to make join with other table to apply some conditions, or if you wish to create a view of original table with additional columns (exactly my case). I was trying to find a way to do it using only public api, but, hm, didn't find.

marcoarment commented 10 years ago

I appreciate the request, but must decline for now.

We discussed freeform SELECTs and JOINs in issues #17 and #15. The problem, in brief, is two big assumptions in FCModel:

  1. All instances include every column. There's no lazy-loading or hydrating of properties — models are always loaded from a SELECT * statement. The potential bugs that could result from partially-loaded model instances are exacerbated by FCModel's current unique-in-memory instance design.
  2. Any column read from the database can be written back to the database as a column with that name. For instance, if your SELECT had a column alias a + b AS total being read into the model's @property int total, then the value of .total was changed and save was called, it would try and fail to write that back to nonexistent column total in the database.

Problem 1 will be partially alleviated by FCModel's future direction in the non-unique-instances branch, but I'd rather not take on the additional complexity and bug potential of trying to work around the other issues.

ivanzoid commented 10 years ago

Thanks. I think I solved my problem another way! I created a view for my original table where I added 2 columns from other table (CREATE VIEW C AS SELECT A.*, B.foo, B.bar FROM A, B WHERE A.baz = B.baz) and created subclass of class for original table and named it the same as view. I assume this should work (however didn't test it yet).