From 1c7fc13a6703941b68e6ce2f7b7c4374a37de7b2 Mon Sep 17 00:00:00 2001 From: "lijianghuai@xiaoyouzi.com" Date: Mon, 19 Jun 2017 16:06:00 +0800 Subject: [PATCH] code clean & fix #106 --- LKDBHelper/Helper/LKDB+Mapping.m | 18 +- LKDBHelper/Helper/LKDBHelper.h | 8 +- LKDBHelper/Helper/LKDBHelper.m | 158 ++++++--------- LKDBHelper/Helper/LKDBUtils.h | 54 ++--- LKDBHelper/Helper/LKDBUtils.m | 2 +- LKDBHelper/Helper/NSObject+LKDBHelper.h | 10 +- LKDBHelper/Helper/NSObject+LKDBHelper.m | 7 +- LKDBHelper/Helper/NSObject+LKModel.h | 4 +- LKDBHelper/Helper/NSObject+LKModel.m | 258 +++++++++--------------- 9 files changed, 199 insertions(+), 320 deletions(-) diff --git a/LKDBHelper/Helper/LKDB+Mapping.m b/LKDBHelper/Helper/LKDB+Mapping.m index 2fa185d..faa4144 100644 --- a/LKDBHelper/Helper/LKDB+Mapping.m +++ b/LKDBHelper/Helper/LKDB+Mapping.m @@ -74,8 +74,7 @@ + (void)setUserCalculateForCN:(NSString *)columnName LKDBProperty *property = [infos objectWithSqlColumnName:columnName]; if (property) { [property enableUserCalculate]; - } - else { + } else { [infos addDBPropertyWithType:LKSQL_Mapping_UserCalculate cname:columnName ctype:LKSQL_Type_Text pname:columnName ptype:@"NSString"]; } } @@ -115,11 +114,9 @@ + (void)setTableColumnName:(NSString *)columnName bindingPropertyName:(NSString if (column) { [infos updateProperty:column propertyName:propertyName]; column.propertyType = property.propertyType; - } - else if ([property.sqlColumnName isEqualToString:property.propertyName]) { + } else if ([property.sqlColumnName isEqualToString:property.propertyName]) { [infos updateProperty:property sqlColumnName:columnName]; - } - else { + } else { [infos addDBPropertyWithType:LKSQL_Mapping_Binding cname:columnName ctype:LKSQL_Type_Text pname:propertyName ptype:property.propertyType]; } } @@ -169,14 +166,12 @@ - (id)initWithKeyMapping:(NSDictionary *)keyMapping propertyNames:(NSArray *)pro if ([mappingValue isEqualToString:LKSQL_Mapping_UserCalculate]) { type = LKSQL_Mapping_UserCalculate; column_type = LKSQL_Type_Text; - } - else { + } else { if ([mappingValue isEqualToString:LKSQL_Mapping_Inherit] || [mappingValue isEqualToString:LKSQL_Mapping_Binding]) { type = LKSQL_Mapping_Inherit; property_name = column_name; - } - else { + } else { type = LKSQL_Mapping_Binding; property_name = mappingValue; } @@ -191,8 +186,7 @@ - (id)initWithKeyMapping:(NSDictionary *)keyMapping propertyNames:(NSArray *)pro [self addDBPropertyWithType:type cname:column_name ctype:column_type pname:property_name ptype:property_type]; } - } - else { + } else { for (NSInteger i = 0; i < propertyNames.count; i++) { type = LKSQL_Mapping_Inherit; diff --git a/LKDBHelper/Helper/LKDBHelper.h b/LKDBHelper/Helper/LKDBHelper.h index 558896d..beb8c41 100644 --- a/LKDBHelper/Helper/LKDBHelper.h +++ b/LKDBHelper/Helper/LKDBHelper.h @@ -6,12 +6,12 @@ // Copyright (c) 2012年 LJH. All rights reserved. // -#import -#import -#import "LKDBUtils.h" #import "LKDB+Mapping.h" +#import "LKDBUtils.h" #import "NSObject+LKDBHelper.h" #import "NSObject+LKModel.h" +#import +#import NS_ASSUME_NONNULL_BEGIN @@ -176,7 +176,7 @@ NS_ASSUME_NONNULL_BEGIN orderBy:(nullable NSString *)orderBy offset:(NSInteger)offset count:(NSInteger)count - callback:(void (^)(NSMutableArray * _Nullable array))block; + callback:(void (^)(NSMutableArray *_Nullable array))block; ///return first model or nil - (nullable id)searchSingle:(Class)modelClass where:(nullable id)where orderBy:(nullable NSString *)orderBy; diff --git a/LKDBHelper/Helper/LKDBHelper.m b/LKDBHelper/Helper/LKDBHelper.m index 786882e..1afe994 100644 --- a/LKDBHelper/Helper/LKDBHelper.m +++ b/LKDBHelper/Helper/LKDBHelper.m @@ -107,15 +107,13 @@ + (LKDBHelper *)dbHelperWithPath:(NSString *)dbFilePath save:(LKDBHelper *)helpe LKDBWeakObject *weakObj = [[LKDBWeakObject alloc] init]; weakObj.obj = helper; [dbArray addObject:weakObj]; - } - else if (dbFilePath) { + } else if (dbFilePath) { for (NSInteger i = 0; i < dbArray.count;) { LKDBWeakObject *weakObj = [dbArray objectAtIndex:i]; if (weakObj.obj == nil) { [dbArray removeObjectAtIndex:i]; continue; - } - else if ([weakObj.obj.dbPath isEqualToString:dbFilePath]) { + } else if ([weakObj.obj.dbPath isEqualToString:dbFilePath]) { instance = weakObj.obj; break; } @@ -148,8 +146,7 @@ - (instancetype)initWithDBPath:(NSString *)filePath if (helper) { self = helper; - } - else { + } else { self = [super init]; if (self) { @@ -171,8 +168,7 @@ + (NSString *)getDBPathWithDBName:(NSString *)dbName if ([dbName hasSuffix:@".db"] == NO) { fileName = [NSString stringWithFormat:@"%@.db", dbName]; - } - else { + } else { fileName = dbName; } @@ -202,7 +198,7 @@ - (void)setDBPath:(NSString *)filePath if ((isCreated == NO) || (isDir == NO)) { NSError *error = nil; #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED - NSDictionary *attributes = @{ NSFileProtectionKey : NSFileProtectionNone }; + NSDictionary *attributes = @{NSFileProtectionKey: NSFileProtectionNone}; #else NSDictionary *attributes = nil; #endif @@ -213,14 +209,13 @@ - (void)setDBPath:(NSString *)filePath if (success == NO) { LKErrorLog(@"create dir error: %@", error.debugDescription); } - } - else { - /** + } else { +/** * @brief Disk I/O error when device is locked * https://github.com/ccgus/fmdb/issues/262 */ #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED - [fileManager setAttributes:@{ NSFileProtectionKey : NSFileProtectionNone } + [fileManager setAttributes:@{ NSFileProtectionKey: NSFileProtectionNone } ofItemAtPath:dirPath error:nil]; #endif @@ -240,10 +235,10 @@ - (void)setDBPath:(NSString *)filePath flags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FILEPROTECTION_NONE]; #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED if ([fileManager fileExistsAtPath:filePath]) { - [fileManager setAttributes:@{ NSFileProtectionKey : NSFileProtectionNone } ofItemAtPath:filePath error:nil]; + [fileManager setAttributes:@{ NSFileProtectionKey: NSFileProtectionNone } ofItemAtPath:filePath error:nil]; } #endif - + ///reset encryptionKey _encryptionKey = nil; @@ -264,8 +259,7 @@ - (void)executeDB:(void (^)(FMDatabase *db))block if (self.usingdb != nil) { block(self.usingdb); - } - else { + } else { if (self.bindingQueue == nil) { self.bindingQueue = [[FMDatabaseQueue alloc] initWithPath:_dbPath]; [self.createdTableNames removeAllObjects]; @@ -293,8 +287,7 @@ - (BOOL)executeSQL:(NSString *)sql arguments:(NSArray *)args [self executeDB:^(FMDatabase *db) { if (args.count > 0) { execute = [db executeUpdate:sql withArgumentsInArray:args]; - } - else { + } else { execute = [db executeUpdate:sql]; } @@ -314,8 +307,7 @@ - (NSString *)executeScalarWithSQL:(NSString *)sql arguments:(NSArray *)args if (args.count > 0) { set = [db executeQuery:sql withArgumentsInArray:args]; - } - else { + } else { set = [db executeQuery:sql]; } @@ -352,8 +344,7 @@ - (void)executeForTransaction:(BOOL (^)(LKDBHelper *))block if (!inTransacttion) { if (isCommit) { [db commit]; - } - else { + } else { [db rollback]; } } @@ -367,8 +358,7 @@ - (NSMutableArray *)extractQuery:(NSMutableString *)query where:(id)where if ([where isKindOfClass:[NSString class]] && ([LKDBUtils checkStringIsEmpty:where] == NO)) { [query appendFormat:@" where %@", where]; - } - else if ([where isKindOfClass:[NSDictionary class]]) { + } else if ([where isKindOfClass:[NSDictionary class]]) { NSDictionary *dicWhere = where; if (dicWhere.count > 0) { @@ -406,12 +396,10 @@ - (NSString *)dictionaryToSqlWhere:(NSDictionary *)dic andValues:(NSMutableArray [values addObject:vlist_obj]; }]; [wherekey appendString:@")"]; - } - else { + } else { if (wherekey.length > 0) { [wherekey appendFormat:@" and %@=?", key]; - } - else { + } else { [wherekey appendFormat:@" %@=?", key]; } [values addObject:obj]; @@ -437,8 +425,7 @@ - (NSMutableString *)primaryKeyWhereSQLWithModel:(NSObject *)model addPValues:(N if (property && [property.type isEqualToString:LKSQL_Mapping_UserCalculate]) { pvalue = [model userGetValueForModel:property]; - } - else if (pk && property) { + } else if (pk && property) { pvalue = [model modelGetValue:property]; } @@ -450,8 +437,7 @@ - (NSMutableString *)primaryKeyWhereSQLWithModel:(NSObject *)model addPValues:(N if (addPValues) { [pwhere appendFormat:@" %@=? ", pk]; [addPValues addObject:pvalue]; - } - else { + } else { [pwhere appendFormat:@" %@='%@' ", pk, pvalue]; } } @@ -605,12 +591,11 @@ - (void)fixSqlColumnsWithClass:(Class)clazz tableName:(NSString *)tableName [addColumePars appendFormat:@" %@ %@", LKSQL_Attribute_Default, property.defaultValue]; } NSString *alertSQL = [NSString stringWithFormat:@"alter table %@ add column %@", tableName, addColumePars]; - NSString *defaultValue = @"0"; + NSString *defaultValue = property.defaultValue ?: @"0"; if ([property.sqlColumnType isEqualToString:LKSQL_Type_Text]) { if (LKDBNullIsEmptyString) { defaultValue = @""; - } - else { + } else { defaultValue = @"null"; } } @@ -769,20 +754,18 @@ - (id)modelValueWithProperty:(LKDBProperty *)property model:(NSObject *)model if (property.isUserCalculate) { value = [model userGetValueForModel:property]; - } - else { + } else { value = [model modelGetValue:property]; } if (value == nil) { if (LKDBNullIsEmptyString) { value = @""; - } - else { + } else { value = [NSNull null]; } } - + return value; } @@ -853,8 +836,7 @@ - (void)search:(Class)modelClass where:(id)where orderBy:(NSString *)orderBy off if ([where isKindOfClass:[NSDictionary class]]) { params.whereDic = where; - } - else if ([where isKindOfClass:[NSString class]]) { + } else if ([where isKindOfClass:[NSString class]]) { params.where = where; } @@ -884,7 +866,7 @@ - (NSMutableArray *)searchBaseWithParams:(LKDBQueryParams *)params LKErrorLog(@"you search pars:%@! \n tableName is empty", params.getAllPropertysString); return nil; } - + // 检测是否创建过表 [self.threadLock lock]; if ([self.createdTableNames containsObject:db_tableName] == NO) { @@ -898,13 +880,11 @@ - (NSMutableArray *)searchBaseWithParams:(LKDBQueryParams *)params if (params.columnArray.count > 0) { columnCount = params.columnArray.count; columnsString = [params.columnArray componentsJoinedByString:@","]; - } - else if ([LKDBUtils checkStringIsEmpty:params.columns] == NO) { + } else if ([LKDBUtils checkStringIsEmpty:params.columns] == NO) { columnsString = params.columns; NSArray *array = [params.columns componentsSeparatedByString:@","]; columnCount = array.count; - } - else { + } else { columnsString = @"*"; } @@ -915,8 +895,7 @@ - (NSMutableArray *)searchBaseWithParams:(LKDBQueryParams *)params whereValues = [NSMutableArray arrayWithCapacity:params.whereDic.count]; NSString *wherekey = [self dictionaryToSqlWhere:params.whereDic andValues:whereValues]; [query appendFormat:@" where %@", wherekey]; - } - else if ([LKDBUtils checkStringIsEmpty:params.where] == NO) { + } else if ([LKDBUtils checkStringIsEmpty:params.where] == NO) { [query appendFormat:@" where %@", params.where]; } @@ -937,15 +916,13 @@ - (NSMutableArray *)searchBaseWithParams:(LKDBQueryParams *)params if (whereValues.count == 0) { set = [db executeQuery:query]; - } - else { + } else { set = [db executeQuery:query withArgumentsInArray:whereValues]; } if (columnCount == 1) { results = [self executeOneColumnResult:set]; - } - else { + } else { results = [self executeResult:set Class:params.toClass tableName:db_tableName]; } @@ -962,8 +939,7 @@ - (NSMutableArray *)searchWithParams:(LKDBQueryParams *)params params.callback(array); LKDBCode_Async_End; return nil; - } - else { + } else { return [self searchBaseWithParams:params]; } } @@ -976,15 +952,13 @@ - (NSMutableArray *)searchBase:(Class)modelClass columns:(id)columns where:(id)w if ([columns isKindOfClass:[NSArray class]]) { params.columnArray = columns; - } - else if ([columns isKindOfClass:[NSString class]]) { + } else if ([columns isKindOfClass:[NSString class]]) { params.columns = columns; } if ([where isKindOfClass:[NSDictionary class]]) { params.whereDic = where; - } - else if ([where isKindOfClass:[NSString class]]) { + } else if ([where isKindOfClass:[NSString class]]) { params.where = where; } @@ -1000,7 +974,7 @@ - (NSString *)replaceTableNameIfNeeded:(NSString *)sql withModelClass:(Class)mod if (!modelClass) { return sql; } - + // replace @t to model table name NSString *replaceString = [[modelClass getTableName] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if ([sql hasSuffix:@" @t"]) { @@ -1071,8 +1045,7 @@ - (void)sqlString:(NSMutableString *)sql groupBy:(NSString *)groupBy orderBy:(NS if (count > 0) { [sql appendFormat:@" limit %ld offset %ld", (long)count, (long)offset]; - } - else if (offset > 0) { + } else if (offset > 0) { [sql appendFormat:@" limit %d offset %ld", INT_MAX, (long)offset]; } } @@ -1086,8 +1059,7 @@ - (NSMutableArray *)executeOneColumnResult:(FMResultSet *)set if (string) { [array addObject:string]; - } - else { + } else { NSData *data = [set dataForColumnIndex:0]; if (data) { @@ -1104,19 +1076,18 @@ - (NSMutableArray *)executeResult:(FMResultSet *)set Class:(Class)modelClass tab NSMutableArray *array = [NSMutableArray arrayWithCapacity:0]; if (!modelClass) { while ([set next]) { - NSDictionary * dict = [set resultDictionary]; + NSDictionary *dict = [set resultDictionary]; if (dict) { [array addObject:dict]; } } - } - else { + } else { LKModelInfos *infos = [modelClass getModelInfos]; NSInteger columnCount = [set columnCount]; - + ///当主键是int类型时 会替换掉rowid NSString *rowidAliasName = [modelClass db_rowidAliasName]; - + while ([set next]) { NSObject *bindingModel = [[modelClass alloc] init]; if (bindingModel == nil) { @@ -1125,28 +1096,26 @@ - (NSMutableArray *)executeResult:(FMResultSet *)set Class:(Class)modelClass tab for (int i = 0; i < columnCount; i++) { NSString *sqlName = [set columnNameForIndex:i]; LKDBProperty *property = [infos objectWithSqlColumnName:sqlName]; - + BOOL isRowid = [[sqlName lowercaseString] isEqualToString:@"rowid"]; - + if ((isRowid == NO) && (property == nil)) { continue; } - + if (isRowid && ((property == nil) || [property.sqlColumnType isEqualToString:LKSQL_Type_Int])) { bindingModel.rowid = [set longForColumnIndex:i]; - } - else { + } else { BOOL isUserCalculate = [property.type isEqualToString:LKSQL_Mapping_UserCalculate]; - + if (property.propertyName && (isUserCalculate == NO)) { NSString *sqlValue = [set stringForColumnIndex:i]; [bindingModel modelSetValue:property value:sqlValue]; - + if ([rowidAliasName isEqualToString:sqlName]) { bindingModel.rowid = [set longForColumnIndex:i]; } - } - else { + } else { NSData *sqlData = [set dataForColumnIndex:i]; NSString *sqlValue = nil; if (sqlData) { @@ -1348,8 +1317,7 @@ - (BOOL)updateToDBBase:(NSObject *)model where:(id)where if (rowCount > 0) { continue; } - } - else { + } else { continue; } } @@ -1364,18 +1332,15 @@ - (BOOL)updateToDBBase:(NSObject *)model where:(id)where // 添加where 语句 if ([where isKindOfClass:[NSString class]] && ([LKDBUtils checkStringIsEmpty:where] == NO)) { [updateSQL appendString:where]; - } - else if ([where isKindOfClass:[NSDictionary class]] && ([(NSDictionary *)where count] > 0)) { + } else if ([where isKindOfClass:[NSDictionary class]] && ([(NSDictionary *)where count] > 0)) { NSMutableArray *valuearray = [NSMutableArray array]; NSString *sqlwhere = [self dictionaryToSqlWhere:where andValues:valuearray]; [updateSQL appendString:sqlwhere]; [updateValues addObjectsFromArray:valuearray]; - } - else if (model.rowid > 0) { + } else if (model.rowid > 0) { [updateSQL appendFormat:@" rowid=%ld", (long)model.rowid]; - } - else { + } else { // 如果不通过 rowid 来 更新数据 那 primarykey 一定要有值 NSString *pwhere = [self primaryKeyWhereSQLWithModel:model addPValues:updateValues]; @@ -1446,8 +1411,7 @@ - (BOOL)deleteToDBBase:(NSObject *)model if (model.rowid > 0) { [deleteSQL appendFormat:@"rowid = %ld", (long)model.rowid]; - } - else { + } else { NSString *pwhere = [self primaryKeyWhereSQLWithModel:model addPValues:parsArray]; if (pwhere.length == 0) { @@ -1504,8 +1468,7 @@ - (BOOL)isExistsModel:(NSObject *)model if (model.rowid > 0) { pwhere = [NSString stringWithFormat:@"rowid=%ld", (long)model.rowid]; - } - else { + } else { pwhere = [self primaryKeyWhereSQLWithModel:model addPValues:nil]; } @@ -1553,13 +1516,12 @@ + (void)clearFileWithTable:(Class)modelClass columns:(NSArray *)columns type:(NS dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ NSString *testpath = nil; switch (type) { - case 1: - testpath = [modelClass getDBImagePathWithName:LKTestDirFilename]; - break; - - case 2: - testpath = [modelClass getDBDataPathWithName:LKTestDirFilename]; - break; + case 1: { + testpath = [modelClass getDBImagePathWithName:LKTestDirFilename]; + } break; + case 2: { + testpath = [modelClass getDBDataPathWithName:LKTestDirFilename]; + } break; } if ([LKDBUtils checkStringIsEmpty:testpath]) { diff --git a/LKDBHelper/Helper/LKDBUtils.h b/LKDBHelper/Helper/LKDBUtils.h index bff4270..4672ae2 100644 --- a/LKDBHelper/Helper/LKDBUtils.h +++ b/LKDBHelper/Helper/LKDBUtils.h @@ -57,39 +57,39 @@ NS_ASSUME_NONNULL_BEGIN #define __LKDBWeak #endif -static NSString * const LKSQL_Type_Text = @"text"; -static NSString * const LKSQL_Type_Int = @"integer"; -static NSString * const LKSQL_Type_Double = @"double"; -static NSString * const LKSQL_Type_Blob = @"blob"; +static NSString *const LKSQL_Type_Text = @"text"; +static NSString *const LKSQL_Type_Int = @"integer"; +static NSString *const LKSQL_Type_Double = @"double"; +static NSString *const LKSQL_Type_Blob = @"blob"; -static NSString * const LKSQL_Attribute_NotNull = @"NOT NULL"; -static NSString * const LKSQL_Attribute_PrimaryKey = @"PRIMARY KEY"; -static NSString * const LKSQL_Attribute_Default = @"DEFAULT"; -static NSString * const LKSQL_Attribute_Unique = @"UNIQUE"; -static NSString * const LKSQL_Attribute_Check = @"CHECK"; -static NSString * const LKSQL_Attribute_ForeignKey = @"FOREIGN KEY"; +static NSString *const LKSQL_Attribute_NotNull = @"NOT NULL"; +static NSString *const LKSQL_Attribute_PrimaryKey = @"PRIMARY KEY"; +static NSString *const LKSQL_Attribute_Default = @"DEFAULT"; +static NSString *const LKSQL_Attribute_Unique = @"UNIQUE"; +static NSString *const LKSQL_Attribute_Check = @"CHECK"; +static NSString *const LKSQL_Attribute_ForeignKey = @"FOREIGN KEY"; -static NSString * const LKSQL_Convert_FloatType = @"float_double_decimal"; -static NSString * const LKSQL_Convert_IntType = @"int_char_short_long"; -static NSString * const LKSQL_Convert_BlobType = @""; +static NSString *const LKSQL_Convert_FloatType = @"float_double_decimal"; +static NSString *const LKSQL_Convert_IntType = @"int_char_short_long"; +static NSString *const LKSQL_Convert_BlobType = @""; -static NSString * const LKSQL_Mapping_Inherit = @"LKDBInherit"; -static NSString * const LKSQL_Mapping_Binding = @"LKDBBinding"; -static NSString * const LKSQL_Mapping_UserCalculate = @"LKDBUserCalculate"; +static NSString *const LKSQL_Mapping_Inherit = @"LKDBInherit"; +static NSString *const LKSQL_Mapping_Binding = @"LKDBBinding"; +static NSString *const LKSQL_Mapping_UserCalculate = @"LKDBUserCalculate"; -static NSString * const LKDB_TypeKey = @"DB_Type"; +static NSString *const LKDB_TypeKey = @"DB_Type"; -static NSString * const LKDB_TypeKey_Model = @"DB_Type_Model"; -static NSString * const LKDB_TypeKey_JSON = @"DB_Type_JSON"; -static NSString * const LKDB_TypeKey_Combo = @"DB_Type_Combo"; -static NSString * const LKDB_TypeKey_Date = @"DB_Type_Date"; +static NSString *const LKDB_TypeKey_Model = @"DB_Type_Model"; +static NSString *const LKDB_TypeKey_JSON = @"DB_Type_JSON"; +static NSString *const LKDB_TypeKey_Combo = @"DB_Type_Combo"; +static NSString *const LKDB_TypeKey_Date = @"DB_Type_Date"; -static NSString * const LKDB_ValueKey = @"DB_Value"; +static NSString *const LKDB_ValueKey = @"DB_Value"; -static NSString * const LKDB_TableNameKey = @"DB_TableName"; -static NSString * const LKDB_ClassKey = @"DB_Class"; -static NSString * const LKDB_RowIdKey = @"DB_RowId"; -static NSString * const LKDB_PValueKey = @"DB_PKeyValue"; +static NSString *const LKDB_TableNameKey = @"DB_TableName"; +static NSString *const LKDB_ClassKey = @"DB_Class"; +static NSString *const LKDB_RowIdKey = @"DB_RowId"; +static NSString *const LKDB_PValueKey = @"DB_PKeyValue"; ///Object-c type converted to SQLite type 把Object-c 类型 转换为sqlite 类型 extern NSString *LKSQLTypeFromObjcType(NSString *objcType); @@ -114,7 +114,7 @@ extern NSString *LKSQLTypeFromObjcType(NSString *objcType); @property (nullable, nonatomic, assign) Class toClass; -@property (nullable, nonatomic, copy) void (^callback)(NSMutableArray * _Nullable results); +@property (nullable, nonatomic, copy) void (^callback)(NSMutableArray *_Nullable results); @end diff --git a/LKDBHelper/Helper/LKDBUtils.m b/LKDBHelper/Helper/LKDBUtils.m index 83b71af..94444aa 100644 --- a/LKDBHelper/Helper/LKDBUtils.m +++ b/LKDBHelper/Helper/LKDBUtils.m @@ -56,7 +56,7 @@ - (NSString *)stringFromNumber:(NSNumber *)number { NSString *string = [number stringValue]; if (!string) { - string = [NSString stringWithFormat:@"%lf",[number doubleValue]]; + string = [NSString stringWithFormat:@"%lf", [number doubleValue]]; } return string; } diff --git a/LKDBHelper/Helper/NSObject+LKDBHelper.h b/LKDBHelper/Helper/NSObject+LKDBHelper.h index a64aa36..7e0f6b5 100644 --- a/LKDBHelper/Helper/NSObject+LKDBHelper.h +++ b/LKDBHelper/Helper/NSObject+LKDBHelper.h @@ -6,8 +6,8 @@ // Copyright (c) 2013年 ljh. All rights reserved. // -#import #import "LKDBHelper.h" +#import NS_ASSUME_NONNULL_BEGIN @@ -95,15 +95,15 @@ NS_ASSUME_NONNULL_BEGIN ///异步插入数据 async insert array , completed 也是在子线程直接回调的 + (void)insertArrayByAsyncToDB:(NSArray *)models; -+ (void)insertArrayByAsyncToDB:(NSArray *)models completed:(void (^ _Nullable)(BOOL allInserted))completedBlock; ++ (void)insertArrayByAsyncToDB:(NSArray *)models completed:(void (^_Nullable)(BOOL allInserted))completedBlock; ///begin translate for insert models 开始事务插入数组 + (void)insertToDBWithArray:(NSArray *)models - filter:(void (^ _Nullable)(id model, BOOL inserted, BOOL * _Nullable rollback))filter; + filter:(void (^_Nullable)(id model, BOOL inserted, BOOL *_Nullable rollback))filter; + (void)insertToDBWithArray:(NSArray *)models - filter:(void (^ _Nullable)(id model, BOOL inserted, BOOL * _Nullable rollback))filter - completed:(void (^ _Nullable)(BOOL allInserted))completedBlock; + filter:(void (^_Nullable)(id model, BOOL inserted, BOOL *_Nullable rollback))filter + completed:(void (^_Nullable)(BOOL allInserted))completedBlock; @end diff --git a/LKDBHelper/Helper/NSObject+LKDBHelper.m b/LKDBHelper/Helper/NSObject+LKDBHelper.m index 20da168..dc05b11 100644 --- a/LKDBHelper/Helper/NSObject+LKDBHelper.m +++ b/LKDBHelper/Helper/NSObject+LKDBHelper.m @@ -167,8 +167,7 @@ - (BOOL)updateToDB { if (self.rowid > 0) { return [self.class updateToDB:self where:nil]; - } - else { + } else { return [self saveToDB]; } } @@ -193,7 +192,7 @@ + (void)insertArrayByAsyncToDB:(NSArray *)models [self insertArrayByAsyncToDB:models completed:nil]; } -+ (void)insertArrayByAsyncToDB:(NSArray *)models completed:(void (^ _Nullable)(BOOL))completedBlock ++ (void)insertArrayByAsyncToDB:(NSArray *)models completed:(void (^_Nullable)(BOOL))completedBlock { if (models.count > 0) { dispatch_async(dispatch_get_global_queue(0, 0), ^{ @@ -226,7 +225,7 @@ + (void)insertToDBWithArray:(NSArray *)models filter:(void (^)(id model, BOOL in } return (isRollback == NO); }]; - + if (completedBlock) { completedBlock(allInserted); } diff --git a/LKDBHelper/Helper/NSObject+LKModel.h b/LKDBHelper/Helper/NSObject+LKModel.h index f0c1893..4d9df9a 100644 --- a/LKDBHelper/Helper/NSObject+LKModel.h +++ b/LKDBHelper/Helper/NSObject+LKModel.h @@ -53,7 +53,7 @@ NS_ASSUME_NONNULL_BEGIN * 联合主键 * @return */ -+ (nullable NSArray *)getPrimaryKeyUnionArray; ++ (nullable NSArray *)getPrimaryKeyUnionArray; /** * overwrite in your models set column attribute @@ -81,7 +81,7 @@ NS_ASSUME_NONNULL_BEGIN * * @return property the data after conversion */ -- (nullable id)userGetValueForModel:(LKDBProperty *)property; +- (nullable id)userGetValueForModel:(LKDBProperty *)property; /*** * @brief overwrite in your models,return insert sqlite table data diff --git a/LKDBHelper/Helper/NSObject+LKModel.m b/LKDBHelper/Helper/NSObject+LKModel.m index 02045e9..1685ab8 100644 --- a/LKDBHelper/Helper/NSObject+LKModel.m +++ b/LKDBHelper/Helper/NSObject+LKModel.m @@ -6,8 +6,8 @@ // Copyright (c) 2013年 ljh. All rights reserved. // -#import "NSObject+LKModel.h" #import "LKDBHelper.h" +#import "NSObject+LKModel.h" #import #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED @@ -123,60 +123,47 @@ - (id)modelGetValue:(LKDBProperty *)property id returnValue = value; if (value == nil) { return nil; - } - else if ([value isKindOfClass:[NSString class]]) { + } else if ([value isKindOfClass:[NSString class]]) { returnValue = [value copy]; - } - else if ([value isKindOfClass:[NSNumber class]]) { + } else if ([value isKindOfClass:[NSNumber class]]) { returnValue = [[LKDBUtils numberFormatter] stringFromNumber:value]; - } - else if ([value isKindOfClass:[NSDate class]]) { + } else if ([value isKindOfClass:[NSDate class]]) { NSDateFormatter *formatter = [self.class getModelDateFormatter]; if (formatter) { returnValue = [formatter stringFromDate:value]; - } - else { + } else { returnValue = [LKDBUtils stringWithDate:value]; } returnValue = [returnValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - } - else if ([value isKindOfClass:[LKDBColor class]]) { + } else if ([value isKindOfClass:[LKDBColor class]]) { LKDBColor *color = value; CGFloat r, g, b, a; [color getRed:&r green:&g blue:&b alpha:&a]; returnValue = [NSString stringWithFormat:@"%.3f,%.3f,%.3f,%.3f", r, g, b, a]; - } - else if ([value isKindOfClass:[NSValue class]]) { + } else if ([value isKindOfClass:[NSValue class]]) { NSString *columnType = property.propertyType; #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED if ([columnType isEqualToString:@"CGRect"]) { returnValue = NSStringFromCGRect([value CGRectValue]); - } - else if ([columnType isEqualToString:@"CGPoint"]) { + } else if ([columnType isEqualToString:@"CGPoint"]) { returnValue = NSStringFromCGPoint([value CGPointValue]); - } - else if ([columnType isEqualToString:@"CGSize"]) { + } else if ([columnType isEqualToString:@"CGSize"]) { returnValue = NSStringFromCGSize([value CGSizeValue]); - } - else if ([columnType isEqualToString:@"_NSRange"]) { + } else if ([columnType isEqualToString:@"_NSRange"]) { returnValue = NSStringFromRange([value rangeValue]); } #else if ([columnType hasSuffix:@"Rect"]) { returnValue = NSStringFromRect([value rectValue]); - } - else if ([columnType hasSuffix:@"Point"]) { + } else if ([columnType hasSuffix:@"Point"]) { returnValue = NSStringFromPoint([value pointValue]); - } - else if ([columnType hasSuffix:@"Size"]) { + } else if ([columnType hasSuffix:@"Size"]) { returnValue = NSStringFromSize([value sizeValue]); - } - else if ([columnType hasSuffix:@"Range"]) { + } else if ([columnType hasSuffix:@"Range"]) { returnValue = NSStringFromRange([value rangeValue]); } #endif - } - else if ([value isKindOfClass:[LKDBImage class]]) { + } else if ([value isKindOfClass:[LKDBImage class]]) { long random = arc4random(); long date = [[NSDate date] timeIntervalSince1970]; NSString *filename = [NSString stringWithFormat:@"img%ld%ld", date & 0xFFFFF, random & 0xFFF]; @@ -193,8 +180,7 @@ - (id)modelGetValue:(LKDBProperty *)property atomically:YES]; returnValue = filename; - } - else if ([value isKindOfClass:[NSData class]]) { + } else if ([value isKindOfClass:[NSData class]]) { long random = arc4random(); long date = [[NSDate date] timeIntervalSince1970]; NSString *filename = [NSString stringWithFormat:@"data%ld%ld", date & 0xFFFFF, random & 0xFFF]; @@ -202,18 +188,14 @@ - (id)modelGetValue:(LKDBProperty *)property [value writeToFile:[self.class getDBDataPathWithName:filename] atomically:YES]; returnValue = filename; - } - else if ([value isKindOfClass:[NSURL class]]) { + } else if ([value isKindOfClass:[NSURL class]]) { returnValue = [value absoluteString]; - } - else { + } else { if ([value isKindOfClass:[NSArray class]]) { returnValue = [self db_jsonObjectFromArray:value]; - } - else if ([value isKindOfClass:[NSDictionary class]]) { + } else if ([value isKindOfClass:[NSDictionary class]]) { returnValue = [self db_jsonObjectFromDictionary:value]; - } - else { + } else { returnValue = [self db_jsonObjectFromModel:value]; } returnValue = [self db_jsonStringFromObject:returnValue]; @@ -237,8 +219,7 @@ - (void)modelSetValue:(LKDBProperty *)property value:(NSString *)value if (value) { modelValue = [[LKDBUtils numberFormatter] numberFromString:value]; } - } - else if ([LKSQL_Convert_IntType rangeOfString:columnType].location != NSNotFound) { + } else if ([LKSQL_Convert_IntType rangeOfString:columnType].location != NSNotFound) { if (value) { modelValue = [[LKDBUtils numberFormatter] numberFromString:value]; } @@ -248,35 +229,28 @@ - (void)modelSetValue:(LKDBProperty *)property value:(NSString *)value if (value) { CGRect rect = CGRectFromString(value); modelValue = [NSValue valueWithCGRect:rect]; - } - else { + } else { modelValue = [NSValue valueWithCGRect:CGRectZero]; } - } - else if ([columnType isEqualToString:@"CGPoint"]) { + } else if ([columnType isEqualToString:@"CGPoint"]) { if (value) { CGPoint point = CGPointFromString(value); modelValue = [NSValue valueWithCGPoint:point]; - } - else { + } else { modelValue = [NSValue valueWithCGPoint:CGPointZero]; } - } - else if ([columnType isEqualToString:@"CGSize"]) { + } else if ([columnType isEqualToString:@"CGSize"]) { if (value) { CGSize size = CGSizeFromString(value); modelValue = [NSValue valueWithCGSize:size]; - } - else { + } else { modelValue = [NSValue valueWithCGSize:CGSizeZero]; } - } - else if ([columnType isEqualToString:@"_NSRange"]) { + } else if ([columnType isEqualToString:@"_NSRange"]) { if (value) { NSRange range = NSRangeFromString(value); modelValue = [NSValue valueWithRange:range]; - } - else { + } else { modelValue = [NSValue valueWithRange:NSMakeRange(0, 0)]; } } @@ -285,35 +259,28 @@ - (void)modelSetValue:(LKDBProperty *)property value:(NSString *)value if (value) { NSRect rect = NSRectFromString(value); modelValue = [NSValue valueWithRect:rect]; - } - else { + } else { modelValue = [NSValue valueWithRect:NSZeroRect]; } - } - else if ([columnType hasSuffix:@"Point"]) { + } else if ([columnType hasSuffix:@"Point"]) { if (value) { NSPoint point = NSPointFromString(value); modelValue = [NSValue valueWithPoint:point]; - } - else { + } else { modelValue = [NSValue valueWithPoint:NSZeroPoint]; } - } - else if ([columnType hasSuffix:@"Size"]) { + } else if ([columnType hasSuffix:@"Size"]) { if (value) { NSSize size = NSSizeFromString(value); modelValue = [NSValue valueWithSize:size]; - } - else { + } else { modelValue = [NSValue valueWithSize:NSZeroSize]; } - } - else if ([columnType hasSuffix:@"Range"]) { + } else if ([columnType hasSuffix:@"Range"]) { if (value) { NSRange range = NSRangeFromString(value); modelValue = [NSValue valueWithRange:range]; - } - else { + } else { modelValue = [NSValue valueWithRange:NSMakeRange(0, 0)]; } } @@ -322,29 +289,23 @@ - (void)modelSetValue:(LKDBProperty *)property value:(NSString *)value if (modelValue == nil) { modelValue = @0; } - } - else if (!value || ![value isKindOfClass:[NSString class]]) { + } else if (!value || ![value isKindOfClass:[NSString class]]) { //不继续遍历 - } - else if ([columnClass isSubclassOfClass:[NSString class]]) { + } else if ([columnClass isSubclassOfClass:[NSString class]]) { if (![LKDBHelper nullIsEmpty] || value.length > 0) { modelValue = [columnClass stringWithString:value]; } - } - else if ([columnClass isSubclassOfClass:[NSNumber class]]) { + } else if ([columnClass isSubclassOfClass:[NSNumber class]]) { modelValue = [[LKDBUtils numberFormatter] numberFromString:value]; - } - else if ([columnClass isSubclassOfClass:[NSDate class]]) { + } else if ([columnClass isSubclassOfClass:[NSDate class]]) { NSString *datestr = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; NSDateFormatter *formatter = [self.class getModelDateFormatter]; if (formatter) { modelValue = [formatter dateFromString:datestr]; - } - else { + } else { modelValue = [LKDBUtils dateWithString:datestr]; } - } - else if ([columnClass isSubclassOfClass:[LKDBColor class]]) { + } else if ([columnClass isSubclassOfClass:[LKDBColor class]]) { NSString *colorString = value; NSArray *array = [colorString componentsSeparatedByString:@","]; float r, g, b, a; @@ -352,39 +313,33 @@ - (void)modelSetValue:(LKDBProperty *)property value:(NSString *)value g = [[array objectAtIndex:1] floatValue]; b = [[array objectAtIndex:2] floatValue]; a = [[array objectAtIndex:3] floatValue]; - + modelValue = [LKDBColor colorWithRed:r green:g blue:b alpha:a]; - } - else if ([columnClass isSubclassOfClass:[LKDBImage class]]) { + } else if ([columnClass isSubclassOfClass:[LKDBImage class]]) { NSString *filename = value; NSString *filepath = [self.class getDBImagePathWithName:filename]; if ([LKDBUtils isFileExists:filepath]) { modelValue = [[LKDBImage alloc] initWithContentsOfFile:filepath]; } - } - else if ([columnClass isSubclassOfClass:[NSData class]]) { + } else if ([columnClass isSubclassOfClass:[NSData class]]) { NSString *filename = value; NSString *filepath = [self.class getDBDataPathWithName:filename]; if ([LKDBUtils isFileExists:filepath]) { modelValue = [NSData dataWithContentsOfFile:filepath]; } - } - else if ([columnClass isSubclassOfClass:[NSURL class]]) { + } else if ([columnClass isSubclassOfClass:[NSURL class]]) { NSString *urlString = value; modelValue = [NSURL URLWithString:urlString]; - } - else { + } else { modelValue = [self db_modelWithJsonValue:value]; BOOL isValid = NO; if ([modelValue isKindOfClass:[NSArray class]] && [columnClass isSubclassOfClass:[NSArray class]]) { isValid = YES; modelValue = [columnClass arrayWithArray:modelValue]; - } - else if ([modelValue isKindOfClass:[NSDictionary class]] && [columnClass isSubclassOfClass:[NSDictionary class]]) { + } else if ([modelValue isKindOfClass:[NSDictionary class]] && [columnClass isSubclassOfClass:[NSDictionary class]]) { isValid = YES; modelValue = [columnClass dictionaryWithDictionary:modelValue]; - } - else if ([modelValue isKindOfClass:columnClass]) { + } else if ([modelValue isKindOfClass:columnClass]) { isValid = YES; } ///如果类型不对 则设置为空 @@ -399,19 +354,18 @@ - (void)modelSetValue:(LKDBProperty *)property value:(NSString *)value - (id)db_jsonObjectFromDictionary:(NSDictionary *)dic { if ([NSJSONSerialization isValidJSONObject:dic]) { - NSDictionary *bomb = @{ LKDB_TypeKey : LKDB_TypeKey_JSON, LKDB_ValueKey : dic }; + NSDictionary *bomb = @{LKDB_TypeKey: LKDB_TypeKey_JSON, LKDB_ValueKey: dic}; return bomb; - } - else { + } else { NSMutableDictionary *toDic = [NSMutableDictionary dictionary]; - [dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { + [dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL *_Nonnull stop) { id jsonObject = [self db_jsonObjectWithObject:obj]; if (jsonObject) { toDic[key] = jsonObject; } }]; if (toDic.count) { - NSDictionary *bomb = @{ LKDB_TypeKey : LKDB_TypeKey_Combo, LKDB_ValueKey : toDic }; + NSDictionary *bomb = @{LKDB_TypeKey: LKDB_TypeKey_Combo, LKDB_ValueKey: toDic}; return bomb; } } @@ -420,10 +374,9 @@ - (id)db_jsonObjectFromDictionary:(NSDictionary *)dic - (id)db_jsonObjectFromArray:(NSArray *)array { if ([NSJSONSerialization isValidJSONObject:array]) { - NSDictionary *bomb = @{ LKDB_TypeKey : LKDB_TypeKey_JSON, LKDB_ValueKey : array }; + NSDictionary *bomb = @{LKDB_TypeKey: LKDB_TypeKey_JSON, LKDB_ValueKey: array}; return bomb; - } - else { + } else { NSMutableArray *toArray = [NSMutableArray array]; NSInteger count = array.count; for (NSInteger i = 0; i < count; i++) { @@ -435,7 +388,7 @@ - (id)db_jsonObjectFromArray:(NSArray *)array } if (toArray.count) { - NSDictionary *bomb = @{ LKDB_TypeKey : LKDB_TypeKey_Combo, LKDB_ValueKey : toArray }; + NSDictionary *bomb = @{LKDB_TypeKey: LKDB_TypeKey_Combo, LKDB_ValueKey: toArray}; return bomb; } } @@ -447,28 +400,23 @@ - (id)db_jsonObjectWithObject:(id)obj id jsonObject = nil; if ([obj isKindOfClass:[NSString class]] || [obj isKindOfClass:[NSNumber class]]) { jsonObject = obj; - } - else if ([obj isKindOfClass:[NSDate class]]) { + } else if ([obj isKindOfClass:[NSDate class]]) { NSString *dateString = nil; NSDateFormatter *formatter = [self.class getModelDateFormatter]; if (formatter) { dateString = [formatter stringFromDate:obj]; - } - else { + } else { dateString = [LKDBUtils stringWithDate:obj]; } dateString = [dateString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if (dateString.length > 0) { - jsonObject = @{ LKDB_TypeKey : LKDB_TypeKey_Date, LKDB_ValueKey : dateString }; + jsonObject = @{LKDB_TypeKey: LKDB_TypeKey_Date, LKDB_ValueKey: dateString}; } - } - else if ([obj isKindOfClass:[NSArray class]]) { + } else if ([obj isKindOfClass:[NSArray class]]) { jsonObject = [self db_jsonObjectFromArray:obj]; - } - else if ([obj isKindOfClass:[NSDictionary class]]) { + } else if ([obj isKindOfClass:[NSDictionary class]]) { jsonObject = [self db_jsonObjectFromArray:obj]; - } - else { + } else { jsonObject = [self db_jsonObjectFromModel:obj]; } @@ -485,15 +433,13 @@ - (id)db_jsonObjectFromModel:(NSObject *)model if (model.rowid > 0) { [model updateToDB]; jsonObject = [self db_readInfoWithModel:model class:clazz]; - } - else { + } else { if (model.db_inserting == NO && [clazz getModelInfos].count > 0) { BOOL success = [model saveToDB]; if (success) { jsonObject = [self db_readInfoWithModel:model class:clazz]; } - } - else { + } else { NSAssert(NO, @"目前LKDB 还不支持 循环引用。 比如 A 持有 B, B 持有 A,这种的存储"); } } @@ -510,12 +456,12 @@ - (NSDictionary *)db_readInfoWithModel:(NSObject *)model class:(Class)clazz NSAssert(NO, @"none class"); return nil; } - + jsonObject[LKDB_TypeKey] = LKDB_TypeKey_Model; jsonObject[LKDB_TableNameKey] = model.db_tableName; jsonObject[LKDB_ClassKey] = NSStringFromClass(clazz); jsonObject[LKDB_RowIdKey] = @(model.rowid); - + NSDictionary *dic = [model db_getPrimaryKeysValues]; if (dic.count > 0 && [NSJSONSerialization isValidJSONObject:dic]) { jsonObject[LKDB_PValueKey] = dic; @@ -523,7 +469,7 @@ - (NSDictionary *)db_readInfoWithModel:(NSObject *)model class:(Class)clazz return jsonObject; } -- (NSString *)db_jsonStringFromObject:(NSObject *)jsonObject + - (NSString *)db_jsonStringFromObject : (NSObject *)jsonObject { if (jsonObject && [NSJSONSerialization isValidJSONObject:jsonObject]) { NSData *data = [NSJSONSerialization dataWithJSONObject:jsonObject options:0 error:nil]; @@ -539,8 +485,7 @@ - (id)db_modelWithJsonValue:(id)value NSData *jsonData = nil; if ([value isKindOfClass:[NSString class]]) { jsonData = [value dataUsingEncoding:NSUTF8StringEncoding]; - } - else if ([value isKindOfClass:[NSData class]]) { + } else if ([value isKindOfClass:[NSData class]]) { jsonData = value; } @@ -559,8 +504,7 @@ - (id)db_objectWithArray:(NSArray *)array id value = [array objectAtIndex:i]; if ([value isKindOfClass:[NSDictionary class]]) { value = [self db_objectWithDictionary:value]; - } - else if ([value isKindOfClass:[NSArray class]]) { + } else if ([value isKindOfClass:[NSArray class]]) { value = [self db_objectWithArray:value]; } @@ -592,8 +536,7 @@ - (id)db_objectWithDictionary:(NSDictionary *)dic NSInteger result = [[[clazz getUsingLKDBHelper] executeScalarWithSQL:rowCountWhere arguments:nil] integerValue]; if (result > 0) { where = [NSString stringWithFormat:@"select rowid,* from %@ where rowid=%ld limit 1", tableName, (long)rowid]; - } - else { + } else { NSDictionary *pv = [dic objectForKey:LKDB_PValueKey]; if (pv.count > 0) { BOOL isNeedAddDot = NO; @@ -624,46 +567,38 @@ - (id)db_objectWithDictionary:(NSDictionary *)dic return result; } } - } - else if ([type isEqualToString:LKDB_TypeKey_JSON]) { + } else if ([type isEqualToString:LKDB_TypeKey_JSON]) { id value = [dic objectForKey:LKDB_ValueKey]; return value; - } - else if ([type isEqualToString:LKDB_TypeKey_Combo]) { + } else if ([type isEqualToString:LKDB_TypeKey_Combo]) { id value = [dic objectForKey:LKDB_ValueKey]; if ([value isKindOfClass:[NSArray class]]) { return [self db_objectWithArray:value]; - } - else if ([value isKindOfClass:[NSDictionary class]]) { + } else if ([value isKindOfClass:[NSDictionary class]]) { return [self db_objectWithDictionary:value]; - } - else { + } else { return value; } - } - else if ([type isEqualToString:LKDB_TypeKey_Date]) { + } else if ([type isEqualToString:LKDB_TypeKey_Date]) { NSString *datestr = [dic objectForKey:LKDB_ValueKey]; NSDateFormatter *formatter = [self.class getModelDateFormatter]; if (formatter) { return [formatter dateFromString:datestr]; - } - else { + } else { return [LKDBUtils dateWithString:datestr]; } } - } - else { - + } else { + NSMutableDictionary *toDic = [NSMutableDictionary dictionary]; - [dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull value, BOOL * _Nonnull stop) { + [dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull value, BOOL *_Nonnull stop) { id saveObj = value; if ([value isKindOfClass:[NSArray class]]) { saveObj = [self db_objectWithArray:value]; - } - else if ([value isKindOfClass:[NSDictionary class]]) { + } else if ([value isKindOfClass:[NSDictionary class]]) { saveObj = [self db_objectWithDictionary:value]; } - + if (saveObj) { toDic[key] = saveObj; } @@ -701,13 +636,12 @@ - (NSDictionary *)db_getPrimaryKeysValues LKModelInfos *infos = [self.class getModelInfos]; NSArray *array = infos.primaryKeys; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; - [array enumerateObjectsUsingBlock:^(NSString *pname, NSUInteger idx, BOOL * _Nonnull stop) { + [array enumerateObjectsUsingBlock:^(NSString *pname, NSUInteger idx, BOOL *_Nonnull stop) { LKDBProperty *property = [infos objectWithSqlColumnName:pname]; id value = nil; if ([property.type isEqualToString:LKSQL_Mapping_UserCalculate]) { value = [self userGetValueForModel:property]; - } - else { + } else { value = [self modelGetValue:property]; } if (value) { @@ -740,8 +674,7 @@ - (BOOL)singlePrimaryKeyValueIsEmpty return NO; } return YES; - } - else { + } else { return (pkvalue == nil); } } @@ -762,8 +695,7 @@ - (id)singlePrimaryKeyValue if (property) { if ([property.type isEqualToString:LKSQL_Mapping_UserCalculate]) { return [self userGetValueForModel:property]; - } - else { + } else { return [self modelGetValue:property]; } } @@ -826,8 +758,7 @@ + (LKModelInfos *)getModelInfos } if (pronames.count > 0) { infos = [[LKModelInfos alloc] initWithKeyMapping:keymapping propertyNames:pronames propertyType:protypes primaryKeys:pkArray]; - } - else { + } else { infos = [[LKModelInfos alloc] init]; } oncePropertyDic[className] = infos; @@ -907,32 +838,25 @@ + (void)getSelfPropertys:(NSMutableArray *)pronames protypes:(NSMutableArray *)p } } } - } - else if ([propertyType hasPrefix:@"T{"]) { + } else if ([propertyType hasPrefix:@"T{"]) { NSRange range = [propertyType rangeOfString:@"="]; if (range.location > 2 && range.location <= propertyType.length) { range = NSMakeRange(2, range.location - 2); propertyClassName = [propertyType substringWithRange:range]; } - } - else { + } else { propertyType = [propertyType lowercaseString]; if ([propertyType hasPrefix:@"ti"] || [propertyType hasPrefix:@"tb"]) { propertyClassName = @"int"; - } - else if ([propertyType hasPrefix:@"tf"]) { + } else if ([propertyType hasPrefix:@"tf"]) { propertyClassName = @"float"; - } - else if ([propertyType hasPrefix:@"td"]) { + } else if ([propertyType hasPrefix:@"td"]) { propertyClassName = @"double"; - } - else if ([propertyType hasPrefix:@"tl"] || [propertyType hasPrefix:@"tq"]) { + } else if ([propertyType hasPrefix:@"tl"] || [propertyType hasPrefix:@"tq"]) { propertyClassName = @"long"; - } - else if ([propertyType hasPrefix:@"tc"]) { + } else if ([propertyType hasPrefix:@"tc"]) { propertyClassName = @"char"; - } - else if ([propertyType hasPrefix:@"ts"]) { + } else if ([propertyType hasPrefix:@"ts"]) { propertyClassName = @"short"; } }