Skip to content

Commit

Permalink
默认不开启自动关闭数据库连接功能
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Nov 30, 2017
1 parent a19b26c commit 0d6fb8c
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions LKDBHelper/Helper/LKDBHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ - (instancetype)initWithDBPath:(NSString *)filePath
self.threadLock = [[NSRecursiveLock alloc] init];
self.createdTableNames = [NSMutableArray array];
self.lastExecuteDBTime = [[NSDate date] timeIntervalSince1970];
self.autoCloseDBDelayTime = 20;
[self startAutoCloseTimer];

[self setDBPath:filePath];
[LKDBHelper dbHelperWithPath:nil save:self];
Expand Down Expand Up @@ -304,29 +302,35 @@ - (void)executeDB:(void (^)(FMDatabase *db))block

self.lastExecuteDBTime = [[NSDate date] timeIntervalSince1970];

if (!self.runingAutoCloseTimer) {
if (self.autoCloseDBDelayTime > 0) {
[self startAutoCloseTimer];
}

[self.threadLock unlock];
}

- (void)setAutoCloseDBTime:(NSInteger)time {
if (time < 10) {
time = 10;
if (time < 0) {
time = 0;
}
self.autoCloseDBDelayTime = time;
if (time > 0) {
[self startAutoCloseTimer];
}
}

- (void)startAutoCloseTimer {
if (self.runingAutoCloseTimer) {
return;
}
self.runingAutoCloseTimer = YES;
__weak LKDBHelper *wself = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.autoCloseDBDelayTime * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
__strong LKDBHelper *self = wself;
[self.threadLock lock];
self.runingAutoCloseTimer = NO;
BOOL hasClosed = [self autoCloseDBConnection];
if (!hasClosed) {
if (!hasClosed && self.autoCloseDBDelayTime > 0) {
[self startAutoCloseTimer];
}
[self.threadLock unlock];
Expand All @@ -335,7 +339,7 @@ - (void)startAutoCloseTimer {

- (BOOL)autoCloseDBConnection {
NSInteger now = [[NSDate date] timeIntervalSince1970];
if (now - self.lastExecuteDBTime > 10) {
if (now - self.lastExecuteDBTime > 5) {
[self closeDB];
return YES;
}
Expand Down Expand Up @@ -844,7 +848,7 @@ - (void)asyncBlock:(void (^)(void))block
#pragma mark - row count operation
- (NSInteger)rowCount:(Class)modelClass where:(id)where
{
return [self rowCountWithTableName:[modelClass getTableName] where:where];
return [self _rowCountWithTableName:nil where:where modelClass:modelClass];
}

- (void)rowCount:(Class)modelClass where:(id)where callback:(void (^)(NSInteger))callback
Expand All @@ -853,14 +857,32 @@ - (void)rowCount:(Class)modelClass where:(id)where callback:(void (^)(NSInteger)
return;
}
LKDBCode_Async_Begin;
NSInteger result = [sself rowCountWithTableName:[modelClass getTableName] where:where];
NSInteger result = [sself _rowCountWithTableName:nil where:where modelClass:modelClass];
callback(result);
LKDBCode_Async_End;
}

- (NSInteger)rowCountWithTableName:(NSString *)tableName where:(id)where
{
return [self _rowCountWithTableName:tableName where:where modelClass:nil];
}

- (NSInteger)_rowCountWithTableName:(NSString *)tableName where:(id)where modelClass:(Class)modelClass
{
if (!tableName) {
tableName = [modelClass getTableName];
}

LKDBCheck_tableNameIsInvalid(tableName);

if (modelClass) {
// 检测是否创建过表
[self.threadLock lock];
if ([self.createdTableNames containsObject:tableName] == NO) {
[self _createTableWithModelClass:modelClass tableName:tableName];
}
[self.threadLock unlock];
}

NSMutableString *rowCountSql = [NSMutableString stringWithFormat:@"select count(rowid) from %@", tableName];

Expand Down Expand Up @@ -1041,9 +1063,23 @@ - (NSString *)replaceTableNameIfNeeded:(NSString *)sql withModelClass:(Class)mod
if (!modelClass) {
return sql;
}

NSString *tableName = [modelClass getTableName];
if (!tableName) {
return sql;
}

if (modelClass) {
// 检测是否创建过表
[self.threadLock lock];
if ([self.createdTableNames containsObject:tableName] == NO) {
[self _createTableWithModelClass:modelClass tableName:tableName];
}
[self.threadLock unlock];
}

// replace @t to model table name
NSString *replaceString = [[modelClass getTableName] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *replaceString = [tableName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([sql hasSuffix:@" @t"]) {
sql = [sql stringByAppendingString:@" "];
}
Expand Down

0 comments on commit 0d6fb8c

Please sign in to comment.