Code Monkey home page Code Monkey logo

fmresultsmapping's Introduction

FMResultsMapping

teaser

Small, but usefull FMResultSet extenstion which helps to obtain the results from SQLite. Inspired by (boy, almost stolen from) [EasyMapping] (https://github.com/EasyMapping/EasyMapping).

Build Status Build Status

Usage

What's the usual way we deal with FMResultSet?

Something like that:

NSString *query = @"SELECT * FROM PERSON";
FMResultSet *resultsSet = [self.database executeQuery:query];

NSMutableArray *results = [NSMutableArray new];
while ([resultsSet next])
{
    NSInteger personId = [resultsSet intForColumn:@"ID"];
    NSString *name = [resultsSet stringForColumn:@"name"];
    NSInteger age = [resultsSet intForColumn:@"age"];
    NSString *address = [resultsSet stringForColumn:@"ADDRESS"];
    
    NSTimeInterval interval = [resultsSet doubleForColumn:@"BORN"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSMutableDictionary *dictionary = [NSMutableDictionary new];
    
    [dictionary setObject:@(personId) forKey:@"personId"];
    
    if (name != nil) {
        [dictionary setObject:name forKey:@"name"];
    }
    
    if (age > 0) {
        [dictionary setObject:@(age) forKey:@"birthDate"];
    }
    
    if (address != nil) {
        [dictionary setObject:address forKey:@"address"];
    }
    
    if (date != nil) {
        [dictionary setObject:date forKey:@"birthDate"];
    }
    
    [results addObject:dictionary];
}

Arrrgggh, what a horridness:)

With FMResultsMapping you can do it in this way:

NSString *query = @"SELECT * FROM PERSON";
FMResultSet *resultsSet = [self.database executeQuery:query];

NSMutableArray *results = [NSMutableArray new];
while ([resultsSet next])
{
    NSDictionary *mappedDict = [resultsSet dictionaryWithMapping:^(FMResultMapping *mapping)
    {
        /* Plain mapping */
        [mapping mapColumnValue:@"ID" toKey:@"personId"];
        [mapping mapColumnValue:@"NAME" toKey:@"name"];
        [mapping mapColumnValue:@"AGE" toKey:@"age"];
        [mapping mapColumnValue:@"ADDRESS" toKey:@"address"];
        
        /* Mapping with value block */
        [mapping mapColumnValue:@"BORN" toKey:@"birthDate" valueBlock:^id _Nullable(NSString * _Nonnull key, id  _Nonnull object)
        {
            NSTimeInterval interval = [object doubleValue];
            return [NSDate dateWithTimeIntervalSince1970:interval];
        }];
    }];
    [results addObject:mappedDict];
}

... or even:

NSString *query = @"SELECT * FROM PERSON";
FMResultSet *resultsSet = [self.database executeQuery:query];

NSArray *results = [FMResultsMapper importMappedResultsFromSet:resultsSet mapping:^(FMResultMapping *mapping)
{
    /* Plain mapping */
    [mapping mapColumnValue:@"ID" toKey:@"personId"];
    [mapping mapColumnValue:@"NAME" toKey:@"name"];
    [mapping mapColumnValue:@"AGE" toKey:@"age"];
    [mapping mapColumnValue:@"ADDRESS" toKey:@"address"];
    
    /* Mapping with value block */
    [mapping mapColumnValue:@"BORN" toKey:@"birthDate" valueBlock:^id _Nullable(NSString * _Nonnull key, id  _Nonnull object)
     {
         NSTimeInterval interval = [object doubleValue];
         return [NSDate dateWithTimeIntervalSince1970:interval];
     }];
}];

Pretty nice and clean, yeah?

###Advantages:

  1. Re-using mappings
  2. No boilerplate code nightmare
  3. Value transforming via Value Blocks.

This example transforms unix time interval stored in sqlite database into NSDate

/* Mapping with value block */
[mapping mapColumnValue:@"BORN" toKey:@"birthDate" valueBlock:^id _Nullable(NSString * _Nonnull key, id  _Nonnull object)
 {
     NSTimeInterval interval = [object doubleValue];
     return [NSDate dateWithTimeIntervalSince1970:interval];
 }];

Requirements

iOS8+

OSX 10.8+

Inspiration

  1. EasyMapping
  2. [FMDB] (https://github.com/ccgus/fmdb)

Installation

FMResultsMapping is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "FMResultsMapping"

Author

Serg Krivoblotsky, [email protected]

License

FMResultsMapping is available under the MIT license. See the LICENSE file for more info.

fmresultsmapping's People

Contributors

krivoblotsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.