No Description

FLLocalStorage.m 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // FLLocalStorage.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 16/1/15.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FLLocalStorage.h"
  9. static NSString *const FK_DATABASE_VER = @"1";
  10. #pragma mark - table for history explore keyword
  11. NSString *const FK_TABLE_NAME_FOR_EXPLORE_HISTORY = @"fk_explore_keyword";
  12. NSString *const FK_EXPLORE_KEYWORD_FIELD_PRIMARYKEY = @"id";
  13. NSString *const FK_EXPLORE_KEYWORD_FIELD_KEYWORD = @"keyword";
  14. NSString *const FK_EXPLORE_KEYWORD_FIELD_CREATE_TIME = @"create_time";
  15. #pragma mark - table for local brand match
  16. NSString *const FK_TABLE_NAME_FOR_EXPLORE_LOCAL_BRAND = @"fk_local_brand";
  17. NSString *const FK_EXPLORE_BRAND_FIELD_PRIMARYKEY = @"id";
  18. NSString *const FK_EXPLORE_BRAND_FIELD_BRABD_ID = @"brand_id";
  19. NSString *const FK_EXPLORE_BRAND_FIELD_NAME = @"brand_name";
  20. NSString *const FK_EXPLORE_BRAND_PHOTO_URL = @"photo_url";
  21. NSString *const FK_EXPLORE_BRAND_FIELD_CREATE_TIME = @"create_time";
  22. @interface FLLocalStorage ()
  23. @property (nonatomic, strong) FMDatabase *database;
  24. @end
  25. @implementation FLLocalStorage
  26. - (id)init {
  27. self = [super init];
  28. if (self) {
  29. [self makeDirectory];
  30. [self makeDatabase];
  31. [self makeTables];
  32. }
  33. return self;
  34. }
  35. - (NSString *)directoryPath {
  36. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  37. NSString *path = [paths objectAtIndex:0];
  38. return [path stringByAppendingPathComponent:@"FKDatabase"];
  39. }
  40. - (void)makeDirectory {
  41. NSString *dirPath = [self directoryPath];
  42. NSFileManager *fileManager = [NSFileManager defaultManager];
  43. if (![fileManager fileExistsAtPath:dirPath]) {
  44. [fileManager createDirectoryAtPath:dirPath
  45. withIntermediateDirectories:NO
  46. attributes:nil
  47. error:nil];
  48. }
  49. }
  50. - (void)makeDatabase {
  51. NSString *dirPath = [self directoryPath];
  52. DDLogDebug(@"%@", dirPath);
  53. NSString *dbName = [NSString stringWithFormat:@"FKDatabase_%@.sqlite", FK_DATABASE_VER];
  54. NSString* dbPath = [dirPath stringByAppendingPathComponent:dbName];
  55. self.database = [FMDatabase databaseWithPath:dbPath];
  56. }
  57. - (void)makeTables {
  58. if (self.database && [self.database open]) {
  59. NSString *makeExploreTableSql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@ integer primary key autoincrement, %@ text not null, %@ text);"
  60. , FK_TABLE_NAME_FOR_EXPLORE_HISTORY
  61. , FK_EXPLORE_KEYWORD_FIELD_PRIMARYKEY
  62. , FK_EXPLORE_KEYWORD_FIELD_KEYWORD
  63. , FK_EXPLORE_KEYWORD_FIELD_CREATE_TIME];
  64. NSString *makeLocalBrandTableSql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@ integer primary key autoincrement, %@ text, %@ text not null, %@ text, %@ text);"
  65. , FK_TABLE_NAME_FOR_EXPLORE_LOCAL_BRAND
  66. , FK_EXPLORE_BRAND_FIELD_PRIMARYKEY
  67. , FK_EXPLORE_BRAND_FIELD_BRABD_ID
  68. , FK_EXPLORE_BRAND_FIELD_NAME
  69. , FK_EXPLORE_BRAND_PHOTO_URL
  70. , FK_EXPLORE_BRAND_FIELD_CREATE_TIME];
  71. [self.database executeUpdate:makeExploreTableSql];
  72. [self.database executeUpdate:makeLocalBrandTableSql];
  73. }
  74. }
  75. - (FMDatabase *)getDatabase {
  76. return self.database;
  77. }
  78. @end