Skip to content

Commit c03e461

Browse files
committed
增加修改数据库密码的API
1 parent 459885b commit c03e461

File tree

6 files changed

+138
-93
lines changed

6 files changed

+138
-93
lines changed

LKDBHelper.podspec.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "LKDBHelper",
3-
"version": "2.1.6",
3+
"version": "2.1.7",
44
"summary": "全自动的插入,查询,更新,删除, an automatic database operation thread-safe and not afraid of recursive deadlock",
55
"description": "全面支持 NSArray,NSDictionary, ModelClass, NSNumber, NSString, NSDate, NSData, UIColor, UIImage, CGRect, CGPoint, CGSize, NSRange, int,char,float, double, long.. 等属性的自动化操作(插入和查询)",
66
"homepage": "https://github.com/li6185377/LKDBHelper-SQLite-ORM",
@@ -10,7 +10,7 @@
1010
},
1111
"source": {
1212
"git": "https://github.com/li6185377/LKDBHelper-SQLite-ORM.git",
13-
"tag": "2.1.6"
13+
"tag": "2.1.7"
1414
},
1515
"platforms": {
1616
"ios": "4.3",

LKDBHelper/Helper/LKDBHelper.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66
// Copyright (c) 2012年 LJH. All rights reserved.
77
//
88

9-
#import <Foundation/Foundation.h>
10-
#import "FMDatabaseQueue.h"
119
#import "FMDatabase.h"
10+
#import "FMDatabaseQueue.h"
11+
#import <Foundation/Foundation.h>
1212

1313
#import "LKDBUtils.h"
1414

1515
#import "LKDB+Mapping.h"
1616

17-
#import "NSObject+LKModel.h"
1817
#import "NSObject+LKDBHelper.h"
18+
#import "NSObject+LKModel.h"
1919

2020
@interface LKDBHelper : NSObject
21+
22+
/**
23+
* @brief 是否打印数据库出错日志 默认 NO
24+
*/
25+
+ (void)setLogError:(BOOL)logError;
26+
2127
/**
2228
* @brief filepath the use of : "documents/db/" + fileName + ".db"
2329
* refer: FMDatabase.h + (instancetype)databaseWithPath:(NSString*)inPath;
@@ -32,11 +38,19 @@
3238
- (instancetype)initWithDBPath:(NSString*)filePath;
3339
- (void)setDBPath:(NSString*)filePath;
3440

35-
/**
36-
* @brief set and save encryption key.
37-
* refer: FMDatabase.h - (BOOL)setKey:(NSString*)key;
41+
/**
42+
* @brief current encryption key.
43+
*/
44+
@property (copy, readonly, nonatomic) NSString* encryptionKey;
45+
46+
/**
47+
* @brief Set encryption key
48+
refer: FMDatabase.h - (BOOL)setKey:(NSString*)key;
49+
* invoking after the `LKDBHelper initialize` in YourModelClass.m `getUsingLKDBHelper` function
3850
*/
39-
@property (strong, nonatomic) NSString* encryptionKey;
51+
- (BOOL)setKey:(NSString*)key;
52+
/// Reset encryption key
53+
- (BOOL)rekey:(NSString*)key;
4054

4155
/**
4256
* @brief execute database operations synchronously,not afraid of recursive deadlock
@@ -133,7 +147,7 @@
133147
NSMutableArray* array = [[LKDBHelper getUsingLKDBHelper] searc:[ModelClass class] withSQL:@"select rowid from name_table where name = ?", @"Swift"];
134148
*
135149
*/
136-
-(NSMutableArray *)search:(Class)modelClass withSQL:(NSString *)sql,...;
150+
- (NSMutableArray*)search:(Class)modelClass withSQL:(NSString*)sql, ...;
137151

138152
/**
139153
columns may NSArray or NSString if query column count == 1 return single column string array
@@ -242,5 +256,6 @@
242256
#pragma mark - deprecated
243257
+ (LKDBHelper*)sharedDBHelper __deprecated_msg("Method deprecated. Use `[Model getUsingLKDBHelper]`");
244258
- (BOOL)createTableWithModelClass:(Class)modelClass __deprecated_msg("Now you can not call it. Will automatically determine whether you need to create");
259+
- (void)setEncryptionKey:(NSString*)encryptionKey __deprecated_msg("Method deprecated. Use `setKey: OR resetKey:` invoking after the `LKDBHelper initialize` in YourModelClass.m `getUsingLKDBHelper` function");
245260
#pragma mark -
246261
@end

LKDBHelper/Helper/LKDBHelper.m

Lines changed: 95 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,32 @@ @interface LKDBHelper ()
5858
@end
5959

6060
@implementation LKDBHelper
61+
@synthesize encryptionKey = _encryptionKey;
62+
63+
static BOOL LKDBLogErrorEnable = NO;
64+
+(void)setLogError:(BOOL)logError
65+
{
66+
if (LKDBLogErrorEnable == logError) {
67+
return;
68+
}
69+
#ifdef DEBUG
70+
LKDBLogErrorEnable = logError;
71+
NSMutableArray* dbArray = [self dbHelperSingleArray];
72+
@synchronized(dbArray)
73+
{
74+
[dbArray enumerateObjectsUsingBlock:^(LKDBWeakObject* weakObj, NSUInteger idx, BOOL *stop) {
75+
[weakObj.obj executeDB:^(FMDatabase *db) {
76+
db.logsErrors = LKDBLogErrorEnable;
77+
}];
78+
}];
79+
}
80+
#endif
81+
}
82+
6183
+ (NSMutableArray*)dbHelperSingleArray
6284
{
6385
static __strong NSMutableArray* dbArray;
6486
static dispatch_once_t onceToken;
65-
6687
dispatch_once(&onceToken, ^{
6788
dbArray = [NSMutableArray array];
6889
});
@@ -83,7 +104,6 @@ + (LKDBHelper*)dbHelperWithPath:(NSString*)dbFilePath save:(LKDBHelper*)helper
83104
else if (dbFilePath) {
84105
for (NSInteger i = 0; i < dbArray.count;) {
85106
LKDBWeakObject* weakObj = [dbArray objectAtIndex:i];
86-
87107
if (weakObj.obj == nil) {
88108
[dbArray removeObjectAtIndex:i];
89109
continue;
@@ -92,7 +112,6 @@ + (LKDBHelper*)dbHelperWithPath:(NSString*)dbFilePath save:(LKDBHelper*)helper
92112
instance = weakObj.obj;
93113
break;
94114
}
95-
96115
i++;
97116
}
98117
}
@@ -217,11 +236,11 @@ - (void)setDBPath:(NSString*)filePath
217236
}
218237
#endif
219238

239+
///reset encryptionKey
220240
_encryptionKey = nil;
221-
222-
// 不打印错误日志
241+
223242
[_bindingQueue inDatabase:^(FMDatabase* db) {
224-
db.logsErrors = NO;
243+
db.logsErrors = LKDBLogErrorEnable;
225244
}];
226245
}
227246

@@ -237,14 +256,12 @@ - (void)executeDB:(void (^)(FMDatabase* db))block
237256
if (_bindingQueue == nil) {
238257
self.bindingQueue = [[FMDatabaseQueue alloc] initWithPath:_dbPath];
239258
[_bindingQueue inDatabase:^(FMDatabase* db) {
240-
// 不打印错误日志
241-
db.logsErrors = NO;
259+
db.logsErrors = LKDBLogErrorEnable;
242260
if (_encryptionKey.length > 0) {
243261
[db setKey:_encryptionKey];
244262
}
245263
}];
246264
}
247-
248265
[_bindingQueue inDatabase:^(FMDatabase* db) {
249266
self.usingdb = db;
250267
block(db);
@@ -353,55 +370,42 @@ - (NSMutableArray*)extractQuery:(NSMutableString*)query where:(id)where
353370
// dic where parse
354371
- (NSString*)dictionaryToSqlWhere:(NSDictionary*)dic andValues:(NSMutableArray*)values
355372
{
356-
NSMutableString* wherekey = [NSMutableString stringWithCapacity:0];
357-
358-
if ((dic != nil) && (dic.count > 0)) {
359-
NSArray* keys = dic.allKeys;
360-
361-
for (NSInteger i = 0; i < keys.count; i++) {
362-
NSString* key = [keys objectAtIndex:i];
363-
id va = [dic objectForKey:key];
364-
365-
if ([va isKindOfClass:[NSArray class]]) {
366-
NSArray* vlist = va;
367-
368-
if (vlist.count == 0) {
369-
continue;
370-
}
371-
372-
if (wherekey.length > 0) {
373-
[wherekey appendString:@" and"];
374-
}
375-
376-
[wherekey appendFormat:@" %@ in(", key];
377-
378-
for (NSInteger j = 0; j < vlist.count; j++) {
379-
[wherekey appendString:@"?"];
380-
381-
if (j == vlist.count - 1) {
382-
[wherekey appendString:@")"];
383-
}
384-
else {
385-
[wherekey appendString:@","];
386-
}
373+
if (dic.count == 0) {
374+
return @"";
375+
}
376+
387377

388-
[values addObject:[vlist objectAtIndex:j]];
378+
NSMutableString* wherekey = [NSMutableString stringWithCapacity:0];
379+
[dic enumerateKeysAndObjectsUsingBlock:^(NSString* key, id obj, BOOL *stop) {
380+
if ([obj isKindOfClass:[NSArray class]]) {
381+
NSArray* vlist = obj;
382+
if (vlist.count == 0) {
383+
return;
384+
}
385+
if (wherekey.length > 0) {
386+
[wherekey appendString:@" and"];
387+
}
388+
[wherekey appendFormat:@" %@ in(", key];
389+
[vlist enumerateObjectsUsingBlock:^(id vlist_obj, NSUInteger idx, BOOL *stop) {
390+
[wherekey appendString:@"?"];
391+
if (idx > 0) {
392+
[wherekey appendString:@","];
389393
}
394+
[values addObject:vlist_obj];
395+
}];
396+
[wherekey appendString:@")"];
397+
}
398+
else {
399+
if (wherekey.length > 0) {
400+
[wherekey appendFormat:@" and %@=?", key];
390401
}
391402
else {
392-
if (wherekey.length > 0) {
393-
[wherekey appendFormat:@" and %@=?", key];
394-
}
395-
else {
396-
[wherekey appendFormat:@" %@=?", key];
397-
}
398-
399-
[values addObject:va];
403+
[wherekey appendFormat:@" %@=?", key];
400404
}
405+
[values addObject:obj];
401406
}
402-
}
403-
404-
return wherekey;
407+
}];
408+
return [wherekey copy];
405409
}
406410

407411
// where sql statements about model primary keys
@@ -447,17 +451,32 @@ - (NSMutableString*)primaryKeyWhereSQLWithModel:(NSObject*)model addPValues:(NSM
447451
}
448452

449453
#pragma mark - set key
450-
- (void)setEncryptionKey:(NSString*)encryptionKey
454+
-(BOOL)setKey:(NSString *)key
451455
{
452-
_encryptionKey = encryptionKey;
453-
454-
if (_bindingQueue && (encryptionKey.length > 0)) {
455-
[self executeDB:^(FMDatabase* db) {
456-
[db setKey:_encryptionKey];
456+
_encryptionKey = [key copy];
457+
__block BOOL success = NO;
458+
if (_bindingQueue && (_encryptionKey.length > 0)) {
459+
[self executeDB:^(FMDatabase *db) {
460+
success = [db setKey:_encryptionKey];
457461
}];
458462
}
463+
return success;
464+
}
465+
-(BOOL)rekey:(NSString *)key
466+
{
467+
_encryptionKey = [key copy];
468+
__block BOOL success = NO;
469+
if (_bindingQueue && (_encryptionKey.length > 0)) {
470+
[self executeDB:^(FMDatabase *db) {
471+
success = [db rekey:_encryptionKey];
472+
}];
473+
}
474+
return success;
475+
}
476+
-(NSString *)encryptionKey
477+
{
478+
return _encryptionKey;
459479
}
460-
461480
#pragma mark - dealloc
462481
- (void)dealloc
463482
{
@@ -749,7 +768,7 @@ - (void)rowCount:(Class)modelClass where:(id)where callback:(void (^)(NSInteger)
749768
{
750769
if (callback) {
751770
LKDBCode_Async_Begin
752-
NSInteger result = [sself rowCountWithTableName:[modelClass getTableName] where:where];
771+
NSInteger result = [sself rowCountWithTableName:[modelClass getTableName] where:where];
753772
callback(result);
754773
LKDBCode_Async_End
755774
}
@@ -793,7 +812,7 @@ - (void)search:(Class)modelClass where:(id)where orderBy:(NSString*)orderBy offs
793812
{
794813
if (block) {
795814
LKDBCode_Async_Begin
796-
LKDBQueryParams* params = [[LKDBQueryParams alloc] init];
815+
LKDBQueryParams* params = [[LKDBQueryParams alloc] init];
797816
params.toClass = modelClass;
798817

799818
if ([where isKindOfClass:[NSDictionary class]]) {
@@ -897,7 +916,7 @@ - (NSMutableArray*)searchWithParams:(LKDBQueryParams*)params
897916
{
898917
if (params.callback) {
899918
LKDBCode_Async_Begin
900-
NSMutableArray* array = [sself searchBaseWithParams:params];
919+
NSMutableArray* array = [sself searchBaseWithParams:params];
901920
params.callback(array);
902921
LKDBCode_Async_End return nil;
903922
}
@@ -1094,7 +1113,7 @@ - (BOOL)insertToDB:(NSObject*)model
10941113
- (void)insertToDB:(NSObject*)model callback:(void (^)(BOOL))block
10951114
{
10961115
LKDBCode_Async_Begin
1097-
BOOL result = [sself insertBase:model];
1116+
BOOL result = [sself insertBase:model];
10981117

10991118
if (block) {
11001119
block(result);
@@ -1115,7 +1134,7 @@ - (BOOL)insertWhenNotExists:(NSObject*)model
11151134
- (void)insertWhenNotExists:(NSObject*)model callback:(void (^)(BOOL))block
11161135
{
11171136
LKDBCode_Async_Begin
1118-
BOOL result = [sself insertWhenNotExists:model];
1137+
BOOL result = [sself insertWhenNotExists:model];
11191138

11201139
if (block) {
11211140
block(result);
@@ -1220,7 +1239,7 @@ - (BOOL)updateToDB:(NSObject*)model where:(id)where
12201239
- (void)updateToDB:(NSObject*)model where:(id)where callback:(void (^)(BOOL))block
12211240
{
12221241
LKDBCode_Async_Begin
1223-
BOOL result = [sself updateToDBBase:model where:where];
1242+
BOOL result = [sself updateToDBBase:model where:where];
12241243

12251244
if (block) {
12261245
block(result);
@@ -1352,7 +1371,7 @@ - (BOOL)deleteToDB:(NSObject*)model
13521371
- (void)deleteToDB:(NSObject*)model callback:(void (^)(BOOL))block
13531372
{
13541373
LKDBCode_Async_Begin
1355-
BOOL isDeleted = [sself deleteToDBBase:model];
1374+
BOOL isDeleted = [sself deleteToDBBase:model];
13561375

13571376
if (block) {
13581377
block(isDeleted);
@@ -1412,7 +1431,7 @@ - (BOOL)deleteWithClass:(Class)modelClass where:(id)where
14121431
- (void)deleteWithClass:(Class)modelClass where:(id)where callback:(void (^)(BOOL))block
14131432
{
14141433
LKDBCode_Async_Begin
1415-
BOOL isDeleted = [sself deleteWithTableName:[modelClass getTableName] where:where];
1434+
BOOL isDeleted = [sself deleteWithTableName:[modelClass getTableName] where:where];
14161435

14171436
if (block) {
14181437
block(isDeleted);
@@ -1552,6 +1571,15 @@ + (void)clearFileWithTable:(Class)modelClass columns:(NSArray*)columns type:(NSI
15521571
@end
15531572

15541573
@implementation LKDBHelper (Deprecated_Nonfunctional)
1574+
-(void)setEncryptionKey:(NSString *)encryptionKey
1575+
{
1576+
_encryptionKey = [encryptionKey copy];
1577+
if (_bindingQueue && (_encryptionKey.length > 0)) {
1578+
[self executeDB:^(FMDatabase* db) {
1579+
[db setKey:_encryptionKey];
1580+
}];
1581+
}
1582+
}
15551583
+ (LKDBHelper*)sharedDBHelper
15561584
{
15571585
return [LKDBHelper getUsingLKDBHelper];

LKDBHelper/Helper/LKDBUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
+ (NSString*)stringWithDate:(NSDate*)date;
3333
///把String 转换成Date
3434
+ (NSDate*)dateWithString:(NSString*)str;
35+
///单例formatter
36+
+ (NSNumberFormatter*)numberFormatter;
37+
3538
@end
3639

3740
#ifdef DEBUG

0 commit comments

Comments
 (0)