[ANNOUNCE] SQLite Persisted Objects for Cocoa
[ANNOUNCE] SQLite Persisted Objects for Cocoa
- Subject: [ANNOUNCE] SQLite Persisted Objects for Cocoa
- From: Jeff LaMarche <email@hidden>
- Date: Tue, 26 Aug 2008 14:22:31 -0400
Greetings all.
I recently created a project at Google Code that I wanted to bring to
your attention.
http://code.google.com/p/sqlitepersistentobjects/
The goal of this project is to create Objective-C data model objects
that automagically know how to persist themselves to a SQLite database
based on their Objective-C 2.0 properties without any need to write
SQL or use the SQLite3 API directly. The version of the source code
that is currently available on Google Code is a solid, functional
beta. The only major feature not currently implemented is the ability
to rollback an object to its saved state, and I'm currently working on
that. If you use it, you should expect to encounter some bugs (it's an
early beta, after all), but it should work for most purposes.
If you've ever used Ruby's ActiveRecord implementation, this is fairly
similar, except that instead of being driven by the database tables,
it's driven by the Object's properties. This allows you to build a
persistable data model without having to create a mapping as Core Data
requires. You just create your classes and all the mapping to and from
the database is done behind the scenes thanks to the superclass.
This supports storing most common object types and scalars in data
fields. It also supports collection classes (NSSet, NSArray,
NSDictionary) using cross-reference tables, and stores references to
other persisted objects as quasi-foreign-key relationships (I couldn't
use the FK constraints in the database because they are table-
specific, don't lend themselves to polymorphism, and aren't enforced
by SQLite3 anyway). It also supports adding indexes to the underlying
tables by overriding a class method.
In order to create persistable objects, you simply add the files from
this project to your Xcode project, link to sqlite3.dylib, then create
subclasses of SQLitePersistentObject, like so:
#import <foundation/foundation.h>
#import "SQLitePersistentObject.h"
@interface PersistablePerson : SQLitePersistentObject {
NSString *lastName;
NSString *firstName;
}
@property (nonatomic, retain) NSString * lastName;
@property (nonatomic, retain) NSString * firstName;
@end
Once you've done that, you can just create your objects as usual:
PersistablePerson *person = [[PersistablePerson alloc] init];
person.firstName = @"Joe";
person.lastName = @"Smith";
Then you can save those objects to the database (the data base and
table will be created automatically) like so:
[person save];
Loading it back in is almost as easy. All persistable object classes
gets dynamic class methods added to them to allow you to search for
instances. So, for example, you could retrieve all the
PersistablePerson objects that had a last name of "Smith" like so:
NSArray *people = [PersistablePerson findByLastName:@"Smith"]
Or, we could specify the exact criteria like this:
PeristablePerson *joeSmith = [PersistablePerson
findFirstByCriteria:@"WHERE last_name = 'Smith' AND first_name = 'Joe'];
This is licensed under a very liberal license - you can use it in any
software, free or commercial, without limitation. You do NOT have to
give credit and you do NOT have to publish your changes to the source
code (although I'll certainly welcome any bug fixes or enhancements
you wish to give back). The only restriction I've placed on this
project is that if you choose to distribute the source code you must
leave the copyright and license information in the file, though you
are welcome to add additional comments if you wish.
I welcome all comments and/or feedback.
Thanks,
Jeff
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden