SimpleDB 1.1.4

SimpleDB 1.1.4

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最新发布2014年12月

Aaron Bratcher 维护。



SimpleDB 1.1.4

SimpleDB 是一个键值持久化数据库,它可以让您的应用程序非常容易地存储和检索数据或对象状态。

因为存储的值必须是 JSON 格式,所以可以通过排序完成,并返回数据的特定部分。这目前依赖于我的 ABSQLite 访问类。我将逐步淘汰这些类的依赖。我还会添加一个示例项目。请提供所有反馈。

特别功能

  • 非常容易使用 - 不需要 SQL!
  • 自动删除指定日期后的条目
  • 不需要直接与数据库交互即可使用类 - 所有操作都需要它完成
  • 所有方法都是类级别的方法,因此不需要类实例
  • 保存和检索符合 SimpleDBSerialization 协议的对象
  • 线程安全

入门指南

  • 将 SimpleDB_Classes 和 ABSQLite_Classes 文件夹添加到您的项目中,或者使用 CocoaPods

    pod 'SimpleDB', '~> 1.1'

  • 将 SQLite 库包括在 XCode 的链接区域

  • 开始保存和检索数据
[SimpleDB setValue:userInfoJSON forKey:userKey inTable:@"Users"];
NSArray *users = [SimpleDB keysInTable:@"Users"];
NSDictionary *userDict = [SimpleDB dictionaryValueForKey:users[0] inTable:@"Users"];

完整 API

@protocol SimpleDBSerialization <NSObject>
-(id)initWithJSON:(NSString*)json;
-(NSString*) jsonValue;
-(NSString*) keyValue;
@end

+(BOOL) hasKey:(NSString*) key inTable:(NSString*) table;
+(NSArray*) keysInTable:(NSString*) table;
+(NSArray*) keysInTable:(NSString*) table orderByJSONValueForKey:(NSString*)jsonOrderKey passingTest:(BOOL (^)(NSString* key, NSString* value, NSDate* dateAdded, NSDate* dateModified));
+(NSArray*) keysInTable:(NSString*) table reverseOrderByJSONValueForKey:(NSString*)jsonOrderKey passingTest:(BOOL (^)(NSString* key, NSString* value, NSDate* dateAdded, NSDate* dateModified));

+(NSString*) valueForKey:(NSString*) key inTable:(NSString*) table;
+(NSDictionary*) dictionaryValueForKey:(NSString*) key inTable:(NSString*) table;
+(id) jsonValueForKey:(NSString*) jsonKey tableKey:(NSString*) key inTable:(NSString*) table;
+(id) instanceOfClassForKey:(NSString*) key inTable:(NSString*) table;

+(void) setValue:(NSString*) value forKey:(NSString*) key inTable:(NSString*) table;
+(void) setValue:(NSString*) value forKey:(NSString*) key inTable:(NSString*) table autoDeleteAfter:(NSDate*) date;
+(void) setValueOfObject:(id)object inTable:(NSString*) table;
+(void) setValueOfObject:(id)object inTable:(NSString*) table autoDeleteAfter:(NSDate*) date;

+(void) deleteForKey:(NSString*) key inTable:(NSString*) table;

+(void) dropTable:(NSString*) table;
+(void) dropAllTables;

+(dbStatus) status;
+(NSString*) guid;
+(NSString*) stringValueForDate:(NSDate*) date;
+(NSDate*) dateValueForString:(NSString*) string;

ARC

包含的类需要 ARC

许可证

SimpleDB 在 MIT 许可下可用。请参阅包含的许可证文件

示例类和用法

Gift.h

#import <Foundation/Foundation.h>
#import "SimpleDB.h"

extern NSString *const kGiftTable;

@interface Gift : NSObject<SimpleDBSerialization>

@property (copy) NSString *event_id;
@property (copy) NSString *gift_id;
@property (strong) NSDate *date;
@property (copy) NSString *name;
@property (copy) NSString *email;
@property int giftAmount;
@property BOOL acknowledged;

+(instancetype) instanceForKey:(NSString *)key;
-(void) save;
-(instancetype) initWithDictionary:(NSDictionary *)gift;
-(instancetype) initWithJSON:(NSString *)json;
-(NSDictionary *) dictionaryValue;

@end

Gift.m

#import "Gift.h"

NSString *const kGiftTable = @"Gifts";

@implementation Gift

+(instancetype) instanceForKey:(NSString *)key {
    return [SimpleDB instanceOfClassForKey:key inTable:kGiftTable];
}

-(void) save {
    NSCalendar *gregorian = [NSCalendar currentCalendar];
    NSDateComponents *comps = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];

    [comps setYear:[comps year] + 1];
    NSDate *nextYear = [gregorian dateFromComponents:comps];

    [SimpleDB setValueOfObject:self inTable:kGiftTable autoDeleteAfter:nextYear];
}

-(instancetype) initWithDictionary:(NSDictionary *)gift {
    if (self = [super init]) {
        self.event_id = gift[@"event_id"];
        self.gift_id = gift[@"id"];
        self.date = [SimpleDB dateValueForString:gift[@"date"]];
        self.giftAmount = [gift[@"giftAmount"] intValue];
        self.acknowledged = [gift[@"acknowledged"] boolValue];
        self.name = gift[@"name"];
        self.email = gift[@"email"];
        return self;
    }

    return nil;
}

-(instancetype) initWithJSON:(NSString *)json {
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[json dataValue] options:0 error:NULL];

    return [self initWithDictionary:dict];
}

-(NSDictionary *) dictionaryValue {
    NSMutableDictionary *gift = [NSMutableDictionary dictionaryWithDictionary:@{ @"id": self.gift_id
                                                                                 , @"date": [SimpleDB stringValueForDate:self.date]
                                                                                 , @"giftAmount": @(self.giftAmount)
                                                                                 , @"acknowledged": (self.acknowledged ? @"YES" : @"NO") }];

    if (self.event_id) {
        gift[@"event_id"] = self.event_id;
    }

    if (self.name) {
        gift[@"name"] = self.name;
    }

    if (self.email) {
        gift[@"email"] = self.email;
    }

    return gift;
}

-(NSString *) jsonValue {
    NSDictionary *dict = [self dictionaryValue];    
    return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL] encoding:NSUTF8StringEncoding];
}

-(NSString *) keyValue {
    return self.gift_id;
}

-(BOOL) isEqual:(id)object {
    if (![object isKindOfClass:[self class]]) return NO;

    Gift *testObject = object;
    return [testObject.gift_id isEqualToString:self.gift_id];
}

-(NSUInteger) hash {
    return [self.gift_id hash];
}

@end

使用该类

// get array of unacknowledged gifts, order by amount
NSArray *keys = [SimpleDB keysInTable:kGiftTable
               orderByJSONValueForKey:@"amount"
                         passingTest:^BOOL (NSString *key, NSString *value, NSDate *dateAdded, NSDate *dateModified) {
                            Gift *gift = [[Gift alloc] initWithJSON:value];
                            return [!gift.acknowledged];
                         }];

for (NSString *giftKey in keys) {
  Gift *gift = [Gift instanceForKey:giftKey];
  if (gift) {
    // do some processing here;

    gift.acknowledged = YES;
    [gift save];
  }
}