Skip to content

Commit 24074a1

Browse files
committed
在读取时 如果没找到该 rowid ,会参试用 主键值去搜索
1 parent 190ee69 commit 24074a1

File tree

2 files changed

+86
-14
lines changed

2 files changed

+86
-14
lines changed

LKDBHelper/Helper/LKDBUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static NSString* const LKDB_ValueKey = @"DB_Value";
8181
static NSString* const LKDB_TableNameKey = @"DB_TableName";
8282
static NSString* const LKDB_ClassKey = @"DB_Class";
8383
static NSString* const LKDB_RowIdKey = @"DB_RowId";
84+
static NSString* const LKDB_PValueKey = @"DB_PKeyValue";
8485

8586
///Object-c type converted to SQLite type 把Object-c 类型 转换为sqlite 类型
8687
extern NSString* LKSQLTypeFromObjcType(NSString *objcType);

LKDBHelper/Helper/NSObject+LKModel.m

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,7 @@ -(id)db_jsonObjectFromModel:(NSObject*)model
472472
NSDictionary* jsonObject = nil;
473473
if(model.rowid > 0)
474474
{
475-
jsonObject = @{LKDB_TypeKey:LKDB_TypeKey_Model,
476-
LKDB_TableNameKey:model.db_tableName,
477-
LKDB_ClassKey:NSStringFromClass(clazz),
478-
LKDB_RowIdKey:@(model.rowid)};
475+
jsonObject = [self db_readInfoWithModel:model class:clazz];
479476
}
480477
else
481478
{
@@ -487,15 +484,28 @@ -(id)db_jsonObjectFromModel:(NSObject*)model
487484
BOOL success = [model saveToDB];
488485
if(success)
489486
{
490-
jsonObject = @{LKDB_TypeKey:LKDB_TypeKey_Model,
491-
LKDB_TableNameKey:model.db_tableName,
492-
LKDB_ClassKey:NSStringFromClass(clazz),
493-
LKDB_RowIdKey:@(model.rowid)};
487+
jsonObject = [self db_readInfoWithModel:model class:clazz];
494488
}
495489
}
496490
}
497491
return jsonObject;
498492
}
493+
-(NSDictionary*)db_readInfoWithModel:(NSObject*)model class:(Class)clazz
494+
{
495+
NSMutableDictionary* jsonObject = [NSMutableDictionary dictionary];
496+
[jsonObject setObject:LKDB_TypeKey_Model forKey:LKDB_TypeKey];
497+
[jsonObject setObject:model.db_tableName forKey:LKDB_TableNameKey];
498+
[jsonObject setObject:NSStringFromClass(clazz) forKey:LKDB_ClassKey];
499+
[jsonObject setObject:@(model.rowid) forKey:LKDB_RowIdKey];
500+
501+
NSDictionary* dic = [model db_getPrimaryKeysValues];
502+
if(dic.count > 0 && [NSJSONSerialization isValidJSONObject:dic])
503+
{
504+
[jsonObject setObject:dic forKey:LKDB_PValueKey];
505+
}
506+
return jsonObject;
507+
}
508+
499509
-(NSString*)db_jsonStringFromObject:(NSObject*)jsonObject
500510
{
501511
if(jsonObject && [NSJSONSerialization isValidJSONObject:jsonObject])
@@ -572,12 +582,50 @@ -(id)db_objectWithDictionary:(NSDictionary*)dic
572582
int rowid = [[dic objectForKey:LKDB_RowIdKey] intValue];
573583
NSString* tableName = [dic objectForKey:LKDB_TableNameKey];
574584

575-
NSArray* array = [[clazz getUsingLKDBHelper] searchWithSQL:[NSString stringWithFormat:@"select rowid,* from %@ where rowid=%d limit 1",tableName,rowid] toClass:clazz];
576-
if(array.count > 0)
585+
NSString* where = nil;
586+
587+
NSString* rowCountWhere = [NSString stringWithFormat:@"select count(rowid) from %@ where rowid=%d limit 1",tableName,rowid];
588+
int result = [[[clazz getUsingLKDBHelper] executeScalarWithSQL:rowCountWhere arguments:nil] intValue];
589+
if(result > 0)
590+
{
591+
where = [NSString stringWithFormat:@"select rowid,* from %@ where rowid=%d limit 1",tableName,rowid];
592+
}
593+
else
594+
{
595+
NSDictionary* pv = [dic objectForKey:LKDB_PValueKey];
596+
if(pv.count > 0)
597+
{
598+
BOOL isNeedAddDot = NO;
599+
NSMutableString* sb = [NSMutableString stringWithFormat:@"select rowid,* from %@ where",tableName];
600+
601+
NSArray* allKeys = pv.allKeys;
602+
for (NSString* key in allKeys)
603+
{
604+
id obj = [pv objectForKey:key];
605+
if(isNeedAddDot)
606+
{
607+
[sb appendString:@" and"];
608+
}
609+
[sb appendFormat:@" %@ = '%@'",key,obj];
610+
611+
isNeedAddDot = YES;
612+
}
613+
614+
[sb appendString:@" limit 1"];
615+
616+
where = [NSString stringWithString:sb];
617+
}
618+
}
619+
620+
if(where)
577621
{
578-
NSObject* result = [array objectAtIndex:0];
579-
result.db_tableName = tableName;
580-
return result;
622+
NSArray* array = [[clazz getUsingLKDBHelper] searchWithSQL:where toClass:clazz];
623+
if(array.count > 0)
624+
{
625+
NSObject* result = [array objectAtIndex:0];
626+
result.db_tableName = tableName;
627+
return result;
628+
}
581629
}
582630
}
583631
else if([type isEqualToString:LKDB_TypeKey_JSON])
@@ -649,7 +697,30 @@ -(id)userGetValueForModel:(LKDBProperty *)property
649697
return nil;
650698
}
651699

652-
700+
-(NSDictionary *)db_getPrimaryKeysValues
701+
{
702+
LKModelInfos* infos = [self.class getModelInfos];
703+
NSArray* array = infos.primaryKeys;
704+
NSMutableDictionary* dic = [NSMutableDictionary dictionary];
705+
for (NSString* pname in array)
706+
{
707+
LKDBProperty* property = [infos objectWithSqlColumnName:pname];
708+
id value = nil;
709+
if([property.type isEqualToString:LKSQL_Mapping_UserCalculate])
710+
{
711+
value = [self userGetValueForModel:property];
712+
}
713+
else
714+
{
715+
value = [self modelGetValue:property];
716+
}
717+
if(value)
718+
{
719+
[dic setObject:value forKey:property.sqlColumnName];
720+
}
721+
}
722+
return dic;
723+
}
653724
//主键值 是否为空
654725
-(BOOL)singlePrimaryKeyValueIsEmpty
655726
{

0 commit comments

Comments
 (0)