// // FLLocalStorage.m // FirstLink // // Created by ascii on 16/1/15. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FLLocalStorage.h" static NSString *const FK_DATABASE_VER = @"1"; #pragma mark - table for history explore keyword NSString *const FK_TABLE_NAME_FOR_EXPLORE_HISTORY = @"fk_explore_keyword"; NSString *const FK_EXPLORE_KEYWORD_FIELD_PRIMARYKEY = @"id"; NSString *const FK_EXPLORE_KEYWORD_FIELD_KEYWORD = @"keyword"; NSString *const FK_EXPLORE_KEYWORD_FIELD_CREATE_TIME = @"create_time"; #pragma mark - table for local brand match NSString *const FK_TABLE_NAME_FOR_EXPLORE_LOCAL_BRAND = @"fk_local_brand"; NSString *const FK_EXPLORE_BRAND_FIELD_PRIMARYKEY = @"id"; NSString *const FK_EXPLORE_BRAND_FIELD_BRABD_ID = @"brand_id"; NSString *const FK_EXPLORE_BRAND_FIELD_NAME = @"brand_name"; NSString *const FK_EXPLORE_BRAND_PHOTO_URL = @"photo_url"; NSString *const FK_EXPLORE_BRAND_FIELD_CREATE_TIME = @"create_time"; @interface FLLocalStorage () @property (nonatomic, strong) FMDatabase *database; @end @implementation FLLocalStorage - (id)init { self = [super init]; if (self) { [self makeDirectory]; [self makeDatabase]; [self makeTables]; } return self; } - (NSString *)directoryPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; return [path stringByAppendingPathComponent:@"FKDatabase"]; } - (void)makeDirectory { NSString *dirPath = [self directoryPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:dirPath]) { [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:NO attributes:nil error:nil]; } } - (void)makeDatabase { NSString *dirPath = [self directoryPath]; DDLogDebug(@"%@", dirPath); NSString *dbName = [NSString stringWithFormat:@"FKDatabase_%@.sqlite", FK_DATABASE_VER]; NSString* dbPath = [dirPath stringByAppendingPathComponent:dbName]; self.database = [FMDatabase databaseWithPath:dbPath]; } - (void)makeTables { if (self.database && [self.database open]) { NSString *makeExploreTableSql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@ integer primary key autoincrement, %@ text not null, %@ text);" , FK_TABLE_NAME_FOR_EXPLORE_HISTORY , FK_EXPLORE_KEYWORD_FIELD_PRIMARYKEY , FK_EXPLORE_KEYWORD_FIELD_KEYWORD , FK_EXPLORE_KEYWORD_FIELD_CREATE_TIME]; NSString *makeLocalBrandTableSql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@ integer primary key autoincrement, %@ text, %@ text not null, %@ text, %@ text);" , FK_TABLE_NAME_FOR_EXPLORE_LOCAL_BRAND , FK_EXPLORE_BRAND_FIELD_PRIMARYKEY , FK_EXPLORE_BRAND_FIELD_BRABD_ID , FK_EXPLORE_BRAND_FIELD_NAME , FK_EXPLORE_BRAND_PHOTO_URL , FK_EXPLORE_BRAND_FIELD_CREATE_TIME]; [self.database executeUpdate:makeExploreTableSql]; [self.database executeUpdate:makeLocalBrandTableSql]; } } - (FMDatabase *)getDatabase { return self.database; } @end