fmdb-orm-library is an iOS (probably even MacOS, thus not tested yet) ORM library. It is taking the FMDB SQLite Database Handler to the next level. And perfectly integrates the ORM approach into the FMDatabase environments generating a minimum of object handling overhead by caching frequently used parts.
The library uses ARC so if you want to use it in a non-ARC project you will have to set the compiler-flag -fobjc-arc
to the FMDBORM[...]-Classes.
fmdb-orm-library defines a protocol to interact with every database type you'd like to.
Already integrated into the Library is a prototype class called FMDBORMModel
. By subclassing this class you can define and map new objects for interaction with the underlying SQLLite database.
Clone the library into your project and add the FMDBLibrary
folder into your project file. You will also have to add the fmdb library. Either you do it by cloning it from their repository or you use the copy included in the Frameworks/fmdb
folder which definetly works with the current version.
Create a subclass of FMDBORMModel
.
By default every entity inheriting from that prototype class already has a field named uid
which identifies the object in your database table.
Override the class method +(NSString*)tablename
which returns the name of your table. The name has to conform to the SQLLite standard of the underlying database.
Override the class method +(NSDictionary)mapping
like shown below.
Assuming there is a property entityKey
of type NSString
present.
+(NSDictionary)mapping {
NSMutableDictionary *mapping = [NSMutableDictionary dictionaryWithDictionary:[super mapping]];
[mapping setObject:[[FMDBORMColumn alloc] initWithColumnName:@"entity_key" andType:FMDBORMTypeText andExtraModifier:@[FMDBORMExtraUnique] forKey:@"entityKey"];
return mapping;
}
You don't have to care about performance because this part will be called once and then be cached.
If you're in the opinion that your model is ready to use, you finally habe to override the Methode +(BOOL)isAbstract
as follows.
+(BOOL)isAbstract {
return NO;
}
Assuming that there is a Model named Bike
with mapped properties NSInteger speed, NSString *facturer, CGFloat frameHeight, NSDate *factoringDate, NSData *image
.
FMDatabaseQueue *queue = ...
[queue inDatabase:^(FMDatabase db) {
Bike *stevens = [[Bike alloc] init];
stevens.facturer = @"Stevens";
stevens.frameHeight = 1.2;
stevens.factoringDate = [NSDate now];
stevens.image = UIPNGDataRepresentation([[UIImage alloc] initWithContentsOfFile:@"filepath"]);
// Creates or updates the object
[stevens put:db];
...
// Delete the object
[stevens erase:db];
}
If you're working with for example Google App Engine you soon will find the standard model to be inappropriate. But since the model is highly flexible you can override some methods to achieve your desired goal.