Skip to content

greedlab/GreedDB

Repository files navigation

GreedDB Join the chat at https://gitter.im/greedlab/GreedDB Build Status

A storage for ios. based on FMDB and GreedJSON. could save NSDictionary,NSArray,NSData,NSString,NSNumber or NSObject directly

中文版

Installation

pod 'GreedDB'

Usage

storage for the default model GRDatabaseDefaultModel

/**
 *  create table
 */
- (void)initDefault {
    self.database = [[GRDatabaseDefaultQueue alloc] init];
    NSLog(@"%@", _database.dbPath);

    [_database setTableName:@"testDefault"];
    [_database setCreateFilterIndex:YES];
    [_database setCreateKeyIndex:YES];
    [_database setCreateSortIndex:YES];

    [_database recreateTable];
}

/**
 *  insert data
 */
- (void)saveDefault {
    GRTestDefaultModel *model = [[GRTestDefaultModel alloc] init];
    model.string = @"string";
    model.integer = 1;
    NSString *filter = [GRTestDefaultModel filterForFilter1:@"filter1" filter2:@"filter1"];

    GRDatabaseDefaultModel *dbModel = [[GRDatabaseDefaultModel alloc] init];
    [dbModel setKey:@1];
    [dbModel setValue:model];
    [dbModel setFilter:filter];

    [_database saveWithModel:dbModel];
}

/**
 *  update data
 */
- (void)updateDefault {
    NSString *filter = [GRTestDefaultModel filterForFilter1:@"filter1" filter2:@"filter1"];

    GRTestDefaultModel *model = [[GRTestDefaultModel alloc] init];
    model.string = @"update";
    model.integer = 2;

    [_database updateValue:model byKey:@1 filter:filter];
}

/**
 *  get data
 */
- (void)getDefault {
    NSString *filter = [GRTestDefaultModel filterForFilter1:@"filter1" filter2:@"filter1"];

    NSArray *array = [_database getValuesByKey:@1 filter:filter];

    NSLog(@"%@", array);
}

storage for mult filters. eg :search with more keys

/**
 *  create table
 */
- (void)initMultFilter {
    self.multFilterQueue = [[GRDatabaseMultFilterQueue alloc] init];
    NSLog(@"%@", _multFilterQueue.dbPath);
    [_multFilterQueue setValueName:[GRTestMultFilterModel valueName]];
    [_multFilterQueue setFilterNames:[GRTestMultFilterModel filterNames]];
    [_multFilterQueue setTableName:@"testMultFilter"];
    [_multFilterQueue recreateTable];
}

/**
 *  insert data
 */
- (void)saveMultFilter {
    GRTestMultFilterModel *model = [[GRTestMultFilterModel alloc] init];
    model.value = @"value";
    model.filter1 = @"filter1";
    model.filter4 = @"filter4";

    [_multFilterQueue saveWithValueFiltersDictionary:[model gr_dictionary]];
}

/**
 *  get data
 */
- (void)getMultFilter {
    GRTestMultFilterModel *model = [[GRTestMultFilterModel alloc] init];
    model.filter1 = @"filter1";
    model.filter4 = @"filter4";
    NSArray *array = [_multFilterQueue getValuesByFiltersDictionary:[model gr_noNUllDictionary]];
    NSLog(@"%@", array);
}

/**
 *  delete data
 */
- (void)delMultFilter {
    GRTestMultFilterModel *model = [[GRTestMultFilterModel alloc] init];
    model.filter1 = @"filter1";
    model.filter4 = @"filter4";

    [_multFilterQueue delByValueFiltersDictionary:[model gr_noNUllDictionary]];
}

Release Notes

CHANGELOG.md

LICENSE

MIT