Dumb Record is a very simplistic ORM for Objective-C and Cocoa project. It is built on top of sqlite.
- Build the framework
- Copy the framework to your project
- Add a copy build phase, edit the type from Resource to Framework
- Drag and drop the framework to that new copy phase
Models must inherit the DRModel class, the attributes must correspond to the columns of the table.
All properties must be declared using Objective-C 2.0 dot notation (@property ...)
There should be one column name <table_name>_id and it has to be of type int.
The name of the class will be pluralized (it's adding an s
to the lower cased class name).
You should use the following class declaration
#import <Cocoa/Cocoa.h>
#import "DumbRecord/DumbRecord.h"
@interface Track : DRModel {
int track_id;
NSString *name;
NSNumber *duration;
NSNumber *created_at;
}
@property (nonatomic) int track_id;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSNumber *duration;
@end
NSString *database = @"tracks.sql";
DRLite *db = [DumbRecord setup: database withModels: [NSArray arrayWithObjects: @"Track", nil]];
This will auto-inspect the classes passed in the array and automatically create the tables (it won't update exisiting table - see TODO list)
For the following SQL
CREATE TABLE tracks (track_id INTEGER PRIMARY KEY, name VARCHAR(255), duration INTEGER);
The following code would create the table
NSError *error = nil;
DRLite *db = [DRLite liteWithDatabase: @"tracks.sql"];
[db query: @"CREATE TABLE tracks (track_id INTEGER PRIMARY KEY, name VARCHAR(255), duration INTEGER)" withError: &error];
To insert a new object in the database
Track *track = [[Track alloc] init];
track.name = @"Song title";
track.duration = [NSNumber numberWithInt: 156];
[track insert: db];
To update the value of that object
track.duration = [NSNumber numberWithInt: 136];
[track update: db];
To remove that object from the database
[track delete: db];
After that you can find objects in a sqlite database as follows:
DRLite *db = [DRLite liteWithDatabase: @"tracks.sql"];
NSArray *tracks = [Track findWhere: [NSDictionary dictionaryWithObjectsAndKeys: @"Song title", @"name", nil] inDB: db];
The where dictionary is column name for the key and the value to match the search for. It does AND
on all entries.
- Automagically update the underlying schema
- Add support for NSDate
- Add more options to search for objects (bringing support for
OR
andLIMIT
) - Use bindings for the queries to prevent SQL Injections
- Improve pluralization method